team-toll-2 / app.py
HugMoE's picture
Update app.py
af883fb
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 = [['CarLongPlate686_jpg.rf.97172961f3f90ae6e4b0ef1edfa24b98.jpg', 0.25, 'keremberke/yolov5m-license-plate'], ['CarLongPlate834_jpg.rf.c6da1db4c7c6ce9d9d864a90bb46ff1d.jpg', 0.25, 'keremberke/yolov5m-license-plate'], ['CarLongPlateGen3663_jpg.rf.26f54b241dbee94a3faabc9a08fd638a.jpg', 0.25, 'keremberke/yolov5m-license-plate'], ['CarLongPlateGen570_jpg.rf.305252bdd2798c370af7f1d702c0dd97.jpg', 0.25, 'keremberke/yolov5m-license-plate'], ['xemay1024_jpg.rf.1d25cb47787faa4e72967cf4c356af2a.jpg', 0.25, 'keremberke/yolov5m-license-plate'], ['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)
predictions = results.pred[0]
print(predictions[:, :4])
#x1 = predictions[0]
#y1 = predictions[1]
#x2 = predictions[2]
#y2 = predictions[3]
#print(predictions)
#print("Das Kennzeichen befindet sich im Rechteck zwischen den folgenden Punkten:")
#print("X1: " + x1 + "Y1: " + y1)
#print("X2: " + x2 + "Y2: " + y2)
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)