目标检测——清洗数据

清洗VOC格式数据集代码示例

python 复制代码
import os
import xml.etree.ElementTree as ET

def process_annotations(image_folder, annotation_folder):
    # 遍历标签文件夹中的所有XML文件
    for xml_file in os.listdir(annotation_folder):
        if not xml_file.endswith('.xml'):
            continue
        
        xml_path = os.path.join(annotation_folder, xml_file)
        tree = ET.parse(xml_path)
        root = tree.getroot()
        
        # 标记是否保留该文件
        keep_file = False
        
        # 遍历所有<object>标签
        for obj in root.findall('object'):
            name = obj.find('name').text
            if name == 'person':  # 需修改,保留哪个类别就写哪个类别
                keep_file = True
            else:
                root.remove(obj)  # 移除非Pedestrian的<object>
        
        # 如果没有Pedestrian类别,删除对应的图片和标签
        if not keep_file:
            image_name = root.find('filename').text
            image_path = os.path.join(image_folder, image_name)
            if os.path.exists(image_path):
                os.remove(image_path)
            os.remove(xml_path)
        else:
            # 保存修改后的XML文件
            tree.write(xml_path)

# 示例用法
image_folder = r'D:\BaiduNetdiskDownload\VOCdevkit\VOCdevkit\VOC2007\JPEGImages'  # 替换为图片文件夹路径
annotation_folder = r'D:\BaiduNetdiskDownload\VOCdevkit\VOCdevkit\VOC2007\Annotations'  # 替换为标签文件夹路径
process_annotations(image_folder, annotation_folder)

需根据自己的数据集修改name及文件路径!!!

清洗YOLO格式数据集代码示例

python 复制代码
import os

def process_labels(image_folder, label_folder):
    # 遍历标签文件夹中的所有标签文件
    for label_file in os.listdir(label_folder):
        if not label_file.endswith('.txt'):
            continue
        
        label_path = os.path.join(label_folder, label_file)
        image_name = os.path.splitext(label_file)[0] + '.png'
        image_path = os.path.join(image_folder, image_name)
        
        # 读取标签文件内容
        with open(label_path, 'r') as f:
            lines = f.readlines()
        
        # 需修改!!!根据自己想要的类别保留!筛选类别为0的行
        filtered_lines = [line for line in lines if line.strip().split()[0] == '0']
        
        # 如果没有类别为0的行,删除对应的图片和标签
        if not filtered_lines:
            if os.path.exists(image_path):
                os.remove(image_path)
            os.remove(label_path)
        else:
            # 保存修改后的标签文件
            with open(label_path, 'w') as f:
                f.writelines(filtered_lines)

# 示例用法
label_folder = r'D:\BaiduNetdiskDownload\annotations_trainval2017\txt'  # 替换为图片文件夹路径
image_folder = r'D:\BaiduNetdiskDownload\val2017\val2017'  # 替换为标签文件夹路径
process_labels(image_folder, label_folder)

需根据自己的数据集修改line及文件路径!!!

相关推荐
xier_ran10 分钟前
关键词解释:对比学习(Contrastive Learning)
人工智能·深度学习·学习·机器学习·对比学习
Jay200211123 分钟前
【机器学习】27 异常检测(密度估计)
人工智能·机器学习
ziwu29 分钟前
【岩石种类识别系统】Python+TensorFlow+Django+人工智能+深度学习+卷积神经网络算法
人工智能·深度学习·图像识别
AI即插即用36 分钟前
即插即用系列 | CVPR SwiftFormer:移动端推理新王者!0.8ms 延迟下 ImageNet 78.5% 准确率,吊打 MobileViT
图像处理·人工智能·深度学习·目标检测·计算机视觉·cnn·视觉检测
得贤招聘官1 小时前
AI招聘:HR领域的智能化变革与行业趋势
人工智能
ziwu1 小时前
【中草药识别系统】Python+TensorFlow+Django+人工智能+深度学习+卷积神经网络算法
人工智能·深度学习·图像识别
c#上位机2 小时前
halcon图像去噪—导向滤波
图像处理·人工智能·计算机视觉·c#·halcon
行云流水20002 小时前
青少年编程学习:考级与竞赛结合提升能力的方法
人工智能·学习·青少年编程
Blossom.1182 小时前
基于多智能体强化学习的云资源调度系统:如何用MARL把ECS成本打下来60%
人工智能·python·学习·决策树·机器学习·stable diffusion·音视频
Coding茶水间2 小时前
基于深度学习的苹果病害检测系统演示与介绍(YOLOv12/v11/v8/v5模型+Pyqt5界面+训练代码+数据集)
图像处理·人工智能·深度学习·yolo·目标检测·计算机视觉