List转csv、真实框标注数据可视化操作

问题:先前博客有提到获取labelimg标注真实框的宽高、归一化数据存入列表当中之后,怎么用plt模块将其表示出来?
解决:将list转csv之后,再结合matplotlib将其用坐标表示出来
话不多说:
步骤一获取csv文件,更改自己路径运行如下代码即可(推荐用debug模式运行)

import csv
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
import math

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'))) / 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)
#x=xmax - xmin
#y=ymax - ymin
#print(x)
#print(y)
#print(len(datas))

name_attribute = ['width', 'height']
# writerCSV=pd.DataFrame(columns=name_attribute,data=data)
# writerCSV.to_csv('./no_fre.csv',encoding='utf-8')

csvFile = open('./no_fre1.csv', "w+")
try:
    writer = csv.writer(csvFile)
    writer.writerow(name_attribute)
    for i in range(len(datas)):
        #for j in range(len(datas[0])):

        writer.writerow(datas[i])
finally:
    csvFile.close()


csv结果:List转csv、真实框标注数据可视化操作_第1张图片

步骤二:注释转csv格式代码,调用matplotlib模块的plt.scatter()画出散点图

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
import math
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'))) / 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)
#x=xmax - xmin
#y=ymax - ymin
#print(x)
#print(y)
#print(len(datas))
#将其注释,不然有报错
"""
name_attribute = ['width', 'height']
# writerCSV=pd.DataFrame(columns=name_attribute,data=data)
# writerCSV.to_csv('./no_fre.csv',encoding='utf-8')

csvFile = open('./no_fre1.csv', "w+")
try:
    writer = csv.writer(csvFile)
    writer.writerow(name_attribute)
    for i in range(len(datas)):
        #for j in range(len(datas[0])):

        writer.writerow(datas[i])
finally:
    csvFile.close()
"""
#x:y=1:1,放大5倍,且画出中间平分线
plt.figure(figsize=(5,5))
x = [0,0.2,0.4,0.6,0.8,1.0]
plt.plot(x, x)
hw=pd.read_csv('no_fre1.csv')#导入csv文件
#s为画散点的粗细,可自行调整;color参数可以换比如:"yollew","grey","red"
plt.scatter(hw['width'], hw['height'],s=6,color="blue")#s指的是点的面积
#注意这里x轴命名为"width",y轴为"height",与下面xlabel,ylabel的命名要对应
plt.xlabel(u"width")
plt.ylabel(u"height")
#画出散点图
plt.grid()  # 网格线显示
plt.show()
#将散点图显示出来
plt.savefig('point.png')


效果展示:
List转csv、真实框标注数据可视化操作_第2张图片
希望能给到大家帮助,引用请注明出处。

你可能感兴趣的:(DL,目标检测)