【目标检测】修改YOLO标注索引,批量修改txt文件指定内容

假设需要将原索引“0,1,2,3”都修改为“0”:

import os

path = 'train'
total_txt = os.listdir(path)
deleteList = []

for file in total_txt:
    fileName = path + '/' + file
    file = open(fileName, 'r')
    lines = file.readlines()

    for index, line in enumerate(lines):
        strT = lines[index]

        if strT[0:1] == "0":
            strT = "0 "+strT[2:]
            lines[index] = strT
        elif strT[0:1] == "1":
            strT = "0 "+strT[2:]
            lines[index] = strT
        elif strT[0:1] == "2":
            strT = "0 "+strT[2:]
            lines[index] = strT
        elif strT[0:1] == "3":
            strT = "0 "+strT[2:]
            lines[index] = strT

    file.close()

    strT = "".join(lines)

    file = open('new/' + fileName, 'w')
    file.write(strT)
    file.close()

你可能感兴趣的:(深度学习,人工智能)