最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python中的txt文件轉(zhuǎn)換為XML

 更新時(shí)間:2022年12月14日 17:11:34   作者:LGDDDDDD  
這篇文章主要介紹了python中的txt文件轉(zhuǎn)換為XML問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

txt文件轉(zhuǎn)換為XML

很多目標(biāo)檢測(cè)的模型都是默認(rèn)需要VOC的文件輸入格式

手上數(shù)據(jù)label是txt文件。

為了避免不必要的bug,還是選擇轉(zhuǎn)換下格式

將數(shù)據(jù)按VOC形式放置

文件夾內(nèi)容
Annotations存放生成的XML文件
JPEGImagesJPG圖片
ImageSets標(biāo)明訓(xùn)練集測(cè)試集的txt文件
Labelsstxt格式的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)

不過(guò)代碼只使用于每個(gè)label文件只有一個(gè)標(biāo)注框,可在生成bndbox節(jié)點(diǎn)處加入循環(huán)

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python 3 使用Pillow生成漂亮的分形樹圖片

    Python 3 使用Pillow生成漂亮的分形樹圖片

    這篇文章主要介紹了Python 3 使用Pillow生成漂亮的分形樹圖片,本文通過(guò)實(shí)例代碼介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-12-12
  • 深度學(xué)習(xí)詳解之初試機(jī)器學(xué)習(xí)

    深度學(xué)習(xí)詳解之初試機(jī)器學(xué)習(xí)

    機(jī)器學(xué)習(xí)可應(yīng)用在各個(gè)方面,本篇將在系統(tǒng)性進(jìn)入機(jī)器學(xué)習(xí)方向前,初步認(rèn)識(shí)機(jī)器學(xué)習(xí),利用線性回歸預(yù)測(cè)波士頓房?jī)r(jià),讓我們一起來(lái)看看吧
    2021-04-04
  • Python的三種主要模塊介紹

    Python的三種主要模塊介紹

    這篇文章介紹了Python的三類主要模塊,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • 如何使用matplotlib讓你的數(shù)據(jù)更加生動(dòng)

    如何使用matplotlib讓你的數(shù)據(jù)更加生動(dòng)

    數(shù)據(jù)可視化用于以更直接的表示方式顯示數(shù)據(jù),并且更易于理解,下面這篇文章主要給大家介紹了關(guān)于如何使用matplotlib讓你的數(shù)據(jù)更加生動(dòng)的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • Python裝飾器語(yǔ)法糖

    Python裝飾器語(yǔ)法糖

    今天小編就為大家分享一篇關(guān)于Python裝飾器語(yǔ)法糖,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • Python Django框架url反向解析實(shí)現(xiàn)動(dòng)態(tài)生成對(duì)應(yīng)的url鏈接示例

    Python Django框架url反向解析實(shí)現(xiàn)動(dòng)態(tài)生成對(duì)應(yīng)的url鏈接示例

    這篇文章主要介紹了Python Django框架url反向解析實(shí)現(xiàn)動(dòng)態(tài)生成對(duì)應(yīng)的url鏈接,結(jié)合實(shí)例形式分析了Django框架URL反向解析具體原理與應(yīng)用操作技巧,需要的朋友可以參考下
    2019-10-10
  • python遠(yuǎn)程登錄代碼

    python遠(yuǎn)程登錄代碼

    因?yàn)?python 已內(nèi)建了一個(gè) pop3 的函式庫(kù),所以我們直接用它來(lái)完成郵件的下載和處理。事實(shí)上, 如果我們不用 poplib 的話,我們還是可以完成那個(gè)例子中的所有作業(yè):就是通過(guò)模擬 telnet的協(xié)定。
    2008-04-04
  • Python常用的模塊和簡(jiǎn)單用法

    Python常用的模塊和簡(jiǎn)單用法

    這篇文章主要給大家介紹Python#常用的模塊和簡(jiǎn)單用法,以random 隨機(jī)模塊展開話題,感興趣的小伙伴可以參考一下
    2021-10-10
  • 五個(gè)Pandas?實(shí)戰(zhàn)案例帶你分析操作數(shù)據(jù)

    五個(gè)Pandas?實(shí)戰(zhàn)案例帶你分析操作數(shù)據(jù)

    pandas是基于NumPy的一種工具,該工具是為了解決數(shù)據(jù)分析任務(wù)而創(chuàng)建的。Pandas納入了大量庫(kù)和一些標(biāo)準(zhǔn)的數(shù)據(jù)模型,提供了高效操作大型數(shù)據(jù)集的工具。pandas提供大量快速便捷地處理數(shù)據(jù)的函數(shù)和方法。你很快就會(huì)發(fā)現(xiàn),它是使Python強(qiáng)大而高效的數(shù)據(jù)分析環(huán)境的重要因素之一
    2022-01-01
  • Python struct模塊解析

    Python struct模塊解析

    我們知道python只定義了6種數(shù)據(jù)類型,字符串,整數(shù),浮點(diǎn)數(shù),列表,元組,字典。但是C語(yǔ)言中有些字節(jié)型的變量,在python中該如何實(shí)現(xiàn)呢?這點(diǎn)頗為重要,特別是要在網(wǎng)絡(luò)上進(jìn)行數(shù)據(jù)傳輸?shù)脑挕?/div> 2014-06-06

最新評(píng)論

滦平县| 泽普县| 肇东市| 丰县| 泰安市| 徐汇区| 普洱| 清镇市| 长岭县| 开江县| 习水县| 长寿区| 镇安县| 拜城县| 岢岚县| 礼泉县| 开江县| 宁城县| 墨脱县| 北安市| 资源县| 郑州市| 苍溪县| 怀仁县| 德安县| 金沙县| 公主岭市| 西贡区| 南华县| 井陉县| 玉山县| 南阳市| 枣强县| 尚志市| 乌拉特前旗| 济南市| 宜兰市| 舞阳县| 红原县| 宜川县| 胶州市|