Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,30 @@
|
|
1 |
-
|
2 |
import os
|
3 |
-
os.environ['YOLO_CONFIG_DIR'] = '/tmp/Ultralytics' # Set Ultralytics config path
|
4 |
import gradio as gr
|
5 |
import cv2
|
6 |
import numpy as np
|
7 |
from ultralytics import YOLO
|
8 |
import tempfile
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
# Load both YOLO models
|
13 |
-
model_yolo11 =
|
14 |
-
model_best =
|
15 |
|
16 |
def process_video(video_path, model_name, conf_threshold=0.4):
|
17 |
"""
|
18 |
Process the input video frame by frame using the selected YOLO model,
|
19 |
draw bounding boxes, and return the processed video path.
|
20 |
"""
|
21 |
-
# Select model
|
22 |
model = model_yolo11 if model_name == "YOLO11n" else model_best
|
23 |
|
24 |
# Open video capture
|
@@ -31,8 +37,10 @@ def process_video(video_path, model_name, conf_threshold=0.4):
|
|
31 |
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
32 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
33 |
|
34 |
-
#
|
35 |
-
|
|
|
|
|
36 |
|
37 |
while cap.isOpened():
|
38 |
ret, frame = cap.read()
|
@@ -50,15 +58,12 @@ def process_video(video_path, model_name, conf_threshold=0.4):
|
|
50 |
|
51 |
# Draw bounding boxes
|
52 |
for result in results:
|
53 |
-
im_array = result.plot()
|
54 |
-
|
55 |
|
56 |
cap.release()
|
57 |
-
|
58 |
-
|
59 |
-
temp_video_path = os.path.join(tempfile.gettempdir(), "output.mp4")
|
60 |
-
clip = ImageSequenceClip(processed_frames, fps=fps)
|
61 |
-
clip.write_videofile(temp_video_path, codec='libx264')
|
62 |
|
63 |
return temp_video_path
|
64 |
|
@@ -102,3 +107,4 @@ with gr.Blocks() as app:
|
|
102 |
|
103 |
if __name__ == "__main__":
|
104 |
app.launch()
|
|
|
|
1 |
+
```python
|
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 |
+
|
9 |
+
# Set Ultralytics config path
|
10 |
+
os.environ['YOLO_CONFIG_DIR'] = '/tmp/Ultralytics'
|
11 |
+
|
12 |
+
# Custom function to load model with weights_only=False
|
13 |
+
def load_model_with_trusted_weights(model_path):
|
14 |
+
import torch
|
15 |
+
with torch.serialization.safe_globals():
|
16 |
+
return YOLO(model_path)
|
17 |
|
18 |
# Load both YOLO models
|
19 |
+
model_yolo11 = load_model_with_trusted_weights('./data/yolo11n.pt')
|
20 |
+
model_best = load_model_with_trusted_weights('./data/best.pt')
|
21 |
|
22 |
def process_video(video_path, model_name, conf_threshold=0.4):
|
23 |
"""
|
24 |
Process the input video frame by frame using the selected YOLO model,
|
25 |
draw bounding boxes, and return the processed video path.
|
26 |
"""
|
27 |
+
# Select model
|
28 |
model = model_yolo11 if model_name == "YOLO11n" else model_best
|
29 |
|
30 |
# Open video capture
|
|
|
37 |
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
38 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
39 |
|
40 |
+
# Define output video path
|
41 |
+
temp_video_path = os.path.join(tempfile.gettempdir(), "output.mp4")
|
42 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Codec for MP4
|
43 |
+
out = cv2.VideoWriter(temp_video_path, fourcc, fps, (width, height))
|
44 |
|
45 |
while cap.isOpened():
|
46 |
ret, frame = cap.read()
|
|
|
58 |
|
59 |
# Draw bounding boxes
|
60 |
for result in results:
|
61 |
+
im_array = result.plot() # Plot boxes
|
62 |
+
out.write(im_array) # Write frame to output video
|
63 |
|
64 |
cap.release()
|
65 |
+
out.release()
|
66 |
+
cv2.destroyAllWindows()
|
|
|
|
|
|
|
67 |
|
68 |
return temp_video_path
|
69 |
|
|
|
107 |
|
108 |
if __name__ == "__main__":
|
109 |
app.launch()
|
110 |
+
```
|