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 = "NFL Object Detection" | |
models_ids = ['keremberke/yolov5n-nfl', 'keremberke/yolov5s-nfl', 'keremberke/yolov5m-nfl'] | |
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/nfl-object-detection'>huggingface.co/keremberke/nfl-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/57638_001089_Endzone_frame262_jpg.rf.4a34e04af4f7b46c8dd9454e34740317.jpg', 0.25, 'keremberke/yolov5m-nfl'], ['test_images/57660_001234_Endzone_frame0845_jpg.rf.745d52b49774ae36d821d752435c8481.jpg', 0.25, 'keremberke/yolov5m-nfl'], ['test_images/57848_002061_Sideline_frame0529_jpg.rf.3747d55691fd4bd0ca5f26e713531f6e.jpg', 0.25, 'keremberke/yolov5m-nfl'], ['test_images/57873_003005_Endzone_frame356_jpg.rf.5f45decabc82c2f9c102bfe4200ece25.jpg', 0.25, 'keremberke/yolov5m-nfl'], ['test_images/57928_002004_Sideline_frame0820_jpg.rf.31445da39fd67e4455b8107cbe7918f5.jpg', 0.25, 'keremberke/yolov5m-nfl'], ['test_images/58037_001432_Sideline_frame386_jpg.rf.0e72f6bd6a685a8149467eeb50184c56.jpg', 0.25, 'keremberke/yolov5m-nfl']] | |
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) | |