rbanfield commited on
Commit
4542bfb
·
1 Parent(s): 4eab535

Rework the code, add json output

Browse files
Files changed (1) hide show
  1. app.py +139 -231
app.py CHANGED
@@ -1,182 +1,71 @@
 
1
 
2
  import gradio as gr
3
  import cv2
4
- import requests
5
- import os
6
  import numpy as np
7
- from sahi.utils.yolov5 import (
8
- download_yolov5s6_model,
9
- )
10
-
11
 
12
  # import required functions, classes
13
  from sahi import AutoDetectionModel
14
- from sahi.utils.cv import read_image
15
- from sahi.utils.file import download_from_url
16
- from sahi.predict import get_prediction, get_sliced_prediction, predict, visualize_object_predictions
17
- from IPython.display import Image
18
-
19
- from ultralytics import YOLO
20
- import gradio as gr
21
- import cv2
22
- import requests
23
- import os
24
-
25
- from ultralytics import YOLO
26
 
27
 
28
- yolov5_model_path = 'best.pt'
29
  download_yolov5s6_model(destination_path=yolov5_model_path)
30
  detection_model = AutoDetectionModel.from_pretrained(
31
- model_type='yolov5',
32
  model_path=yolov5_model_path,
33
  confidence_threshold=0.01,
34
- device="cpu", # or 'cuda:0'
35
  )
36
 
37
- #model = YOLO('/home/ubuntu/Receptacle_Detection_Demo/best.pt')
38
-
39
-
40
-
41
- demo = gr.Blocks()
42
-
43
  EXAMPLES = [
44
- [ "test1.jpg"],
45
  ["test2.jpg"],
46
  ["test3.jpg"],
47
  ["test4.jpg"],
48
  ]
49
 
50
 
51
- def with_labels(image_path):
52
  result = get_sliced_prediction(
53
- image_path,
54
- detection_model,
55
- slice_height = 512,
56
- slice_width = 512,
57
- overlap_height_ratio = 0.12,
58
- overlap_width_ratio = 0.12)
59
- #result.export_visuals(export_dir="/home/ubuntu/Receptacle_Detection_Demo/")
60
- #image = cv2.imread("/home/ubuntu/Receptacle_Detection_Demo/prediction_visual.png")
61
- #img = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)
62
 
63
  count = -1
64
- new_list=[]
65
  for i in result.object_prediction_list:
66
  count += 1
67
- print(i)
68
  score = i.score
69
  value = score.value
70
  category = i.category
71
  category_name = category.name
72
  if value > confidence_scores[category_name]:
73
- print(value)
74
- print(confidence_scores[category_name])
75
  new_list.append(result.object_prediction_list[count])
76
-
77
- img_converted = cv2.cvtColor(image_path, cv2.COLOR_BGR2RGB)
78
- numpydata = np.asarray(img_converted)
79
- visualize_object_predictions(
80
- numpydata,
81
- object_prediction_list = new_list,
82
- text_size=1,
83
- text_th=1,
84
- hide_labels = 0,
85
- rect_th=3,
86
- output_dir='/home/ubuntu/Receptacle_Detection_Demo/',
87
- file_name = 'result',
88
- export_format = 'png')
89
- image2 = cv2.imread("/home/ubuntu/Receptacle_Detection_Demo/result.png")
90
- img_rgb = cv2.cvtColor(image2, cv2.COLOR_BGR2RGB)
91
-
92
-
93
- class_counts = {}
94
-
95
-
96
- predictions = new_list
97
- for i in predictions:
98
- category = i.category
99
- category_name = category.name
100
- if category_name not in class_counts:
101
- class_counts[category_name] = 1
102
- else:
103
- class_counts[category_name] += 1
104
-
105
-
106
- legend_text = 'Symbols Counted:'
107
- for class_name, count in class_counts.items():
108
- legend_text += f' {class_name}: {count} |'
109
-
110
- font = cv2.FONT_HERSHEY_SIMPLEX
111
- font_scale = 1
112
- font_color = (255, 255, 255)
113
- font_thickness = 2
114
- legend_bg_color = (131, 79, 0)
115
- legend_padding = 10
116
 
