Harika12323 commited on
Commit
604b3af
1 Parent(s): ed167d0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import gradio as gr
3
+ import yolov5
4
+ from PIL import Image
5
+ from huggingface_hub import hf_hub_download
6
+
7
+ app_title = "License Plate Object Detection"
8
+ models_ids = ['keremberke/yolov5n-license-plate', 'keremberke/yolov5s-license-plate', 'keremberke/yolov5m-license-plate']
9
+ 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>"
10
+
11
+ current_model_id = models_ids[-1]
12
+ model = yolov5.load(current_model_id)
13
+
14
+ 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']]
15
+
16
+
17
+ def predict(image, threshold=0.25, model_id=None):
18
+ # update model if required
19
+ global current_model_id
20
+ global model
21
+ if model_id != current_model_id:
22
+ model = yolov5.load(model_id)
23
+ current_model_id = model_id
24
+
25
+ # get model input size
26
+ config_path = hf_hub_download(repo_id=model_id, filename="config.json")
27
+ with open(config_path, "r") as f:
28
+ config = json.load(f)
29
+ input_size = config["input_size"]
30
+
31
+ # perform inference
32
+ model.conf = threshold
33
+ results = model(image, size=input_size)
34
+ numpy_image = results.render()[0]
35
+ output_image = Image.fromarray(numpy_image)
36
+ return output_image
37
+
38
+
39
+ gr.Interface(
40
+ title=app_title,
41
+ description="Created by 'keremberke'",
42
+ article=article,
43
+ fn=predict,
44
+ inputs=[
45
+ gr.Image(type="pil"),
46
+ gr.Slider(maximum=1, step=0.01, value=0.25),
47
+ gr.Dropdown(models_ids, value=models_ids[-1]),
48
+ ],
49
+ outputs=gr.Image(type="pil"),
50
+ examples=examples,
51
+ cache_examples=True if examples else False,
52
+ ).launch(enable_queue=True)