数据集,批量更新分类数值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 小时前
【分布式利器:腾讯TSF】10、TSF故障排查与架构评审实战:Java架构师从救火到防火的生产哲学
java·人工智能·分布式·架构·限流·分布式利器·腾讯tsf
小鸡吃米…7 小时前
机器学习 - K - 中心聚类
人工智能·机器学习·聚类
好奇龙猫8 小时前
【AI学习-comfyUI学习-第三十节-第三十一节-FLUX-SD放大工作流+FLUX图生图工作流-各个部分学习】
人工智能·学习
沈浩(种子思维作者)8 小时前
真的能精准医疗吗?癌症能提前发现吗?
人工智能·python·网络安全·健康医疗·量子计算
minhuan8 小时前
大模型应用:大模型越大越好?模型参数量与效果的边际效益分析.51
人工智能·大模型参数评估·边际效益分析·大模型参数选择
Cherry的跨界思维8 小时前
28、AI测试环境搭建与全栈工具实战:从本地到云平台的完整指南
java·人工智能·vue3·ai测试·ai全栈·测试全栈·ai测试全栈
MM_MS8 小时前
Halcon变量控制类型、数据类型转换、字符串格式化、元组操作
开发语言·人工智能·深度学习·算法·目标检测·计算机视觉·视觉检测
ASF1231415sd8 小时前
【基于YOLOv10n-CSP-PTB的大豆花朵检测与识别系统详解】
人工智能·yolo·目标跟踪
水如烟9 小时前
孤能子视角:“意识“的阶段性回顾,“感质“假说
人工智能
Carl_奕然9 小时前
【数据挖掘】数据挖掘必会技能之:A/B测试
人工智能·python·数据挖掘·数据分析