功能描述:数据集里原来是具有4个类别yolo格式标注的 txt文件,现在需要把这四个类别合并成一个类别,所以就使用以下这个脚本读取源标注文件中的每一行的类别id,如果id!=0,将其修改为0
import os
# 修改非0类别id都为0,即原来的多个类现在统一标为一个类
def change_classID(annotation_path):
f = open(annotation_path,'r')
lines = f.readlines()
f.close()
new_lines = []
for line in lines:
if line == '\n':
continue
class_id=line.split(' ')[0]
if class_id != '0':
line = '0'+line[1:]
new_lines.append(line)
with open(annotation_path,'w') as f:
f.writelines(new_lines)
f.close()
print("成功修改{}".format(annotation_path))
if __name__ == '__main__':
txt_path = r""
txt_list = os.listdir(txt_path)
count = 0
for txt in txt_list:
count += 1
path = os.path.join(txt_path,txt)
change_classID(path)
print("共修改了%d"%count+"个标注")