import gradio as gr import PIL.Image as Image from ultralytics import ASSETS, YOLO model_egg = YOLO("EPR_Detection/weights/egg_best.pt") model_nest = YOLO("EPR_Detection/weights/nest_best.pt") def predict_egg_image(img, conf_threshold, iou_threshold): """Predicts objects in an image using a YOLO model with adjustable confidence and IOU thresholds.""" results = model_egg.predict( source=img, conf=conf_threshold, iou=iou_threshold, show_labels=True, show_conf=True, imgsz=640, ) for r in results: im_array = r.plot() im = Image.fromarray(im_array[..., ::-1]) return im def predict_nest_image(img, conf_threshold, iou_threshold): """Predicts objects in an image using a YOLO model with adjustable confidence and IOU thresholds.""" results = model_nest.predict( source=img, conf=conf_threshold, iou=iou_threshold, show_labels=True, show_conf=True, imgsz=640, ) for r in results: im_array = r.plot() im = Image.fromarray(im_array[..., ::-1]) return im iface_egg = gr.Interface( fn=predict_egg_image, inputs=[ gr.Image(type="pil", label="Upload Image"), gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"), gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold"), ], outputs=gr.Image(type="pil", label="Result"), title="EPR Egg Detection", description="Upload images for egg detection inference.", examples=[ ["EPR_Detection/Examples/Egg/egg1.jpg", 0.25, 0.45], ["EPR_Detection/Examples/Egg/egg2.jpg", 0.25, 0.45], ["EPR_Detection/Examples/Egg/egg3.jpg", 0.25, 0.45], ["EPR_Detection/Examples/Egg/egg4.jpg", 0.25, 0.45], ["EPR_Detection/Examples/Egg/egg5.jpg", 0.25, 0.45] ], ) iface_nest = gr.Interface( fn=predict_nest_image, inputs=[ gr.Image(type="pil", label="Upload Image"), gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"), gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold"), ], outputs=gr.Image(type="pil", label="Result"), title="EPR Nest Detection", description="Upload images for nest detection inference.", examples=[ ["EPR_Detection/Examples/Nest/nest1.jpg", 0.15, 0.15], ["EPR_Detection/Examples/Nest/nest2.jpg", 0.15, 0.15], ["EPR_Detection/Examples/Nest/nest3.jpg", 0.15, 0.15], ["EPR_Detection/Examples/Nest/nest4.jpg", 0.15, 0.15], ["EPR_Detection/Examples/Nest/nest5.jpg", 0.15, 0.15] ], ) iface = gr.TabbedInterface( [iface_egg, iface_nest], ["EPR Egg", "EPR Nest"] ) if __name__ == "__main__": iface.launch()