kadirnar/Yolov10
101
1import gradio as gr2from ultralytics import YOLO3import supervision as sv4 5 6box_annotator = sv.BoxAnnotator()7category_dict = {8 0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 4: 'airplane', 5: 'bus',9 6: 'train', 7: 'truck', 8: 'boat', 9: 'traffic light', 10: 'fire hydrant',10 11: 'stop sign', 12: 'parking meter', 13: 'bench', 14: 'bird', 15: 'cat',11 16: 'dog', 17: 'horse', 18: 'sheep', 19: 'cow', 20: 'elephant', 21: 'bear',12 22: 'zebra', 23: 'giraffe', 24: 'backpack', 25: 'umbrella', 26: 'handbag',13 27: 'tie', 28: 'suitcase', 29: 'frisbee', 30: 'skis', 31: 'snowboard',14 32: 'sports ball', 33: 'kite', 34: 'baseball bat', 35: 'baseball glove',15 36: 'skateboard', 37: 'surfboard', 38: 'tennis racket', 39: 'bottle',16 40: 'wine glass', 41: 'cup', 42: 'fork', 43: 'knife', 44: 'spoon', 45: 'bowl',17 46: 'banana', 47: 'apple', 48: 'sandwich', 49: 'orange', 50: 'broccoli',18 51: 'carrot', 52: 'hot dog', 53: 'pizza', 54: 'donut', 55: 'cake',19 56: 'chair', 57: 'couch', 58: 'potted plant', 59: 'bed', 60: 'dining table',20 61: 'toilet', 62: 'tv', 63: 'laptop', 64: 'mouse', 65: 'remote', 66: 'keyboard',21 67: 'cell phone', 68: 'microwave', 69: 'oven', 70: 'toaster', 71: 'sink',22 72: 'refrigerator', 73: 'book', 74: 'clock', 75: 'vase', 76: 'scissors',23 77: 'teddy bear', 78: 'hair drier', 79: 'toothbrush'24}25 26 27 28def yolov10_inference(image, model_id, image_size, conf_threshold, iou_threshold):29 model = YOLO(f"{model_id}.pt")30 results = model(source=image, imgsz=image_size, iou=iou_threshold, conf=conf_threshold, verbose=False)[0]31 detections = sv.Detections.from_ultralytics(results)32 labels = [33 f"{category_dict[class_id]} {confidence:.2f}"34 for class_id, confidence in zip(detections.class_id, detections.confidence)35 ]36 annotated_image = box_annotator.annotate(image, detections=detections, labels=labels)37 38 return annotated_image39 40def app():41 with gr.Blocks():42 with gr.Row():43 with gr.Column():44 image = gr.Image(type="pil", label="Image")45 46 model_id = gr.Dropdown(47 label="Model",48 choices=[49 "yolov10n",50 "yolov10s",51 "yolov10m",52 "yolov10b",53 "yolov10l",54 "yolov10x",55 ],56 value="yolov10m",57 )58 image_size = gr.Slider(59 label="Image Size",60 minimum=320,61 maximum=1280,62 step=32,63 value=640,64 )65 conf_threshold = gr.Slider(66 label="Confidence Threshold",67 minimum=0.1,68 maximum=1.0,69 step=0.1,70 value=0.25,71 )72 iou_threshold = gr.Slider(73 label="IoU Threshold",74 minimum=0.1,75 maximum=1.0,76 step=0.1,77 value=0.45,78 )79 yolov10_infer = gr.Button(value="Detect Objects")80 81 with gr.Column():82 output_image = gr.Image(type="pil", label="Annotated Image")83 84 yolov10_infer.click(85 fn=yolov10_inference,86 inputs=[87 image,88 model_id,89 image_size,90 conf_threshold,91 iou_threshold,92 ],93 outputs=[output_image],94 )95 96 gr.Examples(97 examples=[98 [99 "dog.jpeg",100 "yolov10x",101 640,102 0.25,103 0.45,104 ],105 [106 "huggingface.jpg",107 "yolov10m",108 640,109 0.25,110 0.45,111 ],112 [113 "zidane.jpg",114 "yolov10b",115 640,116 0.25,117 0.45,118 ],119 ],120 fn=yolov10_inference,121 inputs=[122 image,123 model_id,124 image_size,125 conf_threshold,126 iou_threshold,127 ],128 outputs=[output_image],129 cache_examples="lazy",130 )131 132gradio_app = gr.Blocks()133with gradio_app:134 gr.HTML(135 """136 <h1 style='text-align: center'>137 YOLOv10: Real-Time End-to-End Object Detection138 </h1>139 """)140 gr.HTML(141 """142 <h3 style='text-align: center'>143 Follow me for more!144 <a href='https://twitter.com/kadirnar_ai' target='_blank'>Twitter</a> | <a href='https://github.com/kadirnar' target='_blank'>Github</a> | <a href='https://www.linkedin.com/in/kadir-nar/' target='_blank'>Linkedin</a> | <a href='https://www.huggingface.co/kadirnar/' target='_blank'>HuggingFace</a>145 </h3>146 """)147 with gr.Row():148 with gr.Column():149 app()150 151gradio_app.launch(debug=True)