python:从数据集中每个类中挑选出若干样本

可用于制作训练集、gallery等

"""
By Fivethousand
从图片数据集中的每一类都挑选若干张图片,用于制作gallery
"""
import  os
import random
import shutil

dataset_path='F:\python_programs/tasks_from_King\datasets\CASIA-WebFace-part'                 #图片数据集地址
Save_path='F:\python_programs/tasks_from_King\datasets\mytest'                    #保存地址
NUM_OF_IMAGES=10

assert os.path.exists(dataset_path)
os.mkdir(Save_path)
file_lists=os.listdir(dataset_path)
for file in file_lists:
    os.mkdir(os.path.join(Save_path,file))
    file_path=os.path.join(dataset_path,file)
    print("selecting images from:", file_path)
    if len(os.listdir(file_path))<NUM_OF_IMAGES:    # if imgs in this file are insufficient
        continue
    else:
        imgs_list=os.listdir(file_path)
        imgs_selected = random.sample(imgs_list, NUM_OF_IMAGES)  # to select imgs from this file randomly
        for img in imgs_selected:
            img_path=os.path.join(file_path,img)
            shutil.copy(img_path,os.path.join(Save_path,file,img))		#复制
            #如果要变成移动,则用:
            #shutil.move(img_path,os.path.join(Save_path,file,img))	














你可能感兴趣的:(小程序,python,训练集,gallery,指定数量)