Spaces:
Runtime error
Runtime error
added app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
import yolov9
|
| 5 |
+
|
| 6 |
+
def detect_objects_on_video(video_path, model_path, interval, image_size, conf_threshold, iou_threshold):
|
| 7 |
+
cap = cv2.VideoCapture(video_path)
|
| 8 |
+
count = 0
|
| 9 |
+
detections = []
|
| 10 |
+
while cap.isOpened():
|
| 11 |
+
ret, frame = cap.read()
|
| 12 |
+
if not ret:
|
| 13 |
+
break
|
| 14 |
+
|
| 15 |
+
count += 1
|
| 16 |
+
if count % interval == 0:
|
| 17 |
+
# Perform object detection on the frame
|
| 18 |
+
model = yolov9.load(model_path)
|
| 19 |
+
model.conf = conf_threshold
|
| 20 |
+
model.iou = iou_threshold
|
| 21 |
+
results = model(frame, size=image_size)
|
| 22 |
+
|
| 23 |
+
# Optionally, show detection bounding boxes on image
|
| 24 |
+
output = results.render()
|
| 25 |
+
detections.append(output[0])
|
| 26 |
+
|
| 27 |
+
cap.release()
|
| 28 |
+
cv2.destroyAllWindows()
|
| 29 |
+
return detections
|
| 30 |
+
|
| 31 |
+
def app():
|
| 32 |
+
with gr.Blocks():
|
| 33 |
+
with gr.Row():
|
| 34 |
+
with gr.Column():
|
| 35 |
+
video_path = gr.Video(type="file", label="Video")
|
| 36 |
+
model_path = gr.Dropdown(
|
| 37 |
+
label="Model",
|
| 38 |
+
choices=[
|
| 39 |
+
"best.pt",
|
| 40 |
+
],
|
| 41 |
+
value="./best.pt",
|
| 42 |
+
)
|
| 43 |
+
interval = gr.Number(label="Screenshot Interval (seconds)", default=30, step=1)
|
| 44 |
+
image_size = gr.Slider(
|
| 45 |
+
label="Image Size",
|
| 46 |
+
minimum=320,
|
| 47 |
+
maximum=1280,
|
| 48 |
+
step=32,
|
| 49 |
+
value=640,
|
| 50 |
+
)
|
| 51 |
+
conf_threshold = gr.Slider(
|
| 52 |
+
label="Confidence Threshold",
|
| 53 |
+
minimum=0.1,
|
| 54 |
+
maximum=1.0,
|
| 55 |
+
step=0.1,
|
| 56 |
+
value=0.4,
|
| 57 |
+
)
|
| 58 |
+
iou_threshold = gr.Slider(
|
| 59 |
+
label="IoU Threshold",
|
| 60 |
+
minimum=0.1,
|
| 61 |
+
maximum=1.0,
|
| 62 |
+
step=0.1,
|
| 63 |
+
value=0.5,
|
| 64 |
+
)
|
| 65 |
+
yolov9_infer = gr.Button(value="Detect Objects")
|
| 66 |
+
|
| 67 |
+
with gr.Column():
|
| 68 |
+
output_images = gr.Image(type="numpy", label="Output Images")
|
| 69 |
+
|
| 70 |
+
yolov9_infer.click(
|
| 71 |
+
fn=detect_objects_on_video,
|
| 72 |
+
inputs=[
|
| 73 |
+
video_path,
|
| 74 |
+
model_path,
|
| 75 |
+
interval,
|
| 76 |
+
image_size,
|
| 77 |
+
conf_threshold,
|
| 78 |
+
iou_threshold,
|
| 79 |
+
],
|
| 80 |
+
outputs=[output_images],
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
gradio_app = gr.Blocks()
|
| 84 |
+
with gradio_app:
|
| 85 |
+
gr.HTML(
|
| 86 |
+
"""
|
| 87 |
+
<h1 style='text-align: center'>
|
| 88 |
+
YOLOv9: Detect Objects in Video
|
| 89 |
+
</h1>
|
| 90 |
+
"""
|
| 91 |
+
)
|
| 92 |
+
with gr.Row():
|
| 93 |
+
with gr.Column():
|
| 94 |
+
app()
|
| 95 |
+
|
| 96 |
+
gradio_app.launch(debug=True)
|