淘先锋技术网

首页 1 2 3 4 5 6 7

删除Yolo项目中数据集labels文件夹中指定类别的txt文件内容,我需要编写一个Python脚本来完成这个任务。请确保你备份了数据集,以防意外删除了不应该删除的内容。

以下是一个Python脚本示例,它将删除指定的类别(5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)在txt文件中的所有行:

import os

def remove_classes_from_txt_files(folder_path, classes_to_remove):
    # 获取labels文件夹中所有的txt文件
    txt_files = [file for file in os.listdir(folder_path) if file.endswith('.txt')]

    for txt_file in txt_files:
        with open(os.path.join(folder_path, txt_file), 'r') as f:
            lines = f.readlines()

        # 保留不包含指定类别的行
        new_lines = [line for line in lines if not should_remove_line(line, classes_to_remove)]

        # 覆盖原来的txt文件内容
        with open(os.path.join(folder_path, txt_file), 'w') as f:
            f.writelines(new_lines)

def should_remove_line(line, classes_to_remove):
    class_id = int(line.split()[0])
    return class_id in classes_to_remove

if __name__ == "__main__":
    dataset_labels_folder = "path/to/your/labels/folder"
    classes_to_remove = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
    remove_classes_from_txt_files(dataset_labels_folder, classes_to_remove)

在这个脚本中,你需要将path/to/your/labels/folder替换为你数据集中labels文件夹的实际路径。classes_to_remove列表定义了需要删除的类别的ID。

运行这个脚本后,它将读取每个txt文件并删除包含指定类别的行。请确保在运行脚本之前做好备份,以防数据意外丢失。

import os

def delete_classes_from_txt(file_path, classes_to_delete):
    """
    从txt文件中删除指定类别的信息

    参数:
        file_path: 文件路径
        classes_to_delete: 要删除的类别列表,例如 [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

    返回:
        无
    """
    lines = []
    with open(file_path, 'r') as file:
        lines = file.readlines()

    updated_lines = []
    for line in lines:
        class_id, *rest = line.split(' ')
        class_id = int(class_id)
        if class_id not in classes_to_delete:
            updated_lines.append(line)

    with open(file_path, 'w') as file:
        file.writelines(updated_lines)

def main():
    folder_path = "路径/到/labels文件夹"  # 替换为实际的文件夹路径
    classes_to_delete = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

    for filename in os.listdir(folder_path):
        if filename.endswith(".txt"):
            file_path = os.path.join(folder_path, filename)
            delete_classes_from_txt(file_path, classes_to_delete)

    print("指定类别信息已成功删除!")

if __name__ == "__main__":
    main()

将类别4修改为类别3

import os

def modify_labels(folder_path):
    # 获取labels文件夹中所有的txt文件
    txt_files = [file for file in os.listdir(folder_path) if file.endswith('.txt')]

    for txt_file in txt_files:
        with open(os.path.join(folder_path, txt_file), 'r') as f:
            lines = f.readlines()

        # 将类别4替换为类别3
        new_lines = [modify_line(line) for line in lines]

        # 覆盖原来的txt文件内容
        with open(os.path.join(folder_path, txt_file), 'w') as f:
            f.writelines(new_lines)

def modify_line(line):
    class_id, *values = line.split()
    if class_id == '4':
        class_id = '3'
    new_line = class_id + " " + " ".join(values) + "\n"
    return new_line

if __name__ == "__main__":
    dataset_labels_folder = "path/to/your/labels/folder"
    modify_labels(dataset_labels_folder)

在这个修改后的脚本中,我只修改了类别ID为4的行,并将其替换为类别ID为3。其余的行保持不变。请将path/to/your/labels/folder替换为你数据集中labels文件夹的实际路径。

运行这个脚本后,它将读取每个txt文件,仅修改类别ID为4的行为类别ID为3。请确保在运行脚本之前做好备份,以防数据意外丢失。