operation = [] fold_rounds = int(deform_rounds * 0.7) for _ in range(fold_rounds): operation.append(0) for _ in range(deform_rounds - fold_rounds): operation.append(1) random.shuffle(operation) file_name = img[:img.index('.')] + '-' + uuid.uuid1().hex[0:8] # generate label_x, label_y = gen_deform_label(img_path, resize_shape_before_deform, operation) img_b, img_g, img_r, label_x, label_y = from_label_deform( label_x, label_y, img_path, resize_shape_before_deform) img_b, img_g, img_r = texture(label_x, img_b, img_g, img_r, texture_path) # resize image and label label_x, label_y, img_b, img_g, img_r = resize_img_label( label_x, label_y, img_b, img_g, img_r, resize_shape_after_deform) img = np.dstack([img_b, img_g, img_r]) cv2.imwrite(os.path.join(target_img_folder, file_name) + '.png', img) # change label precision to lower the volumn of label npz label_x, label_y = label_x.astype(np.float16), label_y.astype( np.float16) np.savez_compressed(os.path.join(target_label_folder, file_name), x=label_x,
if __name__ == '__main__': img_path = '/home/wulei/DocUNet/data_gen/scan/25.png' texture_path = '/home/wulei/DocUNet/data_gen/scan/dtd_3.jpg' operation = [1, 0, 0, 0, 1] data_path = '/home/wulei/DocUNet/data_gen' filename = os.path.basename(img_path) label_x, label_y = gen_deform_label(img_path, operation) # label_visualization(label_x, "x") # label_visualization(label_y, 'y') # cv2.waitKey(0) # cv2.destroyAllWindows() print(f'start deformation...') start = time.time() img_b, img_g, img_r, label_x, label_y = from_label_deform(label_x, label_y, img_path) img = texture(label_x, img_b, img_g, img_r, texture_path) cv2.imwrite(os.path.join(data_path, 'img', filename), img) label_path = os.path.join(data_path, 'labels') if not os.path.exists(label_path): print(f'path {label_path} not exists, mkdir') os.mkdir(label_path) np.savez_compressed(os.path.join(label_path, filename[: filename.index('.')]), x = label_x, y = label_y) print(f'deformation finished. time: {time.time() - start}')