def remove_head_tail_frames(root, recycle_bin=None, num=0): ''' remove num hean&tail frames from videos params: root: the dir of files that need to be processed recycle_bin: the removed frames will be put here defalut: None, that is putting the removed frames in root/_recycle_bin num: the number of frames to be removed ''' if recycle_bin is None: recycle_bin = os.path.join(root, '_recycle_bin') handle_dir(recycle_bin) video_list = listdir(root) for v in video_list: img_list = sorted(glob_match(os.path.join(root, v, "*"))) handle_dir(os.path.join(recycle_bin, v)) for i in range(num): src = img_list[i] dest = os.path.join(recycle_bin, v, os.path.basename(src)) move_file(src, dest) src = img_list[-(i + 1)] dest = os.path.join(recycle_bin, v, os.path.basename(src)) move_file(src, dest)
def extra_files_by_postfix(ori_root, dest_root, match_postfix='', new_postfix=None, match_ext='*'): ''' extra files from ori_root to dest_root by match_postfix and match_ext params: ori_root: the dir of files that need to be processed dest_root: the dir for saving matched files match_postfix: the postfix to be matched new_postfix: the postfix for matched files default: None, that is keeping the ori postfix match_ext: the ext to be matched ''' if new_postfix is None: new_postfix = match_postfix handle_dir(dest_root) flag_img_list = glob_match( os.path.join(ori_root, "*{}.{}".format(match_postfix, match_ext))) for im in flag_img_list: fname, ext = get_fname_ext(im) dest_basename = '{}{}.{}'.format(fname[-len(match_postfix):], new_postfix, ext) src = im dst = os.path.join(dest_root, dest_basename) copy_file(src, dst)
def batch_combine_img(ori_root, dest_root, padding=10): ''' function: combining many patches to image it can be used to combine patches to image, when you finish inferring large image with cropped patches params: ori_root: the dir of images that need to be processed dest_root: the dir to save processed images padding: the padding size of each patch notice: filenames should not contain the character "-" except for the crop flag the crop flag "x-x-x-x" should be at the end of filename when combining ''' handle_dir(dest_root) images_fname = [ fn[:-(len(fn.split('_')[-1]) + 1)] for fn in listdir(ori_root) ] images_fname = list(set(images_fname)) for imf in images_fname: croped_imgs_path = sorted( glob_match(os.path.join(ori_root, "{}*".format(imf)))) croped_imgs = {} for cip in croped_imgs_path: img = cv2.imread(cip) k = cip.split('.')[0].split('_')[-1] croped_imgs[k] = img img_combined = combine_img(croped_imgs, padding=padding) cv2.imwrite(os.path.join(dest_root, "{}.png".format(imf)), img_combined) print("{}.png".format(imf), "combine done !")
def add_files_postfix(root, postfix=''): ''' add postfix to filename params: root: the dir of files that need to be processed postfix: the postfix to be added ''' img_list = glob_match(os.path.join(root, "*")) for im in img_list: fname, ext = get_fname_ext(im) dest_basename = "{}{}.{}".format(fname, postfix, ext) src = im dst = os.path.join(root, dest_basename) rename_file(src, dst)
def resort_files_index(root, template='{:0>4}', start_idx=0): ''' resort files' filename using template that index start from start_idx params: root: the dir of files that need to be processed template: the template for processed filename start_idx: the start index ''' template = template + '.{}' img_list = sorted(glob_match(os.path.join(root, "*"))) for i, im in enumerate(img_list): fname, ext = get_fname_ext(im) dest_basename = template.format(i + start_idx, ext) src = im dst = os.path.join(root, dest_basename) rename_file(src, dst)
def remove_files_prefix(root, prefix=''): ''' remove prefix from filename params: root: the dir of files that need to be processed prefix: the prefix to be removed ''' img_list = glob_match(os.path.join(root, "*")) for im in img_list: basename = os.path.basename(im) now_prefix = basename[:len(prefix)] if now_prefix == prefix: dest_basename = basename[len(prefix):] src = im dst = os.path.join(root, dest_basename) rename_file(src, dst)
def remove_files_postfix(root, postfix=''): ''' remove postfix from filename params: root: the dir of files that need to be processed postfix: the postfix to be removed ''' img_list = glob_match(os.path.join(root, "*")) for im in img_list: fname, ext = get_fname_ext(im) now_postfix = fname[-len(postfix):] if now_postfix == postfix: dest_basename = "{}.{}".format(fname[:-len(postfix)], ext) src = im dst = os.path.join(root, dest_basename) rename_file(src, dst)
def save_flow_pt2mat(ori_root, dest_root): ''' 把 ori_root 中视频的 flow 从 pt 文件转换为 mat 文件后保存到 dest_root 保存在 pt 文件中的 flow 的 shape=[1, 2, h, w] ''' handle_dir(dest_root) video_list = listdir(ori_root) for v in video_list: handle_dir(os.path.join(dest_root, v)) file_list = glob_match(os.path.join(ori_root, v, "*.pt")) for pt_path in file_list: fname, ext = get_fname_ext(pt_path) mat_path = os.path.join(dest_root, v, "{}.mat".format(fname)) try: flow = torch.load(pt_path)[0].permute(1, 2, 0).cpu().numpy() flow_dict = {'flow': flow} scio.savemat(mat_path, flow_dict) print('save {} to {}'.format(pt_path, mat_path)) except: print('skip file {}'.format(pt_path))