对人脸口罩数据库的预处理

import os
import shutil

def dataset_split(source_dir, outputdir):
    origin_dir = os.listdir (source_dir)

    count = 0
    # print (origin_dir)
    #
    # for i in range (len (origin_dir)):
    #     origin_name = os.path.join (source_dir, origin_dir[i])
    #     # print(origin_name)
    #     new_name = os.path.join (source_dir, str (i))
    #     os.rename (origin_name, new_name)

    #以上完成文件夹重命名的功能,将source_dir里面的文件夹全部重新按照1234顺序命名,此时 os.listdir(source_dir)中内容改变

    new_dir = os.listdir(source_dir)

    #new_dir是1,2,3,4

    for sub_dir in new_dir:
        image_path = os.path.join(source_dir,sub_dir)
        # print(image_path)

    #获取图像名称

        image_name = os.listdir(image_path)
        print (image_name)

        for i in range(len(image_name)):
            origin_image_name = os.path.join(image_path,image_name[i])
            print(origin_image_name)
            new_image_name = os.path.join(image_path,str(count)+'_'+str(i)+'.jpg')
            print(new_image_name)
            os.rename (origin_image_name, new_image_name)

            dest_path = str(output_dir+"\\")+str(count)+'_'+str(i)+'.jpg'
            print(dest_path)
            shutil.copy(new_image_name,dest_path)

        count = count + 1

    print('Done')

#以上代码完成功能:将原数据集含有人脸照片的子文件夹重命名为12345,并且将子文件夹内的图片名称重命名为0_0,0_1,0_2..1_0这类格式并将所有子文件夹的图片放入一个新文件夹中


if __name__ == "__main__":
    source_dir = "E:\RealLifeMaskedFaceDataset\self-built-masked-face-recognition-dataset\AFDB_masked_face_dataset"
    output_dir = "E:\RealLifeMaskedFaceDataset\self-built-masked-face-recognition-dataset\output\withmask"

    dataset_split(source_dir,output_dir)

你可能感兴趣的:(计算机视觉,opencv,图像处理,python)