Spaces:
Sleeping
Sleeping
Commit ·
ca5e334
1
Parent(s): 5120cf0
Agregar lógica de zona y armado de eventos con pruebas
Browse filesfeet_in_zone decide si los pies de cada persona caen en la zona pintada;
build_events agrupa los cuadros en entradas/salidas, tolerando huecos
cortos de detección (MAX_GAP_S).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- app.py +32 -0
- test_zone.py +14 -0
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""CamGuard: detecta personas que entran a una zona restringida de un video.
|
| 2 |
+
|
| 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):
|
| 12 |
+
"""True para cada punto (x, y) que cae dentro de la máscara de zona."""
|
| 13 |
+
h, w = zone.shape
|
| 14 |
+
x = np.clip(feet_xy[:, 0].astype(int), 0, w - 1)
|
| 15 |
+
y = np.clip(feet_xy[:, 1].astype(int), 0, h - 1)
|
| 16 |
+
return zone[y, x]
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def build_events(frames_in_zone, fps, max_gap_s=MAX_GAP_S):
|
| 20 |
+
"""{id: [índices de cuadro dentro de la zona]} -> filas [id, entró, salió, duración] en segundos."""
|
| 21 |
+
max_gap = max(1, round(max_gap_s * fps))
|
| 22 |
+
rows = []
|
| 23 |
+
for tid, frames in frames_in_zone.items():
|
| 24 |
+
start = prev = frames[0]
|
| 25 |
+
for f in frames[1:] + [None]:
|
| 26 |
+
if f is not None and f - prev <= max_gap:
|
| 27 |
+
prev = f
|
| 28 |
+
continue
|
| 29 |
+
enter, leave = start / fps, (prev + 1) / fps
|
| 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])
|
test_zone.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
from app import build_events, feet_in_zone
|
| 4 |
+
|
| 5 |
+
zone = np.zeros((10, 10), bool)
|
| 6 |
+
zone[5:, 5:] = True
|
| 7 |
+
feet = np.array([[7, 7], [2, 2], [9.9, 9.9], [50, 50]]) # adentro, afuera, borde, fuera del cuadro
|
| 8 |
+
assert feet_in_zone(feet, zone).tolist() == [True, False, True, True]
|
| 9 |
+
|
| 10 |
+
# fps=5: #1 entra en 0 s y sale en 1 s; #2 entra, sale 2 s y vuelve (dos eventos); un hueco de 1 cuadro no corta
|
| 11 |
+
events = build_events({1: [0, 1, 2, 3, 4], 2: [10, 12, 30]}, fps=5)
|
| 12 |
+
assert events == [[1, 0.0, 1.0, 1.0], [2, 2.0, 2.6, 0.6], [2, 6.0, 6.2, 0.2]], events
|
| 13 |
+
assert build_events({}, fps=5) == []
|
| 14 |
+
print("ok")
|