0、前言
本文提供了一个从PETA数据集中提取性别属性的方法。
1、方法
通过以下代码,可以从数据集中解析出每个图片的属性,并根据性别存放到不同文件夹:
"""
parse person's gender from PETA
"""
import glob
import os
import shutildef parse_peta(root_path, new_dataset_path):classes = ['0_Female', '1_Male']os.makedirs(new_dataset_path, exist_ok=True)Male_path = os.path.join(new_dataset_path, '1_Male')Female_path = os.path.join(new_dataset_path, '0_Female')os.makedirs(Male_path, exist_ok=True)os.makedirs(Female_path, exist_ok=True)for sub_ds in os.listdir(root_path):print("processing: {}".format(sub_ds))label_path = os.path.join(root_path, sub_ds, 'archive', 'Label.txt')imgs_path = os.path.join(root_path, sub_ds, 'archive')with open(label_path, 'r') as f:lines = f.readlines()for line in lines:line_split = line.replace('\n', '').split(' ')person_id = line_split[0]cls_id = 1 if 'personalMale' in line_split else 0class_name = classes[cls_id]person_imgs = glob.glob(imgs_path + '/{}*'.format(person_id))for person_img in person_imgs:name = person_img.split('/')[-1]img_save_to = os.path.join(new_dataset_path, class_name, 'peta_' + name)shutil.copy(person_img, img_save_to)if __name__ == '__main__':root_path = '../PETA/PETA dataset'new_dataset_path = '../from_PETA'parse_peta(root_path=root_path, new_dataset_path=new_dataset_path)