| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- """Video and window frame capture."""
- from __future__ import annotations
- from typing import Iterator, Optional, Tuple
- import cv2
- import numpy as np
- from tracker.window_picker import get_client_screen_rect
- def iter_video_frames(video_path: str) -> Iterator[Tuple[int, np.ndarray]]:
- cap = cv2.VideoCapture(video_path)
- if not cap.isOpened():
- raise RuntimeError(f"Cannot open video: {video_path}")
- idx = 0
- while True:
- ok, frame = cap.read()
- if not ok:
- break
- yield idx, frame
- idx += 1
- cap.release()
- def grab_window_bgr(hwnd: int) -> Optional[np.ndarray]:
- """Capture HWND client area as BGR via mss. Returns None if invalid."""
- import mss
- try:
- left, top, width, height = get_client_screen_rect(hwnd)
- except Exception:
- return None
- if width <= 0 or height <= 0:
- return None
- with mss.mss() as sct:
- monitor = {"left": int(left), "top": int(top), "width": int(width), "height": int(height)}
- shot = sct.grab(monitor)
- frame = np.array(shot) # BGRA
- return cv2.cvtColor(frame, cv2.COLOR_BGRA2BGR)
|