将自己的数据集分成相应的训练集和测试集,并把相应的标签分成两个部分
import os
import cv2
import csv
import math
import random
import shutil
def move(filepath,destpath):
#从数据集中随机选取20%移动到另一文件夹下作为测试集,剩下的80%作为训练集
pathdir=os.listdir(filepath)
ranpath=random.sample(pathdir,int(0.2*len(pathdir)))
print(ranpath)
for alldir in ranpath:
child=os.path.join(filepath,alldir)
dest=os.path.join(destpath,alldir)
shutil.copy(child,dest)
os.remove(child)
def move_label(imgpath,labelpath,testpath):
#根据不同文件夹下的图片移动相应图片的标签
labels=os.listdir(labelpath)
for label in labels:
imgdir=os.listdir(imgpath)
for img in imgdir:
if label.strip('.txt')==img.strip('.jpg'):
print('###')
label_path=os.path.join(labelpath,label)
test_path=os.path.join(testpath,label)
print(label_path)
if labelpath:
shutil.copy(label_path,test_path)
os.remove(label_path)
def move_img(imgpath,labelpath,testpath):
labels=os.listdir(labelpath)
for label in labels:
imgdir=os.listdir(imgpath)
for img in imgdir:
if label.strip('.txt')==img.strip('.jpg'):
print('###')
img_path=os.path.join(imgpath,img)
test_path=os.path.join(testpath,img)
shutil.copy(img_path,test_path)
os.remove(img_path)
if __name__ == "__main__":
move('./train','./test')
move_img("./train",'./labels/test_labels','./test')