IS361Group4 commited on
Commit
8080fda
·
verified ·
1 Parent(s): 5eb49b6

สร้าง app.py

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