统计xml文件包含的标注信息 parse_dataset_annotation

统计xml文件包含的标注信息 parse_dataset_annotation

YOLO算法,统计标注的xml文件中包含的标记框信息

创建parse_dataset_annotation.py

import os
import sys
filedir = os.path.dirname(sys.argv[0])      #获取脚本所在目录
os.chdir(filedir)       #将脚本所在的目录设置为工作目录
wdir = os.getcwd()
print('当前工作目录:{}\n'.format(wdir))      #打印当前工作目录

from xml.dom.minidom import parse

def xml_parser( xml_file ):
    '''
    Parse an xml file and return the annotation info in the file

    :param xml_file: the xml file name to be parsed
    :return: file_name, width, height, objects.
        file_name, filename of the xml file (without extension)
        width, width of the annotated image
        height, height of the annotated image
        objects, annotated objects in the image
        object, (object_name, xmin, ymin, xmax, ymax)
            object_name, name of the annotated object
            xmin, ymin, xmax, ymax, coordinate of the bounding box of the object

    '''
    DOMTree = parse( xml_file )
    collection = DOMTree.documentElement #得到xml文件的根节点
    file_name_xml = collection.getElementsByTagName( 'filename' )[0]
    objects_xml = collection.getElementsByTagName( 'object' )
    size_xml = collection.getElementsByTagName( 'size' )

    file_name = file_name_xml.childNodes[0].data

    for size in size_xml:
        width = size.getElementsByTagName( 'width' )[0]
        height = size.getElementsByTagName( 'height' )[0]

        width = width.childNodes[0].data
        height = height.childNodes[0].data

    objects = []
    for object_xml in objects_xml:
        object_name = object_xml.getElementsByTagName( 'name' )[0]
        bdbox = object_xml.getElementsByTagName( 'bndbox' )[0]
        xmin = bdbox.getElementsByTagName( 'xmin' )[0]
        ymin = bdbox.getElementsByTagName( 'ymin' )[0]
        xmax = bdbox.getElementsByTagName( 'xmax' )[0]
        ymax = bdbox.getElementsByTagName( 'ymax' )[0]

        object = [ object_name.childNodes[0].data,
                   float(xmin.childNodes[0].data),
                   float(ymin.childNodes[0].data),
                   float(xmax.childNodes[0].data),
                   float(ymax.childNodes[0].data) ]

        objects.append( object )

    return file_name, int(width), int(height), objects
    
image_dir = 'images'    
xml_dir = 'labels'
xml_files = os.listdir(xml_dir)
image_files = os.listdir(image_dir)
image_ext = image_files[0].split('.')[-1] #图片文件的扩展名
print(image_ext)
if len(image_files) == len(xml_files):
    print('共有{:d}个xml文件。'.format(len(xml_files)))
else:
    print('图片数量和xml文件数量不一致。')
obj_dict = {}
for xml_file in xml_files:
    annotation = xml_parser(os.path.join(xml_dir, xml_file))
    name_1 = xml_file.split('.')[0] + '.' + image_ext.lower()
    name_2 = xml_file.split('.')[0] + '.' + image_ext.upper()
    if  name_1 not in image_files and name_2 not in image_files:
        print('{:s}没有对应的图片。'.format(xml_file))
    for obj in annotation[-1]:
        key = obj[0]
        x = (obj[1] + obj[3])/2
        y = (obj[2] + obj[4])/2
        width = obj[3] - obj[1]
        height = obj[4] - obj[2]
        box = [x,y,width,height]
        if key in obj_dict:
            obj_dict[key][0] += 1
            n = obj_dict[key][0]
            obj_dict[key][1:5] = [ (i*(n-1)+j)/(n) for i,j in zip(obj_dict[key][1:5] , box)]
            #obj_dict[key][5:9] = [ i if i>=j else j for i,j in zip(obj_dict[key][5:9] , box)]
            #obj_dict[key][9:] = [ i if i>=j else j for i,j in zip(obj_dict[key][9:] , box)]
        else:
            obj_dict[key] = []
            obj_dict[key].append(1)  # 0,个数
            obj_dict[key] += box     # 1-4, 平均坐标
            #obj_dict[key] += box     # 5-8, 最大值
            #obj_dict[key] += box     # 9-12,最小值
for key,value in obj_dict.items():
    print('一共有 {:4d} 个 {:20s},其边框平均位置为{:4.0f} *{:4.0f};平均尺寸为{:3.0f} *{:3.0f}。'.format(value[0],key,*value[1:]))
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/2159.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 2015第14周一

    2015第14周一

    2021年9月4日
    45
  • java 遍历map集合_Java遍历Map对象的四种方式

    关于java中遍历map具体哪四种方式,请看下文详解吧。方式一:这是最常见的并且在大多数情况下也是最可取的遍历方式。在键值都需要时使用。Mapmap=newHashMap();for(Map.Entryentry:map.entrySet()){System.out.println(“Key=”+entry.getKey()+”,Value=”+entry.g…

    2022年4月7日
    38
  • 什么是UDP攻击_机器人打电话营销效果

    什么是UDP攻击_机器人打电话营销效果UDP协议UDP是一个简单的面向数据报的运输层协议,也是最常见的作为流量攻击最多的一种协议,需要用到UDP的主要都是视频通讯,枪战类实时通讯的游戏类。UDP不提供可靠性,它只是把应用程序传给IP层的数据报发送出去,但并不保证它们能到达目的地。由于UDP传输数据前传输数据之前源端和终端不建立连接,且没有超时重发等机制,故而传输速度很快。UDP攻击UDP攻击是DDoS攻击的一种,是典型的流量型攻击。就好比学校中午放学时的食堂,学生大量飞奔食堂,但食堂窗口就那么多,学生数量太多,就只能挤在窗口前等待。

    2022年10月2日
    0
  • 疫情数据可视化_全国疫情数据可视化项目

    疫情数据可视化_全国疫情数据可视化项目###1.作业描述这个作业属于哪个课程课程结对学号221701225,221701208这个作业要求在哪里作业要求这个作业的目标完成需求分析,原型设计等开发前期任务作业正文本文其他参考文献echarts官方文档、Axure中使用echarts图表、Axure使用教程2.客户需求用户需求在主界面—全国地图中在全国地图上使用不同的…

    2022年9月27日
    0
  • convert dynamic命令在win10不可用_对目标文件系统,文件win7.gho过大

    convert dynamic命令在win10不可用_对目标文件系统,文件win7.gho过大全平台通过Termius,你可以安卓、Windows、macOS、Linux下来连接你的服务器,并且会在不同设备间同步,电脑做着做着要离开,换手机接着做官网链接:界面UI非常细腻win上面有xsheel,个人感觉xsheel更加强大。但是他不做Mac版本。由于在Mac上面用过Termius,所以习惯了他的界面。想要在win上面也用Termius汉化:手动方式打开当前这个js文件目录下面的这个js文件。然后全局搜索进行修改,但是一个一个改感觉麻烦创建一个termius.ini的文件,把下面这些复制进去

    2025年7月21日
    0
  • tail 命令详解

    tail 命令详解一、tail命令介绍tail命令可以将文件指定位置到文件结束的内容写到标准输出。如果你不知道tail命令怎样使用,可以在命令行执行命令tail–help就能看到tail命令介绍和详细的参数使用介绍,内容如下(我帮大家翻译了一下)。[root@yanggongzi~]#tail–helpUsage:tail[OPTION]…[FILE]…Printthelast10linesofeachFILEtostandardoutput.Withmore

    2022年6月4日
    42

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号