CelebA 数据集身份或 类

CelebA datasets Identities or classes

我想在人脸识别项目中使用 CelebA 数据集,但是当我下载数据集时,我发现所有图像都在一个文件夹中,没有任何身份或子文件夹分配给 类。此外,下载的信息文件对 类 或身份一无所知!关于其论文和网站上提到的 10,000 个身份的任何信息。

import os
import numpy as np
import cv2
import shutil

def read_identity(identities_filename):
    identities = []
    with open(identities_filename, 'r') as f:
        for line in f.readlines()[1:]:
            pair = line.strip().split()
            identities.append(pair)
            #print(identities)
    return np.array(identities)

def create_identity(celebA_dir,identity_path,output_dir):

    img_identity = read_identity(os.path.expanduser(identity_path))
    #print(img_identity[0][1])
    i=0
    for filename in os.listdir(celebA_dir):

        if filename == img_identity[i][0]:
            # print("Good ", filename ,img_identity)
            # # ----create the sub folder in the output folder
            # save_dir = os.path.join(output_dir,img_identity[i][1])
            # i += 1
            # if not os.path.exists(save_dir):
            #     os.makedirs(save_dir)

            # # ----copy image source to its identity destination
            destination = os.path.join(output_dir, img_identity[i][1])
            source = os.path.join(celebA_dir, img_identity[i][0])
            i += 1
            shutil.copy2(source, destination)







if __name__ == '__main__':

    celebA_dir = r"...." #Add your directory where dataset exist
    output_dir = r"...." #Add directory where you want to save datasets with identities
    identity_path = r"identity_CelebA.txt"

create_identity(celebA_dir,identity_path,output_dir)