Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import cv2
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from ultralytics import YOLO
|
| 5 |
+
|
| 6 |
+
# ── Config ─────────────────────────────────────────────
|
| 7 |
+
MODEL_PATH = "yolov8n.pt" # peso pre-entrenado (COCO)
|
| 8 |
+
CONF_THRES = 0.3 # confianza mínima
|
| 9 |
+
LINE_RATIO = 0.5 # línea virtual: mitad de la altura
|
| 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
|
| 18 |
+
|
| 19 |
+
def count_people(frame):
|
| 20 |
+
global memory, in_count, out_count
|
| 21 |
+
|
| 22 |
+
h, w = frame.shape[:2]
|
| 23 |
+
line_y = int(h * LINE_RATIO)
|
| 24 |
+
|
| 25 |
+
# Detección + tracking ByteTrack interno
|
| 26 |
+
results = model.track(frame, classes=[0], conf=CONF_THRES,
|
| 27 |
+
persist=True, verbose=False) # solo clase “person”
|
| 28 |
+
|
| 29 |
+
annotated = frame.copy()
|
| 30 |
+
cv2.line(annotated, (0, line_y), (w, line_y), (0, 255, 255), 2)
|
| 31 |
+
|
| 32 |
+
if results:
|
| 33 |
+
for box in results[0].boxes:
|
| 34 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
| 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 |
+
# Dibujo
|
| 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
|
| 48 |
+
elif prev_cy > line_y >= cy: # sale
|
| 49 |
+
out_count += 1
|
| 50 |
+
memory[tid] = cy
|
| 51 |
+
|
| 52 |
+
# Overlay de números
|
| 53 |
+
total = in_count - out_count
|
| 54 |
+
label = f"In: {in_count} | Out: {out_count} | Ocupación: {total}"
|
| 55 |
+
cv2.putText(annotated, label, (10, 30),
|
| 56 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
|
| 57 |
+
|
| 58 |
+
return annotated, label
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
demo = gr.Interface(
|
| 62 |
+
fn=count_people,
|
| 63 |
+
inputs=gr.Image(source="webcam", streaming=True),
|
| 64 |
+
outputs=[gr.Image(), gr.Text()],
|
| 65 |
+
title="Contador de personas (entrada única)",
|
| 66 |
+
live=True,
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
demo.launch()
|