Spaces:
Sleeping
Sleeping
primer commit
Browse files- app.py +56 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
# Cargar el modelo de detecci贸n de objetos de Hugging Face (DETR)
|
| 6 |
+
detector = pipeline("object-detection", model="facebook/detr-resnet-50")
|
| 7 |
+
|
| 8 |
+
def process_video(video_path):
|
| 9 |
+
"""
|
| 10 |
+
Procesa un video y devuelve el m谩ximo n煤mero detectado de personas, bicicletas y motos en un fotograma.
|
| 11 |
+
"""
|
| 12 |
+
cap = cv2.VideoCapture(video_path)
|
| 13 |
+
if not cap.isOpened():
|
| 14 |
+
return {"person": 0, "bicycle": 0, "motorcycle": 0}
|
| 15 |
+
|
| 16 |
+
max_counts = {"person": 0, "bicycle": 0, "motorcycle": 0}
|
| 17 |
+
|
| 18 |
+
while True:
|
| 19 |
+
ret, frame = cap.read()
|
| 20 |
+
if not ret:
|
| 21 |
+
break
|
| 22 |
+
|
| 23 |
+
# Convertir el frame de BGR a RGB (requerido por el modelo)
|
| 24 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 25 |
+
|
| 26 |
+
# Realizar la detecci贸n de objetos
|
| 27 |
+
results = detector(frame_rgb)
|
| 28 |
+
|
| 29 |
+
# Contar objetos detectados en el frame actual (usamos un umbral de confianza)
|
| 30 |
+
frame_counts = {"person": 0, "bicycle": 0, "motorcycle": 0}
|
| 31 |
+
for detection in results:
|
| 32 |
+
if detection["score"] < 0.7:
|
| 33 |
+
continue
|
| 34 |
+
label = detection["label"].lower()
|
| 35 |
+
if label in frame_counts:
|
| 36 |
+
frame_counts[label] += 1
|
| 37 |
+
|
| 38 |
+
# Actualizar el conteo m谩ximo si en este frame se detecta m谩s
|
| 39 |
+
for key in frame_counts:
|
| 40 |
+
if frame_counts[key] > max_counts[key]:
|
| 41 |
+
max_counts[key] = frame_counts[key]
|
| 42 |
+
|
| 43 |
+
cap.release()
|
| 44 |
+
return max_counts
|
| 45 |
+
|
| 46 |
+
# Crear la interfaz de Gradio para el Space
|
| 47 |
+
iface = gr.Interface(
|
| 48 |
+
fn=process_video,
|
| 49 |
+
inputs=gr.Video(label="Sube tu video"),
|
| 50 |
+
outputs="json",
|
| 51 |
+
title="Detecci贸n de Objetos en Video",
|
| 52 |
+
description="Carga un video y detecta cu谩ntas personas, bicicletas y motos aparecen usando modelos de Hugging Face."
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
opencv-python
|
| 3 |
+
transformers
|