Update
Browse files
app.py
CHANGED
|
@@ -1,14 +1,9 @@
|
|
| 1 |
-
import time
|
| 2 |
import gradio as gr
|
| 3 |
import cv2 as cv
|
| 4 |
from ultralytics import YOLO
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
model = None
|
| 9 |
-
|
| 10 |
-
def load_yolo_model():
|
| 11 |
-
return YOLO('yolov8n-seg.pt')
|
| 12 |
|
| 13 |
def resize_frame(frame, width=1280):
|
| 14 |
height, width = frame.shape[:2]
|
|
@@ -16,43 +11,20 @@ def resize_frame(frame, width=1280):
|
|
| 16 |
return cv.resize(frame, (width, new_height))
|
| 17 |
|
| 18 |
def process_frame(frame):
|
| 19 |
-
global model
|
| 20 |
frame = resize_frame(frame)
|
| 21 |
-
start = time.perf_counter()
|
| 22 |
results = model(frame)
|
| 23 |
-
end = time.perf_counter()
|
| 24 |
segments = results[0].plot()
|
| 25 |
-
return segments
|
| 26 |
-
|
| 27 |
-
def process_inputs(start_button, stop_button, mode_selection, frame, uploaded_video):
|
| 28 |
-
global process, model
|
| 29 |
-
if start_button and not process:
|
| 30 |
-
process = True
|
| 31 |
-
model = load_yolo_model()
|
| 32 |
-
if mode_selection == "Webcam" and frame is not None:
|
| 33 |
-
return process_frame(frame)
|
| 34 |
-
elif mode_selection == "Video" and uploaded_video is not None:
|
| 35 |
-
cap = cv.VideoCapture(uploaded_video.name)
|
| 36 |
-
ret, frame = cap.read()
|
| 37 |
-
if ret:
|
| 38 |
-
return process_frame(frame)
|
| 39 |
-
elif stop_button and process:
|
| 40 |
-
process = False
|
| 41 |
-
return None, ""
|
| 42 |
|
| 43 |
-
start_button = gr.components.Checkbox(label="Start")
|
| 44 |
-
stop_button = gr.components.Checkbox(label="Stop")
|
| 45 |
-
mode_selection = gr.components.Radio(["Webcam", "Video"], label="Mode Selection")
|
| 46 |
frame = gr.components.Video(label="Webcam Feed")
|
| 47 |
-
uploaded_video = gr.components.Video(label="Upload Video")
|
| 48 |
|
| 49 |
iface = gr.Interface(
|
| 50 |
-
fn=
|
| 51 |
-
inputs=[
|
| 52 |
-
outputs=[gr.components.Image()
|
| 53 |
live=True,
|
| 54 |
title="YOLO Image Segmentation",
|
| 55 |
-
description="This application uses the YOLO model to perform image segmentation on a webcam feed
|
| 56 |
)
|
| 57 |
|
| 58 |
iface.launch(share=True)
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2 as cv
|
| 3 |
from ultralytics import YOLO
|
| 4 |
|
| 5 |
+
# Load the YOLO model
|
| 6 |
+
model = YOLO('yolov8n-seg.pt')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def resize_frame(frame, width=1280):
|
| 9 |
height, width = frame.shape[:2]
|
|
|
|
| 11 |
return cv.resize(frame, (width, new_height))
|
| 12 |
|
| 13 |
def process_frame(frame):
|
|
|
|
| 14 |
frame = resize_frame(frame)
|
|
|
|
| 15 |
results = model(frame)
|
|
|
|
| 16 |
segments = results[0].plot()
|
| 17 |
+
return segments
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
|
|
|
|
|
|
|
|
|
| 19 |
frame = gr.components.Video(label="Webcam Feed")
|
|
|
|
| 20 |
|
| 21 |
iface = gr.Interface(
|
| 22 |
+
fn=process_frame,
|
| 23 |
+
inputs=[frame],
|
| 24 |
+
outputs=[gr.components.Image()],
|
| 25 |
live=True,
|
| 26 |
title="YOLO Image Segmentation",
|
| 27 |
+
description="This application uses the YOLO model to perform image segmentation on a webcam feed.",
|
| 28 |
)
|
| 29 |
|
| 30 |
iface.launch(share=True)
|