json_ImgSta.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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, image_keyname):
  39. print('json read...\n')
  40. with open(js_path, 'r') as load_f:
  41. data = json.load(load_f)
  42. df_img = pd.DataFrame(data[image_keyname])
  43. if png_shape_path is not None:
  44. check_dir(png_shape_path)
  45. sns.jointplot('height', 'width', data=df_img, kind='hex')
  46. plt.savefig(png_shape_path)
  47. plt.close()
  48. print('png save to', png_shape_path)
  49. if png_shapeRate_path is not None:
  50. check_dir(png_shapeRate_path)
  51. df_img['shape_rate'] = (df_img['width'] / df_img['height']).round(1)
  52. df_img['shape_rate'].value_counts().sort_index().plot(kind='bar', title='images shape rate')
  53. plt.savefig(png_shapeRate_path)
  54. plt.close()
  55. print('png save to', png_shapeRate_path)
  56. if csv_path is not None:
  57. check_dir(csv_path)
  58. df_img.to_csv(csv_path)
  59. print('csv save to', csv_path)
  60. def get_args():
  61. parser = argparse.ArgumentParser(description='Json Images Infomation Statistic')
  62. # parameters
  63. parser.add_argument('--json_path', type=str,
  64. help='json path to statistic images information')
  65. parser.add_argument('--csv_path', type=str, default=None,
  66. help='csv path to save statistic images information, default None, do not save')
  67. parser.add_argument('--png_shape_path', type=str, default=None,
  68. help='png path to save statistic images shape information, default None, do not save')
  69. parser.add_argument('--png_shapeRate_path', type=str, default=None,
  70. help='png path to save statistic images shape rate information, default None, do not save')
  71. parser.add_argument('--image_keyname', type=str, default='images',
  72. help='image key name in json, default images')
  73. parser.add_argument('-Args_show', '--Args_show', type=bool, default=True,
  74. help='Args_show(default: True), if True, show args info')
  75. args = parser.parse_args()
  76. if args.Args_show:
  77. print('Args'.center(100, '-'))
  78. for k, v in vars(args).items():
  79. print('%s = %s' % (k, v))
  80. print()
  81. return args
  82. if __name__ == '__main__':
  83. args = get_args()
  84. js_img_sta(args.json_path, args.csv_path, args.png_shape_path, args.png_shapeRate_path, args.image_keyname)