stphtan94117 commited on
Commit
dcca473
·
1 Parent(s): 935485e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -47
app.py CHANGED
@@ -1,51 +1,49 @@
1
  import gradio as gr
2
  import torch
3
- import yolov5
 
 
4
 
5
  # Images
6
- torch.hub.download_url_to_file('https://i.imgur.com/4GmZXID.jpg', '1.jpg')
7
- torch.hub.download_url_to_file('https://i.imgur.com/ktIGRvs.jpg', '2.jpg')
8
- torch.hub.download_url_to_file('https://i.imgur.com/fSEsXoE.jpg', '3.jpg')
9
- torch.hub.download_url_to_file('https://i.imgur.com/lsVJRzd.jpg', '4.jpg')
10
- torch.hub.download_url_to_file('https://i.imgur.com/1OFmJd1.jpg', '5.jpg')
11
- torch.hub.download_url_to_file('https://i.imgur.com/GhfAWMJ.jpg', '6.jpg')
12
-
13
- def yolov5_inference(
14
- image: gr.inputs.Image = None,
15
- model_path: gr.inputs.Dropdown = None,
16
- image_size: gr.inputs.Slider = 1280,
17
- conf_threshold: gr.inputs.Slider = 0.7,
18
- iou_threshold: gr.inputs.Slider = 0.45,
19
- ):
20
-
21
- model = torch.hub.load('yolov5', 'custom', path='plate.pt', source="local")
22
- model.conf = conf_threshold
23
- model.iou = iou_threshold
24
- results = model([image], size=image_size)
25
- return results.render()[0]
26
-
27
-
28
- inputs = [
29
- gr.inputs.Image(type="pil", label="Input Image"),
30
- # gr.inputs.Dropdown(["plate.pt"], label="Model"),
31
- gr.inputs.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size"),
32
- gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.7, step=0.05, label="Confidence Threshold"),
33
- gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.7, step=0.05, label="IOU Threshold"),
34
- ]
35
-
36
- outputs = gr.outputs.Image(type="filepath", label="Output Image")
37
- title = "YOLOv5"
38
- description = "YOLOv5 is a family of object detection models pretrained on COCO dataset. This model is a pip implementation of the original YOLOv5 model."
39
-
40
- examples = [['1.jpg', 'plate.pt', 1280, 0.7, 0.45], ['2.jpg', 'plate.pt', 1280, 0.7, 0.45], ['3.jpg', 'plate.pt', 1280, 0.7, 0.45], ['4.jpg', 'plate.pt', 1280, 0.7, 0.45], ['5.jpg', 'plate.pt', 1280, 0.7, 0.45], ['6.jpg', 'plate.pt', 1280, 0.7, 0.45]]
41
- demo_app = gr.Interface(
42
- fn=yolov5_inference,
43
- inputs=inputs,
44
- outputs=outputs,
45
- title=title,
46
- examples=examples,
47
- cache_examples=True,
48
- live=True,
49
- theme='huggingface',
50
- )
51
- demo_app.launch(debug=True, enable_queue=True)
 
1
  import gradio as gr
2
  import torch
3
+ from PIL import Image
4
+ import json
5
+
6
 
7
  # Images
8
+ torch.hub.download_url_to_file(
9
+ 'https://i.imgur.com/4GmZXID.jpg', '1.jpg')
10
+ torch.hub.download_url_to_file(
11
+ 'https://i.imgur.com/ktIGRvs.jpg', '2.jpg')
12
+ torch.hub.download_url_to_file(
13
+ 'https://i.imgur.com/fSEsXoE.jpg', '3.jpg')
14
+ torch.hub.download_url_to_file(
15
+ 'https://i.imgur.com/lsVJRzd.jpg', '4.jpg')
16
+ torch.hub.download_url_to_file(
17
+ 'https://i.imgur.com/1OFmJd1.jpg', '5.jpg')
18
+ torch.hub.download_url_to_file(
19
+ 'https://i.imgur.com/GhfAWMJ.jpg', '6.jpg')
20
+
21
+ # Model
22
+ # model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # force_reload=True to update
23
+ model = torch.hub.load('ultralytics/yolov5', 'custom', path='plate.pt', source="local")
24
+
25
+ def yolo(im, size=1024):
26
+ g = (size / max(im.size)) # gain
27
+ im = im.resize((int(x * g) for x in im.size), Image.ANTIALIAS) # resize
28
+
29
+ results = model(im) # inference
30
+ results.render() # updates results.imgs with boxes and labels
31
+
32
+ df = results.pandas().xyxy[0].to_json(orient="records")
33
+ res = json.loads(df)
34
+
35
+ return [Image.fromarray(results.imgs[0]), res]
36
+
37
+
38
+ inputs = gr.inputs.Image(type='pil', label="Original Image")
39
+ outputs = [gr.outputs.Image(type="pil", label="Output Image"),
40
+ gr.outputs.JSON(label="Output JSON")]
41
+
42
+ title = "TW_plate_number"
43
+ description = "TW_plate_number"
44
+ # article = "<p style='text-align: center'>TW_plate_number <a href=\"http://codh.rois.ac.jp/char-shape/\">日本古典籍くずし字データセット</a>.</p>"
45
+
46
+ examples = [['1.jpg'], ['2.jpg'], ['3.jpg'], ['4.jpg'], ['5.jpg'], ['6.jpg']]
47
+ gr.Interface(yolo, inputs, outputs, title=title, description=description, examples=examples, theme="huggingface").launch(enable_queue=True) # cache_examples=True,
48
+
49
+