json_ImgSta.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # -*- coding: utf-8 -*-
  2. # @File : json_images_sta.py
  3. # @Author : zhaoHL
  4. # @Contact : huilin16@qq.com
  5. # @Time Create First: 2021/8/1 10:25
  6. # @Contributor : zhaoHL
  7. # @Time Modify Last : 2021/8/1 10:25
  8. '''
  9. @File Description:
  10. # 统计json文件images信息,生成统计结果csv,同时生成图像shape、图像shape比例的二维分布图
  11. python ./coco_tools/json_ImgSta.py \
  12. --json_path=./annotations/instances_val2017.json \
  13. --csv_path=./img_sta/images.csv \
  14. --png_shape_path=./img_sta/images_shape.png \
  15. --png_shapeRate_path=./img_sta/images_shapeRate.png
  16. '''
  17. import json
  18. import argparse
  19. import os.path
  20. import pandas as pd
  21. import seaborn as sns
  22. import matplotlib.pyplot as plt
  23. def check_dir(check_path,show=True):
  24. if os.path.isdir(check_path):
  25. check_directory = check_path
  26. else:
  27. check_directory = os.path.dirname(check_path)
  28. if not os.path.exists(check_directory):
  29. os.makedirs(check_directory)
  30. if show:
  31. print('make dir:',check_directory)
  32. def js_img_sta(js_path, csv_path, png_shape_path, png_shapeRate_path, image_keyname):
  33. print('json read...\n')
  34. with open(js_path, 'r') as load_f:
  35. data = json.load(load_f)
  36. df_img = pd.DataFrame(data[image_keyname])
  37. if png_shape_path is not None:
  38. check_dir(png_shape_path)
  39. sns.jointplot('height', 'width', data=df_img, kind='hex')
  40. plt.savefig(png_shape_path)
  41. plt.close()
  42. print('png save to', png_shape_path)
  43. if png_shapeRate_path is not None:
  44. check_dir(png_shapeRate_path)
  45. df_img['shape_rate'] = (df_img['width'] / df_img['height']).round(1)
  46. df_img['shape_rate'].value_counts().sort_index().plot(kind='bar', title='images shape rate')
  47. plt.savefig(png_shapeRate_path)
  48. plt.close()
  49. print('png save to', png_shapeRate_path)
  50. if csv_path is not None:
  51. check_dir(csv_path)
  52. df_img.to_csv(csv_path)
  53. print('csv save to', csv_path)
  54. def get_args():
  55. parser = argparse.ArgumentParser(description='Json Images Infomation Statistic')
  56. # parameters
  57. parser.add_argument('--json_path', type=str,
  58. help='json path to statistic images information')
  59. parser.add_argument('--csv_path', type=str, default=None,
  60. help='csv path to save statistic images information, default None, do not save')
  61. parser.add_argument('--png_shape_path', type=str, default=None,
  62. help='png path to save statistic images shape information, default None, do not save')
  63. parser.add_argument('--png_shapeRate_path', type=str, default=None,
  64. help='png path to save statistic images shape rate information, default None, do not save')
  65. parser.add_argument('--image_keyname', type=str, default='images',
  66. help='image key name in json, default images')
  67. parser.add_argument('-Args_show', '--Args_show', type=bool, default=True,
  68. help='Args_show(default: True), if True, show args info')
  69. args = parser.parse_args()
  70. if args.Args_show:
  71. print('Args'.center(100, '-'))
  72. for k, v in vars(args).items():
  73. print('%s = %s' % (k, v))
  74. print()
  75. return args
  76. if __name__ == '__main__':
  77. args = get_args()
  78. js_img_sta(args.json_path, args.csv_path, args.png_shape_path, args.png_shapeRate_path, args.image_keyname)