def check_down(self, path): colors_check = np.zeros((15, 6), dtype=np.int) points_check = np.zeros((15, 6), dtype=np.int) img = cv2.imread(path) crop = img[400:1500, 1300:1900] # crop = cv2.flip(crop,-1) saver(crop, "C") crop_gray = cv2.cvtColor(crop, cv2.COLOR_BGR2GRAY) circles = cv2.HoughCircles(crop_gray, cv2.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=15, maxRadius=34) draw = self._drawCircles(crop, circles) saver(draw, "D_%s" % path[-8:-4]) one = circles[0] one = sorted(one, key=cmp_to_key(_sortSmall)) radius = 30 count = 0 points = 0 for c in one: # print(c) c = np.array(c).astype('int') circle = crop[c[1] - radius:c[1] + radius, c[0] - radius:c[0] + radius, :] up_circle = circle[0:radius, radius:2 * radius] down_circle = circle[radius:2 * radius, 0:radius] # saver(circle,"C_%d"%count) # saver(down_circle, "D_%d"%count) # saver(up_circle, "U_%d"%count) color = checkAllColor(circle) points = self._checkPont(up_circle, 1) + self._checkPont( down_circle, 0) colors_check[count // 6][count % 6] = color points_check[count // 6][count % 6] = points count += 1 for c in range(count, 90): colors_check[count // 6][count % 6] = -1 points_check[count // 6][count % 6] = -1 count += 1 return colors_check, points_check
def check_card(self, path): colors_check = np.zeros((15, 6), dtype=np.int) img = cv2.imread(path) crop = img[195:1215, 765:1185] crop_gray = cv2.cvtColor(crop, cv2.COLOR_BGR2GRAY) crop_gray = cv2.GaussianBlur(crop_gray, (5, 5), 0) circles = cv2.HoughCircles(crop_gray, cv2.HOUGH_GRADIENT, 1, 40, param1=45.5, param2=20, minRadius=15, maxRadius=35) draw = self._drawCircles(crop, circles) saver(draw, "D_%s" % path[-8:-4]) one = circles[0] one = sorted(one, key=cmp_to_key(_sortSmall)) radius = 30 count = 0 points = 0 for c in one: # print(c) try: c = np.array(c).astype('int') circle = crop[c[1] - radius:c[1] + radius, c[0] - radius:c[0] + radius, :] color = checkAllColor(circle) colors_check[count // 6][count % 6] = color count += 1 except Exception as e: print("run out of boundry") break for c in range(count, 90): colors_check[count // 6][count % 6] = -1 count += 1 return colors_check
else: return 2 def checkAllColor(img): blue_rate = checkBlue(img).mean() red_rate = checkRed(img).mean() green_rate = checkGreen(img).mean() if red_rate < 30 and blue_rate < 30 and green_rate<30: return -1 if blue_rate>red_rate and blue_rate>green_rate: return 2 if red_rate>blue_rate and red_rate>green_rate: return 1 if green_rate>blue_rate and green_rate>red_rate: return 3 return -1 if __name__ == "__main__": path = "outer/test_C_22.png" img = cv2.imread(path) r = checkGreen(img) print("Green: ", r.mean()) saver(r,"GREEN") print("Blue: ", checkBlue(img).mean()) saver(checkBlue(img),"Blue") print("Green", checkGreen(img).mean()) saver(checkRed(img),"Red")
if args.use_metric: model_metric = ArcFaceLoss(in_features=int(args.layers[-1]), out_features=sc_dataset_train.num_class) model_metric.to(device) parameters.append({'params': model_metric.parameters()}) model_name = 'MLP_{}_metric'.format("-".join(args.layers)) else: model_metric = None model_name = 'MLP_{}'.format("-".join(args.layers)) optimizer = torch.optim.SGD(parameters, lr=args.learning_rate, momentum=args.sgd_momentum) embedding_writer = SummaryWriter(results_path) saver = saver(save_path=results_path,\ model_name=model_name,\ writer=embedding_writer,\ args=args) results = train(dataloaders=dataloader_dict,\ model=model,\ model_metric=model_metric,\ paired=True, criterion=criterion,\ optimizer=optimizer,\ device=device,\ saver=saver,\ num_epoch=args.num_epoch)
kernel = np.ones((4, 4), np.uint8) def clean_noise(im, type): if type == 1: green_mask = checkGreen(im) x2, y2 = green_mask.shape mix_mask = cv2.rectangle(im.copy(), (0, 0), (y2, x2), (255, 255, 255), -1) green_mask = cv2.erode(green_mask, kernel, iterations=1) green_mask = cv2.dilate(green_mask, kernel, iterations=1) _, green_mask_I = cv2.threshold(green_mask.copy(), 10, 255, cv2.THRESH_BINARY_INV) _, green_mask = cv2.threshold(green_mask.copy(), 10, 255, cv2.THRESH_BINARY) clean = cv2.bitwise_and(im, im.copy(), mask=green_mask_I) mix_mask = cv2.bitwise_and(mix_mask, mix_mask.copy(), mask=green_mask) clean = cv2.add(clean, mix_mask) return clean return im if __name__ == "__main__": path = './outer/test_CROP.png' im = cv2.imread(path) cleaned = clean_noise(im, 1) saver(cleaned, "G")
label, num_batch_val * (epoch - 1) + batch, dataformats='NHWC') writer_val.add_image('image', image, num_batch_val * (epoch - 1) + batch, dataformats='NHWC') writer_val.add_image('output', output, num_batch_val * (epoch - 1) + batch, dataformats='NHWC') # Tensorboard 저장하기 (손실함수 저장) writer_val.add_scalar('loss', np.mean(loss_arr), epoch) if epoch // 5 == 0: saver(ckpt_dir=ckpt_dir, net=net, optim=optim, epoch=epoch) writer_train.close() writer_val.close() ## 데이터 시각화 data = dataset_train.__getitem__(0) image = data['image'] label = data['label'] plt.subplot(121) plt.imshow(image.squeeze()) # squeeze를 하지 않으면 데이터의 shape가 (512, 512, 1)이 된다. # squeeze로 차원 하나를 제거하여 (512,512)로 만든다. plt.subplot(122) plt.imshow(label.squeeze())
def check_up(self, path): result = np.zeros((self.size[1]), dtype=np.int) err = 0 y_index = 0 #第几行 count = 0 #第几个 ll = 0 outer_side = 0 # Read img = cv2.imread(path) crop = img[:, self.range[2]:self.range[3], :] if self.type not in [5]: crop = cv2.flip(crop, -1) crop_gray = cv2.cvtColor(crop, cv2.COLOR_BGR2GRAY) circles = self._findCircles(crop_gray) # Logger draw = self._drawCircles(crop, circles) saver(draw, "D_%s" % path[-8:-4]) if path[-8:-4] in ["110", "184"]: err = 1 one = circles[0] one = sorted(one, key=cmp_to_key(_sortCircle)) y_min = one[0][1] x_max = one[0][0] # 过滤Type1的两个噪声 if self.type == 1: one = one[2:] for c in one: count += 1 c = np.array(c).astype('int') circle = crop[c[1] - self.radius:c[1] + self.radius, c[0] - self.radius:c[0] + self.radius, :] # 原则上噪声与可识别区域间隔三行 if c[1] - y_min > self.radius * 6: break color = checkColor(circle) if _abs(c[1] - y_min) > self.radius * 2 - 20: y_index += 1 x_max = c[0] #换行了,代表最右边的坐标x—max更新 y_min = c[1] input_index = y_index #如果没有问题,用y_index作为结果添加的位置 #如果这个圆圈是在左边的圆圈,并且右边没有圆圈(就是说这个圆圈的x等于代表第一个圆圈的x_max) if outer_side != 0 and c[0] == x_max and abs( c[0] - outer_side) < self.radius: input_index = ll # 这个圆圈和它右边的链接不上,应该是换行来的圆圈 if x_max - c[0] > self.radius * 3: input_index = ll else: x_max = c[0] add = 0 if color == 1: add = 1 else: add = -1 #如果两个圆圈颜色不同,就是换行来的 if abs(result[y_index]) == 5 and abs(result[y_index] + add) < abs( result[y_index]) + abs(add): input_index = ll if y_index - input_index >= 2 and abs( result[input_index + 1]) >= 5 and abs( result[input_index]) >= 7: print("GG") if abs(add + result[input_index + 1]) == abs(add) + abs( result[input_index + 1]): input_index += 1 #进行叠加计算 result[input_index] += add if abs(result[y_index]) >= self.size[0]: ll = y_index outer_side = c[0] #识别报错的东西 for i in range(0, y_index): if abs(result[i]) >= 6: for j in range(i + 1, y_index): #print("I: ", i, "J: ",j, result[i], result[j]) if abs(result[i] + result[j]) >= abs(result[i]) + abs( result[j]) and abs(result[j]) >= 6: if abs(result[i]) - 5 >= j - i: err = 1 break return result, one, err