Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import supervision as sv
|
| 3 |
+
from ultralytics import YOLO
|
| 4 |
+
import numpy as np
|
| 5 |
+
import cv2 # for video to image conversion
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
model = YOLO('yolov8s.pt')
|
| 9 |
+
byte_tracker = sv.ByteTrack()
|
| 10 |
+
annotator = sv.BoxAnnotator()
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def process_video(frame):
|
| 14 |
+
results = model(frame)[0]
|
| 15 |
+
detections = sv.Detections.from_ultralytics(results)
|
| 16 |
+
detections = byte_tracker.update_with_detections(detections)
|
| 17 |
+
labels = [
|
| 18 |
+
f"#{tracker_id} {model.model.names[class_id]} {confidence:0.2f}"
|
| 19 |
+
for _, _, confidence, class_id, tracker_id
|
| 20 |
+
in detections
|
| 21 |
+
]
|
| 22 |
+
yield annotator.annotate(scene=frame.copy(),
|
| 23 |
+
detections=detections, labels=labels)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
title = "Object Tracking (w/ YOLOv8)"
|
| 27 |
+
with gr.Blocks() as io:
|
| 28 |
+
gr.Markdown(f"<center><h1>{title}</h1></center>")
|
| 29 |
+
with gr.Row():
|
| 30 |
+
with gr.Column():
|
| 31 |
+
input_image = gr.Image(source='webcam', streaming=True)
|
| 32 |
+
input_button = gr.Button()
|
| 33 |
+
with gr.Column():
|
| 34 |
+
output_image = gr.Image()
|
| 35 |
+
input_image.change(process_video2, inputs=[input_image], outputs=[output_image], show_progress=False)
|
| 36 |
+
|
| 37 |
+
io.queue()
|
| 38 |
+
|
| 39 |
+
io.launch()
|