#some views always miss some pictures
#choose a view that contains all the pictures
import torch
import os
import os.path as osp
import json
import shutil
from tqdm import tqdm
def main():
    
    all_missing_items = []
    split = 'test'
    root_dir_path = osp.dirname(osp.dirname(osp.dirname(osp.abspath(__file__))))
    data_dir = osp.join(root_dir_path, 'data')
    img_dir = osp.join(data_dir, 'images')
    split_image_dir = osp.join(img_dir, split)
    capture_list = os.listdir(split_image_dir)
    capture_list.sort()
    for i in tqdm(range(len(capture_list))):
        capture_name = capture_list[i]
        capture_dir = osp.join(split_image_dir, capture_name)
        motion_list = os.listdir(capture_dir)
        motion_list.sort()
        for j in range(len(motion_list)):
            motion_name = motion_list[j]
            motion_dir = osp.join(capture_dir, motion_name)
            camera_list = os.listdir(motion_dir)
            camera_list.sort()
            short_length = float('inf')
            flag_k = 0
            for k in range(len(camera_list)):
                camera_name = camera_list[k]
                camera_dir = osp.join(motion_dir, camera_name)
                images_name_list = os.listdir(camera_dir)
                images_name_list.sort()
                length = len(images_name_list)
                if length < short_length:
                    short_length = length
                    flag_k = k
            temp_camera_dir = osp.join(motion_dir, camera_list[flag_k])
            temp_images_list = os.listdir(temp_camera_dir)
            temp_images_list.sort()
            # different_camera_length = []
            
            for k in range(len(camera_list)):
                camera_name = camera_list[k]
                camera_dir = osp.join(motion_dir, camera_name)
                images_name_list = os.listdir(camera_dir)
                # different_camera_length.append(len(images_name_list))
                images_name_list.sort()
                missing_items = set(images_name_list) - set(temp_images_list)
                missing_items_list = list(missing_items)
                missing_items_list.sort()
                if missing_items_list != []:
                    for item in missing_items_list:
                        # origion_image_name = osp.join(temp_camera_dir, item)
                        missing_image_name = osp.join(camera_dir, item)#path for missing images
                        all_missing_items.append(missing_image_name)
                        print('test')
                        if os.path.isfile(missing_image_name):
                            os.remove(missing_image_name)
                            print(f'deleted: {missing_image_name}')
    
if __name__  == '__main__':
    main()
                            
                        
                