Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,28 +5,42 @@ import numpy as np
|
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
# Load the YOLOv8 model
|
| 8 |
-
model = torch.hub.load('ultralytics/
|
| 9 |
|
| 10 |
def process_image(image):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
results = model(image)
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
def process_video(
|
| 15 |
-
cap = cv2.VideoCapture(
|
| 16 |
frames = []
|
| 17 |
|
| 18 |
while(cap.isOpened()):
|
| 19 |
ret, frame = cap.read()
|
| 20 |
if not ret:
|
| 21 |
break
|
|
|
|
|
|
|
| 22 |
results = model(frame)
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
cap.release()
|
| 26 |
|
| 27 |
# Convert frames back to a video format
|
| 28 |
height, width, layers = frames[0].shape
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
for frame in frames:
|
| 32 |
video_out.write(frame)
|
|
@@ -36,16 +50,15 @@ def process_video(video):
|
|
| 36 |
return 'output.mp4'
|
| 37 |
|
| 38 |
# Create Gradio interface
|
| 39 |
-
image_input = gr.inputs.Image(type="
|
| 40 |
video_input = gr.inputs.Video(type="mp4", label="Upload a video")
|
| 41 |
|
| 42 |
-
image_output = gr.outputs.Image(type="
|
| 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 |
|
|
@@ -53,4 +66,5 @@ if __name__ == "__main__":
|
|
| 53 |
iface.launch()
|
| 54 |
|
| 55 |
|
|
|
|
| 56 |
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
# Load the YOLOv8 model
|
| 8 |
+
model = torch.hub.load('ultralytics/yolov8', 'custom', path='best.pt') # YOLOv8 specific
|
| 9 |
|
| 10 |
def process_image(image):
|
| 11 |
+
# Convert PIL image to numpy array if necessary
|
| 12 |
+
if isinstance(image, Image.Image):
|
| 13 |
+
image = np.array(image)
|
| 14 |
+
|
| 15 |
+
# Perform detection
|
| 16 |
results = model(image)
|
| 17 |
+
|
| 18 |
+
# Render results
|
| 19 |
+
annotated_image = results.render()[0]
|
| 20 |
+
return Image.fromarray(annotated_image)
|
| 21 |
|
| 22 |
+
def process_video(video_path):
|
| 23 |
+
cap = cv2.VideoCapture(video_path)
|
| 24 |
frames = []
|
| 25 |
|
| 26 |
while(cap.isOpened()):
|
| 27 |
ret, frame = cap.read()
|
| 28 |
if not ret:
|
| 29 |
break
|
| 30 |
+
|
| 31 |
+
# Perform detection
|
| 32 |
results = model(frame)
|
| 33 |
+
|
| 34 |
+
# Render results
|
| 35 |
+
annotated_frame = results.render()[0]
|
| 36 |
+
frames.append(annotated_frame)
|
| 37 |
|
| 38 |
cap.release()
|
| 39 |
|
| 40 |
# Convert frames back to a video format
|
| 41 |
height, width, layers = frames[0].shape
|
| 42 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 43 |
+
video_out = cv2.VideoWriter('output.mp4', fourcc, 30, (width, height))
|
| 44 |
|
| 45 |
for frame in frames:
|
| 46 |
video_out.write(frame)
|
|
|
|
| 50 |
return 'output.mp4'
|
| 51 |
|
| 52 |
# Create Gradio interface
|
| 53 |
+
image_input = gr.inputs.Image(type="pil", label="Upload an image")
|
| 54 |
video_input = gr.inputs.Video(type="mp4", label="Upload a video")
|
| 55 |
|
| 56 |
+
image_output = gr.outputs.Image(type="pil", label="Detected image")
|
| 57 |
video_output = gr.outputs.Video(type="mp4", label="Detected video")
|
| 58 |
|
| 59 |
iface = gr.Interface(fn={'image': process_image, 'video': process_video},
|
| 60 |
inputs=[image_input, video_input],
|
| 61 |
outputs=[image_output, video_output],
|
|
|
|
| 62 |
title="YOLOv8 Object Detection",
|
| 63 |
description="Upload an image or video to detect objects using YOLOv8.")
|
| 64 |
|
|
|
|
| 66 |
iface.launch()
|
| 67 |
|
| 68 |
|
| 69 |
+
|
| 70 |
|