请确保备份原始标签文件,以防需要还原
目录
一、删除多余标签、剩余需要标签
问题:标签格式是yolo格式,保存在txt中,标签中含有5类:0: person 1:dog 2:cat 3:car 4:bicycle, 只想训练 "dog" 和 "bicycle" 这两个类别
——————————————————————————————
如果你的标签是保存在Yolo格式的.txt文件中,你可以通过筛选要训练的类别来实现。在每个标签文件中,每一行表示一个对象,通常以以下格式:
<object-class> <x> <y> <width> <height>
其中<object-class>
是对象的类别。如果你只想训练 "dog" 和 "bicycle" 这两个类别,你可以将标签文件中不属于这两个类别的行删除或者忽略。
例如,如果你有一个标签文件example.txt
,它包含了 "person"、"dog"、"cat"、"car" 和 "bicycle" 类别的对象,你可以通过以下方式筛选出 "dog" 和 "bicycle" 类别的对象:
# 读取标签文件
with open('example.txt', 'r') as file:
lines = file.readlines()
# 选择要保留的类别
desired_classes = ['1', '4']
# 筛选出指定类别的行
filtered_lines = [line for line in lines if line.split(' ')[0] in desired_classes]
# 写入筛选后的标签文件
with open('filtered_labels.txt', 'w') as file:
file.writelines(filtered_lines)
下面这段代码会遍历指定文件夹中的所有.txt标签文件,并将不属于 "dog" 和 "bicycle" 类别的对象行删除,然后将处理后的标签保存回原文件。请确保备份原始标签文件,以防需要还原。
import os
# 定义文件夹路径
label_folder = "./test/labels" # 你的标签文件夹路径
# 定义要保留的类别
desired_classes = ['1', '2'] # 想要训练的类别,对应的类别编号
# 遍历文件夹中的所有.txt标签文件
for filename in os.listdir(label_folder):
if filename.endswith(".txt"):
label_path = os.path.join(label_folder, filename)
# 读取标签文件
with open(label_path, 'r') as file:
lines = file.readlines()
# 筛选出指定类别的行
filtered_lines = [line for line in lines if line.split(' ')[0] in desired_classes]
# 写入筛选后的标签文件
with open(label_path, 'w') as file:
file.writelines(filtered_lines)
你可以根据你的需求自由编辑 desired_classes
列表。你可以将其中的类别编号增加、删除或更换为你需要训练的类别。例如,如果你想训练除 "dog" 和 "bicycle" 之外的其他类别,只需编辑 desired_classes
列表以包含其他类别的编号。
请确保 desired_classes
列表中包含你希望在训练中使用的所有类别的编号。这样,你可以筛选出指定的类别,只留下需要的标签数据。
————————————————————————
二、指定数值替换
问:文件夹中保存多个txt文件,其中一个内容如下: 2 0.2421875 0.41816666666666674 0.32365625 0.32441666666666674 6 0.813625 0.388375 0.2790312499999999 0.3125 1 0.25559374999999995 0.40920833333333334 0.109375 0.1339166666666666 7 0.7879375 0.38987499999999997 0.11606249999999996 0.13691666666666671 我我要将1替换成0,2替换成1,7替换成4,6替换成5
使用以下Python程序来替换指定的类别编号:
注意:测代码生成的新文件为1行
import os
# 定义文件夹路径
folder_path = "./your_folder_path" # 你的文件夹路径
# 定义类别替换字典
class_mapping = {
'1': '0',
'2': '1',
'6': '5',
'7': '4',
}
# 遍历文件夹中的所有.txt标签文件
for filename in os.listdir(folder_path):
if filename.endswith(".txt"):
file_path = os.path.join(folder_path, filename)
# 读取标签文件
with open(file_path, 'r') as file:
lines = file.readlines()
# 替换类别编号
updated_lines = []
for line in lines:
parts = line.split()
if parts[0] in class_mapping:
parts[0] = class_mapping[parts[0]]
updated_line = " ".join(parts)
updated_lines.append(updated_line)
# 写入替换后的标签文件
with open(file_path, 'w') as file:
file.writelines(updated_lines)
这个代码将根据 class_mapping
字典替换类别编号,然后将更新后的内容写回到每个标签文件中。你可以根据需要修改 class_mapping
字典来指定替换规则。
2.2不换行
保留原行列。以下是修改后的代码
import os
# 定义文件夹路径
folder_path = "./test/labels" # 你的文件夹路径
# 定义类别映射字典
class_mapping = {
'1': '0',
'2': '1',
}
# 遍历文件夹中的所有.txt标签文件
for filename in os.listdir(folder_path):
if filename.endswith(".txt"):
file_path = os.path.join(folder_path, filename)
# 读取标签文件
with open(file_path, 'r') as file:
lines = file.readlines()
# 替换类别编号并保留原始行数和列数
updated_lines = []
for line in lines:
parts = line.split()
if parts[0] in class_mapping:
parts[0] = class_mapping[parts[0]]
updated_line = " ".join(parts)
updated_lines.append(updated_line)
# 写入替换后的标签文件(保留原始行数和列数)
with open(file_path, 'w') as file:
for updated_line in updated_lines:
file.write(updated_line + "\n") # 添加换行符以保留原始行数和列数
三、整数0换1,1换为0
import os
import fileinput
# 指定要处理的文件夹路径
folder_path = 'your_folder_path' # 替换为实际文件夹路径
# 遍历文件夹中的所有txt文件
for filename in os.listdir(folder_path):
if filename.endswith('.txt'):
file_path = os.path.join(folder_path, filename)
# 使用fileinput库来处理文件
with fileinput.FileInput(file_path, inplace=True) as file:
for line in file:
# 替换0为1,1为0
line = line.replace('0', 'temp').replace('1', '0').replace('temp', '1')
print(line, end='')
# 完成后,所有文件中的0和1都已替换