File size: 2,076 Bytes
bb7e9b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

import json
import gradio as gr
import yolov5
from PIL import Image
from huggingface_hub import hf_hub_download

app_title = "Football Object Detection"
models_ids = ['keremberke/yolov5n-football', 'keremberke/yolov5s-football', 'keremberke/yolov5m-football']
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/football-object-detection'>huggingface.co/keremberke/football-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/18_pp_jpg.rf.912a54e24d38371daf61114b9a6b18be.jpg', 0.25, 'keremberke/yolov5m-football'], ['test_images/54881_jpg.rf.62b337bc47dbf6fbf5a34e18a361de97.jpg', 0.25, 'keremberke/yolov5m-football'], ['test_images/55219_jpg.rf.cdfe02a50951cf1ad449e940fbb646ac.jpg', 0.25, 'keremberke/yolov5m-football']]


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)