Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
@@ -1,236 +0,0 @@
|
|
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="https://huggingface.co/spaces/orYx-models/object-detection-facebook-ResNets/blob/main/oryx_logo%20(2).png" alt="Logo">
|
84 |
-
<h4 style="color:navy;">1. Select a model.</h4>
|
85 |
-
""")
|
86 |
-
|
87 |
-
model = gr.Radio(["detr-resnet-50", "detr-resnet-101"], value="detr-resnet-50", label="Model name")
|
88 |
-
|
89 |
-
gr.HTML("""<br/>""")
|
90 |
-
gr.HTML("""<h4 style="color:navy;">2-a. Select an example by clicking a thumbnail below.</h4>""")
|
91 |
-
gr.HTML("""<h4 style="color:navy;">2-b. Or upload an image by clicking on the canvas.</h4>""")
|
92 |
-
|
93 |
-
with gr.Row():
|
94 |
-
input_image = gr.Image(label="Input image", type="pil")
|
95 |
-
output_image = gr.Image(label="Output image with predicted instances", type="pil")
|
96 |
-
|
97 |
-
gr.Examples(['https://huggingface.co/spaces/orYx-models/object-detection-facebook-ResNets/blob/main/traffic.jpg',
|
98 |
-
'https://huggingface.co/spaces/orYx-models/object-detection-facebook-ResNets/blob/main/flyover.jpg'
|
99 |
-
|
100 |
-
import torch
|
101 |
-
from transformers import pipeline
|
102 |
-
|
103 |
-
from PIL import Image
|
104 |
-
|
105 |
-
import matplotlib.pyplot as plt
|
106 |
-
import matplotlib.patches as patches
|
107 |
-
|
108 |
-
from random import choice
|
109 |
-
import io
|
110 |
-
|
111 |
-
detector50 = pipeline(model="facebook/detr-resnet-50")
|
112 |
-
|
113 |
-
detector101 = pipeline(model="facebook/detr-resnet-101")
|
114 |
-
|
115 |
-
|
116 |
-
import gradio as gr
|
117 |
-
|
118 |
-
COLORS = ["#ff7f7f", "#ff7fbf", "#ff7fff", "#bf7fff",
|
119 |
-
"#7f7fff", "#7fbfff", "#7fffff", "#7fffbf",
|
120 |
-
"#7fff7f", "#bfff7f", "#ffff7f", "#ffbf7f"]
|
121 |
-
|
122 |
-
fdic = {
|
123 |
-
"family" : "Impact",
|
124 |
-
"style" : "italic",
|
125 |
-
"size" : 15,
|
126 |
-
"color" : "yellow",
|
127 |
-
"weight" : "bold"
|
128 |
-
}
|
129 |
-
|
130 |
-
|
131 |
-
def get_figure(in_pil_img, in_results):
|
132 |
-
plt.figure(figsize=(16, 10))
|
133 |
-
plt.imshow(in_pil_img)
|
134 |
-
#pyplot.gcf()
|
135 |
-
ax = plt.gca()
|
136 |
-
|
137 |
-
for prediction in in_results:
|
138 |
-
selected_color = choice(COLORS)
|
139 |
-
|
140 |
-
x, y = prediction['box']['xmin'], prediction['box']['ymin'],
|
141 |
-
w, h = prediction['box']['xmax'] - prediction['box']['xmin'], prediction['box']['ymax'] - prediction['box']['ymin']
|
142 |
-
|
143 |
-
ax.add_patch(plt.Rectangle((x, y), w, h, fill=False, color=selected_color, linewidth=3))
|
144 |
-
ax.text(x, y, f"{prediction['label']}: {round(prediction['score']*100, 1)}%", fontdict=fdic)
|
145 |
-
|
146 |
-
plt.axis("off")
|
147 |
-
|
148 |
-
return plt.gcf()
|
149 |
-
|
150 |
-
|
151 |
-
def infer(model, in_pil_img):
|
152 |
-
|
153 |
-
results = None
|
154 |
-
if model == "detr-resnet-101":
|
155 |
-
results = detector101(in_pil_img)
|
156 |
-
else:
|
157 |
-
results = detector50(in_pil_img)
|
158 |
-
|
159 |
-
figure = get_figure(in_pil_img, results)
|
160 |
-
|
161 |
-
buf = io.BytesIO()
|
162 |
-
figure.savefig(buf, bbox_inches='tight')
|
163 |
-
buf.seek(0)
|
164 |
-
output_pil_img = Image.open(buf)
|
165 |
-
|
166 |
-
return output_pil_img
|
167 |
-
|
168 |
-
|
169 |
-
with gr.Blocks(title= "DETR Object Detection by orYx Models") as demo:
|
170 |
-
gr.HTML("""
|
171 |
-
<style>
|
172 |
-
.logo {
|
173 |
-
position: absolute;
|
174 |
-
top: 10px;
|
175 |
-
right: 10px;
|
176 |
-
width: 100px; /* Adjust the width of the logo as needed */
|
177 |
-
height: auto;
|
178 |
-
}
|
179 |
-
</style>
|
180 |
-
<div style="font-family:'Times New Roman', 'Serif'; font-size:16pt; font-weight:bold; text-align:center; color:royalblue;">DETR Object Detection</div>
|
181 |
-
<img class="logo" src="https://huggingface.co/spaces/orYx-models/object-detection-facebook-ResNets/blob/main/oryx_logo%20(2).png" alt="Logo">
|
182 |
-
<h4 style="color:navy;">1. Select a model.</h4>
|
183 |
-
""")
|
184 |
-
|
185 |
-
model = gr.Radio(["detr-resnet-50", "detr-resnet-101"], value="detr-resnet-50", label="Model name")
|
186 |
-
|
187 |
-
gr.HTML("""<br/>""")
|
188 |
-
gr.HTML("""<h4 style="color:navy;">Please upload an image by clicking on the canvas. </h4>""")
|
189 |
-
|
190 |
-
with gr.Row():
|
191 |
-
input_image = gr.Image(label="Input image", type="pil")
|
192 |
-
output_image = gr.Image(label="Output image with predicted instances", type="pil")
|
193 |
-
|
194 |
-
gr.Examples(['https://huggingface.co/spaces/orYx-models/object-detection-facebook-ResNets/blob/main/traffic.jpg',
|
195 |
-
'https://huggingface.co/spaces/orYx-models/object-detection-facebook-ResNets/blob/main/flyover.jpg',
|
196 |
-
https://huggingface.co/spaces/orYx-models/object-detection-facebook-ResNets/resolve/main/trees_traffic.jpg'
|
197 |
-
'https://huggingface.co/spaces/orYx-models/object-detection-facebook-ResNets/resolve/main/Saudi_traffic.jpg'], inputs=input_image)
|
198 |
-
|
199 |
-
gr.HTML("""<br/>""")
|
200 |
-
gr.HTML("""<h4 style="color:navy;">3. Then, click "Infer" button to predict object instances. It will take about 10 seconds (on cpu)</h4>""")
|
201 |
-
|
202 |
-
send_btn = gr.Button("Infer")
|
203 |
-
send_btn.click(fn=infer, inputs=[model, input_image], outputs=[output_image])
|
204 |
-
|
205 |
-
gr.HTML("""<br/>""")
|
206 |
-
gr.HTML("""<h4 style="color:navy;">Reference</h4>""")
|
207 |
-
gr.HTML("""<ul>""")
|
208 |
-
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>""")
|
209 |
-
gr.HTML("""</ul>""")
|
210 |
-
|
211 |
-
|
212 |
-
#demo.queue()
|
213 |
-
demo.launch(debug=True)
|
214 |
-
|
215 |
-
|
216 |
-
### EOF ###
|
217 |
-
], inputs=input_image)
|
218 |
-
|
219 |
-
gr.HTML("""<br/>""")
|
220 |
-
gr.HTML("""<h4 style="color:navy;">3. Then, click "Infer" button to predict object instances. It will take about 10 seconds (on cpu)</h4>""")
|
221 |
-
|
222 |
-
send_btn = gr.Button("Infer")
|
223 |
-
send_btn.click(fn=infer, inputs=[model, input_image], outputs=[output_image])
|
224 |
-
|
225 |
-
gr.HTML("""<br/>""")
|
226 |
-
gr.HTML("""<h4 style="color:navy;">Reference</h4>""")
|
227 |
-
gr.HTML("""<ul>""")
|
228 |
-
gr.HTML("""<li><a href="https://colab.research.google.com/github/facebookresearch/detr/blob/colab/notebooks/detr_attention.ipynb" target="_bl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