Spaces:
Build error
Build error
Upload 2 files
Browse files- app.py +65 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
import tempfile
|
| 6 |
+
import json
|
| 7 |
+
from transformers import DetrImageProcessor, DetrForObjectDetection
|
| 8 |
+
from PIL import Image
|
| 9 |
+
|
| 10 |
+
# Load a pretrained object detection model
|
| 11 |
+
model_name = "facebook/detr-resnet-50"
|
| 12 |
+
processor = DetrImageProcessor.from_pretrained(model_name)
|
| 13 |
+
model = DetrForObjectDetection.from_pretrained(model_name)
|
| 14 |
+
|
| 15 |
+
# Video processing function
|
| 16 |
+
def process_video(video_path):
|
| 17 |
+
cap = cv2.VideoCapture(video_path)
|
| 18 |
+
frame_rate = cap.get(cv2.CAP_PROP_FPS)
|
| 19 |
+
timestamped_objects = []
|
| 20 |
+
|
| 21 |
+
frame_number = 0
|
| 22 |
+
while cap.isOpened():
|
| 23 |
+
ret, frame = cap.read()
|
| 24 |
+
if not ret:
|
| 25 |
+
break
|
| 26 |
+
|
| 27 |
+
# Process every 10th frame for efficiency
|
| 28 |
+
if frame_number % int(frame_rate) == 0:
|
| 29 |
+
timestamp = frame_number / frame_rate
|
| 30 |
+
pil_image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
| 31 |
+
|
| 32 |
+
# Model prediction
|
| 33 |
+
inputs = processor(images=pil_image, return_tensors="pt")
|
| 34 |
+
with torch.no_grad():
|
| 35 |
+
outputs = model(**inputs)
|
| 36 |
+
|
| 37 |
+
# Extract results
|
| 38 |
+
target_sizes = torch.tensor([pil_image.size])
|
| 39 |
+
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.7)[0]
|
| 40 |
+
|
| 41 |
+
detected_objects = []
|
| 42 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 43 |
+
label_name = model.config.id2label[label.item()]
|
| 44 |
+
detected_objects.append(label_name)
|
| 45 |
+
|
| 46 |
+
if detected_objects:
|
| 47 |
+
timestamped_objects.append({"timestamp": round(timestamp, 2), "objects": list(set(detected_objects))})
|
| 48 |
+
|
| 49 |
+
frame_number += 1
|
| 50 |
+
|
| 51 |
+
cap.release()
|
| 52 |
+
return json.dumps(timestamped_objects, indent=2)
|
| 53 |
+
|
| 54 |
+
# Gradio Interface
|
| 55 |
+
iface = gr.Interface(
|
| 56 |
+
fn=process_video,
|
| 57 |
+
inputs=gr.Video(label="Upload Video"),
|
| 58 |
+
outputs=gr.JSON(label="Timestamped Object Detections"),
|
| 59 |
+
title="Video Object Detector",
|
| 60 |
+
description="Upload a video and get a timestamped list of detected objects using a pretrained DETR model."
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# Launch the Gradio app
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
torchvision
|
| 3 |
+
transformers
|
| 4 |
+
gradio
|
| 5 |
+
opencv-python
|