117
- legend_size, _ = cv2.getTextSize(legend_text, font, font_scale, font_thickness)
118
- legend_bg_height = legend_size[1] + 2 * legend_padding
119
- legend_bg_width = legend_size[0] + 2 * legend_padding
120
-
121
- legend_bg = np.zeros((legend_bg_height, legend_bg_width, 3), dtype=np.uint8)
122
- legend_bg[:] = legend_bg_color
123
- cv2.putText(legend_bg, legend_text, (legend_padding, legend_padding + legend_size[1]), font,
124
- font_scale, font_color, font_thickness)
125
-
126
- img_height, img_width, _ = img_rgb.shape
127
- legend_x = img_width - legend_bg_width
128
- legend_y = img_height - legend_bg_height
129
-
130
- img_rgb[legend_y:, legend_x:, :] = legend_bg
131
-
132
- result_image_path = '/home/ubuntu/Receptacle_Detection_Demo/result_with_legend.png'
133
- cv2.imwrite(result_image_path, img_rgb)
134
-
135
- return cv2.cvtColor(cv2.imread(result_image_path), cv2.COLOR_BGR2RGB)
136
-
137
- def without_labels(image_path):
138
- result = get_sliced_prediction(
139
- image_path,
140
- detection_model,
141
- slice_height = 512,
142
- slice_width = 512,
143
- overlap_height_ratio = 0.12,
144
- overlap_width_ratio = 0.12)
145
- #result.export_visuals(export_dir="/home/ubuntu/Receptacle_Detection_Demo/")
146
- #image = cv2.imread("/home/ubuntu/Receptacle_Detection_Demo/prediction_visual.png")
147
- #img = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)
148
-
149
- count = -1
150
- new_list=[]
151
- for i in result.object_prediction_list:
152
- count += 1
153
- print(i)
154
- score = i.score
155
- value = score.value
156
- category = i.category
157
- category_name = category.name
158
- if value > confidence_scores[category_name]:
159
- print(value)
160
- print(confidence_scores[category_name])
161
- new_list.append(result.object_prediction_list[count])
162
-
163
  img_converted = cv2.cvtColor(image_path, cv2.COLOR_BGR2RGB)
164
  numpydata = np.asarray(img_converted)
165
  visualize_object_predictions(
166
- numpydata,
167
- object_prediction_list = new_list,
168
- hide_labels = 1,
169
- rect_th=3,
170
- output_dir='/home/ubuntu/Receptacle_Detection_Demo/',
171
- file_name = 'result',
172
- export_format = 'png')
 
 
 
173
  image2 = cv2.imread("/home/ubuntu/Receptacle_Detection_Demo/result.png")
174
  img_rgb = cv2.cvtColor(image2, cv2.COLOR_BGR2RGB)
175
 
176
-
177
  class_counts = {}
178
 
179
-
180
  predictions = new_list
181
  for i in predictions:
182
  category = i.category
@@ -186,16 +75,18 @@ def without_labels(image_path):
186
  else:
187
  class_counts[category_name] += 1
188
 
189
-
190
- legend_text = 'Symbols Counted:'
191
  for class_name, count in class_counts.items():
192
- legend_text += f' {class_name}: {count} |'
193
-
194
  font = cv2.FONT_HERSHEY_SIMPLEX
195
- font_scale = 1.5
196
- font_color = (255, 255, 255)
 
 
 
197
  font_thickness = 2
198
- legend_bg_color = (131, 79, 0)
199
  legend_padding = 10
200
 
201
  legend_size, _ = cv2.getTextSize(legend_text, font, font_scale, font_thickness)
@@ -204,52 +95,62 @@ def without_labels(image_path):
204
 
205
  legend_bg = np.zeros((legend_bg_height, legend_bg_width, 3), dtype=np.uint8)
206
  legend_bg[:] = legend_bg_color
207
- cv2.putText(legend_bg, legend_text, (legend_padding, legend_padding + legend_size[1]), font,
208
- font_scale, font_color, font_thickness)
209
-
 
 
 
 
 
 
 
210
  img_height, img_width, _ = img_rgb.shape
211
  legend_x = img_width - legend_bg_width
212
  legend_y = img_height - legend_bg_height
213
 
214
  img_rgb[legend_y:, legend_x:, :] = legend_bg
215
 
216
- result_image_path = '/home/ubuntu/Receptacle_Detection_Demo/result_with_legend.png'
217
  cv2.imwrite(result_image_path, img_rgb)
218
 
219
- return cv2.cvtColor(cv2.imread(result_image_path), cv2.COLOR_BGR2RGB)
 
 
 
