Spaces:
Sleeping
Sleeping
array
Browse files
app.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
| 1 |
import cv2
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import pipeline
|
|
|
|
| 4 |
|
| 5 |
-
# Cargar el modelo de detecci贸n de objetos
|
| 6 |
-
detector = pipeline("object-detection", model="facebook/detr-resnet-50")
|
| 7 |
|
| 8 |
def process_video(video_path):
|
| 9 |
"""
|
|
@@ -20,13 +21,16 @@ def process_video(video_path):
|
|
| 20 |
if not ret:
|
| 21 |
break
|
| 22 |
|
| 23 |
-
# Convertir el frame de BGR a RGB
|
| 24 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 25 |
|
|
|
|
|
|
|
|
|
|
| 26 |
# Realizar la detecci贸n de objetos
|
| 27 |
-
results = detector(
|
| 28 |
|
| 29 |
-
# Contar objetos detectados en el frame actual (
|
| 30 |
frame_counts = {"person": 0, "bicycle": 0, "motorcycle": 0}
|
| 31 |
for detection in results:
|
| 32 |
if detection["score"] < 0.7:
|
|
@@ -35,7 +39,7 @@ def process_video(video_path):
|
|
| 35 |
if label in frame_counts:
|
| 36 |
frame_counts[label] += 1
|
| 37 |
|
| 38 |
-
# Actualizar el conteo m谩ximo si en este frame se detecta
|
| 39 |
for key in frame_counts:
|
| 40 |
if frame_counts[key] > max_counts[key]:
|
| 41 |
max_counts[key] = frame_counts[key]
|
|
|
|
| 1 |
import cv2
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import pipeline
|
| 4 |
+
from PIL import Image
|
| 5 |
|
| 6 |
+
# Cargar el modelo de detecci贸n de objetos (usando CPU)
|
| 7 |
+
detector = pipeline("object-detection", model="facebook/detr-resnet-50", device=-1)
|
| 8 |
|
| 9 |
def process_video(video_path):
|
| 10 |
"""
|
|
|
|
| 21 |
if not ret:
|
| 22 |
break
|
| 23 |
|
| 24 |
+
# Convertir el frame de BGR a RGB
|
| 25 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 26 |
|
| 27 |
+
# Convertir el array de NumPy a una imagen PIL
|
| 28 |
+
pil_image = Image.fromarray(frame_rgb)
|
| 29 |
+
|
| 30 |
# Realizar la detecci贸n de objetos
|
| 31 |
+
results = detector(pil_image)
|
| 32 |
|
| 33 |
+
# Contar objetos detectados en el frame actual (con umbral de confianza)
|
| 34 |
frame_counts = {"person": 0, "bicycle": 0, "motorcycle": 0}
|
| 35 |
for detection in results:
|
| 36 |
if detection["score"] < 0.7:
|
|
|
|
| 39 |
if label in frame_counts:
|
| 40 |
frame_counts[label] += 1
|
| 41 |
|
| 42 |
+
# Actualizar el conteo m谩ximo si en este frame se detecta un mayor n煤mero
|
| 43 |
for key in frame_counts:
|
| 44 |
if frame_counts[key] > max_counts[key]:
|
| 45 |
max_counts[key] = frame_counts[key]
|