According to the division of the label data set to make the same division of the input data set

When doing machine learning tasks on image processing, sometimes it involves input and label data sets that need to do the same preprocessing, such as segmentation. When the label data set is divided according to the rules, the corresponding parts in the input need to be the same. deal with. Record here if the input is divided equally according to the already divided (random proportionally) label.

1. First write the name of each part of the image under the label into txt

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

2. According to the name of each file in the txt, traverse the input folder, filter out the corresponding images in the input, and move to the specified directory. (There may be one-to-many, if there is a label, but the input has RGB, and IR infrared bands, etc., but the name is often only different in suffix, so use the method of judging whether it contains the corresponding string)

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) #移动

Guess you like

Origin blog.csdn.net/qq_41872271/article/details/107618831