Update
Browse files
app.py
CHANGED
|
@@ -6,65 +6,49 @@ from ultralytics import YOLO
|
|
| 6 |
# Global variables to control the process
|
| 7 |
process = False
|
| 8 |
model = None
|
| 9 |
-
cap = None
|
| 10 |
|
| 11 |
def load_yolo_model():
|
| 12 |
return YOLO('yolov8n-seg.pt')
|
| 13 |
|
| 14 |
-
def
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
cap.release()
|
| 31 |
-
cv.destroyAllWindows()
|
| 32 |
-
|
| 33 |
-
def segment_video(uploaded_video):
|
| 34 |
-
global model, cap
|
| 35 |
-
model = load_yolo_model()
|
| 36 |
-
cap = cv.VideoCapture(uploaded_video.name)
|
| 37 |
-
process_video()
|
| 38 |
-
|
| 39 |
-
def segment_webcam():
|
| 40 |
-
global model, cap
|
| 41 |
-
model = load_yolo_model()
|
| 42 |
-
cap = cv.VideoCapture(0)
|
| 43 |
-
process_video()
|
| 44 |
-
|
| 45 |
-
def process_inputs(start_button, stop_button, mode_selection, uploaded_video):
|
| 46 |
-
global process
|
| 47 |
if start_button and mode_selection:
|
| 48 |
process = True
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
| 53 |
elif stop_button:
|
| 54 |
process = False
|
| 55 |
|
| 56 |
-
start_button = gr.
|
| 57 |
-
stop_button = gr.
|
| 58 |
-
mode_selection = gr.
|
| 59 |
-
|
|
|
|
| 60 |
|
| 61 |
iface = gr.Interface(
|
| 62 |
fn=process_inputs,
|
| 63 |
-
inputs=[start_button, stop_button, mode_selection, uploaded_video],
|
| 64 |
-
outputs=
|
| 65 |
live=True,
|
| 66 |
title="YOLO Image Segmentation",
|
| 67 |
-
description="This application uses the YOLO model to perform image segmentation on a
|
| 68 |
theme="huggingface"
|
| 69 |
)
|
| 70 |
|
|
|
|
| 6 |
# Global variables to control the process
|
| 7 |
process = False
|
| 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]
|
| 15 |
+
new_height = int(height * width / float(width))
|
| 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, f'FPS: {int(1 // (end - start))}'
|
| 26 |
+
|
| 27 |
+
def process_inputs(start_button, stop_button, mode_selection, frame, uploaded_video):
|
| 28 |
+
global process, model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
if start_button and mode_selection:
|
| 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 |
+
return process_frame(uploaded_video)
|
| 36 |
elif stop_button:
|
| 37 |
process = False
|
| 38 |
|
| 39 |
+
start_button = gr.inputs.Button(label="Start")
|
| 40 |
+
stop_button = gr.inputs.Button(label="Stop")
|
| 41 |
+
mode_selection = gr.inputs.Radio(["Webcam", "Video"], label="Mode Selection")
|
| 42 |
+
frame = gr.inputs.Video(shape=(720, 1280), label="Webcam Feed")
|
| 43 |
+
uploaded_video = gr.inputs.Video(shape=(720, 1280), label="Upload Video")
|
| 44 |
|
| 45 |
iface = gr.Interface(
|
| 46 |
fn=process_inputs,
|
| 47 |
+
inputs=[start_button, stop_button, mode_selection, frame, uploaded_video],
|
| 48 |
+
outputs=[gr.outputs.Image(), gr.outputs.Textbox()],
|
| 49 |
live=True,
|
| 50 |
title="YOLO Image Segmentation",
|
| 51 |
+
description="This application uses the YOLO model to perform image segmentation on a webcam feed or an uploaded video. Select 'Webcam' or 'Video', upload a video (if applicable), and click 'Start' to begin. Click 'Stop' to end the process.",
|
| 52 |
theme="huggingface"
|
| 53 |
)
|
| 54 |
|