import json import gradio as gr import yolov5 from PIL import Image from huggingface_hub import hf_hub_download app_title = "Blood Cell Object Detection" models_ids = ['keremberke/yolov5n-blood-cell', 'keremberke/yolov5s-blood-cell', 'keremberke/yolov5m-blood-cell'] article = f"
" current_model_id = models_ids[-1] model = yolov5.load(current_model_id) examples = [['test_images/BloodImage_00004_jpg.rf.32f80737b874b0728582d77e7c409dd5.jpg', 0.25, 'keremberke/yolov5m-blood-cell'], ['test_images/BloodImage_00071_jpg.rf.4eaf043df89d110a17821cd2739cf9c8.jpg', 0.25, 'keremberke/yolov5m-blood-cell'], ['test_images/BloodImage_00182_jpg.rf.166c2fcd2f192794d6b68051171fe261.jpg', 0.25, 'keremberke/yolov5m-blood-cell'], ['test_images/BloodImage_00259_jpg.rf.fbe6e4480e60c75a0f01ad7b8b367262.jpg', 0.25, 'keremberke/yolov5m-blood-cell'], ['test_images/BloodImage_00274_jpg.rf.86d08e08eb6ca331175699cc1ef1ce07.jpg', 0.25, 'keremberke/yolov5m-blood-cell'], ['test_images/BloodImage_00296_jpg.rf.6a50b9decfd0cde034af85c72b5f2c9c.jpg', 0.25, 'keremberke/yolov5m-blood-cell']] 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)