Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,69 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
#
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
#
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import torch
|
4 |
+
from transformers import DetrImageProcessor, DetrForObjectDetection
|
5 |
+
from PIL import Image
|
6 |
+
import tempfile
|
7 |
+
import os
|
8 |
+
|
9 |
+
# Load the DETR processor and model
|
10 |
+
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
|
11 |
+
model = DetrForObjectDetection.from_pretrained("ArrayDice/Vehicle_Detection_Model_Zoom")
|
12 |
+
|
13 |
+
# Function to process video and detect vehicles
|
14 |
+
def detect_vehicles_in_video(video_path):
|
15 |
+
# Load video
|
16 |
+
cap = cv2.VideoCapture(video_path)
|
17 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
18 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
19 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
20 |
+
|
21 |
+
# Temporary output video file
|
22 |
+
temp_video = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
|
23 |
+
out = cv2.VideoWriter(temp_video.name, cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height))
|
24 |
+
|
25 |
+
# Process each frame
|
26 |
+
while cap.isOpened():
|
27 |
+
ret, frame = cap.read()
|
28 |
+
if not ret:
|
29 |
+
break
|
30 |
+
|
31 |
+
# Convert frame to PIL for model processing
|
32 |
+
pil_image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
33 |
+
|
34 |
+
# Run object detection
|
35 |
+
inputs = processor(images=pil_image, return_tensors="pt")
|
36 |
+
outputs = model(**inputs)
|
37 |
+
|
38 |
+
# Set confidence threshold and draw boxes
|
39 |
+
for score, label, box in zip(outputs.logits.softmax(-1)[0], outputs.labels[0], outputs.boxes[0]):
|
40 |
+
if score.max() >= 0.9: # Adjust confidence threshold as needed
|
41 |
+
x_min, y_min, x_max, y_max = map(int, box)
|
42 |
+
cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
|
43 |
+
label_text = f"Vehicle ({score.max():.2f})"
|
44 |
+
cv2.putText(frame, label_text, (x_min, y_min - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
45 |
+
|
46 |
+
# Write annotated frame to output video
|
47 |
+
out.write(frame)
|
48 |
+
|
49 |
+
# Release resources
|
50 |
+
cap.release()
|
51 |
+
out.release()
|
52 |
+
|
53 |
+
return temp_video.name # Return path to the annotated video
|
54 |
+
|
55 |
+
# Gradio interface
|
56 |
+
def vehicle_detection_gradio(video):
|
57 |
+
annotated_video_path = detect_vehicles_in_video(video.name)
|
58 |
+
return annotated_video_path
|
59 |
+
|
60 |
+
# Set up Gradio interface
|
61 |
+
iface = gr.Interface(
|
62 |
+
fn=vehicle_detection_gradio,
|
63 |
+
inputs=gr.Video(type="file", label="Upload a video for vehicle detection"),
|
64 |
+
outputs=gr.Video(label="Annotated Video with Detected Vehicles"),
|
65 |
+
title="Vehicle Detection Model",
|
66 |
+
description="Upload a video, and the model will detect and annotate vehicles in each frame."
|
67 |
+
)
|
68 |
+
|
69 |
+
iface.launch()
|