def pre_bboxes(self, path1, path2=None): files_path = glob.glob('{}/*jpg'.format(path1)) i = 0 total_best_bboxes = [] for image_path in files_path: img = cv2.imread(image_path) save_img_name = image_path[-5:-14:-1][::-1] bboxes_pr = self.predict(img) origial_bboxes = [] for pr_bboxes in bboxes_pr: # print("pr_bboxes",pr_bboxes) bboxes1 = pr_bboxes.tolist() origial_bboxes.append(bboxes1) origial_bboxes = torch.tensor(origial_bboxes) # print("origial_bboxes",origial_bboxes,type(origial_bboxes)) best_bboxes = utils.py_nms(origial_bboxes, 0.1) best_bboxes = best_bboxes.tolist() save_img = utils.draw_bbox(img, best_bboxes, show_label=True) if path2 != None: save_image_path = os.path.join(path2, save_img_name + '.jpg') cv2.imwrite(save_image_path, save_img) print("保存成功") best_bboxes.sort() # print("best_bboxes",len(best_bboxes),best_bboxes) total_best_bboxes.append(best_bboxes) return total_best_bboxes
def two(self, path1, path2=None): files_path = glob.glob('{}/*bmp'.format(path1)) for image_path in files_path: img = cv2.imread(image_path) save_img_name = image_path[-5:-14:-1][::-1] H, W, C = img.shape w1 = 832 m = 60 cw, epoch = math.modf(W / (w1 - m)) per_image = [] for i in range(int(epoch + 1)): if i != int(W / (w1 - m)) and i == 0: image = img[552:w1 + 552, w1 * i:w1 * (i + 1), :] per_image.append(image) elif i != int(W / (w1 - m)) and i >= 1: image = img[552:w1 + 552, w1 * i - m * i:w1 * (i + 1) - m * i, :] per_image.append(image) else: image = img[552:w1 + 552, (w1 - m) * int(epoch):, :] image1 = np.zeros((w1, w1, 3)) image1[:, :W - (w1 - m) * int(epoch), :] = image per_image.append(image1.astype(np.float32)) total_bboxes_pr = [] for i, np_image in enumerate(per_image): bboxes_pr = self.predict(np_image) x_index = int(i) if x_index == 0: for boxes in bboxes_pr: x1, y1, x2, y2, cls, _ = boxes origial_x1 = x1 origial_y1 = y1 + 552 origial_x2 = x2 origial_y2 = y2 + 552 total_bboxes_pr.append( np.array([ origial_x1, origial_y1, origial_x2, origial_y2, cls, _ ])) else: for boxes in bboxes_pr: x1, y1, x2, y2, cls, _ = boxes origial_x1 = x1 + (832 - 60) * x_index origial_y1 = y1 + 552 origial_x2 = x2 + (832 - 60) * x_index origial_y2 = y2 + 552 total_bboxes_pr.append( np.array([ origial_x1, origial_y1, origial_x2, origial_y2, cls, _ ])) origial_bboxes = [] for pr_bboxes in total_bboxes_pr: # print("pr_bboxes",pr_bboxes) bboxes1 = pr_bboxes.tolist() origial_bboxes.append(bboxes1) origial_bboxes = torch.tensor(origial_bboxes) # print("origial_bboxes",origial_bboxes,type(origial_bboxes)) best_bboxes = utils.py_nms(origial_bboxes, 0.1) print("best_bboxes", len(best_bboxes)) best_bboxes = best_bboxes.numpy().tolist() origial_path = r'./docs/test_data_0610/' origial_path_path = os.path.join(origial_path, save_img_name + ".bmp") origial_image = cv2.imread(origial_path_path) big_origial_image = utils.draw_bbox(origial_image, best_bboxes, show_label=self.show_label) if path2 != None: save_image_path = os.path.join(path2, save_img_name + ".pre" + '.bmp') cv2.imwrite(save_image_path, big_origial_image) print("保存成功") exit()
def two(self, path1, path2=None): lines = os.listdir(path1) lines.sort(key=lambda x: int(x[:-4])) total_per_name = [] total_index = [] total_cls = {} for num, line in enumerate(lines): annotation = line.split() image_name = str(annotation).split('_') image_names = image_name[0][2:] index = image_name[1].split('.')[0] total_index.append(int(index)) total_per_name.append(image_names) per_name = list(set(total_per_name)) per_name.sort(key=total_per_name.index) start_ind = 0 for i, per_image_name in enumerate(per_name): end_ind = total_per_name.count(per_name[i]) + start_ind total_cls[per_image_name] = total_index[start_ind:end_ind] start_ind += end_ind for i, (key, value) in enumerate(total_cls.items()): total_bboxes_pr = [] for k in value: image_path = os.path.join(path1, key + "_" + "{}".format(k) + ".jpg") image = cv2.imread(image_path) bboxes_pr = self.predict(image) x_index = int(k) if x_index == 0: for boxes in bboxes_pr: x1, y1, x2, y2, cls, _ = boxes origial_x1 = x1 origial_y1 = y1 + 552 origial_x2 = x2 origial_y2 = y2 + 552 total_bboxes_pr.append( np.array([ origial_x1, origial_y1, origial_x2, origial_y2, cls, _ ])) else: for boxes in bboxes_pr: x1, y1, x2, y2, cls, _ = boxes origial_x1 = x1 + (832 - 60) * x_index origial_y1 = y1 + 552 origial_x2 = x2 + (832 - 60) * x_index origial_y2 = y2 + 552 total_bboxes_pr.append( np.array([ origial_x1, origial_y1, origial_x2, origial_y2, cls, _ ])) origial_bboxes = [] for pr_bboxes in total_bboxes_pr: bboxes1 = pr_bboxes.tolist() origial_bboxes.append(bboxes1) origial_bboxes = torch.tensor(origial_bboxes) best_bboxes = utils.py_nms(origial_bboxes, 0.1) print("best_bboxes", len(best_bboxes)) best_bboxes = best_bboxes.numpy().tolist() origial_path = r'./docs/trian_data_0610/' origial_path_path = os.path.join(origial_path, key + ".bmp") origial_image = cv2.imread(origial_path_path) big_origial_image = utils.draw_bbox(origial_image, best_bboxes, show_label=self.show_label) if path2 != None: save_image_path = os.path.join(path2, key + '.bmp') cv2.imwrite(save_image_path, big_origial_image) print("保存成功")
def pre_bboxes(self, path1, path2=None, output_size=64): files_path = glob.glob('{}/*bmp'.format(path1)) N = 0 lens = [201] for image_path in files_path: img = cv2.imread(image_path) save_img_name = image_path[-5:-14:-1][::-1] H, W, C = img.shape w1 = 832 m = 60 cw, epoch = math.modf(W / (w1 - m)) per_image = [] for i in range(int(epoch + 1)): if i != int(W / (w1 - m)) and i == 0: image = img[552:w1 + 552, w1 * i:w1 * (i + 1), :] per_image.append(image) elif i != int(W / (w1 - m)) and i >= 1: image = img[552:w1 + 552, w1 * i - m * i:w1 * (i + 1) - m * i, :] per_image.append(image) else: image = img[552:w1 + 552, (w1 - m) * int(epoch):, :] image1 = np.zeros((w1, w1, 3)) image1[:, :W - (w1 - m) * int(epoch), :] = image per_image.append(image1.astype(np.float32)) total_bboxes_pr = [] for i, np_image in enumerate(per_image): bboxes_pr = self.predict(np_image) x_index = int(i) if x_index == 0: for boxes in bboxes_pr: x1, y1, x2, y2, cls, _ = boxes origial_x1 = x1 origial_y1 = y1 + 552 origial_x2 = x2 origial_y2 = y2 + 552 total_bboxes_pr.append( np.array([ origial_x1, origial_y1, origial_x2, origial_y2, cls, _ ])) else: for boxes in bboxes_pr: x1, y1, x2, y2, cls, _ = boxes origial_x1 = x1 + (832 - 60) * x_index origial_y1 = y1 + 552 origial_x2 = x2 + (832 - 60) * x_index origial_y2 = y2 + 552 total_bboxes_pr.append( np.array([ origial_x1, origial_y1, origial_x2, origial_y2, cls, _ ])) origial_bboxes = [] for pr_bboxes in total_bboxes_pr: # print("pr_bboxes",pr_bboxes) bboxes1 = pr_bboxes.tolist() origial_bboxes.append(bboxes1) origial_bboxes = torch.tensor(origial_bboxes) # print("origial_bboxes",origial_bboxes,type(origial_bboxes)) best_bboxes = utils.py_nms(origial_bboxes, 0.2) lens.append(len(best_bboxes) + lens[0]) print("best_bboxes", len(best_bboxes)) best_bboxes = best_bboxes.numpy().tolist() best_bboxes.sort() origial_path = r'./docs/trian_data_0610/' origial_path_path = os.path.join(origial_path, save_img_name + ".bmp") origial_image = cv2.imread(origial_path_path) predicted_dir_path = os.path.join(path2, save_img_name) # print('predicted_dir_path', predicted_dir_path) # if os.path.exists(predicted_dir_path): shutil.rmtree(predicted_dir_path) os.mkdir(predicted_dir_path) for kk, pre_bboxes in enumerate(best_bboxes): Offset = [] cls_predicted_dir_path = os.path.join(predicted_dir_path, str(kk + lens[N])) os.mkdir(cls_predicted_dir_path) for ii in range(6): x1, y1, x2, y2, conf, cls = pre_bboxes x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) dw = x2 - x1 dh = y2 - y1 offset = random.randint(-5, 5) Offset.append(offset) print('Offset', Offset) # if offset in Offset: # continue cx = int(x1 + dw / 2 + 1) + offset cy = int(y1 + dh / 2 + 1) + offset print("cx", cx, cy) x11 = int(cx - output_size / 2) x22 = x11 + output_size y11 = int(cy - output_size / 2) y22 = y11 + output_size crop_image = origial_image[y11:y22, x11:x22] save_image_path = os.path.join( cls_predicted_dir_path, str('{}'.format(ii)) + '.jpg') # save_image_path = os.path.join(path2,save_img_name+"."+str(x11)+"."+str(y11)+"."+str(x22)+"."+str(y22)+ "." +str(round(conf,2))+"."+str(cls)+'.jpg') cv2.imwrite(save_image_path, crop_image) print("保存成功") # print('一个循环结束') N += 1 if N == 2: break