Use Python scripts to divide the cat-and-dog training set into two categories

本文介绍了一种使用Python脚本自动将猫和狗的图片从混合训练集中分离到各自目录的方法,通过读取文件名中的标签信息实现精确分类。

Download cat_and_dog Dataset(kaggle dataset)

After downloading the dataset to locally, then extract here, then you will find the dataset structure is like this

Only two directories are included: train and test, The train directory constains all the images cats and dogs. Here we want split all the train images to two directory './train/cat/' or './train/dog/' respectively by they target. Here is the python scripts

import os
path = r'/home/haku/Documents/cat_dog_dataset/train/'
print(os.path.abspath(path))
image_lists = os.listdir(path)
print(len(image_lists))

cat_dir = './train/cat'
print(os.path.abspath(cat_dir))
dog_dir = './train/dog'
print(os.path.abspath(dog_dir))
if not os.path.exists(cat_dir):
    os.makedirs(cat_dir)
if not os.path.exists(dog_dir):
    os.makedirs(dog_dir)

for image in image_lists:
    label = image.split('.')[0]
    if label == 'cat':
        os.system("cp {}{} {}".format(path,image,cat_dir))
    if label == 'dog':
        os.system("cp {}{} {}".format(path,image,dog_dir))
print("final")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值