e2e_metric.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Copyright (c) 2021 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. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. __all__ = ["E2EMetric"]
  18. from ppocr.utils.e2e_metric.Deteval import get_socre_A, get_socre_B, combine_results
  19. from ppocr.utils.e2e_utils.extract_textpoint_slow import get_dict
  20. class E2EMetric(object):
  21. def __init__(
  22. self,
  23. mode,
  24. gt_mat_dir,
  25. character_dict_path,
  26. main_indicator="f_score_e2e",
  27. **kwargs,
  28. ):
  29. self.mode = mode
  30. self.gt_mat_dir = gt_mat_dir
  31. self.label_list = get_dict(character_dict_path)
  32. self.max_index = len(self.label_list)
  33. self.main_indicator = main_indicator
  34. self.reset()
  35. def __call__(self, preds, batch, **kwargs):
  36. if self.mode == "A":
  37. gt_polyons_batch = batch[2]
  38. temp_gt_strs_batch = batch[3][0]
  39. ignore_tags_batch = batch[4]
  40. gt_strs_batch = []
  41. for temp_list in temp_gt_strs_batch:
  42. t = ""
  43. for index in temp_list:
  44. if index < self.max_index:
  45. t += self.label_list[index]
  46. gt_strs_batch.append(t)
  47. for pred, gt_polyons, gt_strs, ignore_tags in zip(
  48. [preds], gt_polyons_batch, [gt_strs_batch], ignore_tags_batch
  49. ):
  50. # prepare gt
  51. gt_info_list = [
  52. {"points": gt_polyon, "text": gt_str, "ignore": ignore_tag}
  53. for gt_polyon, gt_str, ignore_tag in zip(
  54. gt_polyons, gt_strs, ignore_tags
  55. )
  56. ]
  57. # prepare det
  58. e2e_info_list = [
  59. {"points": det_polyon, "texts": pred_str}
  60. for det_polyon, pred_str in zip(pred["points"], pred["texts"])
  61. ]
  62. result = get_socre_A(gt_info_list, e2e_info_list)
  63. self.results.append(result)
  64. else:
  65. img_id = batch[5][0]
  66. e2e_info_list = [
  67. {"points": det_polyon, "texts": pred_str}
  68. for det_polyon, pred_str in zip(preds["points"], preds["texts"])
  69. ]
  70. result = get_socre_B(self.gt_mat_dir, img_id, e2e_info_list)
  71. self.results.append(result)
  72. def get_metric(self):
  73. metrics = combine_results(self.results)
  74. self.reset()
  75. return metrics
  76. def reset(self):
  77. self.results = [] # clear results