当我们拿到一个数据集的时候,可能会出现图像与标签对不上的情况;我们可以通过标签名称,对图像进行筛选,保持两者的一致性。代码如下:
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))