subh71 commited on
Commit
f02e6ac
·
verified ·
1 Parent(s): 10589fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +124 -0
app.py CHANGED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+ from transformers import AutoImageProcessor, AutoModelForObjectDetection
4
+ #from transformers import pipeline
5
+
6
+ from PIL import Image
7
+ import matplotlib.pyplot as plt
8
+ import matplotlib.patches as patches
9
+
10
+ import io
11
+ from random import choice
12
+
13
+
14
+ image_processor_tiny = AutoImageProcessor.from_pretrained("hustvl/yolos-tiny")
15
+ model_tiny = AutoModelForObjectDetection.from_pretrained("hustvl/yolos-tiny")
16
+
17
+ image_processor_small = AutoImageProcessor.from_pretrained("hustvl/yolos-small")
18
+ model_small = AutoModelForObjectDetection.from_pretrained("hustvl/yolos-small")
19
+
20
+
21
+ import gradio as gr
22
+
23
+
24
+ COLORS = ["#ff7f7f", "#ff7fbf", "#ff7fff", "#bf7fff",
25
+ "#7f7fff", "#7fbfff", "#7fffff", "#7fffbf",
26
+ "#7fff7f", "#bfff7f", "#ffff7f", "#ffbf7f"]
27
+
28
+ fdic = {
29
+ "family" : "DejaVu Serif",
30
+ "style" : "normal",
31
+ "size" : 18,
32
+ "color" : "yellow",
33
+ "weight" : "bold"
34
+ }
35
+
36
+
37
+ def get_figure(in_pil_img, in_results):
38
+ plt.figure(figsize=(16, 10))
39
+ plt.imshow(in_pil_img)
40
+ ax = plt.gca()
41
+
42
+ for score, label, box in zip(in_results["scores"], in_results["labels"], in_results["boxes"]):
43
+ selected_color = choice(COLORS)
44
+
45
+ box_int = [i.item() for i in torch.round(box).to(torch.int32)]
46
+ x, y, w, h = box_int[0], box_int[1], box_int[2]-box_int[0], box_int[3]-box_int[1]
47
+ #x, y, w, h = torch.round(box[0]).item(), torch.round(box[1]).item(), torch.round(box[2]-box[0]).item(), torch.round(box[3]-box[1]).item()
48
+
49
+ ax.add_patch(plt.Rectangle((x, y), w, h, fill=False, color=selected_color, linewidth=3, alpha=0.8))
50
+ ax.text(x, y, f"{model_tiny.config.id2label[label.item()]}: {round(score.item()*100, 2)}%", fontdict=fdic, alpha=0.8)
51
+
52
+ plt.axis("off")
53
+
54
+ return plt.gcf()
55
+
56
+
57
+ def infer(in_pil_img, in_model="yolos-tiny", in_threshold=0.9):
58
+ target_sizes = torch.tensor([in_pil_img.size[::-1]])
59
+
60
+ if in_model == "yolos-small":
61
+ inputs = image_processor_small(images=in_pil_img, return_tensors="pt")
62
+ outputs = model_small(**inputs)
63
+
64
+ # convert outputs (bounding boxes and class logits) to COCO API
65
+ results = image_processor_small.post_process_object_detection(outputs, threshold=in_threshold, target_sizes=target_sizes)[0]
66
+
67
+ else:
68
+ inputs = image_processor_tiny(images=in_pil_img, return_tensors="pt")
69
+ outputs = model_tiny(**inputs)
70
+
71
+ # convert outputs (bounding boxes and class logits) to COCO API
72
+ results = image_processor_tiny.post_process_object_detection(outputs, threshold=in_threshold, target_sizes=target_sizes)[0]
73
+
74
+ figure = get_figure(in_pil_img, results)
75
+
76
+ buf = io.BytesIO()
77
+ figure.savefig(buf, bbox_inches='tight')
78
+ buf.seek(0)
79
+ output_pil_img = Image.open(buf)
80
+
81
+ return output_pil_img
82
+
83
+
84
+ with gr.Blocks(title="YOLOS Object Detection - ClassCat",
85
+ css=".gradio-container {background:lightyellow;}"
86
+ ) as demo:
87
+ #sample_index = gr.State([])
88
+
89
+ gr.HTML("""<div style="font-family:'Times New Roman', 'Serif'; font-size:16pt; font-weight:bold; text-align:center; color:royalblue;">YOLOS Object Detection</div>""")
90
+
91
+ gr.HTML("""<h4 style="color:navy;">1. Select a model.</h4>""")
92
+
93
+ model = gr.Radio(["yolos-tiny", "yolos-small"], value="yolos-tiny", label="Model name")
94
+
95
+ gr.HTML("""<br/>""")
96
+ gr.HTML("""<h4 style="color:navy;">2-a. Select an example by clicking a thumbnail below.</h4>""")
97
+ gr.HTML("""<h4 style="color:navy;">2-b. Or upload an image by clicking on the canvas.</h4>""")
98
+
99
+ with gr.Row():
100
+ input_image = gr.Image(label="Input image", type="pil")
101
+ output_image = gr.Image(label="Output image with predicted instances", type="pil")
102
+
103
+ gr.Examples(['samples/cats.jpg', 'samples/detectron2.png', 'samples/cat.jpg', 'samples/hotdog.jpg'], inputs=input_image)
104
+
105
+ gr.HTML("""<br/>""")
106
+ gr.HTML("""<h4 style="color:navy;">3. Set a threshold value (default to 0.9)</h4>""")
107
+
108
+ threshold = gr.Slider(0, 1.0, value=0.9, label='threshold')
109
+
110
+ gr.HTML("""<br/>""")
111
+ gr.HTML("""<h4 style="color:navy;">4. Then, click "Infer" button to predict object instances. It will take about 10 seconds (yolos-tiny) or 20 seconds (yolos-small).</h4>""")
112
+
113
+ send_btn = gr.Button("Infer")
114
+ send_btn.click(fn=infer, inputs=[input_image, model, threshold], outputs=[output_image])
115
+
116
+ gr.HTML("""<br/>""")
117
+ gr.HTML("""<h4 style="color:navy;">Reference</h4>""")
118
+ gr.HTML("""<ul>""")
119
+ gr.HTML("""<li><a href="https://huggingface.co/docs/transformers/model_doc/yolos" target="_blank">Hugging Face Transformers - YOLOS</a>""")
120
+ gr.HTML("""</ul>""")
121
+
122
+
123
+ #demo.queue()
124
+ demo.launch(debug=True)