def cut_text_line(geo, scale_ratio_w, scale_ratio_h, im_array, img_path, s): geo /= [scale_ratio_w, scale_ratio_h] p_min = np.amin(geo, axis=0) p_max = np.amax(geo, axis=0) min_xy = p_min.astype(int) max_xy = p_max.astype(int) + 2 sub_im_arr = im_array[min_xy[1]:max_xy[1], min_xy[0]:max_xy[0], :].copy() for m in range(min_xy[1], max_xy[1]): for n in range(min_xy[0], max_xy[0]): if not point_inside_of_quad(n, m, geo, p_min, p_max): sub_im_arr[m - min_xy[1], n - min_xy[0], :] = 255 sub_im = image.array_to_img(sub_im_arr, scale=False) sub_im.save(img_path + '_subim%d.jpg' % s)
def directCreateDataset(gtFile, outputPath, checkValid=True, map_size=8589934592, data_dir=cfg.data_dir): """ Create LMDB dataset for training and evaluation. ARGS: inputPath : input folder path where starts imagePath outputPath : LMDB output path gtFile : list of image path and label checkValid : if true, check the validity of every image """ os.makedirs(outputPath, exist_ok=True) env = lmdb.open(outputPath, map_size=map_size) # 85899345920/8Gb cache = {} cnt = 1 gtFile = os.path.join(data_dir, gtFile) with open(gtFile, 'r') as data: f_list = data.readlines() nSamples = len(f_list) for line, _ in zip(f_list, tqdm(range(nSamples))): print('第{}张图片:{}'.format(cnt, f_list[cnt - 1])) line_cols = str(line).strip().split(',') img_name, width, height = \ line_cols[0].strip(), int(line_cols[1].strip()), \ int(line_cols[2].strip()) gt = np.zeros((height // cfg.pixel_size, width // cfg.pixel_size, 7)) train_label_dir = os.path.join( data_dir, cfg.train_label_dir_name) # 'labels_%s/' % train_task_id xy_list_array = np.load( os.path.join(train_label_dir, img_name[:-4] + '.npy')) # (N, 4, 2) train_image_dir = os.path.join(data_dir, cfg.train_image_dir_name) if not os.path.exists(os.path.join(train_image_dir, img_name)): print('%s does not exist' % os.path.join(train_image_dir, img_name)) continue # ---------------------------------生成标签--------------------------------- with Image.open(os.path.join(train_image_dir, img_name)) as im: draw = ImageDraw.Draw(im) for xy_list in xy_list_array: _, shrink_xy_list, _ = shrink(xy_list, cfg.shrink_ratio) shrink_1, _, long_edge = shrink(xy_list, cfg.shrink_side_ratio) p_min = np.amin(shrink_xy_list, axis=0) p_max = np.amax(shrink_xy_list, axis=0) # floor of the float ji_min = (p_min / cfg.pixel_size - 0.5).astype(int) - 1 # +1 for ceil of the float and +1 for include the end ji_max = (p_max / cfg.pixel_size - 0.5).astype(int) + 3 imin = np.maximum(0, ji_min[1]) imax = np.minimum(height // cfg.pixel_size, ji_max[1]) jmin = np.maximum(0, ji_min[0]) jmax = np.minimum(width // cfg.pixel_size, ji_max[0]) for i in range(imin, imax): for j in range(jmin, jmax): px = (j + 0.5) * cfg.pixel_size py = (i + 0.5) * cfg.pixel_size if point_inside_of_quad(px, py, shrink_xy_list, p_min, p_max): gt[i, j, 0] = 1 line_width, line_color = 1, 'red' ith = point_inside_of_nth_quad( px, py, xy_list, shrink_1, long_edge) vs = [[[3, 0], [1, 2]], [[0, 1], [2, 3]]] if ith in range(2): gt[i, j, 1] = 1 if ith == 0: line_width, line_color = 2, 'yellow' else: line_width, line_color = 2, 'green' gt[i, j, 2:3] = ith gt[i, j, 3:5] = \ xy_list[vs[long_edge][ith][0]] - [px, py] gt[i, j, 5:] = \ xy_list[vs[long_edge][ith][1]] - [px, py] draw.line([(px - 0.5 * cfg.pixel_size, py - 0.5 * cfg.pixel_size), (px + 0.5 * cfg.pixel_size, py - 0.5 * cfg.pixel_size), (px + 0.5 * cfg.pixel_size, py + 0.5 * cfg.pixel_size), (px - 0.5 * cfg.pixel_size, py + 0.5 * cfg.pixel_size), (px - 0.5 * cfg.pixel_size, py - 0.5 * cfg.pixel_size)], width=line_width, fill=line_color) act_image_dir = os.path.join(cfg.data_dir, cfg.show_act_image_dir_name) if cfg.draw_act_quad: im.save(os.path.join(act_image_dir, img_name)) # train_label_dir = os.path.join(data_dir, cfg.train_label_dir_name) # 'labels_%s/' % train_task_id # np.save(os.path.join(train_label_dir, img_name[:-4] + '_gt.npy'), gt) imagePath = os.path.join(cfg.data_dir, cfg.train_image_dir_name, img_name) label = gt # ---------------------------写入LMDB--------------------------- with open(imagePath, 'rb') as f: imageBin = f.read() if checkValid: try: if not checkImageIsValid(imageBin): print('%s is not a valid image' % imagePath) continue except (Exception): print('error occured', i) with open(outputPath + '/error_image_log.txt', 'a') as log: log.write('%s-th image data occured error\n' % str(i)) continue imageKey = 'image-%09d'.encode() % cnt labelKey = 'label-%09d'.encode() % cnt gt_xy_list_Key = 'gt_xy_list-%09d'.encode() % cnt cache[imageKey] = imageBin cache[labelKey] = label cache[gt_xy_list_Key] = xy_list_array if cnt % 1000 == 0: writeCache(env, cache) cache = {} print('Written %d / %d' % (cnt, nSamples)) cnt += 1 nSamples = cnt - 1 cache['num-samples'.encode()] = str(nSamples).encode() cache['width-height'.encode()] = str(width).encode() writeCache(env, cache) print('Created dataset with %d samples' % nSamples)