根据label数据集的划分来对input数据集作相同的划分

在做关于图像处理方面的机器学习任务时,有时会涉及到input和lable数据集需要做相同的预处理,比如分割,当对label数据集按照规则划分后,需要对input中相对应部分作相同处理。在此记录如果根据已经分割好(按比例随机)的label来对input作相同划分。

1、首先将label下每部分的图像名字写入txt

ls -R /home/datalab/work/datasets/test_7pilang/*.jpg > file.txt

2、根据txt中每个文件名字,遍历input文件夹,将input中与之相对应的图像筛选出来,移动到指定目录。(这儿可能一对多,如果label有一张,但是input有RGB,还有IR红外波段等,但其名字往往只有后缀不同,因此用判断是否包含相应字符串的方法)

import os,shutil
path = r"test_file.txt"
#传入要读的文件路径
filee = open(path,"r",encoding="utf-8",errors="ignore")
sourceDir="/home/xzx/Project/Spatiotemporal/data/singleImage/cloudy" #源目录
targetDir="/home/xzx/Project/Spatiotemporal/data/singleImage/test/" #目标目录

while True:
    mystr = filee.readline()#表示一次读取一行
    mystr = mystr[:-5]
    if not mystr:
#读到数据最后跳出,结束循环。数据的最后也就是读不到数据了,mystr为空的时候
        break
    for root, dirs, files in os.walk(sourceDir):
        for file in files:
            result = mystr in file
            if result == True:
                shutil.move(os.path.join(root,file),targetDir) #移动

猜你喜欢

转载自blog.csdn.net/qq_41872271/article/details/107618831