Spaces:
Sleeping
Sleeping
| """ | |
| video_input.py | |
| Handles video loading from file path, URL, or webcam index. | |
| On Hugging Face Spaces, only file uploads are supported (no webcam). | |
| """ | |
| import cv2 | |
| import os | |
| def load_video(source): | |
| """ | |
| Load a video from a file path or URL. | |
| Args: | |
| source: str — file path or URL | |
| Returns: | |
| cap: cv2.VideoCapture object | |
| meta: dict with fps, width, height, total_frames, source | |
| """ | |
| if isinstance(source, str) and not source.startswith("http"): | |
| if not os.path.exists(source): | |
| raise FileNotFoundError(f"Video file not found: {source}") | |
| cap = cv2.VideoCapture(source) | |
| if not cap.isOpened(): | |
| raise ValueError( | |
| f"Cannot open video source: {source}. " | |
| "Make sure the file is a valid MP4, AVI, MOV, or MKV." | |
| ) | |
| meta = { | |
| "fps": cap.get(cv2.CAP_PROP_FPS) or 25.0, | |
| "width": int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), | |
| "height": int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)), | |
| "total_frames": int(cap.get(cv2.CAP_PROP_FRAME_COUNT)), | |
| "source": str(source), | |
| } | |
| return cap, meta | |
| def release_video(cap): | |
| """Safely release a VideoCapture object.""" | |
| if cap and cap.isOpened(): | |
| cap.release() | |