Vineedhar commited on
Commit
05e82a9
1 Parent(s): 0034815

Upload app.py

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