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

Upload folder using huggingface_hub

Browse files
Files changed (9) hide show
  1. .gitattributes +2 -0
  2. README.md +3 -9
  3. app.py +338 -0
  4. best.pt +3 -0
  5. requirements.txt +7 -0
  6. test1.jpg +3 -0
  7. test2.jpg +0 -0
  8. test3.jpg +3 -0
  9. test4.jpg +0 -0
.gitattributes CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ test1.jpg filter=lfs diff=lfs merge=lfs -text
37
+ test3.jpg filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -1,12 +1,6 @@
1
  ---
2
- title: Receptacle Detection
3
- emoji: 🐢
4
- colorFrom: gray
5
- colorTo: gray
6
- sdk: gradio
7
- sdk_version: 3.47.1
8
  app_file: app.py
9
- pinned: false
 
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: receptacle_detection
 
 
 
 
 
3
  app_file: app.py
4
+ sdk: gradio
5
+ sdk_version: 3.42.0
6
  ---
 
 
app.py ADDED
@@ -0,0 +1,338 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
183
+ category_name = category.name
184
+ if category_name not in class_counts:
185
+ class_counts[category_name] = 1
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)
202
+ legend_bg_height = legend_size[1] + 2 * legend_padding
203
+ legend_bg_width = legend_size[0] + 2 * legend_padding
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:
256
+ gr.Markdown(
257
+ """
258
+ <h1 align="center">Receptacle Detector for Takeoff Automation</h1>
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()
best.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:77e155c040caeedc89419dbf90090e26a78a8a6aa49b7cbd5ffabe40b1f79e68
3
+ size 14835609
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ sahi
2
+ IPython
3
+ yolov5
4
+ ultralytics==8.0.186
5
+
6
+ # At 8.0.187 ultralytics deprecates ultralytics.yolo, but they have not updated the yolov5 package. Which breaks on import :/
7
+
test1.jpg ADDED

Git LFS Details

  • SHA256: 721c8f8100fa159e722cb55e1cdfef27f580d100638dc27dc0befa68d190622b
  • Pointer size: 132 Bytes
  • Size of remote file: 1.12 MB
test2.jpg ADDED
test3.jpg ADDED

Git LFS Details

  • SHA256: c5e27e921041bd54df31a1d95f09699ce961d5343e2058f262f23b8d4cadd443
  • Pointer size: 132 Bytes
  • Size of remote file: 1.17 MB
test4.jpg ADDED