ezamorag commited on
Commit
cae6a01
1 Parent(s): 64a1793

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +168 -0
app.py ADDED
@@ -0,0 +1,168 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import numpy as np
3
+ import gradio as gr
4
+ import io
5
+ import matplotlib.pyplot as plt
6
+ from PIL import Image, ImageDraw, ImageFont
7
+
8
+ def send2api(input_img, api_url):
9
+ buf = io.BytesIO()
10
+ plt.imsave(buf, input_img, format='jpg')
11
+ files = {'image': buf.getvalue()}
12
+ res = requests.post(api_url, files=files)
13
+ try:
14
+ res.raise_for_status()
15
+ if res.status_code != 204:
16
+ response = res.json()
17
+ except Exception as e:
18
+ print(str(e))
19
+ return response
20
+
21
+ def display_detectionsandcountings_detclasim(img_array, detections, c_cnames, c_scinames, coverage, prob_th=0, cth = 0):
22
+ img = Image.fromarray(img_array)
23
+ img1 = ImageDraw.Draw(img)
24
+ h, w = img.size
25
+ ratio = h/4000
26
+
27
+ for (box, _, y_prob, y_class, sciname) in detections:
28
+ y_prob = float(y_prob)
29
+ if y_prob > prob_th:
30
+ img1.rectangle(box, outline='red', width=int(20*ratio))
31
+ img1.text(box[:2], y_class+str(round(y_prob,3)), fill='white')
32
+
33
+ countings_list = list(c_scinames.items())
34
+ countings_list.sort(key = lambda x: x[1], reverse=True)
35
+ yi=int(20*ratio)
36
+ total = 0
37
+ for (y_class,c) in countings_list:
38
+ if c > cth:
39
+ img1.text((int(50*ratio), yi), "# {} = {}".format(y_class, c), fill='red')
40
+ yi += int(100*ratio)
41
+ total += c
42
+ yi += int(100*ratio)
43
+ img1.text((int(50*ratio), yi), "# {} = {}".format('total', total), fill='red')
44
+
45
+ text = f'coverage = {coverage}'+'\n\n'
46
+ text += 'Countings by scientific name:\n'
47
+ countings_list = list(c_scinames.items())
48
+ countings_list.sort(key = lambda x: x[1], reverse=True)
49
+ for key,value in countings_list:
50
+ text += f'{key} = {value}'+'\n'
51
+ text += '\n\n'
52
+ text += 'Countings by common name:\n'
53
+
54
+ countings_list = list(c_cnames.items())
55
+ countings_list.sort(key = lambda x: x[1], reverse=True)
56
+ for key,value in countings_list:
57
+ text += f'{key} = {value}'+'\n'
58
+ text += '\n'
59
+ text += f'total = {total}'+'\n'
60
+ return img, text
61
+
62
+ def display_detectionsandcountings_yolocounter(img_array, detections, countings, coverage, prob_th=0, cth = 0):
63
+ img = Image.fromarray(img_array)
64
+ img1 = ImageDraw.Draw(img)
65
+ h, w = img.size
66
+ ratio = h/4000
67
+
68
+ for (box, _, y_prob, y_class) in detections:
69
+ y_prob = float(y_prob)
70
+ if y_prob > prob_th:
71
+ img1.rectangle(box, outline='red', width=int(20*ratio))
72
+ img1.text(box[:2], y_class+str(round(y_prob,3)), fill='white')
73
+
74
+ countings_list = list(countings.items())
75
+ countings_list.sort(key = lambda x: x[1], reverse=True)
76
+ yi=int(20*ratio)
77
+ total = 0
78
+ for (y_class,c) in countings_list:
79
+ if c > cth:
80
+ img1.text((int(50*ratio), yi), "# {} = {}".format(y_class, c), fill='red')
81
+ yi += int(100*ratio)
82
+ total += c
83
+ yi += int(100*ratio)
84
+ img1.text((int(50*ratio), yi), "# {} = {}".format('total', total), fill='red')
85
+
86
+ text = f'coverage = {coverage}'+'\n\n'
87
+ for key,value in countings_list:
88
+ text += f'{key} = {value}'+'\n'
89
+ text += '\n'
90
+ text += f'total = {total}'+'\n'
91
+ return img, text
92
+
93
+ def display_detectionsandcountings_directcounter(img_array, countings, prob_th=0, cth = 0):
94
+ img = Image.fromarray(img_array)
95
+ img1 = ImageDraw.Draw(img)
96
+ h, w = img.size
97
+ ratio = h/4000
98
+
99
+ countings_list = list(countings.items())
100
+ countings_list.sort(key = lambda x: x[1], reverse=True)
101
+ yi=int(20*ratio)
102
+ total = 0
103
+ for (y_class,c) in countings_list:
104
+ if c > cth:
105
+ img1.text((int(50*ratio), yi), "# {} = {}".format(y_class, c), fill='red')
106
+ yi += int(100*ratio)
107
+ total += c
108
+ yi += int(100*ratio)
109
+ img1.text((int(50*ratio), yi), "# {} = {}".format('total', total), fill='red')
110
+
111
+ text = ''
112
+ for key,value in countings_list:
113
+ text += f'{key} = {value}'+'\n'
114
+ text += '\n'
115
+ text += f'total = {total}'+'\n'
116
+ return img, text
117
+
118
+ def testing_countingid(input_img):
119
+ api_url = 'http://countingid-test.us-east-1.elasticbeanstalk.com/predict'
120
+ response = send2api(input_img, api_url)
121
+ c_cnames = response['countings_cnames']
122
+ c_scinames = response['countings_scinames']
123
+ coverage = response['coverage']
124
+ detections = response['detections']
125
+ img, text = display_detectionsandcountings_detclasim(input_img, detections, c_cnames, c_scinames, coverage, prob_th=0, cth = 0)
126
+ return img, text
127
+
128
+ def testing_yolocounter(input_img):
129
+ api_url = 'http://yolocounter-test.us-east-1.elasticbeanstalk.com/predict'
130
+ response = send2api(input_img, api_url)
131
+ countings = response['countings_scinames']
132
+ coverage = response['coverage']
133
+ detections = response['detections']
134
+ img, text = display_detectionsandcountings_yolocounter(input_img, detections, countings, coverage, prob_th=0, cth = 0)
135
+ return img, text
136
+
137
+ def testing_directcounter(input_img):
138
+ api_url = 'http://directcounter-test.us-east-1.elasticbeanstalk.com/predict'
139
+ response = send2api(input_img, api_url)
140
+ countings = response['countings_scinames']
141
+ img, text = display_detectionsandcountings_directcounter(input_img, countings, prob_th=0, cth = 0)
142
+ return img, text
143
+
144
+ with gr.Blocks() as demo:
145
+ gr.Markdown("Submit an image with insects in a trap")
146
+
147
+ with gr.Tab("DetClaSim-based insect counter"):
148
+ with gr.Row():
149
+ input1 = gr.Image(shape=(500,500))
150
+ output1 =[gr.Image().style(height=500, width=500), gr.Textbox(lines=20)]
151
+ button1 = gr.Button("Submit")
152
+ button1.click(testing_countingid, input1, output1)
153
+
154
+ with gr.Tab("Yolocounter-based insect counter"):
155
+ with gr.Row():
156
+ input2 = gr.Image(shape=(500,500))
157
+ output2 =[gr.Image().style(height=500, width=500), gr.Textbox(lines=20)]
158
+ button2 = gr.Button("Submit")
159
+ button2.click(testing_yolocounter, input2, output2)
160
+
161
+ with gr.Tab("Direct insect counter"):
162
+ with gr.Row():
163
+ input3 = gr.Image(shape=(500,500))
164
+ output3 =[gr.Image().style(height=500, width=500), gr.Textbox(lines=20)]
165
+ button3 = gr.Button("Submit")
166
+ button3.click(testing_directcounter, input3, output3)
167
+
168
+ demo.launch(share=True)