SakshiRathi77's picture
Update app.py
73e8902 verified
import gradio as gr
import cv2
import numpy as np
import yolov9
def detect_objects_on_video(video_path, model_path, interval, image_size, conf_threshold, iou_threshold):
cap = cv2.VideoCapture(video_path)
count = 0
detections = []
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
count += 1
if count % interval == 0:
# Perform object detection on the frame
model = yolov9.load(model_path)
model.conf = conf_threshold
model.iou = iou_threshold
results = model(frame, size=image_size)
# Optionally, show detection bounding boxes on image
output = results.render()
detections.append(output)
cap.release()
cv2.destroyAllWindows()
return detections
def app():
with gr.Blocks():
with gr.Row():
with gr.Column():
video_path = gr.inputs.Video(label="Video")
model_path = gr.inputs.Dropdown(
label="Model",
choices=[
"best.pt",
],
default="best.pt",
)
interval = gr.inputs.Number(label="Screenshot Interval (seconds)", default=30, step=1)
image_size = gr.inputs.Slider(
label="Image Size",
minimum=320,
maximum=1280,
step=32,
default=640,
)
conf_threshold = gr.inputs.Slider(
label="Confidence Threshold",
minimum=0.1,
maximum=1.0,
step=0.1,
default=0.4,
)
iou_threshold = gr.inputs.Slider(
label="IoU Threshold",
minimum=0.1,
maximum=1.0,
step=0.1,
default=0.5,
)
yolov9_infer = gr.Button(value="Detect Objects")
with gr.Column():
output_images = gr.outputs.Image(label="Output Images")
yolov9_infer.click(
fn=detect_objects_on_video,
inputs=[
video_path,
model_path,
interval,
image_size,
conf_threshold,
iou_threshold,
],
outputs=output_images,
)
gradio_app = gr.Blocks()
with gradio_app:
gr.HTML(
"""
<h1 style='text-align: center'>
YOLOv9: Detect Objects in Video
</h1>
"""
)
with gr.Row():
with gr.Column():
app()
gradio_app.launch(debug=True)