您好,登錄后才能下訂單哦!
這篇文章主要介紹了python中的txt文件怎么轉換為XML的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇python中的txt文件怎么轉換為XML文章都會有所收獲,下面我們一起來看看吧。
很多目標檢測的模型都是默認需要VOC的文件輸入格式
手上數據label是txt文件。
為了避免不必要的bug,還是選擇轉換下格式
文件夾 | 內容 |
---|---|
Annotations | 存放生成的XML文件 |
JPEGImages | JPG圖片 |
ImageSets | 標明訓練集測試集的txt文件 |
Labelss | txt格式的Label文件 |
# -*- coding: utf-8 -*- from xml.dom.minidom import Document import os import os.path from PIL import Image import importlib import sys importlib.reload(sys) xml_path = "Annotations\\" img_path = "JPEGImages\\" ann_path = "Labelss\\" if not os.path.exists(xml_path): os.mkdir(xml_path) def writeXml(tmp, imgname, w, h, objbud, wxml): doc = Document() # owner annotation = doc.createElement('annotation') doc.appendChild(annotation) # owner folder = doc.createElement('folder') annotation.appendChild(folder) folder_txt = doc.createTextNode("VOC2007") folder.appendChild(folder_txt) filename = doc.createElement('filename') annotation.appendChild(filename) filename_txt = doc.createTextNode(imgname) filename.appendChild(filename_txt) # ones# source = doc.createElement('source') annotation.appendChild(source) database = doc.createElement('database') source.appendChild(database) database_txt = doc.createTextNode("The VOC2007 Database") database.appendChild(database_txt) annotation_new = doc.createElement('annotation') source.appendChild(annotation_new) annotation_new_txt = doc.createTextNode("PASCAL VOC2007 ") annotation_new.appendChild(annotation_new_txt) image = doc.createElement('image') source.appendChild(image) image_txt = doc.createTextNode("flickr") image.appendChild(image_txt) # onee# # twos# size = doc.createElement('size') annotation.appendChild(size) width = doc.createElement('width') size.appendChild(width) width_txt = doc.createTextNode(str(w)) width.appendChild(width_txt) height = doc.createElement('height') size.appendChild(height) height_txt = doc.createTextNode(str(h)) height.appendChild(height_txt) depth = doc.createElement('depth') size.appendChild(depth) depth_txt = doc.createTextNode("3") depth.appendChild(depth_txt) # twoe# segmented = doc.createElement('segmented') annotation.appendChild(segmented) segmented_txt = doc.createTextNode("0") segmented.appendChild(segmented_txt) # threes# object_new = doc.createElement("object") annotation.appendChild(object_new) name = doc.createElement('name') object_new.appendChild(name) name_txt = doc.createTextNode('cancer') name.appendChild(name_txt) pose = doc.createElement('pose') object_new.appendChild(pose) pose_txt = doc.createTextNode("Unspecified") pose.appendChild(pose_txt) truncated = doc.createElement('truncated') object_new.appendChild(truncated) truncated_txt = doc.createTextNode("0") truncated.appendChild(truncated_txt) difficult = doc.createElement('difficult') object_new.appendChild(difficult) difficult_txt = doc.createTextNode("0") difficult.appendChild(difficult_txt) # threes-1# bndbox = doc.createElement('bndbox') object_new.appendChild(bndbox) xmin = doc.createElement('xmin') bndbox.appendChild(xmin) #objbud存放[類別,xmin,ymin,xmax,ymax] xmin_txt = doc.createTextNode(objbud[1]) xmin.appendChild(xmin_txt) ymin = doc.createElement('ymin') bndbox.appendChild(ymin) ymin_txt = doc.createTextNode(objbud[2]) ymin.appendChild(ymin_txt) xmax = doc.createElement('xmax') bndbox.appendChild(xmax) xmax_txt = doc.createTextNode(objbud[3]) xmax.appendChild(xmax_txt) ymax = doc.createElement('ymax') bndbox.appendChild(ymax) ymax_txt = doc.createTextNode(objbud[4]) ymax.appendChild(ymax_txt) # threee-1# # threee# tempfile = tmp + "test.xml" with open(tempfile, "wb") as f: f.write(doc.toprettyxml(indent="\t", newl="\n", encoding="utf-8")) rewrite = open(tempfile, "r") lines = rewrite.read().split('\n') newlines = lines[1:len(lines) - 1] fw = open(wxml, "w") for i in range(0, len(newlines)): fw.write(newlines[i] + '\n') fw.close() rewrite.close() os.remove(tempfile) return for files in os.walk('E:\ssd_pytorch_cancer\data\cancer_or_not\Labels'): print(files) temp = "/temp/" if not os.path.exists(temp): os.mkdir(temp) for file in files[2]: print(file + "-->start!") img_name = os.path.splitext(file)[0] + '.jpg' fileimgpath = img_path + img_name im = Image.open(fileimgpath) width = int(im.size[0]) height = int(im.size[1]) filelabel = open(ann_path + file, "r") lines = filelabel.read().split(' ') obj = lines[:len(lines)] filename = xml_path + os.path.splitext(file)[0] + '.xml' writeXml(temp, img_name, width, height, obj, filename) os.rmdir(temp)
不過代碼只使用于每個label文件只有一個標注框,可在生成bndbox節點處加入循環
關于“python中的txt文件怎么轉換為XML”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“python中的txt文件怎么轉換為XML”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。