Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,113 +1,60 @@
|
|
1 |
-
|
2 |
-
import os
|
3 |
import gradio as gr
|
|
|
4 |
import cv2
|
5 |
-
import numpy as np
|
6 |
from ultralytics import YOLO
|
7 |
-
import tempfile
|
8 |
-
import torch
|
9 |
-
import ultralytics.nn.tasks
|
10 |
-
|
11 |
-
# Set Ultralytics config path
|
12 |
-
os.environ['YOLO_CONFIG_DIR'] = '/tmp/Ultralytics'
|
13 |
-
|
14 |
-
# Custom function to load model with trusted weights
|
15 |
-
def load_model_with_trusted_weights(model_path):
|
16 |
-
with torch.serialization.safe_globals([
|
17 |
-
ultralytics.nn.tasks.DetectionModel,
|
18 |
-
torch.nn.modules.container.Sequential
|
19 |
-
]):
|
20 |
-
return YOLO(model_path)
|
21 |
-
|
22 |
-
# Load both YOLO models
|
23 |
-
model_yolo11 = load_model_with_trusted_weights('./data/yolo11n.pt')
|
24 |
-
model_best = load_model_with_trusted_weights('./data/best.pt')
|
25 |
-
|
26 |
-
def process_video(video_path, model_name, conf_threshold=0.4):
|
27 |
-
"""
|
28 |
-
Process the input video frame by frame using the selected YOLO model,
|
29 |
-
draw bounding boxes, and return the processed video path.
|
30 |
-
"""
|
31 |
-
# Select model
|
32 |
-
model = model_yolo11 if model_name == "YOLO11n" else model_best
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
raise ValueError("Could not open video file")
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
43 |
|
44 |
-
#
|
45 |
-
|
46 |
-
|
47 |
-
out = cv2.VideoWriter(temp_video_path, fourcc, fps, (width, height))
|
48 |
|
49 |
while cap.isOpened():
|
50 |
ret, frame = cap.read()
|
51 |
if not ret:
|
52 |
break
|
53 |
-
|
54 |
-
#
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
cap.release()
|
69 |
out.release()
|
70 |
-
cv2.destroyAllWindows()
|
71 |
|
72 |
-
return
|
73 |
|
74 |
# Gradio interface
|
75 |
-
|
76 |
-
gr.HTML("""
|
77 |
-
<h1 style='text-align: center'>
|
78 |
-
Video Object Detection with YOLO Models
|
79 |
-
</h1>
|
80 |
-
""")
|
81 |
-
|
82 |
-
with gr.Row():
|
83 |
-
with gr.Column():
|
84 |
-
video_input = gr.Video(label="Upload Video")
|
85 |
-
model_choice = gr.Dropdown(
|
86 |
-
choices=["YOLO11n", "Best Model"],
|
87 |
-
label="Select Model",
|
88 |
-
value="YOLO11n"
|
89 |
-
)
|
90 |
-
conf_threshold = gr.Slider(
|
91 |
-
label="Confidence Threshold",
|
92 |
-
minimum=0.0,
|
93 |
-
maximum=1.0,
|
94 |
-
step=0.05,
|
95 |
-
value=0.4
|
96 |
-
)
|
97 |
-
process_button = gr.Button("Process Video")
|
98 |
-
|
99 |
-
with gr.Column():
|
100 |
-
video_output = gr.Video(
|
101 |
-
label="Processed Video",
|
102 |
-
streaming=True,
|
103 |
-
autoplay=True
|
104 |
-
)
|
105 |
-
|
106 |
-
process_button.click(
|
107 |
-
fn=process_video,
|
108 |
-
inputs=[video_input, model_choice, conf_threshold],
|
109 |
-
outputs=[video_output]
|
110 |
-
)
|
111 |
|
112 |
-
|
113 |
-
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
import cv2
|
|
|
4 |
from ultralytics import YOLO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
+
# Load YOLO models
|
7 |
+
model_yolo11 = YOLO('./data/yolo11n.pt')
|
8 |
+
model_best = YOLO('./data/best.pt')
|
|
|
9 |
|
10 |
+
def process_video(video):
|
11 |
+
# Read video input
|
12 |
+
cap = cv2.VideoCapture(video.name)
|
13 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
14 |
+
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
15 |
+
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
16 |
|
17 |
+
# Create a VideoWriter object to save the output video
|
18 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Codec for .mp4
|
19 |
+
out = cv2.VideoWriter('output_video.mp4', fourcc, fps, (frame_width, frame_height))
|
|
|
20 |
|
21 |
while cap.isOpened():
|
22 |
ret, frame = cap.read()
|
23 |
if not ret:
|
24 |
break
|
25 |
+
|
26 |
+
# Use both YOLO models for detection
|
27 |
+
results_yolo11 = model_yolo11(frame)
|
28 |
+
results_best = model_best(frame)
|
29 |
+
|
30 |
+
# Combine the results from both models
|
31 |
+
# For simplicity, we will overlay bounding boxes and labels from both models
|
32 |
+
for result in results_yolo11:
|
33 |
+
boxes = result.boxes
|
34 |
+
for box in boxes:
|
35 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0].tolist())
|
36 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
37 |
+
label = f"YOLOv11: {box.cls[0]} - {box.conf[0]:.2f}"
|
38 |
+
cv2.putText(frame, label, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
|
39 |
+
|
40 |
+
for result in results_best:
|
41 |
+
boxes = result.boxes
|
42 |
+
for box in boxes:
|
43 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0].tolist())
|
44 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 0), 2)
|
45 |
+
label = f"Best: {box.cls[0]} - {box.conf[0]:.2f}"
|
46 |
+
cv2.putText(frame, label, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)
|
47 |
+
|
48 |
+
# Write the processed frame to the output video
|
49 |
+
out.write(frame)
|
50 |
|
51 |
cap.release()
|
52 |
out.release()
|
|
|
53 |
|
54 |
+
return 'output_video.mp4'
|
55 |
|
56 |
# Gradio interface
|
57 |
+
iface = gr.Interface(fn=process_video, inputs=gr.Video(), outputs=gr.Video(), live=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
+
# Launch the app
|
60 |
+
iface.launch()
|