Spaces:
Sleeping
Sleeping
Commit ·
933bcf5
1
Parent(s): ca5e334
Agregar pipeline de análisis de video
Browse filesSubmuestrea a 5 fps (tope 60 s), detecta personas con RF-DETR, les da
un ID con ByteTrack, marca intrusos por la zona y escribe el video
anotado en H.264 con PyAV para que se reproduzca en el navegador.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
app.py
CHANGED
|
@@ -3,9 +3,23 @@
|
|
| 3 |
Todo corre local, en CPU. El video nunca sale de la máquina.
|
| 4 |
"""
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import numpy as np
|
|
|
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
MAX_GAP_S = 1.0 # si alguien "desaparece" menos que esto, sigue siendo el mismo evento
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
|
| 11 |
def feet_in_zone(feet_xy, zone):
|
|
@@ -30,3 +44,81 @@ def build_events(frames_in_zone, fps, max_gap_s=MAX_GAP_S):
|
|
| 30 |
rows.append([tid, round(enter, 1), round(leave, 1), round(leave - enter, 1)])
|
| 31 |
start = prev = f
|
| 32 |
return sorted(rows, key=lambda r: r[1])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
Todo corre local, en CPU. El video nunca sale de la máquina.
|
| 4 |
"""
|
| 5 |
|
| 6 |
+
import tempfile
|
| 7 |
+
from fractions import Fraction
|
| 8 |
+
|
| 9 |
+
import av
|
| 10 |
+
import cv2
|
| 11 |
+
import gradio as gr
|
| 12 |
import numpy as np
|
| 13 |
+
import supervision as sv
|
| 14 |
|
| 15 |
+
FPS = 5 # cuadros analizados por segundo
|
| 16 |
+
MAX_SECONDS = 60 # tope de video analizado
|
| 17 |
+
CONF = 0.4 # confianza mínima para contar una detección
|
| 18 |
MAX_GAP_S = 1.0 # si alguien "desaparece" menos que esto, sigue siendo el mismo evento
|
| 19 |
+
PERSON_ID = 1 # id COCO de "person" en RF-DETR
|
| 20 |
+
# ponytail: CPU-only (~0.3 s/cuadro); si es lento, exportar RF-DETR a ONNX + onnxruntime-directml
|
| 21 |
+
|
| 22 |
+
model = None # se carga en main(), así los tests no bajan el modelo
|
| 23 |
|
| 24 |
|
| 25 |
def feet_in_zone(feet_xy, zone):
|
|
|
|
| 44 |
rows.append([tid, round(enter, 1), round(leave, 1), round(leave - enter, 1)])
|
| 45 |
start = prev = f
|
| 46 |
return sorted(rows, key=lambda r: r[1])
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def zone_from_editor(editor, shape):
|
| 50 |
+
layers = (editor or {}).get("layers") or []
|
| 51 |
+
mask = np.any([layer[..., 3] > 0 for layer in layers], axis=0) if layers else None
|
| 52 |
+
if mask is None or not mask.any():
|
| 53 |
+
raise gr.Error("Pintá la zona restringida sobre la imagen antes de analizar.")
|
| 54 |
+
if mask.shape != shape:
|
| 55 |
+
mask = cv2.resize(mask.astype(np.uint8), (shape[1], shape[0]), interpolation=cv2.INTER_NEAREST) > 0
|
| 56 |
+
return mask
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def first_frame(video_path):
|
| 60 |
+
ok, frame = cv2.VideoCapture(video_path).read()
|
| 61 |
+
if not ok:
|
| 62 |
+
raise gr.Error("No pude leer el video.")
|
| 63 |
+
return {"background": frame[:, :, ::-1], "layers": [], "composite": None}
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def draw(frame, det, inside, zone):
|
| 67 |
+
out = frame.copy()
|
| 68 |
+
out[zone] = (0.6 * out[zone] + 0.4 * np.array([0, 0, 255])).astype(np.uint8) # rojo (BGR)
|
| 69 |
+
for mask, color, text in ((~inside, sv.Color.GREEN, "persona #{}"), (inside, sv.Color.RED, "INTRUSO #{}")):
|
| 70 |
+
d = det[mask]
|
| 71 |
+
out = sv.BoxAnnotator(color=color, thickness=3).annotate(out, d)
|
| 72 |
+
out = sv.LabelAnnotator(color=color).annotate(out, d, labels=[text.format(t) for t in d.tracker_id])
|
| 73 |
+
return out
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def analyze(video_path, editor, progress=gr.Progress()):
|
| 77 |
+
from trackers import ByteTrackTracker
|
| 78 |
+
|
| 79 |
+
if not video_path:
|
| 80 |
+
raise gr.Error("Primero subí un video.")
|
| 81 |
+
cap = cv2.VideoCapture(video_path)
|
| 82 |
+
src_fps = cap.get(cv2.CAP_PROP_FPS) or 30
|
| 83 |
+
step = max(1, round(src_fps / FPS))
|
| 84 |
+
fps = src_fps / step
|
| 85 |
+
total = int(min(cap.get(cv2.CAP_PROP_FRAME_COUNT) or 1e9, MAX_SECONDS * src_fps))
|
| 86 |
+
w, h = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) // 2 * 2, int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) // 2 * 2
|
| 87 |
+
zone = zone_from_editor(editor, (int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)), int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))))
|
| 88 |
+
|
| 89 |
+
tracker = ByteTrackTracker(frame_rate=fps, track_activation_threshold=CONF, minimum_consecutive_frames=1)
|
| 90 |
+
out_path = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name
|
| 91 |
+
container = av.open(out_path, "w")
|
| 92 |
+
stream = container.add_stream("libx264", rate=Fraction(fps).limit_denominator(1000))
|
| 93 |
+
stream.width, stream.height, stream.pix_fmt = w, h, "yuv420p"
|
| 94 |
+
|
| 95 |
+
in_zone, snapshots, i = {}, [], 0
|
| 96 |
+
for idx in progress.tqdm(range(total), desc="Analizando"):
|
| 97 |
+
if idx % step:
|
| 98 |
+
if not cap.grab():
|
| 99 |
+
break
|
| 100 |
+
continue
|
| 101 |
+
ok, frame = cap.read()
|
| 102 |
+
if not ok:
|
| 103 |
+
break
|
| 104 |
+
det = model.predict(frame[:, :, ::-1].copy(), threshold=CONF)
|
| 105 |
+
det = tracker.update(det[det.class_id == PERSON_ID])
|
| 106 |
+
det = det[det.tracker_id != -1]
|
| 107 |
+
inside = feet_in_zone(det.get_anchors_coordinates(sv.Position.BOTTOM_CENTER), zone)
|
| 108 |
+
out = draw(frame, det, inside, zone)
|
| 109 |
+
for tid in det.tracker_id[inside].tolist():
|
| 110 |
+
if tid not in in_zone:
|
| 111 |
+
snapshots.append((out[:, :, ::-1], f"Intruso #{tid} — {i / fps:.1f} s"))
|
| 112 |
+
in_zone.setdefault(tid, []).append(i)
|
| 113 |
+
for packet in stream.encode(av.VideoFrame.from_ndarray(out[:h, :w], format="bgr24")):
|
| 114 |
+
container.mux(packet)
|
| 115 |
+
i += 1
|
| 116 |
+
for packet in stream.encode():
|
| 117 |
+
container.mux(packet)
|
| 118 |
+
container.close()
|
| 119 |
+
cap.release()
|
| 120 |
+
|
| 121 |
+
rows = build_events(in_zone, fps)
|
| 122 |
+
if not rows:
|
| 123 |
+
gr.Info("No hubo intrusiones en la zona.")
|
| 124 |
+
return out_path, rows, snapshots
|