資源簡介
壓縮文件中對的代碼UA-DETRAC數據集中的xml進行了轉換,轉換成標準的voc中xml格式。代碼運行無誤

代碼片段和文件信息
import?xml.etree.ElementTree?as?ET
from?xml.dom.minidom?import?Document
import?os
import?cv2
import?time
def?ConvertVOCxml(file_path=““?file_name=““):
????tree?=?ET.parse(file_name)
????root?=?tree.getroot()
????#?print(root.tag)
????num?=?0??#?計數
????#?讀xml操作
????frame_lists?=?[]
????output_file_name?=?““
????for?child?in?root:
????????if?(child.tag?==?“frame“):
????????????#?創建dom文檔
????????????doc?=?Document()
????????????#?創建根節點
????????????annotation?=?doc.createElement(‘annotation‘)
????????????#?根節點插入dom樹
????????????doc.appendChild(annotation)
????????????#?print(child.tag?child.attrib[“num“])
????????????pic_id?=?child.attrib[“num“].zfill(5)
????????????#?print(pic_id)
????????????output_file_name?=?root.attrib[“name“]?+?“__img“?+?pic_id?+?“.xml“
????????????#??print(output_file_name)
????????????folder?=?doc.createElement(“folder“)
????????????folder.appendChild(doc.createTextNode(“VOC2007“))
????????????annotation.appendChild(folder)
????????????filename?=?doc.createElement(“filename“)
????????????pic_name?=?“img“?+?pic_id?+?“.jpg“
????????????filename.appendChild(doc.createTextNode(pic_name))
????????????annotation.appendChild(filename)
????????????sizeimage?=?doc.createElement(“size“)
????????????imagewidth?=?doc.createElement(“width“)
????????????imageheight?=?doc.createElement(“height“)
????????????imagedepth?=?doc.createElement(“depth“)
????????????imagewidth.appendChild(doc.createTextNode(“960“))
????????????imageheight.appendChild(doc.createTextNode(“540“))
????????????imagedepth.appendChild(doc.createTextNode(“3“))
????????????sizeimage.appendChild(imagedepth)
????????????sizeimage.appendChild(imagewidth)
????????????sizeimage.appendChild(imageheight)
????????????annotation.appendChild(sizeimage)
????????????target_list?=?child.getchildren()[0]??#?獲取target_list
????????????#?print(target_list.tag)
????????????object?=?None
????????????for?target?in?target_list:
????????????????if?(target.tag?==?“target“):
????????????????????#?print(target.tag)
????????????????????object?=?doc.createElement(‘object‘)
????????????????????bndbox?=?doc.createElement(“bndbox“)
????????????????????for?target_child?in?target:
????????????????????????if?(target_child.tag?==?“box“):
????????????????????????????xmin?=?doc.createElement(“xmin“)
????????????????????????????ymin?=?doc.createElement(“ymin“)
????????????????????????????xmax?=?doc.createElement(“xmax“)
????????????????????????????ymax?=?doc.createElement(“ymax“)
????????????????????????????xmin_value?=?int(float(target_child.attrib[“left“]))
????????????????????????????ymin_value?=?int(float(target_child.attrib[“top“]))
????????????????????????????box_width_value?=?int(float(target_child.attrib[“width“]))
????????????????????????????box_height_value?=?int(float(target_child.attrib[“height“]))
????????????????????????????xmin.appendChild(doc.createTextNode(str(xmin_value)))
????????????????????????????ym
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????????930??2019-11-08?10:05??voc_data_migrate.py
?????文件????????8732??2019-11-09?11:51??covert.py
- 上一篇:Packet tracer 設計 校園網
- 下一篇:由大斷面計算水位流量關系.zip
評論
共有 條評論