Code example for comparing and filtering the names of image datasets and label datasets

When we get a data set, there may be a situation where the image and the label do not match; we can filter the image through the label name to maintain the consistency of the two. code show as below:

import os
from tqdm import tqdm
'''
image_path:原始数据集图像文件夹路径
label_path:原始数据集标签文件夹路径
deal_image_path:对比完成后图像文件夹路径
deal_label_path:对比完成后标签文件夹路径
'''
def compare_image_label_remove_file(image_path,label_path,deal_image_path,deal_label_path):
    count = 0
    remove_count = 0
    label_list = os.listdir(label_path)
    for label_name in tqdm(label_list):
        each_name, _ = os.path.splitext(label_name)
        image_name = os.path.join(image_path, '{}.jpg'.format(each_name))
        label_name = os.path.join(label_path, '{}.txt'.format(each_name))

        if os.path.exists(label_name) and os.path.exists(image_name):
            shutil.move(label_name, deal_label_path)
            shutil.move(image_name, deal_image_path)
            remove_count += 1
        count += 1
    print("compare complete,total %d file,remove %d file" % (count, remove_count))

Guess you like

Origin blog.csdn.net/m0_54361461/article/details/127740717