Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import DetrImageProcessor, DetrForObjectDetection
|
| 5 |
+
|
| 6 |
+
# Cargar el modelo y el procesador
|
| 7 |
+
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
|
| 8 |
+
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
|
| 9 |
+
|
| 10 |
+
def detect_objects(video_path):
|
| 11 |
+
# Leer el video
|
| 12 |
+
cap = cv2.VideoCapture(video_path)
|
| 13 |
+
results = []
|
| 14 |
+
|
| 15 |
+
while cap.isOpened():
|
| 16 |
+
ret, frame = cap.read()
|
| 17 |
+
if not ret:
|
| 18 |
+
break
|
| 19 |
+
|
| 20 |
+
# Procesar el frame
|
| 21 |
+
inputs = processor(images=frame, return_tensors="pt")
|
| 22 |
+
outputs = model(**inputs)
|
| 23 |
+
|
| 24 |
+
# Extraer las predicciones
|
| 25 |
+
target_sizes = torch.tensor([frame.shape[:2]])
|
| 26 |
+
results.append(processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0])
|
| 27 |
+
|
| 28 |
+
cap.release()
|
| 29 |
+
return results
|
| 30 |
+
|
| 31 |
+
# Crear la interfaz de Gradio
|
| 32 |
+
iface = gr.Interface(fn=detect_objects, inputs="video", outputs="json")
|
| 33 |
+
iface.launch()
|