File size: 3,317 Bytes
3216a1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""
frame_extractor.py - Extracts frames from uploaded video blobs.

Receives a video file, extracts N frames at even intervals using OpenCV,
and saves them as images in the session's frames/ directory.
"""

import os
import cv2
import tempfile
import numpy as np

# Configuration
MIN_FRAMES = 10
MAX_FRAMES = 30
FRAME_INTERVAL = 0.3  # seconds between extracted frames


def extract_frames_from_video(video_bytes: bytes, session_path: str) -> list[str]:
    """
    Extract frames from a video blob at even intervals.

    Args:
        video_bytes: Raw bytes of the uploaded video file.
        session_path: Path to the session directory.

    Returns:
        List of file paths to the extracted frame images.
    """
    frames_dir = os.path.join(session_path, "frames")
    os.makedirs(frames_dir, exist_ok=True)

    # Write video bytes to a temporary file for OpenCV to read
    temp_video_path = os.path.join(session_path, "temp_video.webm")
    try:
        with open(temp_video_path, "wb") as f:
            f.write(video_bytes)

        cap = cv2.VideoCapture(temp_video_path)
        if not cap.isOpened():
            print("[FrameExtractor] Failed to open video file.")
            return []

        fps = cap.get(cv2.CAP_PROP_FPS)
        if fps <= 0:
            fps = 30.0  # Default fallback for browser-recorded WebM

        # Calculate frame skip interval based on desired extraction rate
        # Extract approximately 1 frame every FRAME_INTERVAL seconds
        skip_interval = max(1, int(fps * FRAME_INTERVAL))

        # Read frames sequentially (browser WebM doesn't support seeking)
        all_frames = []
        frame_count = 0
        while True:
            ret, frame = cap.read()
            if not ret:
                break

            # Only save every Nth frame
            if frame_count % skip_interval == 0:
                frame_path = os.path.join(frames_dir, f"frame_{len(all_frames):04d}.jpg")
                cv2.imwrite(frame_path, frame)
                all_frames.append(frame_path)

                # Stop once we hit the maximum
                if len(all_frames) >= MAX_FRAMES:
                    break

            frame_count += 1

        cap.release()

        print(f"[FrameExtractor] Extracted {len(all_frames)} frames from video "
              f"(total read: {frame_count}, fps: {fps:.1f}, skip: every {skip_interval})")

        return all_frames

    finally:
        # Clean up temp video file
        if os.path.exists(temp_video_path):
            os.remove(temp_video_path)


def extract_frames_from_images(image_paths: list[str]) -> list[str]:
    """
    Validate and return image paths (for the upload path).
    Images are already saved; this just confirms they're valid.

    Args:
        image_paths: List of paths to uploaded images.

    Returns:
        List of valid image file paths.
    """
    valid_paths = []
    for path in image_paths:
        if os.path.exists(path):
            # Quick validation: try to read the image
            img = cv2.imread(path)
            if img is not None:
                valid_paths.append(path)
            else:
                print(f"[FrameExtractor] Invalid image file skipped: {path}")
        else:
            print(f"[FrameExtractor] File not found: {path}")

    return valid_paths