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)
对人脸口罩数据库的预处理
最新推荐文章于 2022-08-05 11:15:06 发布
本文介绍了一个用于处理人脸数据集的Python脚本。该脚本能够批量重命名数据集中子文件夹及其内部的图像文件,并将这些图像统一复制到指定的目标文件夹中。此过程有助于标准化数据集格式,方便后续的人脸识别或图像处理任务。
摘要由CSDN通过智能技术生成