sudthi commited on
Commit
2203208
·
verified ·
1 Parent(s): 2b2b7b2

ทำต่อ app,py

Browse files
Files changed (1) hide show
  1. app.py +96 -0
app.py CHANGED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import pipeline
3
+
4
+ from PIL import Image
5
+
6
+ import matplotlib.pyplot as plt
7
+ import matplotlib.patches as patches
8
+
9
+ from random import choice
10
+ import io
11
+ import gradio as gr
12
+
13
+ detector50 = pipeline(model="facebook/detr-resnet-50")
14
+
15
+ detecttor101 = pipeline(model="facebook/detr-resnet-101")
16
+
17
+ COLORS = ["#ff7f7f", "#ff7fbf", "#ff7fff", "#bf7fff",
18
+ "#7f7fff", "#7fbfff", "#7fffff", "#7fffbf",
19
+ "#7fff7f", "#bfff7f", "#ffff7f", "#ffbf7f"]
20
+
21
+ fdic = {"family" : "Impact",
22
+ "style" : "italic",
23
+ "size" : 15,
24
+ "color" : "yellow",
25
+ "weight" : "bold"
26
+ }
27
+
28
+ def get_figure(in_pil_img, in_results):
29
+ plt.figure(figsize=(16,10))
30
+ plt.imshow(in_pill_img)
31
+
32
+ ax = plt.gca()
33
+
34
+ for prediction in in_results:
35
+ selected_color = choice(COLORS)
36
+
37
+ x, y = prediction['box']['xmin'], prediction['box']['ymin'],
38
+ w, h = prediction['box']['xmax'] - prediction['box']['ymax'] - prediction['box']['ymin']
39
+ ax.add_patch(plt.Rectangle((x, y), w, h, fill=False, color=selected_color, linewidth=3))
40
+ ax.text(x, y, f"{prediction['label']}: {round(prediction['score']*100, 1)}%", fontdict=fdic)
41
+
42
+ plt.axis("off")
43
+ return plt.gcf()
44
+
45
+ def infer(model, in_pil_img):
46
+ result = None
47
+ if model == "detr-resnet-101":
48
+ results = detector101(in_pil_img)
49
+ else:
50
+ results = detector50(in_pil_img)
51
+
52
+ figure = get_figure(in_pil_img, results)
53
+
54
+ buf = io.BytesIO()
55
+ figure.savefig(buf, bbox_inches='tight')
56
+ buf.seek(0)
57
+ output_pil_img = Image.open(buf)
58
+
59
+ return output_pil_img
60
+
61
+ with gr.Blocks(title="DETR Object Detection - ClassCat",
62
+ css=".gradio-container {background:lightyellow;}"
63
+ ) as demo:
64
+ #sample_index = gr.State([])
65
+
66
+ gr.HTML("""<div style="font-family:'Times New Roman', 'Serif'; font-size:16pt; font-weight:bold; text-align:center; color:royalblue;">DETR Object Detection</div>""")
67
+
68
+ gr.HTML("""<h4 style="color:navy;">1. Select a model.</h4>""")
69
+
70
+ model = gr.Radio(["detr-resnet-50", "detr-resnet-101"], value="detr-resnet-50", label="Model name")
71
+
72
+ gr.HTML("""<br/>""")
73
+ gr.HTML("""<h4 style="color:navy;">2-a. Select an example by clicking a thumbnail below.</h4>""")
74
+ gr.HTML("""<h4 style="color:navy;">2-b. Or upload an image by clicking on the canvas.</h4>""")
75
+
76
+ with gr.Row():
77
+ input_image = gr.Image(label="Input image", type="pil")
78
+ output_image = gr.Image(label="Output image with predicted instances", type="pil")
79
+
80
+ gr.Examples(['samples/cats.jpg', 'samples/detectron2.png', 'samples/cat.jpg', 'samples/hotdog.jpg'], inputs=input_image)
81
+
82
+ gr.HTML("""<br/>""")
83
+ gr.HTML("""<h4 style="color:navy;">3. Then, click "Infer" button to predict object instances. It will take about 10 seconds (on cpu)</h4>""")
84
+
85
+ send_btn = gr.Button("Infer")
86
+ send_btn.click(fn=infer, inputs=[model, input_image], outputs=[output_image])
87
+
88
+ gr.HTML("""<br/>""")
89
+ gr.HTML("""<h4 style="color:navy;">Reference</h4>""")
90
+ gr.HTML("""<ul>""")
91
+ gr.HTML("""<li><a href="https://colab.research.google.com/github/facebookresearch/detr/blob/colab/notebooks/detr_attention.ipynb" target="_blank">Hands-on tutorial for DETR</a>""")
92
+ gr.HTML("""</ul>""")
93
+
94
+
95
+ #demo.queue()
96
+ demo.launch(debug=True)