json文件类别统计加可视化展示(柱状图和饼状图)

from pycocotools.coco import COCO
import numpy as np
import matplotlib.pyplot as plt
 
#dataDir='./json'
#dataType='val2017'
#dataType='train2017'
annFile='json/Detection-BSD.json'
 
# initialize COCO api for instance annotations
coco=COCO(annFile)
 
# display COCO categories and supercategories
cats = coco.loadCats(coco.getCatIds())
cat_nms=[cat['name'] for cat in cats]
print('number of categories: ', len(cat_nms))
print('COCO categories: \n', cat_nms)
 

number_img = []
number_bbox = []


for cat_name in cat_nms:
    catId = coco.getCatIds(catNms=cat_name)     
    imgId = coco.getImgIds(catIds=catId)         
    annId = coco.getAnnIds(catIds=catId)        
 
    print("{:<15} {:<6d} {:<10d}".format(cat_name, len(imgId), len(annId)))

    number_img.append(len(imgId))
    number_bbox.append(len(annId))

# print (number_img)
# print (number_bbox) 
bar_width = 0.3

y1 = number_img
y2 = number_bbox

x = np.arange(5)

str1 = cat_nms

for a, b in zip(x, y1):
    plt.text(a, b+0.05, '%.0f' % b, ha='center', va='bottom', fontsize=10)

for a, b in zip(x, y2):
    plt.text(a+0.3, b+0.05, '%.0f' % b, ha='center', va='bottom', fontsize=10)

#柱状图绘制
p1 = plt.bar(x, height=y1, width=0.3, label="image", tick_label=str1)
p2 = plt.bar(x+bar_width, height=y2, width=0.3, label="bbox", tick_label=str1)

plt.figure(num = 1)
plt.legend()
plt.title('class_status')
plt.savefig('柱状统计图')
#plt.show()

#饼状图绘制
plt.figure(num = 2,figsize=(6,6))
explode=[0.01,0.01,0.01,0.01,0.01]
plt.pie(number_img,explode=explode,labels=str1,autopct='%1.1f%%')
plt.title('images_numbers')
plt.savefig('images统计饼状图')
#plt.show()

plt.figure(num = 3,figsize=(6,6))
explode=[0.01,0.01,0.01,0.01,0.01]
plt.pie(number_bbox,explode=explode,labels=str1,autopct='%1.1f%%')
plt.title('bbox_numbers')
plt.savefig('bbox统计饼状图')
plt.show()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值