两种不同风格的lxml标注文件的解析:pet和Lara_UrbanSeq1_Traffic Light

1. pet数据集标注样式

以Abyssinian_12.xml为例,文件内容如下:

<annotation>
	<folder>OXIIIT</folder>
	<filename>Abyssinian_12.jpg</filename>
	<source>
		<database>OXFORD-IIIT Pet Dataset</database>
		<annotation>OXIIIT</annotation>
		<image>flickr</image>
	</source>
	<size>
		<width>335</width>
		<height>500</height>
		<depth>3</depth>
	</size>
	<segmented>0</segmented>
	<object>
		<name>cat</name>
		<pose>Frontal</pose>
		<truncated>0</truncated>
		<occluded>0</occluded>
		<bndbox>
			<xmin>94</xmin>
			<ymin>83</ymin>
			<xmax>211</xmax>
			<ymax>190</ymax>
		</bndbox>
		<difficult>0</difficult>
	</object>
</annotation>

分析可知,其节点全部为tag:text形式,每个tag不包含attrib。因此,参照Object Detection API官方,采用以下方式来进行递归读取,返回一个包含多层级字典结构的数据。

import numpy as np
import PIL.Image
import tensorflow as tf
from lxml import etree

from object_detection.dataset_tools import tf_record_creation_util
from object_detection.utils import dataset_util
from object_detection.utils import label_map_util

xml_path = "./Annotations/Abyssinian_12.xml"
# xml_path = "./Annotations/Lara_test.xml"

with tf.gfile.GFile(xml_path, 'r') as fid:
    xml_str = fid.read()
    xml = etree.fromstring(xml_str)
#     xml = etree.fromstring(xml_str.encode('utf-8'))
    data = dataset_util.recursive_parse_xml_to_dict(xml)['annotation']
#     data = dataset_util.recursive_parse_xml_to_dict(xml)
    print(data)
#     for item in data:
#         print(type(item))

其中调用的函数recursive_parse_xml_to_dict(xml)如下:

def recursive_parse_xml_to_dict(xml):
  """Recursively parses XML contents to python dict.

  We assume that `object` tags are the only ones that can appear
  multiple times at the same level of a tree.

  Args:
    xml: xml tree obtained by parsing XML file contents using lxml.etree

  Returns:
    Python dictionary holding XML contents.
  """
  if not xml:
    return {xml.tag: xml.text}
  result = {}
  for child in xml:
    child_result = recursive_parse_xml_to_dict(child)
    if child.tag != 'object':
      result[child.tag] = child_result[child.tag]
    else:
      if child.tag not in result:
        result[child.tag] = []
      result[child.tag].append(child_result[child.tag])
  return {xml.tag: result}

2. Lara标注样式

Lara交通标志数据集的标注文件将所有的图片文件信息整合在一个文件中,截取一段如下:

<?xml version="1.0" encoding="UTF-8"?>
<dataset name="Lara_UrbanSeq1" version="0.5" comments="Public database: http://www.lara.prd.fr/benchmarks/trafficlightsrecognition">
    <frame number="6695" sec="487" ms="829">
        <objectlist>
            <object id="18">
                <orientation>90</orientation>
                <box h="39" w="18" xc="294" yc="34"/>
                <appearance>appear</appearance>
                <hypothesislist>
                    <hypothesis evaluation="1.0" id="1" prev="1.0">
                        <type evaluation="1.0">Traffic Light</type>
                        <subtype evaluation="1.0">go</subtype>
                    </hypothesis>
                </hypothesislist>
            </object>
            <object id="19">
                <orientation>90</orientation>
                <box h="15" w="6" xc="518" yc="123"/>
                <appearance>appear</appearance>
                <hypothesislist>
                    <hypothesis evaluation="1.0" id="1" prev="1.0">
                        <type evaluation="1.0">Traffic Light</type>
                        <subtype evaluation="1.0">go</subtype>
                    </hypothesis>
                </hypothesislist>
            </object>
            <object id="20">
                <orientation>90</orientation>
                <box h="15" w="6" xc="382" yc="122"/>
                <appearance>appear</appearance>
                <hypothesislist>
                    <hypothesis evaluation="1.0" id="1" prev="1.0">
                        <type evaluation="1.0">Traffic Light</type>
                        <subtype evaluation="1.0">go</subtype>
                    </hypothesis>
                </hypothesislist>
            </object>
        </objectlist>
        <grouplist>
		</grouplist>
    </frame>
