提取Insight-MVT_Annotation_Train 数据集标签xml文件中的信息

来源:互联网 发布:linux python27 pyqt5 编辑:程序博客网 时间:2024/05/16 12:30

Insight-MVT_Annotation_Train  数据集标签xml文件中的信息

xml文件中解析出所要的信息  type  height  width  top  left  写成Pascaltxt文本格式

Pascal voc  的信息是left     top         right           bottom  

对应TFrecord的    xmin    ymin        xmax           yman

对应              left      top       top+height      left+width

 

代码:

#!/usr/bin/python

# -*- coding: UTF-8 -*-

 

from xml.dom.minidom import parse

import xml.dom.minidom

from xml.dom.minidom import Document

import os

import matplotlib.image as mpimg

#使用minidom解析器打开XML

 

 

if __name__=='__main__':

 

   DOMTree = xml.dom.minidom.parse('MVI_20033.xml')

   collection = DOMTree.documentElement

   path='F:\DETRAC-Train-Annotations-XML\Labels\Labels'

   frames = collection.getElementsByTagName("frame")

 

   id=0

   for frame in frames:

       print("*****MVT*****")

       if frame.hasAttribute("num"):

           print("num: %s" % frame.getAttribute("num"))

       density = frame.getAttribute("density")

       ne = frame.getElementsByTagName("target_list")

       targets = ne[0].getElementsByTagName("target")

 

       txtname=str(id).zfill(6)

       print(txtname)

       id=id+1

       filepath=os.path.join(path,txtname+'.txt')

       f=open(filepath,'w')

       for target in targets:

           print("id: %s" % target.getAttribute("id"))

           box = target.getElementsByTagName("box")

           attribute = target.getElementsByTagName("attribute")

           type = attribute[0].getAttribute("vehicle_type")

           print("type: %s" % type)

           left = box[0].getAttribute("left")

           top = box[0].getAttribute("top")

           width = box[0].getAttribute("width")

           height = box[0].getAttribute("height")

           xmin = left

           ymin = top

           xmax = str(round(float(xmin) + float(height), 1))

           ymax = str(round(float(ymin) + float(width), 1))

           type = 'car'

           text = type + ' ' + xmin + ' ' + ymin + ' ' + xmax + ' ' + ymax

           print("left: %s" % text)

           f.write(text+'\n')

       f.close()

原创粉丝点击