Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -31,13 +31,6 @@ def upload2aws(img_array):
|
|
31 |
s3.put_object(Bucket='predict-packages', Key=f'images_webapp_counters/{unique_name}.jpg', Body=buffer)
|
32 |
return None
|
33 |
|
34 |
-
def vidupload2aws(vid_path):
|
35 |
-
vid_name = os.path.basename(vid_path)
|
36 |
-
_, ext = os.path.splitext(vid_name)
|
37 |
-
unique_name = str(uuid.uuid4())
|
38 |
-
s3.upload_file(vid_path, 'predict-packages', f'images_webapp_counters/videos/{unique_name}{ext}')
|
39 |
-
return None
|
40 |
-
|
41 |
def send2api(input_img, api_url):
|
42 |
buf = io.BytesIO()
|
43 |
plt.imsave(buf, input_img, format='jpg')
|
@@ -51,28 +44,6 @@ def send2api(input_img, api_url):
|
|
51 |
print(str(e))
|
52 |
return response
|
53 |
|
54 |
-
def displaytext_detclasim(c_cnames, c_scinames, coverage):
|
55 |
-
countings_list = list(c_scinames.items())
|
56 |
-
countings_list.sort(key = lambda x: x[1], reverse=True)
|
57 |
-
total = 0
|
58 |
-
for (_,c) in countings_list:
|
59 |
-
total += c
|
60 |
-
|
61 |
-
text = f'coverage = {coverage}'+'\n\n'
|
62 |
-
text += 'Countings by scientific name:\n'
|
63 |
-
for key,value in countings_list:
|
64 |
-
text += f'{key} = {value}'+'\n'
|
65 |
-
text += '\n\n'
|
66 |
-
text += 'Countings by common name:\n'
|
67 |
-
|
68 |
-
countings_list = list(c_cnames.items())
|
69 |
-
countings_list.sort(key = lambda x: x[1], reverse=True)
|
70 |
-
for key,value in countings_list:
|
71 |
-
text += f'{key} = {value}'+'\n'
|
72 |
-
text += '\n'
|
73 |
-
text += f'total = {total}'+'\n'
|
74 |
-
return text
|
75 |
-
|
76 |
def displaytext_yolocounter(countings, coverage):
|
77 |
countings_list = list(countings.items())
|
78 |
countings_list.sort(key = lambda x: x[1], reverse=True)
|
@@ -87,45 +58,6 @@ def displaytext_yolocounter(countings, coverage):
|
|
87 |
text += f'total = {total}'+'\n'
|
88 |
return text
|
89 |
|
90 |
-
def display_detectionsandcountings_directcounter(img_array, countings, prob_th=0, cth = 0):
|
91 |
-
img = Image.fromarray(img_array)
|
92 |
-
img1 = ImageDraw.Draw(img)
|
93 |
-
h, w = img.size
|
94 |
-
ratio = h/4000
|
95 |
-
|
96 |
-
countings_list = list(countings.items())
|
97 |
-
countings_list.sort(key = lambda x: x[1], reverse=True)
|
98 |
-
yi=int(20*ratio)
|
99 |
-
total = 0
|
100 |
-
for (y_class,c) in countings_list:
|
101 |
-
if c > cth:
|
102 |
-
img1.text((int(50*ratio), yi), "# {} = {}".format(y_class, c), fill='red')
|
103 |
-
yi += int(100*ratio)
|
104 |
-
total += c
|
105 |
-
yi += int(100*ratio)
|
106 |
-
img1.text((int(50*ratio), yi), "# {} = {}".format('total', total), fill='red')
|
107 |
-
|
108 |
-
text = ''
|
109 |
-
for key,value in countings_list:
|
110 |
-
text += f'{key} = {value}'+'\n'
|
111 |
-
text += '\n'
|
112 |
-
text += f'total = {total}'+'\n'
|
113 |
-
return img, text
|
114 |
-
|
115 |
-
def testing_countingid(input_img):
|
116 |
-
upload2aws(input_img)
|
117 |
-
|
118 |
-
api_url = 'http://countingid-test.us-east-1.elasticbeanstalk.com/predict'
|
119 |
-
response = send2api(input_img, api_url)
|
120 |
-
c_cnames = response['countings_cnames']
|
121 |
-
c_scinames = response['countings_scinames']
|
122 |
-
coverage = response['coverage']
|
123 |
-
detections = response['detections']
|
124 |
-
img_out = response['img_out']
|
125 |
-
img = Image.open(BytesIO(base64.b64decode(img_out)))
|
126 |
-
text = displaytext_detclasim(c_cnames, c_scinames, coverage)
|
127 |
-
return img, text
|
128 |
-
|
129 |
def testing_yolocounter(input_img):
|
130 |
api_url = 'http://yolocounter-test.us-east-1.elasticbeanstalk.com/predict'
|
131 |
response = send2api(input_img, api_url)
|
@@ -137,51 +69,19 @@ def testing_yolocounter(input_img):
|
|
137 |
text = displaytext_yolocounter(countings, coverage)
|
138 |
return img, text
|
139 |
|
140 |
-
def testing_directcounter(input_img):
|
141 |
-
api_url = 'http://directcounter-test.us-east-1.elasticbeanstalk.com/predict'
|
142 |
-
response = send2api(input_img, api_url)
|
143 |
-
countings = response['countings_scinames']
|
144 |
-
img, text = display_detectionsandcountings_directcounter(input_img, countings, prob_th=0, cth = 0)
|
145 |
-
return img, text
|
146 |
-
|
147 |
-
def video_identity(video):
|
148 |
-
vidupload2aws(video)
|
149 |
-
return video
|
150 |
-
|
151 |
with gr.Blocks() as demo:
|
152 |
gr.Markdown("Submit an image with insects in a trap")
|
153 |
|
154 |
-
with gr.Tab("
|
155 |
with gr.Row():
|
156 |
input1 = gr.Image()
|
157 |
output1 =[gr.Image().style(height=500, width=500), gr.Textbox(lines=20)]
|
158 |
button1 = gr.Button("Submit")
|
159 |
-
button1.click(
|
160 |
-
|
161 |
-
with gr.Tab("Simplified Scientific Name Count"):
|
162 |
-
with gr.Row():
|
163 |
-
#input2 = gr.Image()
|
164 |
-
output2 =[gr.Image().style(height=500, width=500), gr.Textbox(lines=20)]
|
165 |
-
#button2 = gr.Button("Submit")
|
166 |
-
button1.click(testing_yolocounter, input1, output2)
|
167 |
-
|
168 |
-
with gr.Tab("Procesing on a video ...under development!"):
|
169 |
-
with gr.Row():
|
170 |
-
input3 = gr.Video()
|
171 |
-
output3 = gr.Video()
|
172 |
-
button3 = gr.Button("Submit")
|
173 |
-
button3.click(video_identity, input3, output3)
|
174 |
|
175 |
examples_list = glob("img_examples/*.jpg")
|
176 |
random.shuffle(examples_list)
|
177 |
examples = gr.Examples(examples=examples_list[:4],inputs=[input1])
|
178 |
|
179 |
-
""" with gr.Tab("Direct insect counter"):
|
180 |
-
with gr.Row():
|
181 |
-
#input3 = gr.Image()
|
182 |
-
output3 =[gr.Image().style(height=500, width=500), gr.Textbox(lines=20)]
|
183 |
-
#button3 = gr.Button("Submit")
|
184 |
-
button1.click(testing_directcounter, input1, output3)
|
185 |
-
"""
|
186 |
|
187 |
demo.launch()
|
|
|
31 |
s3.put_object(Bucket='predict-packages', Key=f'images_webapp_counters/{unique_name}.jpg', Body=buffer)
|
32 |
return None
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
def send2api(input_img, api_url):
|
35 |
buf = io.BytesIO()
|
36 |
plt.imsave(buf, input_img, format='jpg')
|
|
|
44 |
print(str(e))
|
45 |
return response
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
def displaytext_yolocounter(countings, coverage):
|
48 |
countings_list = list(countings.items())
|
49 |
countings_list.sort(key = lambda x: x[1], reverse=True)
|
|
|
58 |
text += f'total = {total}'+'\n'
|
59 |
return text
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
def testing_yolocounter(input_img):
|
62 |
api_url = 'http://yolocounter-test.us-east-1.elasticbeanstalk.com/predict'
|
63 |
response = send2api(input_img, api_url)
|
|
|
69 |
text = displaytext_yolocounter(countings, coverage)
|
70 |
return img, text
|
71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
with gr.Blocks() as demo:
|
73 |
gr.Markdown("Submit an image with insects in a trap")
|
74 |
|
75 |
+
with gr.Tab("Simplified Scientific Name Count"):
|
76 |
with gr.Row():
|
77 |
input1 = gr.Image()
|
78 |
output1 =[gr.Image().style(height=500, width=500), gr.Textbox(lines=20)]
|
79 |
button1 = gr.Button("Submit")
|
80 |
+
button1.click(testing_yolocounter,, input1, output1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
examples_list = glob("img_examples/*.jpg")
|
83 |
random.shuffle(examples_list)
|
84 |
examples = gr.Examples(examples=examples_list[:4],inputs=[input1])
|
85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
demo.launch()
|