實用小程式(1)之讀取xml並儲存為txt文字

kkdefeng 發表於 2020-11-22

實用小程式(1)之xml轉txt

在這裡插入圖片描述

這段時間在看學習yolo系列演算法,在訓練自己的訓練集時,需要讀取標註的xml檔案的類別並儲存為txt文字格式,在網上找了很久也沒有發現相關的程式碼,所以就自己寫了一個需要的同學自取啦。

(1)獲取訓練圖片的字首名並儲存為txt

import os
import random

xmlfilepath = 'D:\\project\\windows_yolov3\\train_img\\train_image'  # 這裡寫自己圖片儲存的路徑
txtsavepath = 'D:\\project\\windows_yolov3\\train_img'               #生成的txt文字儲存路徑
total_xml = os.listdir(xmlfilepath)

num = len(total_xml)
list = range(num)
ftrainval = open('name.txt', 'w')

for i in list:
    name = total_xml[i][:-4] + '\n'
    ftrainval.write(name)
ftrainval.close()

(2)讀取標註的xml檔案類別

import xml.dom.minidom as xmldom
import os

names_file_path = os.path.expanduser("./name.txt")
with open(names_file_path) as f:
    name_file = f.readlines()
name_file = [int(x.strip('\n')) for x in name_file]
#print(name_file)
length = len(name_file)
#print(length)
for i in range(length):
    xml_filepath = os.path.abspath("./train_img/train_label/" + str(name_file[i]) +".xml")
    # xml_filepath=os.path.abspath("V5-13.xml")
    # 得到檔案物件
    obj = xmldom.parse(xml_filepath)

    # 得到元素物件
    annotation = obj.documentElement
    objects = annotation.getElementsByTagName("object")
    new_txt_name = 'name_attributions.txt'
    for object in objects:
        name = object.getElementsByTagName('name')[0]
        with open(new_txt_name,'a') as f:
            f.write(name.childNodes[0].data)
            f.write(' ')
            #f.write('\r\n')
        #print(name.childNodes[0].data)

    with open(new_txt_name, 'a') as f:
        f.write('\r\n')

歡迎大家討論交流哇!