Spaces:
Runtime error
Runtime error
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647import gradio as grfrom huggingface_hub import hf_hub_downloadimport osimport cv2 # Import OpenCVimport gradio.inputs as gr_inputsdef download_models(model_id):# Check if the model file exists locallylocal_model_path = "./gelan-c-seg.pt"if not os.path.exists(local_model_path):# Download the model from Hugging Face if it doesn't exist locallyhf_hub_download(model_id, filename="gelan-c-seg.pt", local_dir="./")return local_model_pathdef yolov9_inference(img):"""Perform inference on an image using the YOLOv9 model.:param img: Input image.:return: Output image with detections."""# Load the modelmodel_path = download_models("merve/yolov9")# Assuming you're using a YOLOv9 model from Ultralytics, you would typically use their library to load the modelmodel = cv2.dnn.readNetFromDarknet(model_path)# Perform inference# Placeholder for inference code; replace with your actual inference coderesults = model.forward()# Optionally, show detection bounding boxes on imageoutput_image = img # Placeholder for output image; replace with your actual output imagereturn output_imagedef app():return gr.Interface(fn=yolov9_inference,inputs=inputs.Image(type="file", label="Image"), # Use inputs.Image instead of gr.inputs.Imageoutputs="image",title="YOLOv9 Inference",description="Perform object detection using the YOLOv9 model.",theme="compact")gradio_app = app()gradio_app.launch(debug=True)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145import gradio as grimport spacesfrom huggingface_hub import hf_hub_downloaddef download_models(model_id):hf_hub_download("merve/yolov9", filename=f"{model_id}", local_dir=f"./")return f"./{model_id}"@spaces.GPUdef yolov9_inference(img_path, model_id, image_size, conf_threshold, iou_threshold):"""Load a YOLOv9 model, configure it, perform inference on an image, and optionally adjustthe input size and apply test time augmentation.:param model_path: Path to the YOLOv9 model file.:param conf_threshold: Confidence threshold for NMS.:param iou_threshold: IoU threshold for NMS.:param img_path: Path to the image file.:param size: Optional, input size for inference.:return: A tuple containing the detections (boxes, scores, categories) and the results object for further actions like displaying."""# Import YOLOv9import yolov9# Load the modelmodel_path = download_models(model_id)model = yolov9.load(model_path, device="cuda:0")# Set model parametersmodel.conf = conf_thresholdmodel.iou = iou_threshold# Perform inferenceresults = model(img_path, size=image_size)# Optionally, show detection bounding boxes on imageoutput = results.render()return output[0]def app():with gr.Blocks():with gr.Row():with gr.Column():img_path = gr.Image(type="filepath", label="Image")model_path = gr.Dropdown(label="Model",choices=["gelan-c.pt","gelan-e.pt","yolov9-c.pt","yolov9-e.pt",],value="gelan-e.pt",)image_size = gr.Slider(label="Image Size",minimum=320,maximum=1280,step=32,value=640,)conf_threshold = gr.Slider(label="Confidence Threshold",minimum=0.1,maximum=1.0,step=0.1,value=0.4,)iou_threshold = gr.Slider(label="IoU Threshold",minimum=0.1,maximum=1.0,step=0.1,value=0.5,)yolov9_infer = gr.Button(value="Inference")with gr.Column():output_numpy = gr.Image(type="numpy",label="Output")yolov9_infer.click(fn=yolov9_inference,inputs=[img_path,model_path,image_size,conf_threshold,iou_threshold,],outputs=[output_numpy],)gr.Examples(examples=[["data/zidane.jpg","gelan-e.pt",640,0.4,0.5,],["data/huggingface.jpg","yolov9-c.pt",640,0.4,0.5,],],fn=yolov9_inference,inputs=[img_path,model_path,image_size,conf_threshold,iou_threshold,],outputs=[output_numpy],cache_examples=True,)gradio_app = gr.Blocks()with gradio_app:gr.HTML("""<h1 style='text-align: center'>YOLOv9: Learning What You Want to Learn Using Programmable Gradient Information</h1>""")gr.HTML("""<h3 style='text-align: center'>Follow me for more!<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></h3>""")with gr.Row():with gr.Column():app()gradio_app.launch(debug=True)
app.py+132-34