import json import gradio as gr import yolov5 from PIL import Image from huggingface_hub import hf_hub_download app_title = "Smoke Object Detection" models_ids = ['keremberke/yolov5n-smoke', 'keremberke/yolov5s-smoke', 'keremberke/yolov5m-smoke'] article = f"
" current_model_id = models_ids[-1] model = yolov5.load(current_model_id) examples = [['test_images/H_00902_png.rf.127931e9be51d3943ee7fb8a49d6cfa1.jpg', 0.25, 'keremberke/yolov5m-smoke'], ['test_images/H_09986_png.rf.0aeb1695f5989b9adeaa82baaecc65e1.jpg', 0.25, 'keremberke/yolov5m-smoke'], ['test_images/L_00261_png.rf.497e30c8474732bde3c12c31309c774c.jpg', 0.25, 'keremberke/yolov5m-smoke'], ['test_images/L_04459_png.rf.deeec1f4ef32d2d26881c275f71ba2b9.jpg', 0.25, 'keremberke/yolov5m-smoke'], ['test_images/M_00194_png.rf.a2157843f797aab94a8e26b5733c2402.jpg', 0.25, 'keremberke/yolov5m-smoke'], ['test_images/M_00848_png.rf.ec61e10aa03fb5d4f4cd3a4b615c77ad.jpg', 0.25, 'keremberke/yolov5m-smoke']] 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)