220
 
221
- def choose_function(choice, input_text):
222
- if choice == "With Labels":
223
- return with_labels(input_text)
224
- else:
225
- return without_labels(input_text)
226
- print("Starting the demo...")
227
 
 
 
 
228
 
229
 
230
- def update_duplex(val):
231
- confidence_scores['Duplex - Standard'] = val
232
- return 'updated!'
233
  def update_single(val):
234
- confidence_scores['Singleplex - Standard'] = val
235
- return 'updated!'
 
236
 
237
  def update_triplex(val):
238
- confidence_scores['Triplex - Standard'] = val
239
- return 'updated!'
 
240
 
241
  def update_quadruplex(val):
242
- confidence_scores['Quadruplex - Standard'] = val
243
- return 'updated!'
 
244
 
245
  def update_gfci(val):
246
- confidence_scores['Duplex - GFCI'] = val
247
- return 'updated!'
 
248
 
249
  def update_gfciwp(val):
250
- confidence_scores['Duplex - Weatherproof-GFCI'] = val
251
- return 'updated!'
252
 
 
 
253
  theme = gr.themes.Soft()
254
 
255
  with gr.Blocks(theme=theme) as demo:
@@ -259,80 +160,87 @@ with gr.Blocks(theme=theme) as demo:
259
  """
260
  )
261
 
262
- gr.Markdown("### Step 1: Upload an image")
263
-
264
  with gr.Row():
265
  input_image = gr.Image(
266
- label="Upload an image here.", source="upload", interactive=True,
 
 
267
  )
268
  examples = gr.Examples(
269
- examples=EXAMPLES, inputs=[input_image], examples_per_page=4, label="Examples to use.",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270
  )
271
 
272
-
273
- gr.Markdown("### Step 2: Choose either \n With labels: See receptacles detected with type detected/confidence score included\n Without labels: See only bounding boxes")
274
- filter_name = gr.Dropdown(
275
- choices=["With Labels", "Without Labels"], label="With/Without Labels", interactive=True
276
- )
277
- gr.Markdown("### Step 3: Choose confidence score levels for each symbol detected (default are optimal scores)")
278
- filter_name1 = gr.Slider(
279
- minimum = .1,
280
- maximum = 1,
281
- value = .53,
282
- interactive = True,
283
- label = 'Singleplex',
284
- )
285
- filter_name2 = gr.Slider(
286
- minimum = .1,
287
- maximum = 1,
288
- value = .66,
289
- interactive = True,
290
- label = 'Duplex',
291
- )
292
- filter_name3 = gr.Slider(
293
- minimum = .1,
294
- maximum = 1,
295
- value = .65,
296
- interactive = True,
297
- label = 'Triplex',
298
- )
299
- filter_name4 = gr.Slider(
300
- minimum = .1,
301
- maximum = 1,
302
- value = .63,
303
- interactive = True,
304
- label = 'Quadruplex',
305
- )
306
- filter_name5 = gr.Slider(
307
- minimum = .1,
308
- maximum = 1,
309
- value = .31,
310
- interactive = True,
311
- label = 'GFCI',
312
- )
313
- filter_name6 = gr.Slider(
314
- minimum = .1,
315
- maximum = 1,
316
- value = .33,
317
- interactive = True,
318
- label = 'GFCI/WP',
319
- )
320
-
321
- filter_name2.change(fn=update_duplex, inputs=filter_name2)
322
  filter_name1.change(fn=update_single, inputs=filter_name1)
 
323
  filter_name3.change(fn=update_triplex, inputs=filter_name3)
324
  filter_name4.change(fn=update_quadruplex, inputs=filter_name4)
325
  filter_name5.change(fn=update_gfci, inputs=filter_name5)
326
  filter_name6.change(fn=update_gfciwp, inputs=filter_name6)
327
- confidence_scores = {'Triplex - Standard': filter_name3.value,'Duplex - Standard': filter_name2.value,'Singleplex - Standard': filter_name1.value,'Duplex - GFCI': filter_name5.value,'Duplex - Weatherproof-GFCI':filter_name6.value,'Quadruplex - Standard': filter_name4.value}
328
-
329
- gr.Markdown("### Step 4: See results with number of symbols counted in the bottom right corner")
330
- results_button = gr.Button("See Results")
 
 
 
 
 
 
331
  results_button.click(
332
- choose_function,
333
- inputs = [filter_name,input_image],
334
- outputs = [gr.components.Image(type="numpy", label="Output Image")]
335
-
 
 
336
  )
337
-
338
  demo.launch()
 
1
+ import json
2
 
3
  import gradio as gr
4
  import cv2
 
 
5
  import numpy as np
6
+ from sahi.utils.yolov5 import download_yolov5s6_model
 
 
 
7
 
8
  # import required functions, classes
9
  from sahi import AutoDetectionModel
10
+ from sahi.predict import get_sliced_prediction, visualize_object_predictions
 
 
 
 
 
 
 
 
 
 
 
11
 
12
 
13
+ yolov5_model_path = "best.pt"
14
  download_yolov5s6_model(destination_path=yolov5_model_path)
15
  detection_model = AutoDetectionModel.from_pretrained(
16
+ model_type="yolov5",
17
  model_path=yolov5_model_path,
18
  confidence_threshold=0.01,
19
+ device="cpu", # or 'cuda:0'
20
  )
21
 
 
 
 
 
 
 
22
  EXAMPLES = [
23
+ ["test1.jpg"],
24
  ["test2.jpg"],
25
  ["test3.jpg"],
26
  ["test4.jpg"],
27
  ]
28
 
29
 
30
+ def do_detection(image_path, hide_labels=False):
31
  result = get_sliced_prediction(
32
+ image_path,
33
+ detection_model,
34
+ slice_height=512,
35
+ slice_width=512,
36
+ overlap_height_ratio=0.12,
37
+ overlap_width_ratio=0.12,
38
+ )
 
 
39
 
40
  count = -1
41
+ new_list = []
42
  for i in result.object_prediction_list:
43
  count += 1
 
44
  score = i.score
45
  value = score.value
46
  category = i.category
47
  category_name = category.name
48
  if value > confidence_scores[category_name]:
 
 
49
  new_list.append(result.object_prediction_list[count])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  img_converted = cv2.cvtColor(image_path, cv2.COLOR_BGR2RGB)
52
  numpydata = np.asarray(img_converted)
53
  visualize_object_predictions(
54
+ numpydata,
55
+ object_prediction_list=new_list,
56
+ text_size=1,
57
+ text_th=1,
58
+ hide_labels=hide_labels,
59
+ rect_th=3,
60
+ output_dir="/home/ubuntu/Receptacle_Detection_Demo/",
61
+ file_name="result",
62
+ export_format="png",
63
+ )
64
  image2 = cv2.imread("/home/ubuntu/Receptacle_Detection_Demo/result.png")
65
  img_rgb = cv2.cvtColor(image2, cv2.COLOR_BGR2RGB)
66
 
 
67
  class_counts = {}
68
 
 
69
  predictions = new_list
70
  for i in predictions:
71
  category = i.category
 
75
  else:
76
  class_counts[category_name] += 1
77
 
78
+ legend_text = "Symbols Counted:"
 
79
  for class_name, count in class_counts.items():
80
+ legend_text += f" {class_name}: {count} |"
81
+
82
  font = cv2.FONT_HERSHEY_SIMPLEX
83
+ if hide_labels:
84
+ font_scale = 1.5
85
+ else:
86
+ font_scale = 1
87
+ font_color = (255, 255, 255)
88
  font_thickness = 2
89
+ legend_bg_color = (131, 79, 0)
90
  legend_padding = 10
91
 
92
  legend_size, _ = cv2.getTextSize(legend_text, font, font_scale, font_thickness)
 
95
 
96
  legend_bg = np.zeros((legend_bg_height, legend_bg_width, 3), dtype=np.uint8)
97
  legend_bg[:] = legend_bg_color
98
+ cv2.putText(
99
+ legend_bg,
100
+ legend_text,
101
+ (legend_padding, legend_padding + legend_size[1]),
102
+ font,
103
+ font_scale,
104
+ font_color,
105
+ font_thickness,
106
+ )
107
+
108
  img_height, img_width, _ = img_rgb.shape
109
  legend_x = img_width - legend_bg_width
110
  legend_y = img_height - legend_bg_height
111
 
112
  img_rgb[legend_y:, legend_x:, :] = legend_bg
113
 
114
+ result_image_path = "/home/ubuntu/Receptacle_Detection_Demo/result_with_legend.png"
115
  cv2.imwrite(result_image_path, img_rgb)
116
 
117
+ return (
118
+ cv2.cvtColor(cv2.imread(result_image_path), cv2.COLOR_BGR2RGB),
119
+ result.to_coco_predictions(),
120
+ )
121
 
 
 
 
 
 
 
122
 
123
+ def update_duplex(val):
124
+ confidence_scores["Duplex - Standard"] = val
125
+ return "updated!"
126
 
127
 
 
 
 
128
  def update_single(val):
129
+ confidence_scores["Singleplex - Standard"] = val
130
+ return "updated!"
131
+
132
 
133
  def update_triplex(val):
134
+ confidence_scores["Triplex - Standard"] = val
135
+ return "updated!"
136
+
137
 
138
  def update_quadruplex(val):
139
+ confidence_scores["Quadruplex - Standard"] = val
140
+ return "updated!"
141
+
142
 
143
  def update_gfci(val):
144
+ confidence_scores["Duplex - GFCI"] = val
145
+ return "updated!"
146
+
147
 
148
  def update_gfciwp(val):
149
+ confidence_scores["Duplex - Weatherproof-GFCI"] = val
150
+ return "updated!"
151
 
152
+
153
+ demo = gr.Blocks()
154
  theme = gr.themes.Soft()
155
 
156
  with gr.Blocks(theme=theme) as demo:
 
160
  """
