数据集,批量更新分类数值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)
相关推荐
加速财经1 分钟前
2026年网站部署平台怎么选:6款平台,覆盖网页上线与 AI 项目部署
人工智能
长沙京卓4 分钟前
可源码交付|县域低空无人机智能运营平台:AI辅助飞行、巡检闭环与警用低空安防
人工智能·无人机
Python自动化直播5 分钟前
从脚本到语义:AI直播中控技术架构的三次演进与合规设计
人工智能·架构
pjj198546 分钟前
深度学习-自定义Dataset和网络结构
人工智能
程序员cxuan10 分钟前
跟 WebUI 说再见了,最强 DeepSeek 桌面端来了!
人工智能·后端·程序员
sbjdhjd12 分钟前
从 assert 动态调用报错到临时文件链路:PHP 5.6 与 7.3 授权靶场中的短 payload 复盘 | (17字符绕过)
安全·网络安全·数据挖掘·开源·云计算·php·字符绕过
haon112217 分钟前
树模型在信贷风控怎么用——从决策树到 XGBoost
大数据·人工智能·算法·决策树·机器学习·数据挖掘
wordbaby20 分钟前
BM25 是什么?手把手拆解搜索引擎的核心算法
人工智能·算法
古少侠20 分钟前
AI 内容粘到 Word 就乱?用 DS随心转 把复制的内容直接变成排版好的一段
人工智能·c#·word
浅安的邂逅21 分钟前
260910-DeepSeek 发布 V4.1 Flash:1M 上下文、552B MoE,KV 缓存降到上一代 1/4,同步开源
人工智能·开源·ai大模型·deepseek·ai日报