目标检测——清洗数据

清洗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及文件路径!!!

相关推荐
海螺_AIHCC3 分钟前
你被「假排队」坑过吗?抖音新规管两件事:观众知不知道是 AI 做的,和内容本身真不真
人工智能
谁在黄金彼岸12 分钟前
MiniMax H3 的 ncnn + Vulkan 移植:事实核查与现状调研
人工智能
Dawson Zhu20 分钟前
KV Cache原理与大模型推理加速:从基础机制到架构优化的全面解析
人工智能·语言模型·架构·aigc·agi
用户960984579224020 分钟前
从单机配置到跨机工程化:项目级极速门禁、三插件拆分与按需启停实战
人工智能
天远数科24 分钟前
零信任架构实战:基于天远车辆过户详版查询构建自动化抵押车权属合规审查网关
人工智能·python·架构·自动化
用户2022522150628 分钟前
AI 写的代码,备份每天成功、恢复能力却是 0
人工智能
zhengfei61136 分钟前
AI 渗透项目深度研究:从工具自动化到自主攻击系统,下一代 AI 渗透平台应该如何构建?
运维·人工智能·ai·开源·自动化·ai编程·ai-native
注塑机数据采集联网39 分钟前
中科晟达注塑机智能数据采集联网(ZK-D1/D3)接入 MES 与 IOT 平台,并用 AI 大模型分析数据
大数据·人工智能·物联网·数据采集·注塑机网关·注塑机mes系统
Henry-SAP43 分钟前
千问Qwen4开源引爆AI热潮
人工智能
硅谷秋水1 小时前
InfraRSI:利用递归自我提升来训练基础设施
人工智能·机器学习·语言模型