stphtan94117 commited on
Commit
0427a05
·
1 Parent(s): dac41c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -39
app.py CHANGED
@@ -1,44 +1,57 @@
1
- import glob
2
-
3
  import gradio as gr
 
4
  import yolov5
5
- from PIL import Image
6
- # import torch
7
- from huggingface_hub import hf_hub_download
8
 
9
- # Add new models here
10
- model_names = [
11
- "TW_plate_number",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  ]
13
 
14
- def load_model(model_name):
15
- model_path = hf_hub_download(repo_id=model_name, filename="plate.pt")
16
- return model_path
17
-
18
- def yolo_inference(im, model_path, threshold=0.70):
19
- model_loaded = load_model(model_path)
20
- model = yolov5.load(model_loaded)
21
- model.conf = threshold
22
- results = model(im) # inference
23
- numpy_image = results.render()[0]
24
- output_image = Image.fromarray(numpy_image)
25
- return output_image
26
-
27
- title = "TW_plate_number"
28
-
29
- inputs = [gr.Image(type="pil", label="document image"),
30
- gr.inputs.Dropdown(model_names, label="Model", default=model_names[0]),
31
- gr.Slider(maximum=1, step=0.01, value=0.70)]
32
-
33
-
34
- examples = [[str(file),model_names[0], 0.70] for file in glob.glob("./sample/*.jpg")]
35
-
36
- demo=gr.Interface(fn=yolo_inference,
37
- inputs=inputs,
38
- outputs=gr.Image(type="pil", label="annotated document").style(height=800),
39
- title=title,
40
- theme="huggingface",
41
- examples=examples)
42
-
43
- if __name__ == "__main__":
44
- demo.launch(debug=True)
 
 
 
1
  import gradio as gr
2
+ import torch
3
  import yolov5
 
 
 
4
 
5
+ # Images
6
+ torch.hub.download_url_to_file('https://github.com/ultralytics/yolov5/raw/master/data/images/zidane.jpg', 'zidane.jpg')
7
+ torch.hub.download_url_to_file('https://raw.githubusercontent.com/WongKinYiu/yolov7/main/inference/images/image3.jpg', 'image3.jpg')
8
+
9
+ def yolov5_inference(
10
+ image: gr.inputs.Image = None,
11
+ model_path: gr.inputs.Dropdown = None,
12
+ image_size: gr.inputs.Slider = 640,
13
+ conf_threshold: gr.inputs.Slider = 0.25,
14
+ iou_threshold: gr.inputs.Slider = 0.45,
15
+ ):
16
+ """
17
+ YOLOv5 inference function
18
+ Args:
19
+ image: Input image
20
+ model_path: Path to the model
21
+ image_size: Image size
22
+ conf_threshold: Confidence threshold
23
+ iou_threshold: IOU threshold
24
+ Returns:
25
+ Rendered image
26
+ """
27
+ model = yolov5.load(model_path, device="cpu")
28
+ model.conf = conf_threshold
29
+ model.iou = iou_threshold
30
+ results = model([image], size=image_size)
31
+ return results.render()[0]
32
+
33
+
34
+ inputs = [
35
+ gr.inputs.Image(type="pil", label="Input Image"),
36
+ gr.inputs.Dropdown(["plate.pt", "yolov5m.pt", "yolov5l.pt", "yolov5x.pt"], label="Model"),
37
+ gr.inputs.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size"),
38
+ gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.25, step=0.05, label="Confidence Threshold"),
39
+ gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.45, step=0.05, label="IOU Threshold"),
40
  ]
41
 
42
+ outputs = gr.outputs.Image(type="filepath", label="Output Image")
43
+ title = "YOLOv5"
44
+ description = "YOLOv5 is a family of object detection models pretrained on COCO dataset. This model is a pip implementation of the original YOLOv5 model."
45
+
46
+ examples = [['zidane.jpg', 'yolov5s.pt', 640, 0.25, 0.45], ['image3.jpg', 'yolov5s.pt', 640, 0.25, 0.45]]
47
+ demo_app = gr.Interface(
48
+ fn=yolov5_inference,
49
+ inputs=inputs,
50
+ outputs=outputs,
51
+ title=title,
52
+ examples=examples,
53
+ cache_examples=True,
54
+ live=True,
55
+ theme='huggingface',
56
+ )
57
+ demo_app.launch(debug=True, enable_queue=True)