json_anno_sta.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. import os
  15. import json
  16. import argparse
  17. import pandas as pd
  18. import seaborn as sns
  19. import matplotlib.pyplot as plt
  20. SHP_RATE_BINS = [
  21. 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.1, 1.2, 1.3, 1.4, 1.5,
  22. 1.6, 1.7, 1.8, 1.9, 2, 2.1, 2.2, 2.4, 2.6, 3, 3.5, 4, 5
  23. ]
  24. def _check_dir(check_path, show=True):
  25. if os.path.isdir(check_path):
  26. check_directory = check_path
  27. else:
  28. check_directory = os.path.dirname(check_path)
  29. if len(check_directory) > 0 and not os.path.exists(check_directory):
  30. os.makedirs(check_directory)
  31. if show:
  32. print("make dir:", check_directory)
  33. def json_anno_sta(json_path, csv_path, obj_shape_path, obj_shape_rate_path,
  34. obj_pos_path, obj_pos_end_path, obj_cat_path, obj_num_path,
  35. get_relative, img_keyname, anno_keyname):
  36. print("json read...\n")
  37. with open(json_path, "r") as load_f:
  38. data = json.load(load_f)
  39. df_image = pd.DataFrame(data[img_keyname])
  40. sns.jointplot(y="height", x="width", data=df_image, kind="hex")
  41. plt.close()
  42. df_image = df_image.rename(columns={
  43. "id": "image_id",
  44. "height": "image_height",
  45. "width": "image_width"
  46. })
  47. df_anno = pd.DataFrame(data[anno_keyname])
  48. df_anno[["pox_x", "pox_y", "width", "height"]] = pd.DataFrame(df_anno[
  49. "bbox"].values.tolist())
  50. df_anno["width"] = df_anno["width"].astype(int)
  51. df_anno["height"] = df_anno["height"].astype(int)
  52. df_merge = pd.merge(df_image, df_anno, on="image_id")
  53. if obj_shape_path is not None:
  54. _check_dir(obj_shape_path)
  55. sns.jointplot(y="height", x="width", data=df_merge, kind="hex")
  56. plt.savefig(obj_shape_path)
  57. plt.close()
  58. print("png save to", obj_shape_path)
  59. if get_relative:
  60. png_shapeR_path = obj_shape_path.replace(".png", "_relative.png")
  61. df_merge["heightR"] = df_merge["height"] / df_merge["image_height"]
  62. df_merge["widthR"] = df_merge["width"] / df_merge["image_width"]
  63. sns.jointplot(y="heightR", x="widthR", data=df_merge, kind="hex")
  64. plt.savefig(png_shapeR_path)
  65. plt.close()
  66. print("png save to", png_shapeR_path)
  67. if obj_shape_rate_path is not None:
  68. _check_dir(obj_shape_rate_path)
  69. plt.figure(figsize=(12, 8))
  70. df_merge["shape_rate"] = (df_merge["width"] /
  71. df_merge["height"]).round(1)
  72. df_merge["shape_rate"].value_counts(
  73. sort=False, bins=SHP_RATE_BINS).plot(
  74. kind="bar", title="images shape rate")
  75. plt.xticks(rotation=20)
  76. plt.savefig(obj_shape_rate_path)
  77. plt.close()
  78. print("png save to", obj_shape_rate_path)
  79. if obj_pos_path is not None:
  80. _check_dir(obj_pos_path)
  81. sns.jointplot(y="pox_y", x="pox_x", data=df_merge, kind="hex")
  82. plt.savefig(obj_pos_path)
  83. plt.close()
  84. print("png save to", obj_pos_path)
  85. if get_relative:
  86. png_posR_path = obj_pos_path.replace(".png", "_relative.png")
  87. df_merge["pox_yR"] = df_merge["pox_y"] / df_merge["image_height"]
  88. df_merge["pox_xR"] = df_merge["pox_x"] / df_merge["image_width"]
  89. sns.jointplot(y="pox_yR", x="pox_xR", data=df_merge, kind="hex")
  90. plt.savefig(png_posR_path)
  91. plt.close()
  92. print("png save to", png_posR_path)
  93. if obj_pos_end_path is not None:
  94. _check_dir(obj_pos_end_path)
  95. df_merge["pox_y_end"] = df_merge["pox_y"] + df_merge["height"]
  96. df_merge["pox_x_end"] = df_merge["pox_x"] + df_merge["width"]
  97. sns.jointplot(y="pox_y_end", x="pox_x_end", data=df_merge, kind="hex")
  98. plt.savefig(obj_pos_end_path)
  99. plt.close()
  100. print("png save to", obj_pos_end_path)
  101. if get_relative:
  102. png_posEndR_path = obj_pos_end_path.replace(".png", "_relative.png")
  103. df_merge["pox_y_endR"] = df_merge["pox_y_end"] / df_merge[
  104. "image_height"]
  105. df_merge["pox_x_endR"] = df_merge["pox_x_end"] / df_merge[
  106. "image_width"]
  107. sns.jointplot(
  108. y="pox_y_endR", x="pox_x_endR", data=df_merge, kind="hex")
  109. plt.savefig(png_posEndR_path)
  110. plt.close()
  111. print("png save to", png_posEndR_path)
  112. if obj_cat_path is not None:
  113. _check_dir(obj_cat_path)
  114. plt.figure(figsize=(12, 8))
  115. df_merge["category_id"].value_counts().sort_index().plot(
  116. kind="bar", title="obj category")
  117. plt.savefig(obj_cat_path)
  118. plt.close()
  119. print("png save to", obj_cat_path)
  120. if obj_num_path is not None:
  121. _check_dir(obj_num_path)
  122. plt.figure(figsize=(12, 8))
  123. df_merge["image_id"].value_counts().value_counts().sort_index().plot(
  124. kind="bar", title="obj number per image")
  125. plt.xticks(rotation=20)
  126. plt.savefig(obj_num_path)
  127. plt.close()
  128. print("png save to", obj_num_path)
  129. if csv_path is not None:
  130. _check_dir(csv_path)
  131. df_merge.to_csv(csv_path)
  132. print("csv save to", csv_path)
  133. if __name__ == "__main__":
  134. parser = argparse.ArgumentParser(
  135. description="Get annotation infomation statistics")
  136. parser.add_argument("--json_path", type=str, required=True, \
  137. help="Path of the JSON file whose statistics you want to collect.")
  138. parser.add_argument("--csv_path", type=str, default=None, \
  139. help="Path to save the statistics table.")
  140. parser.add_argument("--obj_shape_path", type=str, default=None, \
  141. help="Output image saving path. The image visualizes the two-dimensional distribution of the shape of all object detection boxes.")
  142. parser.add_argument("--obj_shape_rate_path", type=str, default=None, \
  143. help="Output image saving path. The image visualizes the one-dimensional distribution of shape ratio (width/height) of all target bounding boxes.")
  144. parser.add_argument("--obj_pos_path", type=str, default=None, \
  145. help="Output image saving path. The image visualizes the two-dimensional distribution of the coordinates at the upper left corner of all bounding boxes.")
  146. parser.add_argument("--obj_pos_end_path", type=str, default=None, \
  147. help="Output image saving path. The image visualizes the two-dimensional distribution of the coordinates at the lower right corner of all bounding boxes.")
  148. parser.add_argument("--obj_cat_path", type=str, default=None, \
  149. help="Output image saving path. The image visualizes the quantity distribution of objects in each category.")
  150. parser.add_argument("--obj_num_path", type=str, default=None, \
  151. help="Output image saving path. The image visualizes the quantity distribution of annotated objects in a single image.")
  152. parser.add_argument("--get_relative", action="store_true", \
  153. help="Whether to generate the shape of the image target detection frame and the relative ratio of the coordinates of the upper left corner and lower right corner of the object detection frame (horizontal axis coordinates/image length, vertical axis coordinates/image width).")
  154. parser.add_argument("--img_keyname", type=str, default="images", \
  155. help="Image key in the JSON file.")
  156. parser.add_argument("--anno_keyname", type=str, default="annotations", \
  157. help="Annotation key in the JSON file.")
  158. args = parser.parse_args()
  159. json_anno_sta(args.json_path, args.csv_path, args.obj_shape_path,
  160. args.obj_shape_rate_path, args.obj_pos_path,
  161. args.obj_pos_end_path, args.obj_cat_path, args.obj_num_path,
  162. args.get_relative, args.img_keyname, args.anno_keyname)