Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,14 +4,14 @@ import gradio as gr
|
|
4 |
from ultralytics import YOLO
|
5 |
|
6 |
# ── Config ─────────────────────────────────────────────
|
7 |
-
MODEL_PATH = "yolov8n.pt" #
|
8 |
CONF_THRES = 0.3 # confianza mínima
|
9 |
-
LINE_RATIO = 0.5 # línea virtual
|
10 |
# ───────────────────────────────────────────────────────
|
11 |
|
12 |
model = YOLO(MODEL_PATH)
|
13 |
|
14 |
-
# Estado global
|
15 |
memory = {} # {track_id: previous_cy}
|
16 |
in_count = 0
|
17 |
out_count = 0
|
@@ -22,9 +22,14 @@ def count_people(frame):
|
|
22 |
h, w = frame.shape[:2]
|
23 |
line_y = int(h * LINE_RATIO)
|
24 |
|
25 |
-
#
|
26 |
-
results = model.track(
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
annotated = frame.copy()
|
30 |
cv2.line(annotated, (0, line_y), (w, line_y), (0, 255, 255), 2)
|
@@ -35,13 +40,7 @@ def count_people(frame):
|
|
35 |
cx, cy = int((x1 + x2) / 2), int((y1 + y2) / 2)
|
36 |
tid = int(box.id[0]) if box.id is not None else -1
|
37 |
|
38 |
-
#
|
39 |
-
cv2.rectangle(annotated, (x1, y1), (x2, y2), (0, 255, 0), 1)
|
40 |
-
cv2.circle(annotated, (cx, cy), 3, (0, 0, 255), -1)
|
41 |
-
cv2.putText(annotated, str(tid), (x1, y1 - 5),
|
42 |
-
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 1)
|
43 |
-
|
44 |
-
# Lógica de cruce
|
45 |
prev_cy = memory.get(tid, cy)
|
46 |
if prev_cy < line_y <= cy: # entra
|
47 |
in_count += 1
|
@@ -49,21 +48,31 @@ def count_people(frame):
|
|
49 |
out_count += 1
|
50 |
memory[tid] = cy
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
|
|
60 |
|
61 |
demo = gr.Interface(
|
62 |
fn=count_people,
|
63 |
-
inputs=gr.Image(
|
64 |
-
outputs=[gr.Image(), gr.Text()],
|
65 |
-
title="Contador de personas (entrada única)",
|
66 |
live=True,
|
|
|
67 |
)
|
68 |
|
69 |
if __name__ == "__main__":
|
|
|
4 |
from ultralytics import YOLO
|
5 |
|
6 |
# ── Config ─────────────────────────────────────────────
|
7 |
+
MODEL_PATH = "yolov8n.pt" # modelo pre-entrenado (COCO, clase “person”)
|
8 |
CONF_THRES = 0.3 # confianza mínima
|
9 |
+
LINE_RATIO = 0.5 # línea virtual en mitad de la altura
|
10 |
# ───────────────────────────────────────────────────────
|
11 |
|
12 |
model = YOLO(MODEL_PATH)
|
13 |
|
14 |
+
# Estado global para entradas/salidas
|
15 |
memory = {} # {track_id: previous_cy}
|
16 |
in_count = 0
|
17 |
out_count = 0
|
|
|
22 |
h, w = frame.shape[:2]
|
23 |
line_y = int(h * LINE_RATIO)
|
24 |
|
25 |
+
# detección + tracking interno (ByteTrack)
|
26 |
+
results = model.track(
|
27 |
+
frame,
|
28 |
+
classes=[0], # solo “person”
|
29 |
+
conf=CONF_THRES,
|
30 |
+
persist=True,
|
31 |
+
verbose=False
|
32 |
+
)
|
33 |
|
34 |
annotated = frame.copy()
|
35 |
cv2.line(annotated, (0, line_y), (w, line_y), (0, 255, 255), 2)
|
|
|
40 |
cx, cy = int((x1 + x2) / 2), int((y1 + y2) / 2)
|
41 |
tid = int(box.id[0]) if box.id is not None else -1
|
42 |
|
43 |
+
# lógica de cruce
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
prev_cy = memory.get(tid, cy)
|
45 |
if prev_cy < line_y <= cy: # entra
|
46 |
in_count += 1
|
|
|
48 |
out_count += 1
|
49 |
memory[tid] = cy
|
50 |
|
51 |
+
# dibujo
|
52 |
+
cv2.rectangle(annotated, (x1, y1), (x2, y2), (0, 255, 0), 1)
|
53 |
+
cv2.circle(annotated, (cx, cy), 3, (0, 0, 255), -1)
|
54 |
+
cv2.putText(annotated, str(tid), (x1, y1 - 5),
|
55 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 1)
|
56 |
|
57 |
+
total = in_count - out_count
|
58 |
+
cv2.putText(
|
59 |
+
annotated,
|
60 |
+
f"In: {in_count} Out: {out_count} Ocupación: {total}",
|
61 |
+
(10, 30),
|
62 |
+
cv2.FONT_HERSHEY_SIMPLEX,
|
63 |
+
1,
|
64 |
+
(255, 255, 255),
|
65 |
+
2
|
66 |
+
)
|
67 |
|
68 |
+
return annotated, f"In: {in_count} | Out: {out_count} | Ocupación: {total}"
|
69 |
|
70 |
demo = gr.Interface(
|
71 |
fn=count_people,
|
72 |
+
inputs=gr.Image(sources=["webcam"], streaming=True), # 👈 parámetro correcto
|
73 |
+
outputs=[gr.Image(label="Video"), gr.Text(label="Contador")],
|
|
|
74 |
live=True,
|
75 |
+
title="Contador de personas (entrada única)"
|
76 |
)
|
77 |
|
78 |
if __name__ == "__main__":
|