import os
# 设置图像和标签文件夹路径
image_dir = 'images'
label_dir = 'labels'
# 遍历标签文件夹中的所有标签文件
for label_file in os.listdir(label_dir):
label_path = os.path.join(label_dir, label_file)
image_file = label_file.replace('.txt', '.jpg') # 假设图像是 .jpg 格式
image_path = os.path.join(image_dir, image_file)
# 检查标签文件是否为空
with open(label_path, 'r') as file:
lines = file.readlines()
if len(lines) == 0: # 如果标签文件为空
# 打印空标签文件和对应的图像文件
print(f"空标签文件: {label_path}")
print(f"对应的图像文件: {image_path}")
# 删除空标签文件和对应的图像文件
if os.path.exists(label_path):
os.remove(label_path)
if os.path.exists(image_path):
os.remove(image_path)
print("空标签和对应图像已删除")