2lu's picture
yolov8 accident detector
32519f3
raw
history blame
3.08 kB
import gradio as gr
# import torch
from ultralyticsplus import YOLO, render_result
classes: ['car', 'bike', 'person', 'car_car_accident', 'car_bike_accident', 'car_person_accident', 'bike_bike_accidnet', 'bike_person_accident', 'car_object_accident', 'bike_object_accident']
def yolov8_func(image):
#image_size: gr.inputs.Slider = 640,
#conf_threshold: gr.inputs.Slider = 0.4,
#iou_threshold: gr.inputs.Slider = 0.50):
model_path = "best.pt"
model = YOLO(model_path)
results = model.predict(image,
conf = 0.4,
iou = 0.6,
imgsz = 640)
box = results[0].boxes
print("Object type: ", box.cls)
# print("Coordinates: ", box.xyxy)
# print("Probability: ", box.conf)
render = render_result(model=model, image=image, result=results[0])
return render, box.cls
# inputs = [
# gr.inputs.Image(type="filepath", label="Input Image"),
# gr.inputs.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size"),
# gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.25, step=0.05, label="Confidence Threshold"),
# gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.45, step=0.05, label="Iou Threshold")
# ]
# gr.HTML
# outputs = gr.outputs.Image(type="filepath", label="Output Image")
# yolo_app = gr.Interface(
# fn=yolov8_func,
# inputs=inputs,
# outputs=outputs,
# title="Accident detector",
# )
# yolo_app.launch(debug=True, enable_queue=True)
with gr.Blocks(title="YOLOS Object Detection - ClassCat", css=".gradio-container {background:lightyellow;}") as demo:
gr.HTML('<h1>Yolo Object Detection</h1>')
#gr.HTML("<h4>supported objects are [aeroplane,bicycle,bird,boat,bottle,bus,car,cat,chair,cow,diningtable,dog,horse,motorbike,person,pottedplant,sheep,sofa,train,tvmonitor]</h4>")
gr.HTML("<br>")
with gr.Row():
input_image = gr.Image(label="Input image", type="pil")
output_image = gr.Image(label="Output image", type="pil")
output_label = gr.Text(label="output label")
gr.HTML("<br>")
#gr.HTML("<h4>object centre detection threshold means the object centre will be considered a new object if it's value is above threshold</h4>")
#gr.HTML("<p>less means more objects</p>")
#gr.HTML("<h4>bounding box threshold is IOU value threshold. If intersection/union area of two bounding boxes are greater than threshold value the one box will be suppressed</h4>")
#gr.HTML("<p>more means more bounding boxes<p>")
#gr.HTML("<br>")
#obj_threshold = gr.Slider(0, 1.0, value=0.2, label=' object centre detection threshold')
#gr.HTML("<br>")
#bb_threshold = gr.Slider(0, 1.0, value=0.3, label=' bounding box draw threshold')
#gr.HTML("<br>")
send_btn = gr.Button("Detect")
gr.HTML("<br>")
#gr.Examples(['./samples/out_1.jpg'], inputs=input_image)
send_btn.click(fn=yolov8_func, inputs=[input_image], outputs=[output_image, output_label])
demo.launch(debug=True)