Detomo/Object_detection
9
1import gradio as gr2import torch3from sahi.prediction import ObjectPrediction4from sahi.utils.cv import visualize_object_predictions, read_image5from ultralyticsplus import YOLO, render_result6 7 8def yolov8_inference(9 image,10 model_path,11 image_size,12 conf_threshold,13 iou_threshold,14):15 """16 YOLOv8 inference function17 Args:18 image: Input image19 model_path: Path to the model20 image_size: Image size21 conf_threshold: Confidence threshold22 iou_threshold: IOU threshold23 Returns:24 Rendered image25 """26 model = YOLO(f'kadirnar/{model_path}-v8.0')27 # set model parameters28 model.overrides['conf'] = conf_threshold # NMS confidence threshold29 model.overrides['iou'] = iou_threshold # NMS IoU threshold30 model.overrides['agnostic_nms'] = False # NMS class-agnostic31 model.overrides['max_det'] = 1000 # maximum number of detections per image32 results = model.predict(image, imgsz=image_size)33 render = render_result(model=model, image=image, result=results[0])34 return render35 36 37inputs = [38 gr.Image(type="filepath", label="Input Image"),39 gr.Dropdown(["yolov8n", "yolov8m", "yolov8l", "yolov8x"], 40 value="yolov8m", label="Model"),41 gr.Slider(minimum=320, maximum=1280, value=640, step=320, label="Image Size"),42 gr.Slider(minimum=0.0, maximum=1.0, value=0.25, step=0.05, label="Confidence Threshold"),43 gr.Slider(minimum=0.0, maximum=1.0, value=0.45, step=0.05, label="IOU Threshold"),44]45 46outputs = gr.Image(type="filepath", label="Output Image")47title = "State-of-the-Art YOLO Models for Object detection"48 49examples = [['demo_01.jpg', 'yolov8n', 640, 0.25, 0.45], ['demo_02.jpg', 'yolov8l', 640, 0.25, 0.45], ['demo_03.jpg', 'yolov8x', 1280, 0.25, 0.45]]50demo_app = gr.Interface(51 fn=yolov8_inference,52 inputs=inputs,53 outputs=outputs,54 title=title,55 examples=examples,56 cache_examples=True,57)58demo_app.launch(debug=True)