File size: 2,427 Bytes
4eac6ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import gradio as gr
import yolov5
from PIL import Image
from huggingface_hub import hf_hub_download

app_title = "License Plate Object Detection"
models_ids = ['keremberke/yolov5n-license-plate', 'keremberke/yolov5s-license-plate', 'keremberke/yolov5m-license-plate']
article = f"<p style='text-align: center'> <a href='https://huggingface.co/{models_ids[-1]}'>model</a> | <a href='https://huggingface.co/keremberke/license-plate-object-detection'>dataset</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/CarLongPlate686_jpg.rf.97172961f3f90ae6e4b0ef1edfa24b98.jpg', 0.25, 'keremberke/yolov5m-license-plate'], ['test_images/CarLongPlate834_jpg.rf.c6da1db4c7c6ce9d9d864a90bb46ff1d.jpg', 0.25, 'keremberke/yolov5m-license-plate'], ['test_images/CarLongPlateGen3663_jpg.rf.26f54b241dbee94a3faabc9a08fd638a.jpg', 0.25, 'keremberke/yolov5m-license-plate'], ['test_images/CarLongPlateGen570_jpg.rf.305252bdd2798c370af7f1d702c0dd97.jpg', 0.25, 'keremberke/yolov5m-license-plate'], ['test_images/xemay1024_jpg.rf.1d25cb47787faa4e72967cf4c356af2a.jpg', 0.25, 'keremberke/yolov5m-license-plate'], ['test_images/xemay1349_jpg.rf.759edbd383937d1fdc243203450a1823.jpg', 0.25, 'keremberke/yolov5m-license-plate']]


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)