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

2024-02-12 04:32

本文主要是介绍两种不同风格的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 etreefrom object_detection.dataset_tools import tf_record_creation_util
from object_detection.utils import dataset_util
from object_detection.utils import label_map_utilxml_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 appearmultiple times at the same level of a tree.Args:xml: xml tree obtained by parsing XML file contents using lxml.etreeReturns: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 fileReturns: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_nameimg_size = dict()img_size['width']=640img_size['height']=480data['size']=img_sizeobject_list=[]data['object']=object_listfor obj in frame.findall('./objectlist/object'): # 得到该帧里的每个objectobject = 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_nameobject['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=0xmax = obj_xc+int(obj_w//2)ymin = obj_yc-int(obj_h//2)if ymin<0:ymin=0ymax = obj_yc+int(obj_h//2)bndbox = dict()            bndbox['xmin'] = xminbndbox['xmax'] = xmaxbndbox['ymin'] = yminbndbox['ymax'] = ymaxobject['bndbox'] = bndboxobject_list.append(object)data_list.append(data)return data_list

3. 主要对比

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

这篇关于两种不同风格的lxml标注文件的解析:pet和Lara_UrbanSeq1_Traffic Light的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/701668

相关文章

全面解析Golang 中的 Gorilla CORS 中间件正确用法

《全面解析Golang中的GorillaCORS中间件正确用法》Golang中使用gorilla/mux路由器配合rs/cors中间件库可以优雅地解决这个问题,然而,很多人刚开始使用时会遇到配... 目录如何让 golang 中的 Gorilla CORS 中间件正确工作一、基础依赖二、错误用法(很多人一开

Mysql中设计数据表的过程解析

《Mysql中设计数据表的过程解析》数据库约束通过NOTNULL、UNIQUE、DEFAULT、主键和外键等规则保障数据完整性,自动校验数据,减少人工错误,提升数据一致性和业务逻辑严谨性,本文介绍My... 目录1.引言2.NOT NULL——制定某列不可以存储NULL值2.UNIQUE——保证某一列的每一

深度解析Nginx日志分析与499状态码问题解决

《深度解析Nginx日志分析与499状态码问题解决》在Web服务器运维和性能优化过程中,Nginx日志是排查问题的重要依据,本文将围绕Nginx日志分析、499状态码的成因、排查方法及解决方案展开讨论... 目录前言1. Nginx日志基础1.1 Nginx日志存放位置1.2 Nginx日志格式2. 499

MySQL CTE (Common Table Expressions)示例全解析

《MySQLCTE(CommonTableExpressions)示例全解析》MySQL8.0引入CTE,支持递归查询,可创建临时命名结果集,提升复杂查询的可读性与维护性,适用于层次结构数据处... 目录基本语法CTE 主要特点非递归 CTE简单 CTE 示例多 CTE 示例递归 CTE基本递归 CTE 结

Spring Boot 3.x 中 WebClient 示例详解析

《SpringBoot3.x中WebClient示例详解析》SpringBoot3.x中WebClient是响应式HTTP客户端,替代RestTemplate,支持异步非阻塞请求,涵盖GET... 目录Spring Boot 3.x 中 WebClient 全面详解及示例1. WebClient 简介2.

在MySQL中实现冷热数据分离的方法及使用场景底层原理解析

《在MySQL中实现冷热数据分离的方法及使用场景底层原理解析》MySQL冷热数据分离通过分表/分区策略、数据归档和索引优化,将频繁访问的热数据与冷数据分开存储,提升查询效率并降低存储成本,适用于高并发... 目录实现冷热数据分离1. 分表策略2. 使用分区表3. 数据归档与迁移在mysql中实现冷热数据分

C#解析JSON数据全攻略指南

《C#解析JSON数据全攻略指南》这篇文章主要为大家详细介绍了使用C#解析JSON数据全攻略指南,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、为什么jsON是C#开发必修课?二、四步搞定网络JSON数据1. 获取数据 - HttpClient最佳实践2. 动态解析 - 快速

Spring Boot3.0新特性全面解析与应用实战

《SpringBoot3.0新特性全面解析与应用实战》SpringBoot3.0作为Spring生态系统的一个重要里程碑,带来了众多令人兴奋的新特性和改进,本文将深入解析SpringBoot3.0的... 目录核心变化概览Java版本要求提升迁移至Jakarta EE重要新特性详解1. Native Ima

spring中的@MapperScan注解属性解析

《spring中的@MapperScan注解属性解析》@MapperScan是Spring集成MyBatis时自动扫描Mapper接口的注解,简化配置并支持多数据源,通过属性控制扫描路径和过滤条件,利... 目录一、核心功能与作用二、注解属性解析三、底层实现原理四、使用场景与最佳实践五、注意事项与常见问题六

nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析(结合应用场景)

《nginx-t、nginx-sstop和nginx-sreload命令的详细解析(结合应用场景)》本文解析Nginx的-t、-sstop、-sreload命令,分别用于配置语法检... 以下是关于 nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析,结合实际应