import gradio as gr import cv2 import time import numpy as np from sahi import AutoDetectionModel from sahi.predict import get_sliced_prediction from pathlib import Path detection_model = AutoDetectionModel.from_pretrained( model_type='ultralytics', model_path="./DDR.pt", # Replace with your model path confidence_threshold=0.01, device="cpu" # Change to 'cuda:0' if you have a GPU ) OUTPUT_PATH = "./outputs/pred_image.jpg" TEMP_PNG_PATH = "./outputs/pred_image.png" def run_inference(image): Path("./outputs").mkdir(parents=True, exist_ok=True) # Ensure output directory exists result = get_sliced_prediction( image, detection_model, slice_height=256, slice_width=256, overlap_height_ratio=0.2, overlap_width_ratio=0.2 ) result.export_visuals(export_dir=Path(TEMP_PNG_PATH).parent, file_name=Path(TEMP_PNG_PATH).name) time.sleep(2) if not Path(TEMP_PNG_PATH).exists(): raise FileNotFoundError(f"SAHI did not save the PNG file at {TEMP_PNG_PATH}") processed_image = cv2.imread(TEMP_PNG_PATH) cv2.imwrite(OUTPUT_PATH, processed_image) Path(TEMP_PNG_PATH).unlink() return OUTPUT_PATH demo = gr.Interface( fn=run_inference, inputs=gr.Image(type="numpy"), outputs=gr.Image(type="filepath"), title="YOLO11 Object Detection", description="Upload a DDR image to run inference using YOLO11" ) demo.launch()