def scale_base_to(src_folder,
                  dst_folder,
                  dst_size=2048,
                  sharp_folder='sharp',
                  defocused_folder='defocused_blurred',
                  motion_folder='motion_blurred'):

    check_folders_exist(dst_folder, sharp_folder, defocused_folder,
                        motion_folder)

    sharp_images = ospa.listdir(os.path.join(src_folder, sharp_folder))
    defocused_images = ospa.listdir(os.path.join(src_folder, defocused_folder))
    motion_images = ospa.listdir(os.path.join(src_folder, motion_folder))
    print(len(sharp_images), len(defocused_images), len(motion_images))
    for sharp, defocused, motion in tqdm(zip(sharp_images, defocused_images,
                                             motion_images),
                                         total=len(sharp_images)):
        sharp_image = cv2.imread(sharp)
        defocused_image = cv2.imread(defocused)
        motion_image = cv2.imread(motion)
        scale = dst_size / max(sharp_image.shape)
        sharp_image = cv2.resize(sharp_image, (0, 0), fx=scale, fy=scale)
        defocused_image = cv2.resize(defocused_image, (0, 0),
                                     fx=scale,
                                     fy=scale)
        motion_image = cv2.resize(motion_image, (0, 0), fx=scale, fy=scale)
        cv2.imwrite(
            os.path.join(dst_folder, sharp_folder,
                         os.path.split(sharp)[-1]), sharp_image)
        cv2.imwrite(
            os.path.join(dst_folder, defocused_folder,
                         os.path.split(defocused)[-1]), defocused_image)
        cv2.imwrite(
            os.path.join(dst_folder, motion_folder,
                         os.path.split(motion)[-1]), motion_image)
def check_sizes(src_folder,
                sharp_folder='sharp',
                defocused_folder='defocused_blurred',
                motion_folder='motion_blurred'):
    sharp_images = ospa.listdir(os.path.join(src_folder, sharp_folder))
    defocused_images = ospa.listdir(os.path.join(src_folder, defocused_folder))
    motion_images = ospa.listdir(os.path.join(src_folder, motion_folder))
    print(len(sharp_images), len(defocused_images), len(motion_images))
    for sharp, defocused, motion in tqdm(zip(sharp_images, defocused_images,
                                             motion_images),
                                         total=len(sharp_images)):
        sharp_image = cv2.imread(sharp)
        defocused_image = cv2.imread(defocused)
        motion_image = cv2.imread(motion)
        if sharp_image.shape != defocused_image.shape or sharp_image.shape != motion_image.shape:
            print('before', sharp, sharp_image.shape, defocused_image.shape,
                  motion_image.shape)
            min_y = min(sharp_image.shape[0], defocused_image.shape[0],
                        motion_image.shape[0]) - 1
            min_x = min(sharp_image.shape[1], defocused_image.shape[1],
                        motion_image.shape[1]) - 1
            sharp_image = sharp_image[:min_y, :min_x]
            defocused_image = defocused_image[:min_y, :min_x]
            motion_image = motion_image[:min_y, :min_x]
            print('after', sharp, sharp_image.shape, defocused_image.shape,
                  motion_image.shape)
            cv2.imwrite(sharp, sharp_image)
            cv2.imwrite(defocused, defocused_image)
            cv2.imwrite(motion, motion_image)
def fix_id(src_folder,
           dst_folder,
           sharp_folder='sharp',
           defocused_folder='defocused_blurred',
           motion_folder='motion_blurred'):
    id_dict = {}
    check_folders_exist(dst_folder, sharp_folder, defocused_folder,
                        motion_folder)
    sharp_images = ospa.listdir(os.path.join(src_folder, sharp_folder),
                                full_path=False)
    defocused_images = ospa.listdir(os.path.join(src_folder, defocused_folder),
                                    full_path=False)
    motion_images = ospa.listdir(os.path.join(src_folder, motion_folder),
                                 full_path=False)
    for image in sharp_images:
        id_ = int(image.split('_')[0])
        if id_ not in id_dict:
            id_dict[id_] = [-1, -1, -1]
        id_dict[id_][0] = '_'.join(image.split('_')[1:])
    for image in defocused_images:
        id_ = int(image.split('_')[0])
        if id_ not in id_dict:
            id_dict[id_] = [-1, -1, -1]
        id_dict[id_][1] = '_'.join(image.split('_')[1:])
    for image in motion_images:
        id_ = int(image.split('_')[0])
        if id_ not in id_dict:
            id_dict[id_] = [-1, -1, -1]
        id_dict[id_][2] = '_'.join(image.split('_')[1:])

    print(id_dict)
    print(len(id_dict))

    for index, (key, item) in enumerate(id_dict.items()):
        sharp_image_new = os.path.join(dst_folder, sharp_folder,
                                       f'{index}_{item[0]}')
        defocused_image_new = os.path.join(dst_folder, defocused_folder,
                                           f'{index}_{item[1]}')
        motion_image_new = os.path.join(dst_folder, motion_folder,
                                        f'{index}_{item[2]}')
        sharp_image_old = os.path.join(src_folder, sharp_folder,
                                       f'{key}_{item[0]}')
        defocused_image_old = os.path.join(src_folder, defocused_folder,
                                           f'{key}_{item[1]}')
        motion_image_old = os.path.join(src_folder, motion_folder,
                                        f'{key}_{item[2]}')

        shutil.move(sharp_image_old, sharp_image_new)
        shutil.move(defocused_image_old, defocused_image_new)
        shutil.move(motion_image_old, motion_image_new)
def parse_folder(src_folder,
                 sharp_folder='sharp',
                 defocused_folder='defocused_blurred',
                 motion_folder='motion_blurred'):
    sharp_images = ospa.listdir(os.path.join(src_folder, sharp_folder))
    defocused_images = ospa.listdir(os.path.join(src_folder, defocused_folder))
    motion_images = ospa.listdir(os.path.join(src_folder, motion_folder))
    check_id(sharp_images, defocused_images, motion_images)
    check_type(sharp_images)
    check_type(defocused_images)
    check_type(motion_images)
    check_devices(sharp_images, defocused_images, motion_images)
    replace_device_names(sharp_images)
    replace_device_names(defocused_images)
    replace_device_names(motion_images)
def move_bad_ids(src_folder,
                 dst_folder,
                 bad_ids,
                 sharp_folder='sharp',
                 defocused_folder='defocused_blurred',
                 motion_folder='motion_blurred'):
    sharp_images = ospa.listdir(os.path.join(src_folder, sharp_folder),
                                full_path=False)
    defocused_images = ospa.listdir(os.path.join(src_folder, defocused_folder),
                                    full_path=False)
    motion_images = ospa.listdir(os.path.join(src_folder, motion_folder),
                                 full_path=False)
    check_folders_exist(dst_folder, sharp_folder, defocused_folder,
                        motion_folder)

    for images, folder in zip([sharp_images, defocused_images, motion_images],
                              [sharp_folder, defocused_folder, motion_folder]):
        for file_ in images:
            id_ = int(file_.split('_')[0])
            if id_ in bad_ids:
                print(f'I\'ll move {file_}')
                shutil.move(os.path.join(src_folder, folder, file_),
                            os.path.join(dst_folder, folder, file_))