获取labelimg标注真实框的宽高、归一化数据再存入列表当中
作者:互联网
问题:目标检测往往需要聚类anchors,借助标注的真实框信息,怎么才能将可视化呢?
解决思路:获取labelimg标注真实框的宽高、归一化数据再存入列表当中即可。
方法:读取解析xml文件,比如Annotations文件夹下的xml文件(本博客以该方法讲解);txt文本数据挖掘,比如(2007_train.txt)
话不多说,直接上菜:
import csv
import numpy as np
import glob
import xml.etree.ElementTree as ET
import matplotlib.pyplot as plt
import pandas as pd
import math
datas = []
# 对于每一个xml都寻找box
#获取labelimg标注真实框的宽高并归一化存入列表代码
path = r'VOCdevkit/VOC2007/Annotations'
for xml_file in glob.glob('{}/*xml'.format(path)):
tree = ET.parse(xml_file)
height = int(tree.findtext('./size/height'))
width = int(tree.findtext('./size/width'))
if height <= 0 or width <= 0:
continue
# 对于每一个目标都获得它的宽高
for obj in tree.iter('object'):
xmin = int(float(obj.findtext('bndbox/xmin'))) / width
ymin = int(float(obj.findtext('bndbox/ymin'))) / height
xmax = int(float(obj.findtext('bndbox/xmax'))) / width
ymax = int(float(obj.findtext('bndbox/ymax'))) / height
xmin = np.float64(xmin)
ymin = np.float64(ymin)
xmax = np.float64(xmax)
ymax = np.float64(ymax)
# 得到宽高
#取小数点后两位
a=xmax - xmin
b=ymax - ymin
c=2
a=math.floor(a * 10 ** c) / (10 ** c)
b=math.floor(b * 10 ** c) / (10 ** c)
#datas.append([xmax - xmin, ymax - ymin])
datas.append([a, b]
#print(datas)
"""
#获取labelimg标注真实框的宽高后直接存入列表代码
import numpy as np
from PIL import Image
import os
import glob
import random
import xml.etree.ElementTree as ET
import matplotlib.pyplot as plt
import pandas as pd
datas = []
# 对于每一个xml都寻找box
path = r'VOCdevkit/VOC2007/Annotations'
for xml_file in glob.glob('{}/*xml'.format(path)):
tree = ET.parse(xml_file)
height = int(tree.findtext('./size/height'))
width = int(tree.findtext('./size/width'))
if height <= 0 or width <= 0:
continue
# 对于每一个目标都获得它的宽高
for obj in tree.iter('object'):
xmin = int(float(obj.findtext('bndbox/xmin')))
ymin = int(float(obj.findtext('bndbox/ymin')))
xmax = int(float(obj.findtext('bndbox/xmax')))
ymax = int(float(obj.findtext('bndbox/ymax')))
xmin = np.int32(xmin)
ymin = np.int32(ymin)
xmax = np.int32(xmax)
ymax = np.int32(ymax)
# 得到宽高
datas.append([xmax - xmin, ymax - ymin])
#print(datas)
"""
归一化后结果:
未归一化结果:
后期将介绍采用二维列表转csv格式,将真实框信息可视化。
标签:xml,glob,归一化,height,labelimg,import,标注 来源: https://blog.csdn.net/wuzhihuaw/article/details/123639061