json_ImgSta.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """
  15. @File Description:
  16. # 统计json文件images信息,生成统计结果csv,同时生成图像shape、图像shape比例的二维分布图
  17. python ./coco_tools/json_ImgSta.py \
  18. --json_path=./annotations/instances_val2017.json \
  19. --csv_path=./img_sta/images.csv \
  20. --png_shape_path=./img_sta/images_shape.png \
  21. --png_shapeRate_path=./img_sta/images_shapeRate.png
  22. """
  23. import json
  24. import argparse
  25. import os.path
  26. import pandas as pd
  27. import seaborn as sns
  28. import matplotlib.pyplot as plt
  29. def check_dir(check_path, show=True):
  30. if os.path.isdir(check_path):
  31. check_directory = check_path
  32. else:
  33. check_directory = os.path.dirname(check_path)
  34. if not os.path.exists(check_directory):
  35. os.makedirs(check_directory)
  36. if show:
  37. print('make dir:', check_directory)
  38. def js_img_sta(js_path, csv_path, png_shape_path, png_shapeRate_path,
  39. image_keyname):
  40. print('json read...\n')
  41. with open(js_path, 'r') as load_f:
  42. data = json.load(load_f)
  43. df_img = pd.DataFrame(data[image_keyname])
  44. if png_shape_path is not None:
  45. check_dir(png_shape_path)
  46. sns.jointplot('height', 'width', data=df_img, kind='hex')
  47. plt.savefig(png_shape_path)
  48. plt.close()
  49. print('png save to', png_shape_path)
  50. if png_shapeRate_path is not None:
  51. check_dir(png_shapeRate_path)
  52. df_img['shape_rate'] = (df_img['width'] / df_img['height']).round(1)
  53. df_img['shape_rate'].value_counts().sort_index().plot(
  54. kind='bar', title='images shape rate')
  55. plt.savefig(png_shapeRate_path)
  56. plt.close()
  57. print('png save to', png_shapeRate_path)
  58. if csv_path is not None:
  59. check_dir(csv_path)
  60. df_img.to_csv(csv_path)
  61. print('csv save to', csv_path)
  62. def get_args():
  63. parser = argparse.ArgumentParser(
  64. description='Json Images Infomation Statistic')
  65. # Parameters
  66. parser.add_argument(
  67. '--json_path',
  68. type=str,
  69. help='json path to statistic images information')
  70. parser.add_argument(
  71. '--csv_path',
  72. type=str,
  73. default=None,
  74. help='csv path to save statistic images information, default None, do not save'
  75. )
  76. parser.add_argument(
  77. '--png_shape_path',
  78. type=str,
  79. default=None,
  80. help='png path to save statistic images shape information, default None, do not save'
  81. )
  82. parser.add_argument(
  83. '--png_shapeRate_path',
  84. type=str,
  85. default=None,
  86. help='png path to save statistic images shape rate information, default None, do not save'
  87. )
  88. parser.add_argument(
  89. '--image_keyname',
  90. type=str,
  91. default='images',
  92. help='image key name in json, default images')
  93. parser.add_argument(
  94. '-Args_show',
  95. '--Args_show',
  96. type=bool,
  97. default=True,
  98. help='Args_show(default: True), if True, show args info')
  99. args = parser.parse_args()
  100. if args.Args_show:
  101. print('Args'.center(100, '-'))
  102. for k, v in vars(args).items():
  103. print('%s = %s' % (k, v))
  104. print()
  105. return args
  106. if __name__ == '__main__':
  107. args = get_args()
  108. js_img_sta(args.json_path, args.csv_path, args.png_shape_path,
  109. args.png_shapeRate_path, args.image_keyname)