Spaces:
Runtime error
Runtime error
import json | |
import gradio as gr | |
import yolov5 | |
from PIL import Image | |
from huggingface_hub import hf_hub_download | |
app_title = "Construction Safety Object Detection" | |
models_ids = ['keremberke/yolov5n-construction-safety', 'keremberke/yolov5s-construction-safety', 'keremberke/yolov5m-construction-safety'] | |
article = f"<p style='text-align: center'> <a href='https://huggingface.co/{models_ids[-1]}'>huggingface.co/{models_ids[-1]}</a> | <a href='https://huggingface.co/keremberke/construction-safety-object-detection'>huggingface.co/keremberke/construction-safety-object-detection</a> | <a href='https://github.com/keremberke/awesome-yolov5-models'>awesome-yolov5-models</a> </p>" | |
current_model_id = models_ids[-1] | |
model = yolov5.load(current_model_id) | |
examples = [['test_images/-1079-_png_jpg.rf.eae5c731d79f3b240ce6b5ae84589e49.jpg', 0.25, 'keremberke/yolov5m-construction-safety'], ['test_images/construction-1-_mp4-147_jpg.rf.6593d553fd4c445c810aedcc8f9bf5b0.jpg', 0.25, 'keremberke/yolov5m-construction-safety'], ['test_images/construction-1023-_jpg.rf.10ea2a0d607573c1c90d7c38bacf2f04.jpg', 0.25, 'keremberke/yolov5m-construction-safety'], ['test_images/construction-3-_mp4-21_jpg.rf.f90d04a7fe8ee4d1d3331050b4e64e1b.jpg', 0.25, 'keremberke/yolov5m-construction-safety'], ['test_images/image_140_jpg.rf.e7727a5a4bd52d812adbd6f5d2fea6d9.jpg', 0.25, 'keremberke/yolov5m-construction-safety'], ['test_images/Mask-detector1_mov-46_jpg.rf.2122d830c41384952c89ef8cd23734ca.jpg', 0.25, 'keremberke/yolov5m-construction-safety']] | |
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) | |