Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,71 +1,57 @@
|
|
1 |
import cv2
|
2 |
import torch
|
3 |
-
import gradio
|
4 |
import numpy as np
|
5 |
from ultralytics import YOLO
|
6 |
|
7 |
-
# Load YOLOv8 model
|
8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
9 |
-
|
10 |
-
model.to(device)
|
11 |
|
12 |
-
# Define the function that processes the uploaded video
|
13 |
def process_video(video):
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
# Resize to reduce computation (optional)
|
23 |
-
new_width, new_height = 640, 480 # Resize to 640x480 resolution
|
24 |
-
frame_width, frame_height = new_width, new_height
|
25 |
-
|
26 |
-
# Create a VideoWriter object to write processed frames to an output file
|
27 |
-
output_video_path = "processed_output.mp4"
|
28 |
-
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Codec for .mp4 format
|
29 |
-
output_video = cv2.VideoWriter(output_video_path, fourcc, fps, (frame_width, frame_height))
|
30 |
-
|
31 |
-
frame_skip = 1 # Skip 10 frames between each processed frame
|
32 |
frame_count = 0
|
33 |
-
|
|
|
|
|
34 |
while True:
|
35 |
-
|
36 |
-
ret
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
frame_count += 1
|
41 |
if frame_count % frame_skip != 0:
|
42 |
-
continue
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
#
|
48 |
-
results = model(frame)
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
description="Upload a video for object detection using YOLOv8")
|
69 |
-
|
70 |
-
# Launch the interface
|
71 |
-
iface.launch()
|
|
|
1 |
import cv2
|
2 |
import torch
|
3 |
+
import gradio
|
4 |
import numpy as np
|
5 |
from ultralytics import YOLO
|
6 |
|
7 |
+
# Load YOLOv8 model
|
8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
9 |
+
print(f"Using device: {device}") # Debug
|
10 |
+
model = YOLO('./data/best.pt').to(device)
|
11 |
|
|
|
12 |
def process_video(video):
|
13 |
+
cap = cv2.VideoCapture(video)
|
14 |
+
frame_width, frame_height = 320, 240 # Smaller resolution
|
15 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
16 |
+
|
17 |
+
output_path = "processed_output.mp4"
|
18 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
19 |
+
out = cv2.VideoWriter(output_path, fourcc, fourcc, fps, (frame_width, frame_height))
|
20 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
frame_count = 0
|
22 |
+
frame_skip = 5 # Process every 5th frame
|
23 |
+
max_frames = 100 # Limit to 100 frames for testing
|
24 |
+
|
25 |
while True:
|
26 |
+
ret, frame = cap.read()
|
27 |
+
if not ret or frame_count > max_frames:
|
28 |
+
break
|
29 |
+
|
|
|
30 |
frame_count += 1
|
31 |
if frame_count % frame_skip != 0:
|
32 |
+
continue
|
33 |
+
|
34 |
+
frame = cv2.resize(frame, (frame_width, frame_height))
|
35 |
+
print(f"Processing frame {frame_count}") # Debug
|
36 |
+
|
37 |
+
# Inference
|
38 |
+
results = model(frame)
|
39 |
+
annotated_frame = results[0].plot()
|
40 |
+
|
41 |
+
# Write frame
|
42 |
+
out.write(annotated_frame)
|
43 |
+
|
44 |
+
cap.release()
|
45 |
+
out.release()
|
46 |
+
return output_path
|
47 |
+
|
48 |
+
# Gradio interface
|
49 |
+
iface = gr.Interface(
|
50 |
+
fn=process_video,
|
51 |
+
inputs=gr.Video(label="Upload Video"),
|
52 |
+
outputs=gr.Video(label="Processed Video"),
|
53 |
+
title="YOLOv8 Object Detection",
|
54 |
+
description="Upload a short video for testing"
|
55 |
+
)
|
56 |
+
|
57 |
+
iface.launch()
|
|
|
|
|
|
|
|