analyse_dataset.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  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. from cProfile import label
  15. import os
  16. from collections import defaultdict
  17. import matplotlib.pyplot as plt
  18. from matplotlib.backends.backend_agg import FigureCanvasAgg
  19. import numpy as np
  20. from PIL import Image, ImageOps
  21. import cv2
  22. import json
  23. from .....utils.file_interface import custom_open
  24. # show data samples
  25. def simple_analyse(dataset_path, max_recorded_sample_cnts=20, show_label=True):
  26. """
  27. Analyse the dataset samples by return not nore than
  28. max_recorded_sample_cnts image path and label path
  29. Args:
  30. dataset_path (str): dataset path
  31. max_recorded_sample_cnts (int, optional): the number to return. Default: 50.
  32. Returns:
  33. tuple: tuple of sample number, image path and label path for train, val and text subdataset.
  34. """
  35. tags = ['train', 'val', 'test']
  36. sample_cnts = defaultdict(int)
  37. img_paths = defaultdict(list)
  38. lab_paths = defaultdict(list)
  39. lab_infos = defaultdict(list)
  40. res = [None] * 9
  41. delim = '\t'
  42. valid_num_parts = 2
  43. for tag in tags:
  44. file_list = os.path.join(dataset_path, f'{tag}.txt')
  45. if not os.path.exists(file_list):
  46. if tag in ('train', 'val'):
  47. res.insert(0, "数据集不符合规范,请先通过数据校准")
  48. return res
  49. else:
  50. continue
  51. else:
  52. with custom_open(file_list, 'r') as f:
  53. all_lines = f.readlines()
  54. # Each line corresponds to a sample
  55. sample_cnts[tag] = len(all_lines)
  56. for idx, line in enumerate(all_lines):
  57. parts = line.strip("\n").split(delim)
  58. if len(line.strip("\n")) < 1:
  59. continue
  60. if tag in ('train', 'val'):
  61. valid_num_parts_lst = [2]
  62. else:
  63. valid_num_parts_lst = [1, 2]
  64. if len(parts) not in valid_num_parts_lst and len(
  65. line.strip("\n")) > 1:
  66. res.insert(0, "数据集的标注文件不符合规范")
  67. return res
  68. if len(parts) == 2:
  69. img_path, lab_path = parts
  70. else:
  71. # len(parts) == 1
  72. img_path = parts[0]
  73. lab_path = None
  74. # check det label
  75. if len(img_paths[tag]) < max_recorded_sample_cnts:
  76. img_path = os.path.join(dataset_path, img_path)
  77. if lab_path is not None:
  78. label = json.loads(lab_path)
  79. boxes = []
  80. for item in label:
  81. if 'points' not in item or 'transcription' not in item:
  82. res.insert(0, "数据集的标注文件不符合规范")
  83. return res
  84. box = np.array(item['points'])
  85. if box.shape[1] != 2:
  86. res.insert(0, "数据集的标注文件不符合规范")
  87. return res
  88. boxes.append(box)
  89. txt = item['transcription']
  90. if not isinstance(txt, str):
  91. res.insert(0, "数据集的标注文件不符合规范")
  92. return res
  93. if show_label:
  94. lab_img = show_label_img(img_path, boxes)
  95. img_paths[tag].append(img_path)
  96. if show_label:
  97. lab_paths[tag].append(lab_img)
  98. else:
  99. lab_infos[tag].append({
  100. 'img_path': img_path,
  101. 'box': boxes
  102. })
  103. if show_label:
  104. return ("完成数据分析", sample_cnts[tags[0]], sample_cnts[tags[1]],
  105. sample_cnts[tags[2]], img_paths[tags[0]], img_paths[tags[1]],
  106. img_paths[tags[2]], lab_paths[tags[0]], lab_paths[tags[1]],
  107. lab_paths[tags[2]])
  108. else:
  109. return ("完成数据分析", sample_cnts[tags[0]], sample_cnts[tags[1]],
  110. sample_cnts[tags[2]], img_paths[tags[0]], img_paths[tags[1]],
  111. img_paths[tags[2]], lab_infos[tags[0]], lab_infos[tags[1]],
  112. lab_infos[tags[2]])
  113. def show_label_img(img_path, dt_boxes):
  114. """draw ocr detection label"""
  115. img = cv2.imread(img_path)
  116. for box in dt_boxes:
  117. box = np.array(box).astype(np.int32).reshape(-1, 2)
  118. cv2.polylines(img, [box], True, color=(0, 255, 0), thickness=3)
  119. return img[:, :, ::-1]
  120. def deep_analyse(dataset_path, output):
  121. """class analysis for dataset"""
  122. sample_results = simple_analyse(
  123. dataset_path, max_recorded_sample_cnts=float('inf'), show_label=False)
  124. lab_infos = sample_results[-3] + sample_results[-2] + sample_results[-1]
  125. labels_cnt = defaultdict(int)
  126. img_shapes = [] # w, h
  127. ratios_w = []
  128. ratios_h = []
  129. for info in lab_infos:
  130. img = np.asarray(ImageOps.exif_transpose(Image.open(info['img_path'])))
  131. img_h, img_w = np.shape(img)[:2]
  132. img_shapes.append([img_w, img_h])
  133. for box in info['box']:
  134. box = np.array(box).astype(np.int32).reshape(-1, 2)
  135. box_w, box_h = np.max(box, axis=0) - np.min(box, axis=0)
  136. ratio_w = box_w / img_w
  137. ratio_h = box_h / img_h
  138. ratios_w.append(ratio_w)
  139. ratios_h.append(ratio_h)
  140. m_w_img, m_h_img = np.mean(img_shapes, axis=0) # mean img shape
  141. m_num_box = len(ratios_w) / len(lab_infos) # num box per img
  142. ratio_w = [i * 1000 for i in ratios_w]
  143. ratio_h = [i * 1000 for i in ratios_h]
  144. w_bins = int((max(ratio_w) - min(ratio_w)) // 10)
  145. h_bins = int((max(ratio_h) - min(ratio_h)) // 10)
  146. fig, ax = plt.subplots()
  147. ax.hist(ratio_w, bins=w_bins, rwidth=0.8, color='yellowgreen')
  148. ax.set_xlabel('Width rate *1000')
  149. ax.set_ylabel('number')
  150. canvas = FigureCanvasAgg(fig)
  151. canvas.draw()
  152. width, height = fig.get_size_inches() * fig.get_dpi()
  153. bar_array = np.frombuffer(
  154. canvas.tostring_rgb(), dtype='uint8').reshape(
  155. int(height), int(width), 3)
  156. # pie
  157. fig, ax = plt.subplots()
  158. ax.hist(ratio_h, bins=h_bins, rwidth=0.8, color='pink')
  159. ax.set_xlabel('Height rate *1000')
  160. ax.set_ylabel('number')
  161. canvas = FigureCanvasAgg(fig)
  162. canvas.draw()
  163. width, height = fig.get_size_inches() * fig.get_dpi()
  164. pie_array = np.frombuffer(
  165. canvas.tostring_rgb(), dtype='uint8').reshape(
  166. int(height), int(width), 3)
  167. os.makedirs(output, exist_ok=True)
  168. fig_path = os.path.join(output, "histogram.png")
  169. img_array = np.concatenate((bar_array, pie_array), axis=1)
  170. cv2.imwrite(fig_path, img_array)
  171. return {"histogram": os.path.join("check_dataset", "histogram.png")}
  172. # return {
  173. # "图像平均宽度": m_w_img,
  174. # "图像平均高度": m_h_img,
  175. # "每张图平均文本检测框数量": m_num_box,
  176. # "检测框相对宽度分布图": fig1_path,
  177. # "检测框相对高度分布图": fig2_path
  178. # }