1.Introduction

最近目标检测的精度上不去,看看别人的文章,发现可以针对anchor进行参数优化,RPN网络生成的anchor数量与种类很大程度上影响着检测精度,anchor与检测目标越接近,检测精度越高。所以我们就需要统计下自己数据中目标区域的面积和长宽比。

2.Materials and methods

代码思路主要有:

(1)遍历文件夹中的xml文件
(2)定位xmin,xmax,ymin,ymax四个坐标值
(3)计算面积和长宽比,生成列表
(4)统计列表直方图

emm,没啥说的,上代码吧。

# -*- coding: utf-8 -*-
"""
Created on Sun Jan 10 21:48:48 2021@author: YaoYee
"""import os
import xml.etree.cElementTree as et
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdmpath="C:/Users/YaoYee/Desktop/Annotations"
files=os.listdir(path)area_list = []
ratio_list = []def file_extension(path): return os.path.splitext(path)[1] for xmlFile in tqdm(files, desc='Processing'): if not os.path.isdir(xmlFile): if file_extension(xmlFile) == '.xml':tree=et.parse(os.path.join(path,xmlFile))root=tree.getroot()filename=root.find('filename').text# print("--Filename is", xmlFile)for Object in root.findall('object'):bndbox=Object.find('bndbox')xmin=bndbox.find('xmin').textymin=bndbox.find('ymin').textxmax=bndbox.find('xmax').textymax=bndbox.find('ymax').textarea = ( int(ymax)-int(ymin)) * (int(xmax)-int(xmin) )area_list.append(area)# print("Area is", area)ratio = ( int(ymax)-int(ymin)) / (int(xmax)-int(xmin) )ratio_list.append(ratio)# print("Ratio is", round(ratio,2))square_array = np.array(area_list)
square_max = np.max(square_array)
square_min = np.min(square_array)
square_mean = np.mean(square_array)
square_var = np.var(square_array)
plt.figure(1)
plt.hist(square_array,20)
plt.xlabel('Area in pixel')
plt.ylabel('Frequency of area')
plt.title('Area\n' \+'max='+str(square_max)+', min='+str(square_min)+'\n' \+'mean='+str(int(square_mean))+', var='+str(int(square_var)))ratio_array = np.array(ratio_list)
ratio_max = np.max(ratio_array)
ratio_min = np.min(ratio_array)
ratio_mean = np.mean(ratio_array)
ratio_var = np.var(ratio_array)
plt.figure(2)
plt.hist(ratio_array,20)
plt.xlabel('Ratio of length / width')
plt.ylabel('Frequency of ratio')
plt.title('Ratio\n' \+'max='+str(round(ratio_max,2))+', min='+str(round(ratio_min,2))+'\n' \+'mean='+str(round(ratio_mean,2))+', var='+str(round(ratio_var,2)))

3. Results and discussion

运行下看看效果~

最后在生成的图片标题中加入了最值,均值和方差,同时还贴心的配有进度条~

4. Conclusion

好像有点入门Python了~

猜你喜欢:

【Python】计算VOC格式XML文件中目标面积和长宽比并生成直方图相关推荐

  1. 【VOC格式xml文件解析】——Python

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/4/26 12:49 # @Author : @linlianqin # @S ...

  2. python实现批量更改xml文件中内容替换

    import os import io #paths文件目录,files保存文件路径以及名字 paths='/home/xiaobumidm/Yolo_mark-master/VOC数据集/VOC/' ...

  3. python 读取excel格式xml,读取xml格式的xls文件、解析其中数据

    1.python 读取excel格式xml,解析其中数据 当excel文件的格式是xml的时候,window系统是可以正常打开的,但是使用pandas直接读取则会报错,原因就是现在已经是xml文件了, ...

  4. xml文件拆分 python_用Python提取合并由集搜客爬取的多个xml文件中的数据 | 向死而生...

    为了爬点小数据同时试用了八爪鱼和集搜客.两者都有免费版本,但八爪鱼数据导出需要积分,集搜客可以不用积分.不过八爪鱼导出的数据有多种格式可选,而集搜客如果不用积分就只能得到一堆xml文件.本着能省则省的 ...

  5. python中的content方法_content最新:python计算Content-MD5并获取文件的Content-MD5值方式_爱安网 LoveAn.com...

    关于"content"的最新内容 聚合阅读 这篇文章主要介绍了python计算Content-MD5并获取文件的Content-MD5值方式,具有很好的参考价值,希望对大家有所帮助 ...

  6. python提取xml的所有框坐标_python 提取批量xml文件中的坐标信息存入txt文件 xml文件转txt文件...

    读取多个xml文件中的坐标信息:xmin.xmax.ymin.ymax(实际为一个矩形框的坐标值),并通过简单的计算得到矩形框的长.宽,最后按照自己的需求读入到一个txt文档中 xml文件目录及文件如 ...

  7. python 提取批量xml文件中的坐标信息存入txt文件 xml文件转txt文件

    读取多个xml文件中的坐标信息:xmin.xmax.ymin.ymax(实际为一个矩形框的坐标值),并通过简单的计算得到矩形框的长.宽,最后按照自己的需求读入到一个txt文档中 xml文件目录及文件如 ...

  8. 将xml文件中图片格式修改为jpg

    将xml文件中图片格式修改为jpg: 1.只需要修改path路径,到*.xml所在的文件夹,运行之后会把xml 修改为.jpg. import os import os.path import xml ...

  9. (训练三)王朗自然保护区---数据集部分补充说明:计算xml文件里面目标数量   生成训练和测试的txt文件

    1 计算xml文件里面目标数量 # -*- coding:utf-8 -*- import os import xml.etree.ElementTree as ET import numpy as ...

最新文章

  1. 【ZT】我家宝宝不会哭----分享在美国养孩子的妈妈经(必看)
  2. 思科:全球近75%的物联网项目失败
  3. serv-u 自定义html,Serv-U架设教程_Serv-U使用教程图文版
  4. 二叉查找树(二叉排序树)的详细实现
  5. akae-arm9异常
  6. react native 生成APK
  7. 信息学奥赛一本通(2072:【例2.15】歌手大奖赛)
  8. python类class定义_python基础教程之类class定义使用方法
  9. Python使用pyopencl在GPU上并行处理批量判断素数
  10. java appt,(转从ajava)打开ppt
  11. Vue.directive使用注意
  12. 【python使用】使用python读取mid/mif文件,高精地图解析
  13. python身份证号码共18位_涨姿势:用Python完成15位18位身份证的互转
  14. Mycat快速入门(六): Mycat管理命令和监控
  15. [JavaScript]Object(对象)学习
  16. 小心计算机视觉综合征!快来get护眼大法……
  17. ubuntu 将手柄控制信息发布到ros上
  18. Typora收费了, 还有哪些好用的markdown工具
  19. 7-1 厘米换算英尺英寸(15 分)
  20. 使用layui框架迅速搭建后台管理页面

热门文章

  1. 跳转到app下载页面和app评论页面
  2. dell r620 升级idrac_秋明 | dell 720/720xd服务器centos7下idrac固件升级[ipmi,vnc等相关操作]...
  3. 浅析直播间海量聊天消息的架构设计难点
  4. Maven读书系列:Maven仓库
  5. Three.js搭配OrbitControls.js实现flyTo()
  6. 电子凸轮和电子齿轮的差别
  7. 生活的压力和生命的尊严,哪个更重要?
  8. wan端口未连接怎么弄_路由器WAN口未连接怎么解决?
  9. sql sever conver
  10. Android 蓝牙手柄 延迟,安卓免设置,0.15s低延迟,飞智银狐蓝牙耳机体验