161
  )
162
 
 
 
163
  with gr.Row():
164
  input_image = gr.Image(
165
+ label="Upload an image here.",
166
+ source="upload",
167
+ interactive=True,
168
  )
169
  examples = gr.Examples(
170
+ examples=EXAMPLES,
171
+ inputs=[input_image],
172
+ examples_per_page=4,
173
+ label="Examples to use.",
174
+ )
175
+
176
+ hide_labels = gr.Checkbox(label="Hide labels")
177
+ with gr.Accordion("Visualization Confidence Thresholds", open=False):
178
+ filter_name1 = gr.Slider(
179
+ minimum=0.1,
180
+ maximum=1,
181
+ value=0.53,
182
+ interactive=True,
183
+ label="Singleplex",
184
+ )
185
+ filter_name2 = gr.Slider(
186
+ minimum=0.1,
187
+ maximum=1,
188
+ value=0.66,
189
+ interactive=True,
190
+ label="Duplex",
191
+ )
192
+ filter_name3 = gr.Slider(
193
+ minimum=0.1,
194
+ maximum=1,
195
+ value=0.65,
196
+ interactive=True,
197
+ label="Triplex",
198
+ )
199
+ filter_name4 = gr.Slider(
200
+ minimum=0.1,
201
+ maximum=1,
202
+ value=0.63,
203
+ interactive=True,
204
+ label="Quadruplex",
205
+ )
206
+ filter_name5 = gr.Slider(
207
+ minimum=0.1,
208
+ maximum=1,
209
+ value=0.31,
210
+ interactive=True,
211
+ label="GFCI",
212
+ )
213
+ filter_name6 = gr.Slider(
214
+ minimum=0.1,
215
+ maximum=1,
216
+ value=0.33,
217
+ interactive=True,
218
+ label="GFCI/WP",
219
  )
220
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
  filter_name1.change(fn=update_single, inputs=filter_name1)
222
+ filter_name2.change(fn=update_duplex, inputs=filter_name2)
223
  filter_name3.change(fn=update_triplex, inputs=filter_name3)
224
  filter_name4.change(fn=update_quadruplex, inputs=filter_name4)
225
  filter_name5.change(fn=update_gfci, inputs=filter_name5)
226
  filter_name6.change(fn=update_gfciwp, inputs=filter_name6)
227
+ confidence_scores = {
228
+ "Triplex - Standard": filter_name3.value,
229
+ "Duplex - Standard": filter_name2.value,
230
+ "Singleplex - Standard": filter_name1.value,
231
+ "Duplex - GFCI": filter_name5.value,
232
+ "Duplex - Weatherproof-GFCI": filter_name6.value,
233
+ "Quadruplex - Standard": filter_name4.value,
234
+ }
235
+
236
+ results_button = gr.Button("Submit")
237
  results_button.click(
238
+ do_detection,
239
+ inputs=[input_image, hide_labels],
240
+ outputs=[
241
+ gr.Image(type="numpy", label="Output Image"),
242
+ gr.Json(),
243
+ ],
244
  )
245
+
246
  demo.launch()