</dataset>

可见其主要信息都包含在tag:attrib中,是难以用递归函数来实现解析的。
对该文件进行单独测试如下:

# 测试解析xml文件
# examples_path = os.path.join(annotations_dir, 'trainval.txt')
# examples_list = dataset_util.read_examples_list(examples_path)
# xml_path = "./Annotations/Lara_UrbanSeq1_GroundTruth_cvml.xml"
# tree = ET.parse(xml_path)
# root = tree.getroot()
# print(root.tag)
# print(root.attrib)
# print(root[11178].tag)
# print(root[11178].attrib)
# print(root[11178][0][0].tag)
# print(root[11178][0][0].attrib)
# for frame in root.findall("./frame")
# for obj in root[11178][0][0]:
#     print(obj.attrib)
#     print(obj.tag)

主要实现代码如下:

# 从xml文件解析出数据,以list形式返回。每个list的item都是包含相关信息的一个dict
def get_data_list(xml_path, label_map_dict):
    """
    Function: parse xml to a list of image data, every item contain a dict of image name, size, and a list of objects.
    Args:
        xml_path: the path to the xml file
    Returns:
        data_list: a list of data, every data is a dict contain keys.
        {   'filename': 'frame_006630.jpg', 
            'size':    {'width': 640, 'height': 480}, 
            'object':  [ {'bndbox': {'xmin': 368, 'xmax': 378, 'ymin': 94, 'ymax': 116}}, 
                         {'bndbox': {'xmin': 563, 'xmax': 571, 'ymin': 103, 'ymax': 123}}]
        }
    """
    tree = ET.parse(xml_path)
    root = tree.getroot()
    data_list = []
    for frame in root.findall('./frame'):
        frame_number = int(frame.get("number"))
        img_name = "frame_{0:06d}.jpg".format(frame_number) # 得到第一个字段,文件名
        
        data = dict()
        data['filename']=img_name
        
        img_size = dict()
        img_size['width']=640
        img_size['height']=480
        data['size']=img_size
        
        object_list=[]
        data['object']=object_list
        for obj in frame.findall('./objectlist/object'): # 得到该帧里的每个object
            object = dict()
            # 这里待验证。暂时仍用读到的字符串,而没有转换为数字
            class_name = obj.find('./hypothesislist/hypothesis/subtype').text
#             classes_text.append(class_name.encode('utf-8'))
#             classes.append(label_map_dict[class_name])
            object['class_text'] = class_name
            object['class_id'] = label_map_dict[class_name]
            
            obj_h = int(obj.find('box').get("h"))    
            obj_w = int(obj.find('box').get("w"))
            obj_xc = int(obj.find('box').get("xc"))
            obj_yc = int(obj.find('box').get("yc"))
            
            xmin = obj_xc-int(obj_w//2)
            if xmin<0:
                xmin=0
                
            xmax = obj_xc+int(obj_w//2)
            
            ymin = obj_yc-int(obj_h//2)
            if ymin<0:
                ymin=0
                
            ymax = obj_yc+int(obj_h//2)
                        
            bndbox = dict()            
            bndbox['xmin'] = xmin
            bndbox['xmax'] = xmax
            bndbox['ymin'] = ymin
            bndbox['ymax'] = ymax
            object['bndbox'] = bndbox
            object_list.append(object)
        data_list.append(data)
    return data_list

3. 主要对比

前者使用lxml.etree,后者使用xml.etree.ElementTree。解析过程不同。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值