-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
79 lines (66 loc) · 2.24 KB
/
utils.py
File metadata and controls
79 lines (66 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import os
import base64
import json
from PIL import Image
from typing import List, Dict
import cv2 # CHANGE: Added for video processing
def print_png_size_in_mb(file_path):
if not file_path.lower().endswith('.png'):
print("File is not a PNG image.")
return
try:
with Image.open(file_path) as img:
img.verify()
size_bytes = os.path.getsize(file_path)
size_mb = size_bytes / (1024 * 1024)
print(f"Image size: {size_mb:.2f} MB")
except Exception as e:
print(f"Error reading image: {e}")
def get_image_size_from_bytes(image_bytes: bytes):
size_bytes = len(image_bytes)
size_mb = size_bytes/(1024*1024)
return size_mb
def image_file_to_base64(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
# ...existing code...
def encode_image_bytes(image_bytes: bytes) -> str:
"""
CHANGE: Added this function to encode image bytes to a base64 string.
Args:
image_bytes (bytes): Image data in bytes.
Returns:
str: Base64-encoded string of the image.
"""
return base64.b64encode(image_bytes).decode("utf-8")
def get_frames(video_path: str, frame_step: int = 1) -> list:
"""
Args:
video_path (str): Path to the local video file.
frame_step (int): Step between frames to extract (default 1 = every frame).
Returns:
list: List of dicts with 'idx' (frame index) and 'image' (image data as bytes).
"""
frames = []
cap = cv2.VideoCapture(video_path)
idx = 0
frame_idx = 0
if not cap.isOpened():
print(f"Error: Cannot open video file {video_path}")
return frames
while True:
ret, frame = cap.read()
if not ret:
break
if frame_idx % frame_step == 0:
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
success, buffer = cv2.imencode('.png', frame_rgb)
if success:
frames.append({'idx': idx, 'image': buffer.tobytes()})
idx += 1
frame_idx += 1
cap.release()
return frames
def observe_run(states: List[Dict]):
with open("/output/states.json", "w") as f:
json.dump(states, f, indent=2)