def quantization(high_bitdepth=16, low_bitdepth=4): # parent directory of all GT video clips gt_data_dir = "/home/medialab/workspace/hdd/zhen/EDVR/datasets/SDR4k/train/SDR_10BIT_patch" gt_video_list = glob.glob(os.path.join(gt_data_dir, "*")) pbar = ProgressBar(len(gt_video_list)) #print(gt_video_list) parent_dir = os.path.dirname(gt_data_dir) zp_data_dir = os.path.join(parent_dir, "SDR_4BIT_patch") if not os.path.exists(zp_data_dir): os.mkdir(zp_data_dir) for video_path in gt_video_list: pbar.update() filepath, filename = os.path.split(video_path) zp_filepath = os.path.join(zp_data_dir, filename) if not os.path.exists(zp_filepath): os.mkdir(zp_filepath) gt_imglist = glob.glob(os.path.join(video_path, "*.png")) print("Start quantizing video clip %s ..." % filename) for gt_img in gt_imglist: #print(gt_img) imgpath, imgname = os.path.split(gt_img) image = cv2.imread(gt_img, 3) image = image // np.power( 2, high_bitdepth - low_bitdepth) * np.power( 2, high_bitdepth - low_bitdepth) cv2.imwrite(os.path.join(zp_filepath, imgname), image)
def main(): """A multi-thread tool for converting RGB images to gary/Y images.""" input_folder = '/media/iceclear/yangren/val_blur_comp' save_folder = '/media/iceclear/yangren/val_blur_comp_gray' mode = 'y' # 'gray' | 'y': Y channel in YCbCr space compression_level = 0 # 3 is the default value in cv2 # CV_IMWRITE_PNG_COMPRESSION from 0 to 9. A higher value means a smaller size and longer # compression time. If read raw images during training, use 0 for faster IO speed. n_thread = 20 # thread number if not os.path.exists(save_folder): os.makedirs(save_folder) print('mkdir [{:s}] ...'.format(save_folder)) else: print('Folder [{:s}] already exists. Exit...'.format(save_folder)) # sys.exit(1) # print('Parent process {:d}.'.format(os.getpid())) img_list = [] for root, _, file_list in sorted(os.walk(input_folder)): path = [os.path.join(root, x) for x in file_list] # assume only images in the input_folder img_list.extend(path) def update(arg): pbar.update(arg) pbar = ProgressBar(len(img_list)) pool = Pool(n_thread) for path in img_list: pool.apply_async(worker, args=(path, save_folder, mode, compression_level), callback=update) pool.close() pool.join() print('All subprocesses done.')
def extract_single(opt, split_img=False): input_folder = opt['input_folder'] save_folder = opt['save_folder'] if not osp.exists(save_folder): os.makedirs(save_folder) print('mkdir [{:s}] ...'.format(save_folder)) img_list = data_util._get_paths_from_images(input_folder) def update(arg): pbar.update(arg) pbar = ProgressBar(len(img_list)) pool = Pool(opt['n_thread']) if opt['n_thread'] >= 1 else None for path in img_list: # If this fails, change it and the imwrite below to the write extension. assert ".jpg" in path if pool: if split_img: pool.apply_async(worker, args=(path, opt, True, False), callback=update) pool.apply_async(worker, args=(path, opt, True, True), callback=update) else: pool.apply_async(worker, args=(path, opt), callback=update) else: assert not split_img worker(path, opt) pool.close() pool.join() print('All subprocesses done.')
def main(): """A multi-thread tool to crop sub imags.""" if getpass.getuser()=='ekyzivat': # on ethan local input_folder = 'F:\ComputerVision\Planet' save_folder = 'F:\ComputerVision\Planet_sub' elif getpass.getuser()=='ethan_kyzivat' or getpass.getuser()=='ekaterina_lezine': # on GCP input_folder = '/data_dir/Scenes' save_folder = '/data_dir/other/hists/histsv6_test' else: # other raise ValueError('input_folder not specified!') pass crop_sz = 480 # num px in x and y step = 240 thres_sz = 48 compression_level = 3 # 3 is the default value in cv2 # CV_IMWRITE_PNG_COMPRESSION from 0 to 9. A higher value means a smaller size and longer # compression time. If read raw images during training, use 0 for faster IO speed. if not os.path.exists(save_folder): os.makedirs(save_folder) print('mkdir [{:s}] ...'.format(save_folder)) else: # print('Folder [{:s}] already exists. Exit...'.format(save_folder)) # sys.exit(1) pass # uncomment above two lines for ease of working, if necessary img_list = [] for root, dirsfoo, file_list in sorted(os.walk(input_folder)): # +'/*SR.tif' # _ instead of dirsfoo for x in file_list: # assume only images in the input_folder # [38:] if x.endswith("SR.tif"): path = os.path.join(root, x) img_list.append(path) break # ignores files in nested dirs # img_list = ['/data_dir/Scenes/20190619_191648_25_106f_3B_AnalyticMS_SR.tif'] # for testing def update(arg): pbar.update(arg) # img_list=img_list[:30] # to start in middle pbar = ProgressBar(len(img_list)) pool = Pool(n_thread) hist_results=np.zeros((hist_length,4,len(img_list)), dtype='single') # init, TODO: make dynamic j=0 for path in img_list: print(f'Image {j}:\t{path}') hist_results[:,:,j] = pool.apply_async(worker, # args=(path, save_folder, crop_sz, step, thres_sz, compression_level)).get() # ,callback=update if j % save_freq==0: np.save('histograms_temp.npy', hist_results) print('Temp histograms saved to hists_temp.npy') j+=1 pool.close() pool.join() print('All subprocesses done.') np.save('histograms.npy', hist_results) print('Histograms saved to hists.npy')
def main(): """A multi-thread tool to crop sub imags.""" # CV_IMWRITE_PNG_COMPRESSION from 0 to 9. A higher value means a smaller size and longer # compression time. If read raw images during training, use 0 for faster IO speed. # create output folders if they don't already exist for dir in [save_folder, save_mask_folder, save_hist_plot_folder]: if dir != None: if not os.path.exists(dir): os.makedirs(dir) print('mkdir [{:s}] ...'.format(dir)) else: # print('Folder [{:s}] already exists. Exit...'.format(save_folder)) # sys.exit(1) pass # uncomment above two lines for ease of working, if necessary img_list = [] for root, dirsfoo, file_list in sorted( os.walk(input_folder)): # +'/*SR.tif' for x in file_list: # assume only images in the input_folder if x.endswith("SR.tif"): path = os.path.join(root, x) img_list.append(path) break # img_list = ['/data_dir/Scenes/20190619_191648_25_106f_3B_AnalyticMS_SR.tif'] # for testing def update(arg): pbar.update(arg) # img_list=img_list[238:270] # for testing pbar = ProgressBar(len(img_list)) pool = Pool(n_thread) # (n_thread) for path in img_list: if input_mask_folder == None: path_mask = None else: path_mask = name_lookup(path) # lookup mask path pool.apply_async(worker, args=(path, save_folder, crop_sz, step, thres_sz, compression_level, path_mask, save_mask_folder), callback=update) pool.close() pool.join() print('All subprocesses done.')
def main(): """A multi-thread tool to crop sub imags.""" input_folder = "/mnt/SSD/xtwang/BasicSR_datasets/DIV2K800/DIV2K800" save_folder = "/mnt/SSD/xtwang/BasicSR_datasets/DIV2K800/DIV2K800_sub" n_thread = 20 crop_sz = 480 step = 240 thres_sz = 48 compression_level = 3 # 3 is the default value in cv2 # CV_IMWRITE_PNG_COMPRESSION from 0 to 9. A higher value means a smaller size and longer # compression time. If read raw images during training, use 0 for faster IO speed. if not os.path.exists(save_folder): os.makedirs(save_folder) print("mkdir [{:s}] ...".format(save_folder)) else: print("Folder [{:s}] already exists. Exit...".format(save_folder)) sys.exit(1) img_list = [] for root, _, file_list in sorted(os.walk(input_folder)): path = [ os.path.join(root, x) for x in file_list ] # assume only images in the input_folder img_list.extend(path) def update(arg): pbar.update(arg) pbar = ProgressBar(len(img_list)) pool = Pool(n_thread) for path in img_list: pool.apply_async( worker, args=(path, save_folder, crop_sz, step, thres_sz, compression_level), callback=update, ) pool.close() pool.join() print("All subprocesses done.")
def extract_signle(opt): input_folder = opt['input_folder'] save_folder = opt['save_folder'] if not osp.exists(save_folder): os.makedirs(save_folder) print('mkdir [{:s}] ...'.format(save_folder)) else: print('Folder [{:s}] already exists. Exit...'.format(save_folder)) sys.exit(1) img_list = data_util._get_paths_from_images(input_folder) def update(arg): pbar.update(arg) pbar = ProgressBar(len(img_list)) pool = Pool(opt['n_thread']) for path in img_list: pool.apply_async(worker, args=(path, opt), callback=update) pool.close() pool.join() print('All subprocesses done.')
def main(): """A multi-thread tool to crop sub imags.""" input_folder = '/content/own_dataset-20190716T130232Z-001/validation' save_folder = '/content/own_dataset-20190716T130232Z-001/validation_tiles' n_thread = 20 crop_sz = 128 step = 64 thres_sz = 13 compression_level = 0 # 3 is the default value in cv2 # CV_IMWRITE_PNG_COMPRESSION from 0 to 9. A higher value means a smaller size and longer # compression time. If read raw images during training, use 0 for faster IO speed. if not os.path.exists(save_folder): os.makedirs(save_folder) print('mkdir [{:s}] ...'.format(save_folder)) else: print('Folder [{:s}] already exists. Exit...'.format(save_folder)) sys.exit(1) img_list = [] for root, _, file_list in sorted(os.walk(input_folder)): path = [os.path.join(root, x) for x in file_list] # assume only images in the input_folder img_list.extend(path) def update(arg): pbar.update(arg) pbar = ProgressBar(len(img_list)) pool = Pool(n_thread) for path in img_list: pool.apply_async(worker, args=(path, save_folder, crop_sz, step, thres_sz, compression_level), callback=update) pool.close() pool.join() print('All subprocesses done.')
def main(split): """A multi-thread tool to crop sub imags.""" input_folder = '../../datasets/{}_{}_uncropped/'.format( dataset_name, split) save_folder = '../../datasets/{}_{}/'.format(dataset_name, split) n_thread = 20 # CV_IMWRITE_PNG_COMPRESSION from 0 to 9. A higher value means a smaller size and longer # compression time. If read raw images during training, use 0 for faster IO speed. if not os.path.exists(save_folder): os.makedirs(save_folder) print('mkdir [{:s}] ...'.format(save_folder)) else: print('Folder [{:s}] already exists. Exit...'.format(save_folder)) sys.exit(1) img_list = [] for root, _, file_list in sorted(os.walk(input_folder)): path = [os.path.join(root, x) for x in file_list] # assume only images in the input_folder img_list.extend(path) print(img_list) def update(arg): pbar.update(arg) pbar = ProgressBar(len(img_list)) pool = Pool(n_thread) for path in img_list: pool.apply_async(worker, args=(path, save_folder, crop_sz, step, thres_sz, compression_level), callback=update) pool.close() pool.join() print('All subprocesses done.')
def create_LMDB(img_folder, lmdb_save_path, LRGT): # configurations img_folder = img_folder + '/*' lmdb_save_path = lmdb_save_path + '/DIV2K800_sub.lmdb' meta_info = {'name': 'DIV2K800_sub_' + LRGT} mode = 2 # 1 for reading all the images to memory and then writing to lmdb (more memory); # 2 for reading several images and then writing to lmdb, loop over (less memory) batch = 1 # Used in mode 2. After batch images, lmdb commits. ########################################### if not lmdb_save_path.endswith('.lmdb'): raise ValueError("lmdb_save_path must end with \'lmdb\'.") #### whether the lmdb file exist if osp.exists(lmdb_save_path): print('Folder [{:s}] already exists. Exit...'.format(lmdb_save_path)) sys.exit(1) img_list = sorted(glob.glob(img_folder)) if mode == 1: print('Read images...') dataset = [cv2.imread(v, cv2.IMREAD_UNCHANGED) for v in img_list] data_size = sum([img.nbytes for img in dataset]) elif mode == 2: print('Calculating the total size of images...') data_size = sum(os.stat(v).st_size for v in img_list) else: raise ValueError('mode should be 1 or 2') key_l = [] resolution_l = [] pbar = ProgressBar(len(img_list)) env = lmdb.open(lmdb_save_path, map_size=data_size * 30) txn = env.begin(write=True) # txn is a Transaction object for i, v in enumerate(img_list): pbar.update('Write {}'.format(v)) base_name = osp.splitext(osp.basename(v))[0] key = base_name.encode('ascii') data = dataset[i] if mode == 1 else cv2.imread(v, cv2.IMREAD_UNCHANGED) if data.ndim == 2: H, W = data.shape C = 1 else: H, W, C = data.shape txn.put(key, data) key_l.append(base_name) resolution_l.append('{:d}_{:d}_{:d}'.format(C, H, W)) # commit in mode 2 if mode == 2 and i % batch == 1: txn.commit() txn = env.begin(write=True) txn.commit() env.close() print('Finish writing lmdb.') #### create meta information # check whether all the images are the same size same_resolution = (len(set(resolution_l)) <= 1) if same_resolution: meta_info['resolution'] = [resolution_l[0]] meta_info['keys'] = key_l print('All images have the same resolution. Simplify the meta info...') else: meta_info['resolution'] = resolution_l meta_info['keys'] = key_l print( 'Not all images have the same resolution. Save meta info for each image...' ) #### pickle dump pickle.dump(meta_info, open(osp.join(lmdb_save_path, 'meta_info.pkl'), "wb")) print('Finish creating lmdb meta info.')
def main(split): kept_indices = None for quality in ("LR", "HR"): img_folder = '../../datasets/{}_{}_bicLRx4/{}/x4/*'.format( # glob matching pattern dataset_name, split, quality) lmdb_save_path = '../../datasets/{}_{}_bicLRx4/{}/x4{}.lmdb'.format( dataset_name, split, quality, removed_fraction if removed_fraction else "") meta_info = {'name': 'DIV2K800_sub_GT'} mode = 2 # 1 for reading all the images to memory and then writing to lmdb (more memory); # 2 for reading several images and then writing to lmdb, loop over (less memory) batch = 1000 # Used in mode 2. After batch images, lmdb commits. ########################################### if not lmdb_save_path.endswith('.lmdb'): raise ValueError("lmdb_save_path must end with \'lmdb\'.") #### whether the lmdb file exist if osp.exists(lmdb_save_path): print( 'Folder [{:s}] already exists. Exit...'.format(lmdb_save_path)) sys.exit(1) img_list = sorted(glob.glob(img_folder)) if removed_fraction and split == 'valid': if kept_indices is None: kept_indices = binomial(1, (1 - removed_fraction), len(img_list)) img_list = [ item for k, item in enumerate(img_list) if kept_indices[k] ] if mode == 1: print('Read images...') dataset = [cv2.imread(v, cv2.IMREAD_UNCHANGED) for v in img_list] data_size = sum([img.nbytes for img in dataset]) elif mode == 2: print('Calculating the total size of images...') data_size = sum(os.stat(v).st_size for v in img_list) else: raise ValueError('mode should be 1 or 2') key_l = [] resolution_l = [] pbar = ProgressBar(len(img_list)) env = lmdb.open(lmdb_save_path, map_size=data_size * 10) txn = env.begin(write=True) # txn is a Transaction object for i, v in enumerate(img_list): pbar.update('Write {}'.format(v)) base_name = osp.splitext(osp.basename(v))[0] key = base_name.encode('ascii') data = dataset[i] if mode == 1 else cv2.imread( v, cv2.IMREAD_UNCHANGED) if data.ndim == 2: H, W = data.shape C = 1 else: H, W, C = data.shape txn.put(key, data) key_l.append(base_name) resolution_l.append('{:d}_{:d}_{:d}'.format(C, H, W)) # commit in mode 2 if mode == 2 and i % batch == 1: txn.commit() txn = env.begin(write=True) txn.commit() env.close() print('Finish writing lmdb.') #### create meta information # check whether all the images are the same size same_resolution = (len(set(resolution_l)) <= 1) if same_resolution: meta_info['resolution'] = [resolution_l[0]] meta_info['keys'] = key_l print( 'All images have the same resolution. Simplify the meta info...' ) else: meta_info['resolution'] = resolution_l meta_info['keys'] = key_l print( 'Not all images have the same resolution. Save meta info for each image...' ) #### pickle dump pickle.dump(meta_info, open(osp.join(lmdb_save_path, 'meta_info.pkl'), "wb")) print('Finish creating lmdb meta info.')
# dataset = [np.load(v) for v in img_list] dataset = [cv2.imread(v) for v in img_list] data_size = sum([img.nbytes for img in dataset]) elif mode == 2: print('Calculating the total size of images...') data_size = sum(os.stat(v).st_size for v in img_list) * (memory_len * 2 + 1) else: raise ValueError('mode should be 1 or 2') env = lmdb.open(lmdb_save_path, map_size=data_size * 20) txn = env.begin(write=True) # txn is a Transaction object key_l = [] resolution_l = [] pbar = ProgressBar(frame_total) for frame_i in range(memory_len, frame_total - memory_len, 1): pbar.update('Write frame {}'.format(frame_i)) for block_i in range(num_sub): base_name = 'frame_' + str(frame_i) + '_' + str(block_i) key = base_name.encode('ascii') data = [] if info_mode == 'LQ': for cur_frame_in_batch in range(2 * memory_len + 1): cur_data = dataset[ (frame_i + cur_frame_in_batch - memory_len) * num_sub + block_i] data.append(cur_data) elif info_mode == 'GT':
def main(): train_or_test = 'train' qp = 37 channal_num = 3 if train_or_test == 'train': if channal_num == 3: read_lq_file = '/media/iceclear/iceking/YUV_f_img_crop_' + str( qp) + '_rgb/' read_gt_file = '/media/iceclear/iceking/YUV_GT_img_crop_rgb/' elif channal_num == 1: read_lq_file = '/media/iceclear/iceking/YUV_f_img_crop_' + str( qp) + '_yuv/' read_gt_file = '/media/iceclear/iceking/YUV_GT_img_crop_yuv/' elif train_or_test == 'test': read_lq_file = '/media/iceclear/iceking/YUV_f_img_crop_' + str( qp) + '_test/' read_gt_file = '/media/iceclear/iceking/YUV_GT_img_crop_test/' else: print(s) info_mode = 'GT' # GT or LQ if info_mode == 'LQ': read_list = glob.glob(read_lq_file + '/*') elif info_mode == 'GT': read_list = glob.glob(read_gt_file + '/*') # f = open('../yuvInfo.txt','r') n_thread = 5 crop_sz = 224 step = 448 thres_sz = 22 compression_level = 0 # 3 is the default value in cv2 # CV_IMWRITE_PNG_COMPRESSION from 0 to 9. A higher value means a smaller size and longer # compression time. If read raw images during training, use 0 for faster IO speed. print('*********** current mode is: ' + info_mode + ' **************') """A multi-thread tool to crop sub imags.""" for c in read_list: filename = os.path.basename(c) print('>>>>>>>>>>>>>> ' + filename + ' is starting') input_folder = c # if info_mode == 'LQ': # input_folder = '/media/iceclear/iceking/YUV_nof_img_crop_37_Yonly/'+filename # elif info_mode == 'GT': # input_folder = '/media/iceclear/iceking/YUV_GT_img_crop_37_Yonly/'+filename if train_or_test == 'train': if channal_num == 3: save_folder = '/media/iceclear/iceking/YUV_' + info_mode + '_imgrgb_sub' + str( crop_sz) + '_' + str(qp) + '/' + filename elif channal_num == 1: save_folder = '/media/iceclear/iceking/YUV_' + info_mode + '_imgyuv_sub' + str( crop_sz) + '_' + str(qp) + '/' + filename elif train_or_test == 'test': save_folder = '/media/iceclear/iceking/YUV_' + info_mode + '_imgrgb_sub' + str( crop_sz) + '_' + str(qp) + '_test/' + filename if not os.path.exists(save_folder): os.makedirs(save_folder) print('mkdir [{:s}] ...'.format(save_folder)) else: print('Folder [{:s}] already exists. Exit...'.format(save_folder)) # continue # shutil.rmtree(save_folder,True) # os.makedirs(save_folder) # sys.exit(1) img_list = [] for root, _, file_list in sorted(os.walk(input_folder)): path = [os.path.join(root, x) for x in file_list ] # assume only images in the input_folder img_list.extend(path) def update(arg): pbar.update(arg) pbar = ProgressBar(len(img_list)) pool = Pool(n_thread) for path in img_list: pool.apply_async(worker, args=(path, save_folder, crop_sz, step, thres_sz, compression_level), callback=update) pool.close() pool.join() print(filename + 'is done.') print('All subprocesses done.')
print("Folder [{:s}] already exists. Exit...".format(lmdb_save_path)) sys.exit(1) img_list = sorted(glob.glob(img_folder)) if mode == 1: print("Read images...") dataset = [cv2.imread(v, cv2.IMREAD_UNCHANGED) for v in img_list] data_size = sum([img.nbytes for img in dataset]) elif mode == 2: print("Calculating the total size of images...") data_size = sum(os.stat(v).st_size for v in img_list) else: raise ValueError("mode should be 1 or 2") key_l = [] resolution_l = [] pbar = ProgressBar(len(img_list)) env = lmdb.open(lmdb_save_path, map_size=data_size * 10) txn = env.begin(write=True) # txn is a Transaction object for i, v in enumerate(img_list): pbar.update("Write {}".format(v)) base_name = osp.splitext(osp.basename(v))[0] key = base_name.encode("ascii") data = dataset[i] if mode == 1 else cv2.imread(v, cv2.IMREAD_UNCHANGED) if data.ndim == 2: H, W = data.shape C = 1 else: H, W, C = data.shape txn.put(key, data) key_l.append(base_name) resolution_l.append("{:d}_{:d}_{:d}".format(C, H, W))
import os import sys import glob import os.path as osp sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) from utils.util import ProgressBar dataset_path = '../../datasets/SDR4k/train/SDR_10BIT_sub/' video_path_list = glob.glob(os.path.join(dataset_path, '*')) pbar = ProgressBar(len(video_path_list)) for video_path in video_path_list: print('Processing video', video_path) frame_path_list = glob.glob(os.path.join(video_path, '*')) # print(frame_path_list) patch_name_list = sorted(os.listdir(frame_path_list[0])) # print(patch_name_list) video_path, video_name = os.path.split(video_path) # print(video_path, video_name) for patch_name in patch_name_list: save_name = '{}_{}'.format(video_name, os.path.splitext(patch_name)[0]) save_dir = os.path.join(video_path, save_name) if not os.path.exists(save_dir): os.makedirs(save_dir) print("Making dir {}".format(save_dir)) for frame_path in frame_path_list: frame_name = '{}.png'.format(osp.basename(frame_path)) os.system('mv {} {}'.format(os.path.join(frame_path, patch_name), osp.join(save_dir, frame_name))) # print('mv {} {}'.format(os.path.join(frame_path, patch_name), osp.join(save_dir, frame_name))) pbar.update()
def extract_single(opt): input_folder = opt['input_folder'] save_folder = opt['save_folder'] if not osp.exists(save_folder): os.makedirs(save_folder) # print('mkdir [{:s}] ...'.format(save_folder)) else: print('Folder [{:s}] already exists. Continue...'.format(save_folder)) # sys.exit(1) # img_list = data_util._get_paths_from_images(input_folder) video_list, _ = data_util.get_video_paths(input_folder) # def update(arg): # pbar.update(arg) pbar = ProgressBar(len(video_list)) # pool = Pool(opt['n_thread']) # for path in img_list: # pool.apply_async(worker, args=(path, opt), callback=update) # pool.close() # pool.join() # print('All subprocesses done.') crop_sz = opt['crop_sz'] step = opt['step'] thres_sz = opt['thres_sz'] for video_path in video_list: pbar.update() video_name = osp.basename(video_path) save_video_folder = osp.join(opt['save_folder'], video_name) if not osp.exists(save_video_folder): os.makedirs(save_video_folder) # print('mkdir [{:s}] ...'.format(save_folder)) else: print('Folder [{:s}] already exists. Continue...'.format( save_video_folder)) continue print('Processing video {:s}...'.format(video_name)) left, right, bottom, top = rm_border(osp.join( video_path, "001.png")) # cut for all images in one video for img_name in sorted(os.listdir((video_path))): save_folder = osp.join(save_video_folder, osp.splitext(img_name)[0]) # print("save_folder", save_folder) if not osp.exists(save_folder): os.makedirs(save_folder) # print('mkdir [{:s}] ...'.format(save_folder)) else: print('Folder [{:s}] already exists. Continue...'.format( save_folder)) continue print('Processing frame {:s} ...'.format(img_name)) img_path = osp.join(video_path, img_name) img = cv2.imread(img_path, cv2.IMREAD_UNCHANGED) img = img[left:right, bottom:top, :] n_channels = len(img.shape) if n_channels == 2: h, w = img.shape elif n_channels == 3: h, w, c = img.shape else: raise ValueError('Wrong image shape - {}'.format(n_channels)) h_space = np.arange(0, h - crop_sz + 1, step) if h - (h_space[-1] + crop_sz) > thres_sz: h_space = np.append(h_space, h - crop_sz) w_space = np.arange(0, w - crop_sz + 1, step) if w - (w_space[-1] + crop_sz) > thres_sz: w_space = np.append(w_space, w - crop_sz) index = 0 for x in h_space: for y in w_space: index += 1 if n_channels == 2: crop_img = img[x:x + crop_sz, y:y + crop_sz] else: crop_img = img[x:x + crop_sz, y:y + crop_sz, :] crop_img = np.ascontiguousarray(crop_img) cv2.imwrite( osp.join(save_folder, 'p{:03d}.png'.format(index)), crop_img, [ cv2.IMWRITE_PNG_COMPRESSION, opt['compression_level'] ])