-
Notifications
You must be signed in to change notification settings - Fork 0
/
toothbrush_head_final_visualize.py
418 lines (321 loc) · 14 KB
/
toothbrush_head_final_visualize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
## checklist
## file hwakjangja.. .jpg or .png or .bmp
## test_dir name!! check test_dir in visualize.py too!
import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ['CUDA_VISIBLE_DEVICES'] = "0"
import sys
import json
import datetime
import numpy as np
import skimage.draw
import pandas as pd
import cv2
import re
import random
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam
import time
import tensorflow as tf
from efficientnet.keras import EfficientNetB3, EfficientNetB0
from keras.models import load_model
from sklearn.metrics import confusion_matrix
import csv
# Root directory of the project
ROOT_DIR = os.path.abspath("../")
# Import Mask RCNN
from PIL import Image
sys.path.append(ROOT_DIR) # To find local version of the library
from mrcnn.config import Config
from mrcnn import model as modellib, utils
from mrcnn import abrush_visualize
import time
# Directory to save logs and model checkpoints, if not provided
# through the command line argument --logs
# DEFAULT_LOGS_DIR = os.path.join(ROOT_DIR, "logs")
DEFAULT_LOGS_DIR = "./logs"
DEFAULT_IMAGE_DIR = './datasets/brush'
DEFAULT_MRCNN_MODEL_DIR = './models/brush/mask_rcnn_toothbrush_head_0020.h5'
DEFAULT_EFF_MODEL_DIR = './models/brush/efficient-best_weight_220119_2.h5'
total_start = time.time()
############################################################
# Configurations
############################################################
class ToothBrushHeadConfig(Config):
"""Configuration for training on the toy dataset.
Derives from the base Config class and overrides some values.
"""
# Give the configuration a recognizable name
NAME = "toothbrush_head"
# We use a GPU with 12GB memory, which can fit two images.
# Adjust down if you use a smaller GPU.
IMAGES_PER_GPU = 2
# Number of classes (including background)
NUM_CLASSES = 1 + 1 # Background + balloon
# Number of training steps per epoch
STEPS_PER_EPOCH = 100
# Skip detections with < 90% confidence
DETECTION_MIN_CONFIDENCE = 0.9
############################################################
# Dataset
############################################################
def color_splash(image, mask):
"""Apply color splash effect.
image: RGB image [height, width, 3]
mask: instance segmentation mask [height, width, instance count]
Returns result image.
"""
# Make a grayscale copy of the image. The grayscale copy still
# has 3 RGB channels, though.
gray = skimage.color.gray2rgb(skimage.color.rgb2gray(image)) * 255
# Copy color pixels from the original color image where mask is set
if mask.shape[-1] > 0:
# We're treating all instances as one, so collapse the mask into one layer
mask = (np.sum(mask, -1, keepdims=True) >= 1)
splash = np.where(mask, image, gray).astype(np.uint8)
else:
splash = gray.astype(np.uint8)
return splash
detect_time = []
def detect_and_color_splash(model, image_path=None, img_file_name=None):
assert image_path
print("Running on {}".format(image_path))
# Read image
image = skimage.io.imread(image_path)
# Detect objects
# Run model detection and generate the color splash effect
start_inference = time.time()
r = model.detect([image], verbose=1)[0]
## check time for inference
end_inference = time.time()
dd_time = end_inference - start_inference
dd_time = np.floor(dd_time*10) / 10
if "dummy" not in img_file_name:
print("inference_time :", dd_time," sec for", img_file_name)
detect_time.append(dd_time)
# bounding box visualize
class_names = ['background', 'defect']
bbox = utils.extract_bboxes(r['masks'])
file_name_bb = "bb_splash_{}".format(img_file_name)
save_path_bb = os.path.join(DEFAULT_IMAGE_DIR, 'result', file_name_bb)
# print("image_path", image_path)
bv = abrush_visualize.display_instances(save_path_bb, image_path, image, bbox, r['masks'], r['class_ids'], class_names, r['scores'])
#brush_visualize.display_instances(save_path_bb, image_path, image, bbox, r['masks'], r['class_ids'], class_names, r['scores'])
# print("points : ", bv)
############## show input iamge #############
#display_img = cv2.imread(image_path, 3)
#display_img = cv2.resize(display_img, (int(display_img.shape[1]*0.4), int(display_img.shape[0]*0.4)))
# cv2.imshow("input image", display_img)
##########################################################
splash = color_splash(image, r['masks'])
# Save output
file_name = "splash_{}".format(img_file_name)
save_path = os.path.join(DEFAULT_IMAGE_DIR , 'result', file_name)
skimage.io.imsave(save_path, splash)
#display_splash = splash.copy()
#display_splash = cv2.resize(display_splash, (int(display_splash.shape[1]*0.4), int(display_splash.shape[0]*0.4)))
#cv2.imshow("splash", display_splash)
#cv2.waitKey(3)
print("Saved to ", save_path)
return bv
#result =[]
############################################################
# sort filenames by human order
############################################################
def atoi(text):
return int(text) if text.isdigit() else text
def natural_keys(text):
return [ atoi(c) for c in re.split(r'(\d+)', text) ]
############################################################
# classification
############################################################
class_time = []
def binary_classification(imgname, model, points):
test_dir = os.path.join(DEFAULT_IMAGE_DIR+'/cropped/'+imgname)
cnt_cropped = os.path.join(test_dir+'/test')
cropped = len(os.listdir(cnt_cropped))
path = "/home/yjkim/NOAH/gongin/datasets/brush/result/"
res_path = os.path.join(path+'bb_splash_'+imgname+'.png')
or_path = "/home/yjkim/NOAH/gongin/datasets/brush/test/"
ori_path = os.path.join(or_path + imgname+'.bmp')
res_img = cv2.imread(ori_path, 3)
input_img = res_img.copy()
bb_img = cv2.imread(res_path, 3)
if cropped == 25 or cropped == 34:
test_datagen = ImageDataGenerator(
rescale=1 / 255
)
test_generator = test_datagen.flow_from_directory(
test_dir,
target_size=(32, 32),
batch_size=1,
shuffle=False,
class_mode=None
)
#print("check filenames : ", test_generator.filenames)
start_classification = time.time()
preds = model.predict_generator(test_generator, steps=len(test_generator.filenames))
end_classification = time.time()
## check time
inference_time = end_classification - start_classification
inference_time = np.floor(inference_time *10) /10
if "dummy" not in imgname:
print(inference_time," sec for inferencing toothbrush hair")
class_time.append(inference_time)
test_generator.filenames.sort(key=natural_keys)
# print("after sort : ", test_generator.filenames)
# print(preds)
image_ids = [name.split('/')[-1] for name in test_generator.filenames]
predictions = preds.flatten()
error = []
cnt = []
for i in range(len(test_generator.filenames)):
if predictions[i] > 0.5:
error.append('error')
cnt.append(i)
else:
error.append('normal')
data = {'filename': image_ids, 'true_label': test_generator.classes, 'category': error}
submission = pd.DataFrame(data)
for i in cnt:
x1,x2,y1,y2 = points[i]
print(f"(x1, y1) = ({x1}, {y1}), (x2, y2) = ({x2}, {y2})")
cv2.rectangle(res_img, (x1,y1), (x2,y2), (0,0,255), 2)
# text and bbox on result image
res_img = cv2.resize(res_img, (int(res_img.shape[1]*0.5), int(res_img.shape[0]*0.5)))
bb_img = cv2.resize(bb_img, (int(bb_img.shape[1]*0.5), int(bb_img.shape[0]*0.5)))
input_img = cv2.resize(input_img, (int(input_img.shape[1]*0.5), int(input_img.shape[0]*0.5)))
res_img = cv2.putText(res_img, f"{len(cnt)} error brush detected", (200,100), cv2.FONT_HERSHEY_PLAIN, 3, (0,0,255), 2)
cv2.imwrite(f"/home/yjkim/NOAH/gongin/datasets/brush/result/bbox_{imgname}.png", res_img)
cv2.imwrite(f"/home/yjkim/NOAH/gongin/datasets/brush/result/bb_text_{imgname}.png", bb_img)
print(f"brush number {cnt} is error")
cv2.imshow("input image", input_img)
cv2.imshow("detected image", bb_img)
cv2.imshow("result image", res_img)
while(True):
if cv2.waitKey(1)&0xFF == ord('x'):
cv2.destroyAllWindows()
break
# final classification! whether error or not
if (submission['category'] == 'error').any():
print(imgname,"is error tooth brush")
return 0
else:
return 1
else:
inference_time = 0
class_time.append(inference_time)
print(round(inference_time,2), " sec for inferencing toothbrush hair")
# text on result image
# 식모 갯수보다 더 잡았을때 => 식모가 너무 불규칙해서 에러로 잡는 경우
res_img = cv2.putText(res_img, f"more brush detected", (200,100), cv2.FONT_HERSHEY_PLAIN, 3, (0,0,255), 2)
res_img = cv2.resize(res_img, (int(res_img.shape[1]*0.5), int(res_img.shape[0]*0.5)))
input_img = cv2.resize(input_img, (int(input_img.shape[1]*0.5), int(input_img.shape[0]*0.5)))
cv2.imshow("input image", input_img)
cv2.imshow("result image", res_img)
while (True):
if cv2.waitKey(1) & 0xFF == ord('x'):
cv2.destroyAllWindows()
break
#cv2.waitKey(3)
return 0
############################################################
# main
############################################################
if __name__ == '__main__':
class InferenceConfig(ToothBrushHeadConfig):
# Set batch size to 1 since we'll be running inference on
# one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
GPU_COUNT = 1
IMAGES_PER_GPU = 1
config = InferenceConfig()
config.display()
#####
result_path = DEFAULT_IMAGE_DIR+'/result'
cropped_path = DEFAULT_IMAGE_DIR+'/cropped'
if not os.path.isdir(result_path):
os.mkdir(DEFAULT_IMAGE_DIR+'/result')
os.mkdir(DEFAULT_IMAGE_DIR+'/cropped')
################# LOAD MODELS #######################
#### MASK-R-CNN
mrcnn_model = modellib.MaskRCNN(mode="inference", config=config,
model_dir=DEFAULT_LOGS_DIR)
weights_path = DEFAULT_MRCNN_MODEL_DIR
mrcnn_model.load_weights(weights_path, by_name=True)
##### EFFICIENTNET
efficient_net = EfficientNetB0(
weights='imagenet',
input_shape=(32, 32, 3),
include_top=False,
pooling='max'
)
eff_model = Sequential()
eff_model.add(efficient_net)
eff_model.add(Dense(units=120, activation='relu'))
eff_model.add(Dense(units=120, activation='relu'))
eff_model.add(Dense(units=1, activation='sigmoid'))
eff_model.summary()
eff_model.compile(optimizer=Adam(lr=0.0001), loss='binary_crossentropy', metrics=['accuracy'])
eff_model.load_weights(DEFAULT_EFF_MODEL_DIR)
#############################################################################3
# each image in folder
image_path = DEFAULT_IMAGE_DIR
image_dir = os.path.join(image_path + "/test")
dirs = os.listdir(image_dir)
images = [file for file in dirs if file.endswith('.png') or file.endswith('.jpg') or file.endswith('.bmp')]
#images.sort()
random.shuffle(images)
random.shuffle(images)
# print("len iamges :::: ", len(images))
result = []
submission = {}
err_toothbrush_total = []
each_toothbrush_info_total =[]
## arrange results and make csv file
for img in images:
imgname = os.path.join(image_dir, img)
onlyname, _ = os.path.splitext(img)
imgname_png = onlyname + '.png'
# output_imgname = os.path.join(image_path, imgname_png)
bv = detect_and_color_splash(mrcnn_model, image_path=imgname, img_file_name=imgname_png)
#detect_and_color_splash(mrcnn_model, image_path=imgname, img_file_name=imgname_png)
result = binary_classification(onlyname, eff_model, bv)
submission[onlyname] = result
#print("submission : ", submission)
TN = 0
TP = 0
normal_file = 0
error_file = 0
total_test_img = len(submission) - 2
for i in submission:
if "Frame" in i and "dummy" not in i:
normal_file += 1
if "Frame" not in i and "dummy" not in i:
error_file += 1
if "dummy" in i:
continue
if "Frame" not in i and submission[i] == 0:
TN += 1
elif "Frame" in i and submission[i] == 1:
TP += 1 ### compute time
total_end = time.time()
print("total time : ", total_end- total_start)
detect_avg = np.floor((sum(detect_time, 0.0) / len(detect_time)) * 10) /10
classi_avg = np.floor((sum(class_time, 0.0) / len(class_time)) * 10) / 10
acc = (TN + TP) / total_test_img
print("length of detect_time : ", len(detect_time))
print("length of class_time : ", len(class_time))
total_avg = detect_avg + classi_avg
print("total_test_img", total_test_img)
print("normal_file : ", normal_file)
print("err_file : ", error_file)
print("Average classification time : ", classi_avg)
print("Average detection time : ", detect_avg)
print("==========================================================")
print("True Positive", TP)
print("True Negative", TN)
# print(f"(TP + TN) / 전체이미지 : ({TP} + {TN}) / {len(images)}")
print("Accuracy : ", round(acc, 2)*100, "%")
print("Average Time per Image : ", total_avg, "s")