Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,56 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
-
from ultralytics import YOLO
|
4 |
import cv2
|
5 |
-
import
|
|
|
6 |
|
7 |
-
# Load the
|
8 |
-
model =
|
9 |
|
10 |
-
def
|
11 |
results = model(image)
|
12 |
-
#
|
13 |
-
annotated_image = results[0].plot() # plot the results on the image
|
14 |
-
return annotated_image
|
15 |
|
16 |
-
def
|
17 |
-
# Read the video file
|
18 |
cap = cv2.VideoCapture(video)
|
19 |
-
|
20 |
-
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
21 |
-
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
22 |
-
|
23 |
-
# Create a temporary file to save the output video
|
24 |
-
out_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
|
25 |
-
out_path = out_file.name
|
26 |
-
|
27 |
-
# Define the codec and create VideoWriter object
|
28 |
-
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
29 |
-
out = cv2.VideoWriter(out_path, fourcc, fps, (width, height))
|
30 |
|
31 |
-
while
|
32 |
ret, frame = cap.read()
|
33 |
if not ret:
|
34 |
break
|
35 |
results = model(frame)
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
cap.release()
|
40 |
-
out.release()
|
41 |
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
# Create Gradio interface
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
)
|
58 |
|
59 |
if __name__ == "__main__":
|
60 |
-
|
61 |
|
62 |
|
63 |
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
|
|
3 |
import cv2
|
4 |
+
import numpy as np
|
5 |
+
from PIL import Image
|
6 |
|
7 |
+
# Load the YOLOv8 model
|
8 |
+
model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt', force_reload=True) # Adjust to yolov8 if needed
|
9 |
|
10 |
+
def process_image(image):
|
11 |
results = model(image)
|
12 |
+
return results.render()[0] # Returns an image with boxes drawn
|
|
|
|
|
13 |
|
14 |
+
def process_video(video):
|
|
|
15 |
cap = cv2.VideoCapture(video)
|
16 |
+
frames = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
while(cap.isOpened()):
|
19 |
ret, frame = cap.read()
|
20 |
if not ret:
|
21 |
break
|
22 |
results = model(frame)
|
23 |
+
frames.append(results.render()[0])
|
24 |
+
|
|
|
25 |
cap.release()
|
|
|
26 |
|
27 |
+
# Convert frames back to a video format
|
28 |
+
height, width, layers = frames[0].shape
|
29 |
+
video_out = cv2.VideoWriter('output.mp4', cv2.VideoWriter_fourcc(*'mp4v'), 30, (width, height))
|
30 |
+
|
31 |
+
for frame in frames:
|
32 |
+
video_out.write(frame)
|
33 |
+
|
34 |
+
video_out.release()
|
35 |
+
|
36 |
+
return 'output.mp4'
|
37 |
|
38 |
# Create Gradio interface
|
39 |
+
image_input = gr.inputs.Image(type="numpy", label="Upload an image")
|
40 |
+
video_input = gr.inputs.Video(type="mp4", label="Upload a video")
|
41 |
+
|
42 |
+
image_output = gr.outputs.Image(type="numpy", label="Detected image")
|
43 |
+
video_output = gr.outputs.Video(type="mp4", label="Detected video")
|
44 |
+
|
45 |
+
iface = gr.Interface(fn={'image': process_image, 'video': process_video},
|
46 |
+
inputs=[image_input, video_input],
|
47 |
+
outputs=[image_output, video_output],
|
48 |
+
live=True,
|
49 |
+
title="YOLOv8 Object Detection",
|
50 |
+
description="Upload an image or video to detect objects using YOLOv8.")
|
|
|
51 |
|
52 |
if __name__ == "__main__":
|
53 |
+
iface.launch()
|
54 |
|
55 |
|
56 |
|