使用python对数据集进行批量转移

使用python对数据集进行批量转移

将数据集中的图像转移到train和test文件夹中

import os
import shutil
#数据路径
sour_path='./'# 原数据集,还未拆分为train和test两个子集,填写相对路径
train_path='./train/'
test_path='./test/'
# 训练集和测试集需要多少子文件夹,自行设置。这里是200个子文件夹
for i in range(200):
    os.mkdir(train_path+'('+str(i+1)+')')
    os.mkdir(test_path+'('+str(i+1)+')')
    
# 接下来开始批量转移文件到train和test中。需要将上面的for循环注释掉。
for file in os.listdir(sour_path):
    folder = os.path.join(sour_path, file)
    if os.path.isdir(folder):
        for i in range(3):
            # 将前3张放在train文件夹下
            img = folder + '/(' + str(i+1) + ')'+'.bmp'
            if os.path.exists(img):
                shutil.copy(img, train_path + file + '/(' + str(i+1) + ')' + '.bmp')
        for j in range(3):
            # 将后3张放在valid文件夹下
            img = folder + '/(' + str(j+4) + ')' + '.bmp'
            if os.path.exists(img):
                shutil.copy(img, test_path + file + '/(' + str(j+4) + ')' + '.bmp')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值