我是靠谱客的博主 高挑香菇,这篇文章主要介绍python:从数据集中每个类中挑选出若干样本,现在分享给大家,希望可以做个参考。

可用于制作训练集、gallery等

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

dataset_path='F:python_programs/tasks_from_KingdatasetsCASIA-WebFace-part'                 #图片数据集地址
Save_path='F:python_programs/tasks_from_Kingdatasetsmytest'                    #保存地址
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:从数据集中每个类中挑选出若干样本的全部内容,更多相关python内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(101)

评论列表共有 0 条评论

立即
投稿
返回
顶部