41 lines
964 B
Python
41 lines
964 B
Python
|
|
import os
|
||
|
|
import tempfile
|
||
|
|
import zipfile
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
import cv2
|
||
|
|
|
||
|
|
from .config import Config
|
||
|
|
|
||
|
|
|
||
|
|
def load_frames(zip_path: Path, max_frames: int):
|
||
|
|
video_bytes = zipfile.ZipFile(zip_path).read("left.mp4")
|
||
|
|
|
||
|
|
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as f:
|
||
|
|
f.write(video_bytes)
|
||
|
|
tmp_path = f.name
|
||
|
|
|
||
|
|
cap = cv2.VideoCapture(tmp_path)
|
||
|
|
fps = cap.get(cv2.CAP_PROP_FPS) or Config.FPS_FALLBACK
|
||
|
|
|
||
|
|
frames = []
|
||
|
|
while len(frames) < max_frames:
|
||
|
|
ok, frame = cap.read()
|
||
|
|
if not ok:
|
||
|
|
break
|
||
|
|
frames.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
||
|
|
|
||
|
|
cap.release()
|
||
|
|
os.unlink(tmp_path)
|
||
|
|
|
||
|
|
if not frames:
|
||
|
|
raise RuntimeError(f"No frames found in {zip_path}")
|
||
|
|
|
||
|
|
h, w = frames[0].shape[:2]
|
||
|
|
scale = Config.DISPLAY_MAX / max(h, w)
|
||
|
|
dh, dw = int(h * scale), int(w * scale)
|
||
|
|
|
||
|
|
frames = [cv2.resize(f, (dw, dh)) for f in frames]
|
||
|
|
|
||
|
|
return frames, fps, dh, dw, h, w
|