from pathlib import Path import cv2 from PIL import Image from config import MAX_IMAGE_DIMENSIONS, MAX_VIDEO_FRAMES, MAX_VIDEO_DURATION_SEC from managers import video_capture from vision_utils import VideoInfo def validate_example_files(image_paths: list[str], video_paths: dict[str, str]) -> list[str]: """Validate all example files exist at startup. Returns list of missing files.""" missing = [] for path in image_paths: if not Path(path).exists(): missing.append(path) for path in video_paths.values(): if not Path(path).exists(): missing.append(path) return missing def validate_image(image: Image.Image | None) -> tuple[bool, str]: """ Validate image before processing. """ if image is None: return False, "No image provided" width, height = image.size if width > MAX_IMAGE_DIMENSIONS[0] or height > MAX_IMAGE_DIMENSIONS[1]: return False, ( f"Image too large ({width}x{height}). " f"Maximum: {MAX_IMAGE_DIMENSIONS[0]}x{MAX_IMAGE_DIMENSIONS[1]}" ) return True, "" def validate_video(video_path: str) -> tuple[bool, str, VideoInfo | None]: """ Validate video file before processing, checks fps, number of frames. :param video_path: Path to video :return: Tuple of: is_valid flag, error message, and video information """ with video_capture(video_path) as cap: if not cap.isOpened(): return False, "Could not open video file.", None fps = cap.get(cv2.CAP_PROP_FPS) total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) video_info = VideoInfo(fps=fps, total_frames=total_frames, width=width, height=height) if fps < 0 or fps > 240: return False, f"Invalid video fps: {fps}.", video_info if total_frames <= 0: return False, "Video has no frames.", video_info if width <= 0 or height <= 0: return False, f"Invalid dimensions: {width}x{height}.", video_info if total_frames > MAX_VIDEO_FRAMES: return False, ( f"Video too long ({total_frames} frames). " f"Maximum: {MAX_VIDEO_FRAMES} frames (~{MAX_VIDEO_DURATION_SEC}s at 30FPS)" ), video_info return True, "", video_info