Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -5,8 +5,7 @@ import requests, validators
|
|
5 |
import torch
|
6 |
import pathlib
|
7 |
from PIL import Image
|
8 |
-
from transformers import AutoFeatureExtractor, DetrForObjectDetection
|
9 |
-
|
10 |
import os
|
11 |
|
12 |
# colors for visualization
|
@@ -52,23 +51,20 @@ def visualize_prediction(pil_img, output_dict, threshold=0.7, id2label=None):
|
|
52 |
plt.axis("off")
|
53 |
return fig2img(plt.gcf())
|
54 |
|
55 |
-
def detect_objects(model_name,
|
56 |
-
|
|
|
57 |
#Extract model and feature extractor
|
58 |
feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
|
59 |
|
60 |
-
if 'detr' in model_name:
|
61 |
-
|
62 |
-
model = DetrForObjectDetection.from_pretrained(model_name)
|
63 |
-
|
64 |
-
elif 'yolos' in model_name:
|
65 |
-
|
66 |
-
model = YolosForObjectDetection.from_pretrained(model_name)
|
67 |
-
|
68 |
-
if validators.url(url_input):
|
69 |
-
image = Image.open(requests.get(url_input, stream=True).raw)
|
70 |
|
71 |
-
|
|
|
|
|
|
|
|
|
72 |
image = image_input
|
73 |
|
74 |
#Make prediction
|
@@ -86,68 +82,50 @@ def set_example_url(example: list) -> dict:
|
|
86 |
return gr.Textbox.update(value=example[0])
|
87 |
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
description = """
|
92 |
-
Links to HuggingFace Models:
|
93 |
-
- [facebook/detr-resnet-50](https://huggingface.co/facebook/detr-resnet-50)
|
94 |
-
- [facebook/detr-resnet-101](https://huggingface.co/facebook/detr-resnet-101)
|
95 |
-
- [hustvl/yolos-small](https://huggingface.co/hustvl/yolos-small)
|
96 |
-
- [hustvl/yolos-tiny](https://huggingface.co/hustvl/yolos-tiny)
|
97 |
-
"""
|
98 |
-
|
99 |
-
models = ["facebook/detr-resnet-50","facebook/detr-resnet-101",'hustvl/yolos-small','hustvl/yolos-tiny']
|
100 |
-
urls = ["https://c8.alamy.com/comp/J2AB4K/the-new-york-stock-exchange-on-the-wall-street-in-new-york-J2AB4K.jpg"]
|
101 |
-
|
102 |
-
twitter_link = """
|
103 |
-
[](https://twitter.com/nickmuchi)
|
104 |
-
"""
|
105 |
-
|
106 |
css = '''
|
107 |
h1#title {
|
108 |
text-align: center;
|
109 |
}
|
110 |
'''
|
111 |
-
demo = gr.Blocks(
|
112 |
|
113 |
with demo:
|
114 |
-
|
115 |
-
gr.Markdown(description)
|
116 |
-
gr.Markdown(twitter_link)
|
117 |
options = gr.Dropdown(choices=models,label='Select Object Detection Model',show_label=True)
|
118 |
slider_input = gr.Slider(minimum=0.2,maximum=1,value=0.7,label='Prediction Threshold')
|
119 |
|
120 |
with gr.Tabs():
|
121 |
with gr.TabItem('Image URL'):
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
with gr.Row():
|
127 |
-
example_url = gr.Dataset(components=[url_input],samples=[[str(url)] for url in urls])
|
128 |
|
129 |
-
|
130 |
-
|
|
|
|
|
|
|
131 |
with gr.TabItem('Image Upload'):
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
|
136 |
-
|
137 |
-
|
138 |
-
samples=[[
|
139 |
-
for path in sorted(pathlib.Path('images').rglob('*.JPG'))])
|
140 |
|
141 |
-
|
142 |
|
143 |
-
|
144 |
-
|
145 |
-
img_but.click(detect_objects,inputs=[options,url_input,img_input,slider_input],outputs=img_output_from_upload,queue=True)
|
146 |
example_images.click(fn=set_example_image,inputs=[example_images],outputs=[img_input])
|
147 |
example_url.click(fn=set_example_url,inputs=[example_url],outputs=[url_input])
|
148 |
|
149 |
|
150 |
-
gr.Markdown("")
|
151 |
|
152 |
|
153 |
demo.launch(enable_queue=True)
|
|
|
5 |
import torch
|
6 |
import pathlib
|
7 |
from PIL import Image
|
8 |
+
from transformers import AutoFeatureExtractor, DetrForObjectDetection
|
|
|
9 |
import os
|
10 |
|
11 |
# colors for visualization
|
|
|
51 |
plt.axis("off")
|
52 |
return fig2img(plt.gcf())
|
53 |
|
54 |
+
def detect_objects(model_name,image_input,threshold):
|
55 |
+
print(type(image_input))
|
56 |
+
|
57 |
#Extract model and feature extractor
|
58 |
feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
|
59 |
|
60 |
+
if 'detr' in model_name:
|
61 |
+
model = DetrForObjectDetection.from_pretrained(model_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
+
if image_input:
|
64 |
+
if isinstance(image_input,str):
|
65 |
+
if validators.url(image_input):
|
66 |
+
image = Image.open(requests.get(image_input, stream=True).raw)
|
67 |
+
else:
|
68 |
image = image_input
|
69 |
|
70 |
#Make prediction
|
|
|
82 |
return gr.Textbox.update(value=example[0])
|
83 |
|
84 |
|
85 |
+
models = ["facebook/detr-resnet-50","facebook/detr-resnet-101"]
|
86 |
+
#examples = ['1daaadc1e83fcecc7bfa920ed2773653.jpeg']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
css = '''
|
88 |
h1#title {
|
89 |
text-align: center;
|
90 |
}
|
91 |
'''
|
92 |
+
demo = gr.Blocks()
|
93 |
|
94 |
with demo:
|
95 |
+
#r.Markdown(title)
|
96 |
+
#gr.Markdown(description)
|
|
|
97 |
options = gr.Dropdown(choices=models,label='Select Object Detection Model',show_label=True)
|
98 |
slider_input = gr.Slider(minimum=0.2,maximum=1,value=0.7,label='Prediction Threshold')
|
99 |
|
100 |
with gr.Tabs():
|
101 |
with gr.TabItem('Image URL'):
|
102 |
+
with gr.Row():
|
103 |
+
url_input = gr.Textbox(lines=2,label='Enter valid image URL here..')
|
104 |
+
img_output_from_url = gr.Image(shape=(650,650))
|
|
|
|
|
|
|
105 |
|
106 |
+
with gr.Row():
|
107 |
+
example_url = gr.Dataset(components=[url_input],samples=[['https://miro.medium.com/max/960/1*ACc03086R6H_LyLydy8Z4g.jpeg'],['https://www.exposit.com/wp-content/uploads/2021/12/Blog_cover-52-scaled.jpeg']])
|
108 |
+
|
109 |
+
url_but = gr.Button('Detect')
|
110 |
+
|
111 |
with gr.TabItem('Image Upload'):
|
112 |
+
with gr.Row():
|
113 |
+
img_input = gr.Image(type='pil')
|
114 |
+
img_output_from_upload= gr.Image(shape=(650,650))
|
115 |
|
116 |
+
with gr.Row():
|
117 |
+
example_images = gr.Dataset(components=[img_input],
|
118 |
+
samples=[["airport.jpg"],['football-match.jpg']])
|
|
|
119 |
|
120 |
+
img_but = gr.Button('Detect')
|
121 |
|
122 |
+
url_but.click(detect_objects,inputs=[options,url_input,slider_input],outputs=img_output_from_url,queue=True)
|
123 |
+
img_but.click(detect_objects,inputs=[options,img_input,slider_input],outputs=img_output_from_upload,queue=True)
|
|
|
124 |
example_images.click(fn=set_example_image,inputs=[example_images],outputs=[img_input])
|
125 |
example_url.click(fn=set_example_url,inputs=[example_url],outputs=[url_input])
|
126 |
|
127 |
|
128 |
+
#gr.Markdown("")
|
129 |
|
130 |
|
131 |
demo.launch(enable_queue=True)
|