You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

postprocess.py 5.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # Copyright 2021 Huawei Technologies Co., Ltd
  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. """Evaluation for Deeptext"""
  16. import argparse
  17. import os
  18. from PIL import Image
  19. import numpy as np
  20. import mmcv
  21. from src.config import config
  22. from src.utils import metrics
  23. parser = argparse.ArgumentParser(description="Deeptext evaluation")
  24. parser.add_argument("--result_path", type=str, required=True, help="result file path")
  25. parser.add_argument("--label_path", type=str, required=True, help="label path")
  26. parser.add_argument("--img_path", type=str, required=True, help="img path")
  27. args_opt = parser.parse_args()
  28. config.test_batch_size = 1
  29. def get_pred(file, result_path):
  30. file_name = file.split('.')[0][3:]
  31. all_bbox_file = os.path.join(result_path, file_name + "_0.bin")
  32. all_label_file = os.path.join(result_path, file_name + "_1.bin")
  33. all_mask_file = os.path.join(result_path, file_name + "_2.bin")
  34. all_bbox = np.fromfile(all_bbox_file, dtype=np.float32).reshape(config.test_batch_size, 1000, 5)
  35. all_label = np.fromfile(all_label_file, dtype=np.int32).reshape(config.test_batch_size, 1000, 1)
  36. all_mask = np.fromfile(all_mask_file, dtype=np.bool).reshape(config.test_batch_size, 1000, 1)
  37. return all_bbox, all_label, all_mask
  38. def get_gt_bboxes_labels(label_file, img_file):
  39. img_data = np.array(Image.open(img_file))
  40. img_data, w_scale, h_scale = mmcv.imresize(
  41. img_data, (config.img_width, config.img_height), return_scale=True)
  42. scale_factor = np.array(
  43. [w_scale, h_scale, w_scale, h_scale], dtype=np.float32)
  44. img_shape = (config.img_height, config.img_width, 1.0)
  45. img_shape = np.asarray(img_shape, dtype=np.float32)
  46. file = open(label_file)
  47. lines = file.readlines()
  48. boxes = []
  49. gt_label = []
  50. for line in lines:
  51. label_info = line.split(",")
  52. boxes.append([float(label_info[0]), float(label_info[1]), float(label_info[2]), float(label_info[3])])
  53. gt_label.append(int(1))
  54. gt_bboxes = np.array(boxes)
  55. gt_bboxes = gt_bboxes * scale_factor
  56. gt_bboxes[:, 0::2] = np.clip(gt_bboxes[:, 0::2], 0, img_shape[1] - 1)
  57. gt_bboxes[:, 1::2] = np.clip(gt_bboxes[:, 1::2], 0, img_shape[0] - 1)
  58. return gt_bboxes, gt_label
  59. def deeptext_eval_test(result_path='', label_path='', img_path=''):
  60. eval_iter = 0
  61. print("\n========================================\n")
  62. print("Processing, please wait a moment.")
  63. max_num = 32
  64. pred_data = []
  65. files = os.listdir(label_path)
  66. for file in files:
  67. eval_iter = eval_iter + 1
  68. img_file = os.path.join(img_path, file.split('gt_')[1].replace("txt", "jpg"))
  69. label_file = os.path.join(label_path, file)
  70. gt_bboxes, gt_labels = get_gt_bboxes_labels(label_file, img_file)
  71. gt_bboxes = np.array(gt_bboxes).astype(np.float32)
  72. all_bbox, all_label, all_mask = get_pred(file, result_path)
  73. all_label = all_label + 1
  74. for j in range(config.test_batch_size):
  75. all_bbox_squee = np.squeeze(all_bbox[j, :, :])
  76. all_label_squee = np.squeeze(all_label[j, :, :])
  77. all_mask_squee = np.squeeze(all_mask[j, :, :])
  78. all_bboxes_tmp_mask = all_bbox_squee[all_mask_squee, :]
  79. all_labels_tmp_mask = all_label_squee[all_mask_squee]
  80. if all_bboxes_tmp_mask.shape[0] > max_num:
  81. inds = np.argsort(-all_bboxes_tmp_mask[:, -1])
  82. inds = inds[:max_num]
  83. all_bboxes_tmp_mask = all_bboxes_tmp_mask[inds]
  84. all_labels_tmp_mask = all_labels_tmp_mask[inds]
  85. pred_data.append({"boxes": all_bboxes_tmp_mask,
  86. "labels": all_labels_tmp_mask,
  87. "gt_bboxes": gt_bboxes,
  88. "gt_labels": gt_labels})
  89. precisions, recalls = metrics(pred_data)
  90. print("\n========================================\n")
  91. for i in range(config.num_classes - 1):
  92. j = i + 1
  93. f1 = (2 * precisions[j] * recalls[j]) / (precisions[j] + recalls[j] + 1e-6)
  94. print("class {} precision is {:.2f}%, recall is {:.2f}%,"
  95. "F1 is {:.2f}%".format(j, precisions[j] * 100, recalls[j] * 100, f1 * 100))
  96. if config.use_ambigous_sample:
  97. break
  98. if __name__ == '__main__':
  99. deeptext_eval_test(args_opt.result_path, args_opt.label_path, args_opt.img_path)