import gradio as gr from PIL import Image from ultralytics import YOLO # Load YOLO model once globally model = YOLO("best.pt") def predict_image(img, conf_threshold, iou_threshold): """Predict objects in an image using YOLOv8 with adjustable thresholds.""" results = model.predict( source=img, conf=conf_threshold, iou=iou_threshold, imgsz=640, show_labels=True, show_conf=True, ) r = results[0] im_array = r.plot() im = Image.fromarray(im_array[..., ::-1]) # Convert BGR → RGB return im iface = gr.Interface( fn=predict_image, inputs=[ gr.Image(type="pil", label="Upload Image"), gr.Slider(minimum=0, maximum=1, value=0.05, label="Confidence threshold (Object detection confidence)"), gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold (Overlap between boxes)") ], outputs=gr.Image(type="pil", label="Result"), title="Urban Issues Detection Gradio Application 🚀", description="Upload images for inference. The trained YOLOv8 model will detect the urban issues.", examples=[ ["samples/test1.jpg", 0.05, 0.45], ["samples/test3.jpg", 0.05, 0.45], ["samples/test4.jpg", 0.05, 0.45], ["samples/test5.jpg", 0.05, 0.45], ], ) iface.launch(share=True)