import json import gradio as gr import yolov5 from PIL import Image from huggingface_hub import hf_hub_download app_title = "Forklift Object Detection" models_ids = ['keremberke/yolov5n-forklift', 'keremberke/yolov5s-forklift', 'keremberke/yolov5m-forklift'] article = f"
" current_model_id = models_ids[-1] model = yolov5.load(current_model_id) examples = [ ['test_images/0EZMOXIBCFJ2_jpg.rf.e02f09f440a32adc6b8cd173d64048dd.jpg', 0.25, 'keremberke/yolov5m-forklift'], ['test_images/32LSCZQDHZO7_jpg.rf.8fddaa4b5ed4db87d19a32d4554b9c23.jpg', 0.25, 'keremberke/yolov5m-forklift'], ['test_images/998PSJQQZBXV_jpg.rf.b1302d4d0209aed943753848438f8aa6.jpg', 0.25, 'keremberke/yolov5m-forklift'], ['test_images/KUN8WG9S97QO_jpg.rf.7ec95d32b4559f0c5d4e4e1a5f0623d7.jpg', 0.25, 'keremberke/yolov5m-forklift'], ['test_images/LTDX8N8ZKBT2_jpg.rf.6e09889a432d15c19fa0fbdbb62d347f.jpg', 0.25, 'keremberke/yolov5m-forklift'], ['test_images/V75EBJ0AG2HV_jpg.rf.88822c95c57d6bfb33092eb5ec0a020c.jpg', 0.25, 'keremberke/yolov5m-forklift'], ['test_images/XVMZ924NFFU3_jpg.rf.5c1f76b37c69b3db293151909c912e42.jpg', 0.25, 'keremberke/yolov5m-forklift'], ] 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)