Python统计Labelme标注文件信息并绘制散点图
前言
- 本文是个人使用Python处理文件的电子笔记,由于水平有限,难免出现错漏,敬请批评改正。
- 更多精彩内容,可点击进入我的个人主页查看
前提条件
- 熟悉Python
相关介绍
- Python是一种跨平台的计算机程序设计语言。是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越多被用于独立的、大型项目的开发。
- Python OS模块是负责程序与操作系统的交互,提供了访问操作系统底层的接口和非常丰富的方法用来处理文件和目录。
实验环境
- Python 3.x (面向对象的高级语言)
Python统计Labelme标注文件信息并绘制散点图
import os
import cv2
import json
import pyecharts.options as opts
from pyecharts.charts import Scatter
from pyecharts.commons.utils import JsCode
from pyecharts.faker import Faker
# 图像显示函数
def show(name, img):
cv2.namedWindow(name, 0) # 用来创建指定名称的窗口,0表示CV_WINDOW_NORMAL
# cv2.resizeWindow(name, img.shape[1], img.shape[0]); # 设置宽高大小为640*480
cv2.imshow(name, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
def count_json_label(in_json_path,label_num,width_list,height_list,area_list):
with open(in_json_path, "r", encoding='utf-8') as f:
# json.load数据到变量json_data
json_data = json.load(f)
# 统计标签数
for i in json_data['shapes']:
if i['label'] in label_num.keys():
label_num[i['label']] = label_num[i['label']] + 1
else:
label_num[i['label']] = 1
# 生成存储长和宽数据的列表
for i in json_data['shapes']:
w = i['points'][1][0] - i['points'][0][0]
h = i['points'][1][1] - i['points'][0][1]
# print(w,h)
width_list.append(abs(w))
height_list.append(abs(h))
area_list.append(abs(w)*abs(h))
def write_info_txt(txt_path,label_num,width_list,height_list,area_list):
with open('rect_info.txt','w') as f:
f.write('总标注数:'+str(sum(label_num.values()))+'\n')
f.write('label_num:'+str(label_num)+ '\n')
f.write('min(width_list):'+str(min(width_list))+'\n')
f.write('len(width_list):'+str(len(width_list))+'\n')
f.write('min(height_list):'+str(min(height_list))+'\n')
f.write('len(height_list):'+str(len(height_list))+'\n')
f.write('min(area_list):'+str(min(area_list))+'\n')
f.write('len(area_list):'+str(len(area_list))+'\n')
def draw_scatter(width_list,height_list):
x_data = width_list
y_data = height_list
(
Scatter()
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="(width,height)",
y_axis=y_data,
symbol_size=3,
label_opts=opts.LabelOpts(is_show=False),
)
.set_series_opts()
.set_global_opts(
xaxis_opts=opts.AxisOpts(
name = 'Width',
type_="value", splitline_opts=opts.SplitLineOpts(is_show=True)
),
yaxis_opts=opts.AxisOpts(
name = 'Height',
type_="value",
axistick_opts=opts.AxisTickOpts(is_show=True),
splitline_opts=opts.SplitLineOpts(is_show=True),
),
tooltip_opts=opts.TooltipOpts(
formatter=JsCode(
"function (params) {return ' ( ' +params.value[0] + ' , ' + params.value[1]+ ' )';}"
)
),
)
.render("w_h_scatter_chart.html")
)
if __name__=="__main__":
in_dir_name = 'jsons/' # 图片和json所在文件夹
# out_dir_name = 'output/'
all_name_list = os.listdir(in_dir_name) # 获取文件夹内所有文件名列表
# 获取所有json文件,并修改
json_name_list = [i for i in all_name_list if i.endswith('.json')] # 图片路径列表
# print(json_name_list)
label_num = {}
width_list = []
height_list = []
area_list = []
for json_name in json_name_list: # 遍历json文件并修改
in_json_path = in_dir_name + json_name
# print(in_json_path)
count_json_label(in_json_path,label_num,width_list,height_list,area_list)
print('总标注数:',sum(label_num.values()))
print('label_num:',label_num)
# print('width_list:',width_list)
print('min(width_list):',min(width_list))
print('len(width_list):',len(width_list))
# print('height_list:',height_list)
print('min(height_list):',min(height_list))
print('len(height_list):',len(height_list))
# print('area_list:',area_list)
print('min(area_list):',min(area_list))
print('len(area_list):',len(area_list))
# 写入信息到txt文件中
write_info_txt('rect_info.txt',label_num,width_list,height_list,area_list)
# 生成散点图
draw_scatter(width_list,height_list)
总标注数: 3504
label_num: {'1': 2612, '2': 228, '0': 151}
min(width_list): 7.420494699646497
len(width_list): 3504
min(height_list): 9.30232558139528
len(height_list): 3504
min(area_list): 76.04040504938182
len(area_list): 3504
更多精彩内容,可点击进入Python日常小操作专栏或我的个人主页查看