数据集,批量更新分类数值OR批量删除分类行数据

数据集批量更新分类OR删除分类行数据

python 复制代码
import os


def remove_class_from_file(file_path, class_to_remove):
    """
    从YOLO格式的标注文件中删除指定类别的行记录,并去除空行。

    :param file_path: YOLO标注文件路径
    :param class_to_remove: 需要删除的类别ID
    :return: 删除的行数
    """
    deleted_lines = 0

    # 读取原始数据
    with open(file_path, 'r') as file:
        lines = file.readlines()

    # 过滤掉指定类别的行
    filtered_lines = [line for line in lines if not line.startswith(f"{class_to_remove} ")]

    # 去除空行
    cleaned_lines = [line for line in filtered_lines if line.strip()]

    # 计算删除的行数
    deleted_lines = len(lines) - len(cleaned_lines)

    # 将清理后的数据写回文件
    with open(file_path, 'w') as file:
        file.writelines(cleaned_lines)

    return deleted_lines


def update_class_in_file(file_path, old_class, new_class):
    """
    将YOLO格式的标注文件中指定类别的数字更新为新的类别ID。

    :param file_path: YOLO标注文件路径
    :param old_class: 需要更新的旧类别ID
    :param new_class: 更新后的新类别ID
    :return: 更新的行数
    """
    updated_lines = 0

    # 读取原始数据
    with open(file_path, 'r') as file:
        lines = file.readlines()

    # 更新类别ID
    updated_lines = 0
    new_lines = []
    for line in lines:
        if line.startswith(f"{old_class} "):
            new_lines.append(line.replace(f"{old_class} ", f"{new_class} ", 1))
            updated_lines += 1
        else:
            new_lines.append(line)

    # 将更新后的数据写回文件
    with open(file_path, 'w') as file:
        file.writelines(new_lines)

    return updated_lines


def process_directory(directory_path, class_to_remove=None, old_class=None, new_class=None):
    """
    处理指定目录下的所有YOLO标注文件,可以选择删除指定类别的记录或更新分类数字。

    :param directory_path: 目录路径
    :param class_to_remove: 需要删除的类别ID(如果需要删除指定类别的记录)
    :param old_class: 需要更新的旧类别ID(如果需要更新分类数字)
    :param new_class: 更新后的新类别ID(如果需要更新分类数字)
    """
    total_deleted_lines = 0
    total_updated_lines = 0

    # 遍历目录中的文件
    for filename in os.listdir(directory_path):
        if filename.endswith('.txt'):  # 假设标注文件都是以 .txt 结尾的
            file_path = os.path.join(directory_path, filename)

            if class_to_remove is not None:
                deleted_lines = remove_class_from_file(file_path, class_to_remove)
                print(f"已从文件 {filename} 中删除类别 {class_to_remove} 的记录,共删除 {deleted_lines} 行。")
                total_deleted_lines += deleted_lines

            if old_class is not None and new_class is not None:
                updated_lines = update_class_in_file(file_path, old_class, new_class)
                print(f"已从文件 {filename} 中将类别 {old_class} 更新为 {new_class},共更新 {updated_lines} 行。")
                total_updated_lines += updated_lines

    if class_to_remove is not None:
        print(f"总共删除了 {total_deleted_lines} 行记录。")

    if old_class is not None and new_class is not None:
        print(f"总共更新了 {total_updated_lines} 行记录。")


# 使用示例
directory_path = 'C:\\Users\\合格的牛马\\Desktop\\datasets\\test-delete\\labels'  # 替换为你的目录路径

# 删除指定类别的记录
class_to_remove = 5  # 替换为你要删除的类别ID
process_directory(directory_path, class_to_remove=class_to_remove)

# 更新指定类别的分类数字
old_class = 1  # 替换为旧类别ID
new_class = 100  # 替换为新类别ID
process_directory(directory_path, old_class=old_class, new_class=new_class)
相关推荐
一切皆是因缘际会43 分钟前
从概率拟合到内生心智:2026 下一代 AI 架构演进与落地实践
人工智能·深度学习·算法·架构
科研前沿1 小时前
镜像视界 CameraGraph™+多智能体:构建自感知自决策的全域空间认知网络技术方案
大数据·运维·人工智能·数码相机·计算机视觉
爱学习的张大1 小时前
具身智能论文问答(2):Diffusion Policy
人工智能
AI科技星1 小时前
全域数学·72分册·射影原本 无穷维射影几何卷细化子目录【乖乖数学】
人工智能·线性代数·算法·机器学习·数学建模·数据挖掘·量子计算
Chef_Chen1 小时前
论文解读:MemOS首次把记忆变成大模型的一等公民资源,Scaling Law迎来第三条曲线
人工智能·agent·memory
风落无尘1 小时前
《智能重生:从垃圾堆到AI工程师》——第四章 变化的艺术
人工智能·线性代数·算法
发哥来了1 小时前
AI视频生成模型选型指南:五大核心维度对比评测
大数据·人工智能·机器学习·ai·aigc
发哥来了1 小时前
AI驱动生产线的实际落地:一个东莞厂商的技术选型实录
大数据·人工智能·机器学习·ai·aigc
AC赳赳老秦1 小时前
知识产权辅助:用 OpenClaw 批量生成专利交底书 / 软著申请材料,自动校验格式与内容合规性
java·人工智能·python·算法·elasticsearch·deepseek·openclaw
AI科技2 小时前
原因大揭秘:为什么别人的编曲伴奏做得又快又好,2026年度甄选5款AI编曲软件汇总
人工智能