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结果:在这里插入图片描述

步骤二:注释转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')


效果展示:
在这里插入图片描述
希望能给到大家帮助,引用请注明出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会飞的渔WZH

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值