0%

xml文件的数据分析

上篇得到了飞机的tif和对应的xml文件后(xml文件中的object只留下了飞机),我们需要对飞机的信息进行分析汇总,此处对每一类飞机主要想统计的信息有如下:

  • 该类别的图片数量
  • 飞机的最小宽度
  • 飞机的最小高度
  • 飞机最小的宽高比
  • 飞机的最大宽度
  • 飞机的最大高度
  • 飞机最大的宽高比
  • 图片的最小宽度
  • 图片的最小高度
  • 图片的最小深度
  • 图片的最大宽度
  • 图片的最大高度
  • 图片的最大深度

在之前的xml转json格式的博客中,那个read_xml_gtbox_and_label函数中加入如下的信息统计,即多读取一个size,size中三个数分别代表宽度、高度和深度,并将size数据return返回,注意其是字符串格式的。

1
2
3
4
5
6
7
8
9
if child_of_root.tag == 'size':
size = [-1,-1,-1]#[width,height,depth]
for child_item in child_of_root:
if child_item.tag == 'width':
size[0] = child_item.text
if child_item.tag == 'height':
size[1] = child_item.text
if child_item.tag == 'depth':
size[2] = child_item.text

read_xml_gtbox_and_label函数的博客链接如下:

xml格式转json

然后得到返回后,进行信息统计,统计得到的numpy数组进行保存。飞机的长宽以及长宽比,其实是按照矩形框来的,由于给的数据集是四边形框,我将这个框变成了xy方向上的水平矩形框进行计算高和宽。值得注意的是,飞机的尺寸竟然出现了浮点数,不知道是组委会给的xml文件有问题还是有浮点数是正常的,不过坐标是标记像素的位置的,出现浮点数有点不太正常。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'''
统计gf2021中目标大小(影响滑动窗口进行切片),统计飞机的目标大小,统计含括飞机的图片尺寸,把
有飞机的图片另存为,同时另存xml,把xml也修改一下,把非飞机的目标丢弃掉。
'''
import os
import numpy as np
import scipy.misc as misc
from xml.dom.minidom import Document
import numpy as np
import copy, cv2
# import imageio
import json
import glob
import xml.etree.cElementTree as ET
import re
from osgeo import gdal, gdalconst

def information_statistics_save(image_path, xml_path):
plane_data = np.zeros((10,13))#num,min_width,min_height,min_ratio,max_width,max_height,max_ratio。min_pic_width,min_pic_height,min_pic_depth,max_pic_width,max_pic_height,max_pic_depth
initialization=np.array([0,10000,10000,10000,-1,-1,-1,10000,10000,10000,-1,-1,-1])
plane_data = plane_data + initialization #初始化,广播机制
plane_label = ["Boeing737","Boeing747","Boeing777","C919","A220","A321","A330","A350","ARJ21","other-airplane"]
for count, xml in enumerate(glob.glob(xml_path + '/*.xml')):#进行索引,*代表任意匹配,得到可迭代对象
# to avoid path error in different development platform
xml = xml.replace('\\', '/')#用/替换\\,后面的替换前面的
(_, xml_name) = os.path.split(xml)#分割。返回路径和文件名。若本身就是一个文件夹路径,则返回路径和空
#print(xml)
img_name, box , size, difficult_list, tmp_score_list = read_xml_gtbox_and_label(xml)
img_name=xml_name.replace('.xml', '.tif')
img_path = image_path + '/' + img_name

size = np.float32(size)

for box_one_object in box:
if box_one_object[8] in plane_label:
index_label=plane_label.index(box_one_object[8])
#print(index_label)
plane_data[index_label,0]=plane_data[index_label,0]+1#该类型飞机数量加1
box_float=np.float32(box_one_object[0:8])#float只能对单个数使用,数组类的用np.float
x_box=box_float[[0,2,4,6]]
y_box=box_float[[1,3,5,7]]
#print(x_box,y_box)
width = np.max(x_box)-np.min(x_box)
height = np.max(y_box)-np.min(y_box)
if int(width*100) ==758:
print(xml)
if width < plane_data[index_label,1]:
plane_data[index_label,1]=width
if height < plane_data[index_label,2]:
plane_data[index_label,2]=height
if width/height < plane_data[index_label,3]:
plane_data[index_label,3]=width/height
if width > plane_data[index_label,4]:
plane_data[index_label,4]=width
if height > plane_data[index_label,5]:
plane_data[index_label,5]=height
if width/height > plane_data[index_label,6]:
plane_data[index_label,6]=width/height
if size[0] < plane_data[index_label,7]:
plane_data[index_label,7]=size[0]
if size[1] < plane_data[index_label,8]:
plane_data[index_label,8]=size[1]
if size[2] < plane_data[index_label,9]:
plane_data[index_label,9]=size[2]
if size[0] > plane_data[index_label,10]:
plane_data[index_label,10]=size[0]
if size[1] > plane_data[index_label,11]:
plane_data[index_label,11]=size[1]
if size[2] > plane_data[index_label,12]:
plane_data[index_label,12]=size[2]
else:
print("出现错误")


if not os.path.exists(img_path):
print('{} is not exist!'.format(img_path))
continue
# view_bar('Conversion progress', count + 1,
# len(glob.glob(xml_path + '/*.xml')))
print(plane_data)
np.save("plane_data.npy",plane_data)
#b = np.load("filename.npy")
print('\nStatistics is complete!')
return plane_data

if __name__ == "__main__":
image_path=r"/emwuser/gzl/gf2021/data/FAIR1M/train/part1_plane/images"
xml_path=r"/emwuser/gzl/gf2021/data/FAIR1M/train/part1_plane/labelXmls"

information_statistics_save(image_path,xml_path)