import json import gradio as gr import yolov5 from PIL import Image from huggingface_hub import hf_hub_download app_title = "Aerial Sheep Object Detection" models_ids = ['keremberke/yolov5n-aerial-sheep', 'keremberke/yolov5s-aerial-sheep', 'keremberke/yolov5m-aerial-sheep'] article = f"

model | dataset | awesome-yolov5-models

" current_model_id = models_ids[-1] model = yolov5.load(current_model_id) examples = [['test_images/DJI_0039_MOV-252_jpg.rf.a9d3f531dc347711c06539af59ca7329.jpg', 0.25, 'keremberke/yolov5m-aerial-sheep'], ['test_images/DJI_0040_MOV-141_jpg.rf.b2b23a4bd86ee5f50ff4a063ab4671ca.jpg', 0.25, 'keremberke/yolov5m-aerial-sheep'], ['test_images/DJI_0043_MOV-102_jpg.rf.4f0018c8c5de23731256755050f0819a.jpg', 0.25, 'keremberke/yolov5m-aerial-sheep'], ['test_images/DJI_0043_MOV-161_jpg.rf.a2197218b8c9f58272e59d7a8c6cf493.jpg', 0.25, 'keremberke/yolov5m-aerial-sheep'], ['test_images/DJI_0043_MOV-84_jpg.rf.22ea78648b21f64c276ab348ba82cf49.jpg', 0.25, 'keremberke/yolov5m-aerial-sheep'], ['test_images/img_373_jpg.rf.494e557cd96f79f20750ab7942c9d9c5.jpg', 0.25, 'keremberke/yolov5m-aerial-sheep']] def predict(image, threshold=0.25, model_id=None): # update model if required global current_model_id global model if model_id != current_model_id: model = yolov5.load(model_id) current_model_id = model_id # get model input size config_path = hf_hub_download(repo_id=model_id, filename="config.json") with open(config_path, "r") as f: config = json.load(f) input_size = config["input_size"] # perform inference model.conf = threshold results = model(image, size=input_size) numpy_image = results.render()[0] output_image = Image.fromarray(numpy_image) return output_image gr.Interface( title=app_title, description="Created by 'keremberke'", article=article, fn=predict, inputs=[ gr.Image(type="pil"), gr.Slider(maximum=1, step=0.01, value=0.25), gr.Dropdown(models_ids, value=models_ids[-1]), ], outputs=gr.Image(type="pil"), examples=examples, cache_examples=True if examples else False, ).launch(enable_queue=True)