由于自动标注图像后,有些图像并未被标注,所以标签少于图像数据。
这个代码的目的就是一键删除多余的图像,省的自己一个一个去删除。
注意:
1.我的图像都是jpg
2.两个文件夹内不能包含其他文件
import os
# 输入图片路径
path_jpg = "E:/nx1/jpg"
# 输入的标签路径
path_txt = "E:/nx1/1"
list_jpg = os.listdir(path_jpg)
list_txt = os.listdir(path_txt)
jpg_name = []
txt_name = []
for i in range(len(list_jpg)):
name_jpg, _ = list_jpg[i].split(".")
jpg_name.append(name_jpg)
for j in range(len(list_txt)):
name_txt, _ = list_txt[j].split(".")
txt_name.append(name_txt)
del_jpg = []
for name_j in range(len(jpg_name)):
if jpg_name[name_j] in txt_name:
continue
else:
jpg = jpg_name[name_j] + ".jpg"
del_jpg.append(jpg)
for del_name_jpg in del_jpg:
# 合并路径
name_del_jpg = os.path.join(path_jpg, del_name_jpg)
os.remove(name_del_jpg)
print(len(del_jpg))