Example #1
0
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)
Example #2
0
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.')
Example #3
0
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.')
Example #4
0
    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':
                cur_data = dataset[frame_i * num_sub + block_i]
                data.append(cur_data)
Example #5
0
    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)
Example #6
0
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()
Example #7
0
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']
                        ])
Example #8
0
    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)