Create utils/media_utils.py
Browse files- utils/media_utils.py +59 -0
utils/media_utils.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
from typing import Optional, Dict, Tuple
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
import cv2
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
def pil_from_path(path: str) -> Optional[Image.Image]:
|
| 9 |
+
try:
|
| 10 |
+
return Image.open(path).convert("RGB")
|
| 11 |
+
except Exception:
|
| 12 |
+
return None
|
| 13 |
+
|
| 14 |
+
def first_frame(path: str, max_side: int = 960) -> Tuple[Optional[Image.Image], Dict[str, float]]:
|
| 15 |
+
info: Dict[str, float] = {}
|
| 16 |
+
try:
|
| 17 |
+
cap = cv2.VideoCapture(path)
|
| 18 |
+
if not cap.isOpened():
|
| 19 |
+
return None, {"error": "Cannot open video"}
|
| 20 |
+
fps = cap.get(cv2.CAP_PROP_FPS) or 0.0
|
| 21 |
+
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) or 0)
|
| 22 |
+
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) or 0)
|
| 23 |
+
frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT) or 0)
|
| 24 |
+
ok, frame = cap.read()
|
| 25 |
+
cap.release()
|
| 26 |
+
if not ok or frame is None:
|
| 27 |
+
return None, {"error": "Failed to read first frame"}
|
| 28 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 29 |
+
scale = min(1.0, max_side / max(h, w)) if max(h, w) > 0 else 1.0
|
| 30 |
+
if scale < 1.0:
|
| 31 |
+
frame = cv2.resize(frame, (int(w*scale), int(h*scale)), interpolation=cv2.INTER_AREA)
|
| 32 |
+
dur = (frames / fps) if fps > 0 else 0.0
|
| 33 |
+
return Image.fromarray(frame), {"width": w, "height": h, "fps": round(fps,3), "frames": frames, "duration_s": round(dur,2)}
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return None, {"error": str(e)}
|
| 36 |
+
|
| 37 |
+
def mask_debug_on_image(img: Image.Image) -> Image.Image:
|
| 38 |
+
"""Classical single-frame mask heuristic for quick sanity checks (no models)."""
|
| 39 |
+
ar = np.array(img)
|
| 40 |
+
if ar.ndim == 3 and ar.shape[2] == 3:
|
| 41 |
+
gray = cv2.cvtColor(ar, cv2.COLOR_RGB2GRAY)
|
| 42 |
+
else:
|
| 43 |
+
gray = ar if ar.ndim == 2 else cv2.cvtColor(ar, cv2.COLOR_RGBA2GRAY)
|
| 44 |
+
|
| 45 |
+
edges = cv2.Canny(gray, 80, 160)
|
| 46 |
+
edges = cv2.dilate(edges, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)), 1)
|
| 47 |
+
|
| 48 |
+
border = np.concatenate([gray[0, :], gray[-1, :], gray[:, 0], gray[:, -1]])
|
| 49 |
+
bg_median = np.median(border)
|
| 50 |
+
diff = np.abs(gray.astype(np.float32) - bg_median)
|
| 51 |
+
thresh = (diff > 28).astype(np.uint8) * 255
|
| 52 |
+
|
| 53 |
+
mask = cv2.bitwise_or(thresh, edges)
|
| 54 |
+
k7 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7, 7))
|
| 55 |
+
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, k7)
|
| 56 |
+
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, k7)
|
| 57 |
+
mask_c = cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB)
|
| 58 |
+
overlay = (0.6 * ar + 0.4 * np.dstack([mask, np.zeros_like(mask), np.zeros_like(mask)])).astype(np.uint8)
|
| 59 |
+
return Image.fromarray(overlay)
|