Spaces:
Sleeping
Sleeping
File size: 10,541 Bytes
67f4ecf 2fdb608 67f4ecf 2fdb608 67f4ecf 2fdb608 67f4ecf | 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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | """
MotionScope Pro - Core Movement Detection Engine
Combines MediaPipe HandLandmarker (tasks API) with background subtraction.
"""
import os
import urllib.request
import cv2
import numpy as np
import mediapipe as mp
from enum import Enum
from dataclasses import dataclass
from typing import Tuple, Generator
# MediaPipe tasks API (lazy-loaded via attribute access)
_BaseOptions = mp.tasks.BaseOptions
_HandLandmarker = mp.tasks.vision.HandLandmarker
_HandLandmarkerOptions = mp.tasks.vision.HandLandmarkerOptions
_RunningMode = mp.tasks.vision.RunningMode
# Path to the hand landmarker model (shipped alongside this file)
_MODEL_PATH = os.path.join(os.path.dirname(__file__), "hand_landmarker.task")
def _ensure_model_exists():
"""Download the model if it doesn't exist locally."""
if not os.path.exists(_MODEL_PATH):
print(f"Downloading model to {_MODEL_PATH}...")
url = "https://storage.googleapis.com/mediapipe-models/hand_landmarker/hand_landmarker/float16/latest/hand_landmarker.task"
urllib.request.urlretrieve(url, _MODEL_PATH)
class DetectionMode(Enum):
"""Available detection modes."""
HAND_TRACKING = "Hand Tracking"
MOTION_DETECTION = "Motion Detection"
COMBINED = "Combined"
@dataclass
class DetectionConfig:
"""Tunable parameters for detection."""
# MediaPipe hand settings
min_detection_confidence: float = 0.5
min_tracking_confidence: float = 0.5
max_num_hands: int = 2
# Motion detection settings
motion_threshold: int = 180
min_contour_area: int = 1000
blur_kernel_size: Tuple[int, int] = (5, 5)
morph_kernel_size: Tuple[int, int] = (3, 3)
# Background subtractor settings
bg_history: int = 500
bg_var_threshold: int = 16
bg_detect_shadows: bool = True
class MovementDetector:
"""
Professional movement detector combining MediaPipe hands + MOG2
background subtraction.
"""
def __init__(self, config: DetectionConfig | None = None):
self.config = config or DetectionConfig()
self.hand_landmarker = self._build_hand_landmarker()
self.back_sub = self._build_back_sub()
self.frame_count: int = 0
# ------------------------------------------------------------------
# Builder helpers
# ------------------------------------------------------------------
def _build_hand_landmarker(self):
_ensure_model_exists()
options = _HandLandmarkerOptions(
base_options=_BaseOptions(model_asset_path=_MODEL_PATH),
running_mode=_RunningMode.IMAGE,
num_hands=self.config.max_num_hands,
min_hand_detection_confidence=self.config.min_detection_confidence,
min_tracking_confidence=self.config.min_tracking_confidence,
)
return _HandLandmarker.create_from_options(options)
def _build_back_sub(self):
return cv2.createBackgroundSubtractorMOG2(
history=self.config.bg_history,
varThreshold=self.config.bg_var_threshold,
detectShadows=self.config.bg_detect_shadows,
)
def rebuild(self, config: DetectionConfig):
"""Rebuild internal models when the user changes settings."""
self.config = config
self.hand_landmarker.close()
self.hand_landmarker = self._build_hand_landmarker()
self.back_sub = self._build_back_sub()
self.frame_count = 0
# ------------------------------------------------------------------
# Hand detection (new tasks API)
# ------------------------------------------------------------------
def detect_hands(self, frame: np.ndarray) -> np.ndarray:
"""
Detect hands and draw landmarks + labels on *frame* (BGR).
Uses MediaPipe tasks API HandLandmarker.
"""
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=rgb)
result = self.hand_landmarker.detect(mp_image)
h, w, _ = frame.shape
if result.hand_landmarks:
for idx, landmarks in enumerate(result.hand_landmarks):
# Draw connections manually since draw_landmarks expects
# NormalizedLandmarkList but we have a list of landmarks
self._draw_hand_skeleton(frame, landmarks, w, h)
# Label near wrist (landmark 0)
wrist = landmarks[0]
cx, cy = int(wrist.x * w), int(wrist.y * h)
label = "Hand"
if result.handedness and idx < len(result.handedness):
label = result.handedness[idx][0].category_name
cv2.putText(
frame, label, (cx - 30, cy - 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2,
)
return frame
def _draw_hand_skeleton(self, frame, landmarks, w, h):
"""Draw landmark points and connections on *frame*."""
# Define the 21 hand landmark connections (pairs of indices)
connections = [
(0, 1), (1, 2), (2, 3), (3, 4), # Thumb
(0, 5), (5, 6), (6, 7), (7, 8), # Index
(0, 9), (9, 10), (10, 11), (11, 12), # Middle
(0, 13), (13, 14), (14, 15), (15, 16), # Ring
(0, 17), (17, 18), (18, 19), (19, 20), # Pinky
(5, 9), (9, 13), (13, 17), # Palm
]
# Convert normalized landmarks to pixel coordinates
pts = []
for lm in landmarks:
px, py = int(lm.x * w), int(lm.y * h)
pts.append((px, py))
# Draw connections
for start, end in connections:
cv2.line(frame, pts[start], pts[end], (0, 255, 0), 2)
# Draw landmark dots
for px, py in pts:
cv2.circle(frame, (px, py), 5, (255, 0, 128), -1)
cv2.circle(frame, (px, py), 5, (255, 255, 255), 1)
# ------------------------------------------------------------------
# Motion detection
# ------------------------------------------------------------------
def detect_motion(self, frame: np.ndarray) -> Tuple[np.ndarray, np.ndarray, int]:
"""
Background-subtraction motion detection.
Returns
-------
processed : BGR frame with bounding boxes
mask : cleaned foreground mask
count : number of detected moving objects
"""
fg_mask = self.back_sub.apply(frame)
_, mask_thresh = cv2.threshold(
fg_mask, self.config.motion_threshold, 255, cv2.THRESH_BINARY,
)
mask_blur = cv2.GaussianBlur(mask_thresh, self.config.blur_kernel_size, 0)
kernel = cv2.getStructuringElement(
cv2.MORPH_ELLIPSE, self.config.morph_kernel_size,
)
mask_clean = cv2.morphologyEx(mask_blur, cv2.MORPH_OPEN, kernel)
mask_clean = cv2.morphologyEx(mask_clean, cv2.MORPH_CLOSE, kernel)
contours, _ = cv2.findContours(
mask_clean, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE,
)
valid = []
for cnt in contours:
area = cv2.contourArea(cnt)
if area > self.config.min_contour_area:
valid.append(cnt)
x, y, bw, bh = cv2.boundingRect(cnt)
cv2.rectangle(frame, (x, y), (x + bw, y + bh), (0, 0, 255), 2)
cv2.putText(
frame, f"Area: {int(area)}", (x, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2,
)
cv2.putText(
frame, f"Moving objects: {len(valid)}", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2,
)
return frame, mask_clean, len(valid)
# ------------------------------------------------------------------
# High-level frame dispatcher
# ------------------------------------------------------------------
def process_frame(self, frame: np.ndarray, mode: DetectionMode) -> np.ndarray:
"""Process a single frame according to the selected *mode*."""
self.frame_count += 1
out = frame.copy()
if mode == DetectionMode.HAND_TRACKING:
return self.detect_hands(out)
elif mode == DetectionMode.MOTION_DETECTION:
processed, _, _ = self.detect_motion(out)
return processed
elif mode == DetectionMode.COMBINED:
motion_frame, _, _ = self.detect_motion(out)
return self.detect_hands(motion_frame)
return out
# ------------------------------------------------------------------
# Full-video processing generator
# ------------------------------------------------------------------
def process_video(
self,
source: str,
mode: DetectionMode = DetectionMode.MOTION_DETECTION,
output_path: str = "output.mp4",
) -> Generator[Tuple[np.ndarray | None, str | None, float], None, None]:
"""
Iterate over every frame in *source*, yield processed RGB frames.
Yields
------
(display_frame_rgb | None, output_path | None, progress)
"""
self.frame_count = 0
self.back_sub = self._build_back_sub() # fresh background model
cap = cv2.VideoCapture(source)
if not cap.isOpened():
raise ValueError(f"Cannot open video: {source}")
frame_w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS)) or 30
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) or 1
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_w, frame_h))
try:
while True:
ret, frame = cap.read()
if not ret:
break
processed = self.process_frame(frame, mode)
out.write(processed)
display = cv2.cvtColor(processed, cv2.COLOR_BGR2RGB)
progress = min(self.frame_count / total_frames, 1.0)
yield display, None, progress
finally:
cap.release()
out.release()
yield None, output_path, 1.0
# ------------------------------------------------------------------
# Cleanup
# ------------------------------------------------------------------
def release(self):
"""Free resources."""
self.hand_landmarker.close()
|