在数据集标注中,将检测物体标注为1,标注完后觉得1不合适,又想将全部的标签文件中的目标1改成2,txt的修改方法,如下图所示
import os
def makexml(txtPath): # 读取txt路径,xml保存路径,数据集图片所在路径
files = os.listdir(txtPath)
for i, name in enumerate(files):
txtname= txtPath + name
#使用with open,不用close
with open(txtname) as txtFile:
txtList = txtFile.readlines()
with open(txtname, 'w') as f:
for line in txtList:
line_split = line.strip().split()
if line_split[0] == '2':
line_split[0] = '1'
f.write(
line_split[0] + ' ' +
line_split[1] + " " +
line_split[2] + " " +
line_split[3] + " " +
line_split[4] + '\n')
pass
if __name__ == "__main__":
txtPath = "F:/bishe/project/yolov5-mangren/VOCdevkit/labels2change/val/"
makexml(txtPath)