Spaces:
Sleeping
Sleeping
Use separate tab interface for image & video UI
Browse files- app.py +70 -50
- requirements.txt +1 -1
app.py
CHANGED
@@ -1,68 +1,88 @@
|
|
1 |
-
from ultralytics import YOLO
|
2 |
import gradio as gr
|
|
|
|
|
|
|
3 |
import cv2
|
4 |
import tempfile
|
5 |
-
import os
|
6 |
-
import subprocess
|
7 |
|
8 |
# Load YOLOv8 model
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
# Function to process videos
|
13 |
def process_video(video_path):
|
14 |
-
#
|
|
|
|
|
15 |
cap = cv2.VideoCapture(video_path)
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
23 |
|
24 |
-
# Define a temporary raw output filename
|
25 |
-
raw_output = tempfile.NamedTemporaryFile(suffix=".avi", delete=False).name
|
26 |
-
|
27 |
-
# Define codec and output format for intermediate video
|
28 |
-
fourcc = cv2.VideoWriter_fourcc(*'XVID') # Intermediate codec
|
29 |
-
out = cv2.VideoWriter(raw_output, fourcc, fps, (width, height))
|
30 |
-
|
31 |
while cap.isOpened():
|
32 |
ret, frame = cap.read()
|
33 |
if not ret:
|
34 |
break
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
#
|
40 |
-
annotated_frame = results[0].plot()
|
41 |
-
|
42 |
-
# Write the annotated frame to the intermediate output video
|
43 |
-
out.write(annotated_frame)
|
44 |
|
45 |
cap.release()
|
46 |
out.release()
|
|
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
-
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from ultralytics import YOLO
|
3 |
+
from PIL import Image
|
4 |
+
import os
|
5 |
import cv2
|
6 |
import tempfile
|
|
|
|
|
7 |
|
8 |
# Load YOLOv8 model
|
9 |
+
model = YOLO('./best_yolo8_model/best.pt')
|
10 |
+
|
11 |
+
# Define fixed dimensions
|
12 |
+
image_height = 300
|
13 |
+
image_width = 400
|
14 |
+
video_height = 300
|
15 |
+
video_width = 400
|
16 |
+
|
17 |
+
def process_image(image):
|
18 |
+
# Perform inference on the uploaded image
|
19 |
+
results = model.predict(source=image, conf=0.5)
|
20 |
+
result_img = results[0].plot()
|
21 |
+
# Ensure image color stays consistent
|
22 |
+
return Image.fromarray(cv2.cvtColor(result_img, cv2.COLOR_BGR2RGB))
|
23 |
|
|
|
24 |
def process_video(video_path):
|
25 |
+
# Perform inference on the uploaded video
|
26 |
+
temp_dir = tempfile.mkdtemp()
|
27 |
+
output_video_path = os.path.join(temp_dir, "processed_video.mp4")
|
28 |
cap = cv2.VideoCapture(video_path)
|
29 |
+
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
30 |
+
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
31 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
32 |
+
|
33 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
34 |
+
out = cv2.VideoWriter(output_video_path, fourcc, fps, (frame_width, frame_height))
|
|
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
while cap.isOpened():
|
37 |
ret, frame = cap.read()
|
38 |
if not ret:
|
39 |
break
|
40 |
+
results = model.predict(source=frame, conf=0.5)
|
41 |
+
result_frame = results[0].plot()
|
42 |
+
# Ensure color correction for the video frame
|
43 |
+
result_frame_corrected = cv2.cvtColor(result_frame, cv2.COLOR_BGR2RGB)
|
44 |
+
out.write(cv2.cvtColor(result_frame_corrected, cv2.COLOR_RGB2BGR)) # Maintain video color format
|
|
|
|
|
|
|
|
|
45 |
|
46 |
cap.release()
|
47 |
out.release()
|
48 |
+
return output_video_path
|
49 |
|
50 |
+
# Create Gradio interface with tabs
|
51 |
+
with gr.Blocks() as app:
|
52 |
+
gr.Markdown("## YOLOv8 Object Detection - Image & Video")
|
53 |
+
gr.Markdown(
|
54 |
+
"This app detects objects in images and videos using a YOLOv8s model. It detects DURIAN and RAMBUTAN fruits. Use the tabs below to process images or videos."
|
55 |
+
)
|
56 |
+
|
57 |
+
with gr.Tabs():
|
58 |
+
with gr.TabItem("Image Detection"):
|
59 |
+
with gr.Row():
|
60 |
+
with gr.Column():
|
61 |
+
image_input = gr.Image(
|
62 |
+
type="pil", label="Input Image", elem_id="image_input",
|
63 |
+
width=image_width, height=image_height
|
64 |
+
)
|
65 |
+
with gr.Column():
|
66 |
+
image_output = gr.Image(
|
67 |
+
type="pil", label="Output Image", elem_id="image_output",
|
68 |
+
width=image_width, height=image_height
|
69 |
+
)
|
70 |
+
image_submit = gr.Button("Detect Objects in Image")
|
71 |
+
image_submit.click(process_image, inputs=image_input, outputs=image_output)
|
72 |
+
|
73 |
+
with gr.TabItem("Video Detection"):
|
74 |
+
with gr.Row():
|
75 |
+
with gr.Column():
|
76 |
+
video_input = gr.Video(
|
77 |
+
label="Input Video", elem_id="video_input",
|
78 |
+
width=video_width, height=video_height
|
79 |
+
)
|
80 |
+
with gr.Column():
|
81 |
+
video_output = gr.Video(
|
82 |
+
label="Output Video", elem_id="video_output",
|
83 |
+
width=video_width, height=video_height
|
84 |
+
)
|
85 |
+
video_submit = gr.Button("Detect Objects in Video")
|
86 |
+
video_submit.click(process_video, inputs=video_input, outputs=video_output)
|
87 |
|
88 |
+
app.launch()
|
requirements.txt
CHANGED
@@ -2,4 +2,4 @@ ultralytics
|
|
2 |
gradio
|
3 |
huggingface_hub
|
4 |
pillow
|
5 |
-
|
|
|
2 |
gradio
|
3 |
huggingface_hub
|
4 |
pillow
|
5 |
+
ffmpeg-python
|