result.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # Copyright (c) 2024 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 typing import Dict
  15. import numpy as np
  16. from ...common.result import BaseCVResult, JsonMixin
  17. class SealRecognitionResult(BaseCVResult):
  18. """Seal Recognition Result"""
  19. def _to_img(self) -> Dict[str, np.ndarray]:
  20. res_img_dict = {}
  21. layout_det_res = self["layout_det_res"]
  22. if len(layout_det_res) > 0:
  23. res_img_dict["layout_det_res"] = layout_det_res.img["res"]
  24. model_settings = self["model_settings"]
  25. if model_settings["use_doc_preprocessor"]:
  26. res_img_dict.update(**self["doc_preprocessor_res"].img)
  27. for sno in range(len(self["seal_res_list"])):
  28. seal_res = self["seal_res_list"][sno]
  29. seal_region_id = seal_res["seal_region_id"]
  30. sub_seal_res_dict = seal_res.img
  31. key = f"seal_res_region{seal_region_id}"
  32. res_img_dict[key] = sub_seal_res_dict["ocr_res_img"]
  33. return res_img_dict
  34. def _to_str(self, *args, **kwargs) -> Dict[str, str]:
  35. """Converts the instance's attributes to a dictionary and then to a string.
  36. Args:
  37. *args: Additional positional arguments passed to the base class method.
  38. **kwargs: Additional keyword arguments passed to the base class method.
  39. Returns:
  40. Dict[str, str]: A dictionary with the instance's attributes converted to strings.
  41. """
  42. data = {}
  43. data["input_path"] = self["input_path"]
  44. data["model_settings"] = self["model_settings"]
  45. if self["model_settings"]["use_doc_preprocessor"]:
  46. data["doc_preprocessor_res"] = self["doc_preprocessor_res"].str["res"]
  47. if len(self["layout_det_res"]) > 0:
  48. data["layout_det_res"] = self["layout_det_res"].str["res"]
  49. data["seal_res_list"] = []
  50. for sno in range(len(self["seal_res_list"])):
  51. seal_res = self["seal_res_list"][sno]
  52. data["seal_res_list"].append(seal_res.str["res"])
  53. return JsonMixin._to_str(data, *args, **kwargs)
  54. def _to_json(self, *args, **kwargs) -> Dict[str, str]:
  55. """
  56. Converts the object's data to a JSON dictionary.
  57. Args:
  58. *args: Positional arguments passed to the JsonMixin._to_json method.
  59. **kwargs: Keyword arguments passed to the JsonMixin._to_json method.
  60. Returns:
  61. Dict[str, str]: A dictionary containing the object's data in JSON format.
  62. """
  63. data = {}
  64. data["input_path"] = self["input_path"]
  65. data["page_index"] = self["page_index"]
  66. data["model_settings"] = self["model_settings"]
  67. if self["model_settings"]["use_doc_preprocessor"]:
  68. data["doc_preprocessor_res"] = self["doc_preprocessor_res"].json["res"]
  69. if len(self["layout_det_res"]) > 0:
  70. data["layout_det_res"] = self["layout_det_res"].json["res"]
  71. data["seal_res_list"] = []
  72. for sno in range(len(self["seal_res_list"])):
  73. seal_res = self["seal_res_list"][sno]
  74. data["seal_res_list"].append(seal_res.json["res"])
  75. return JsonMixin._to_json(data, *args, **kwargs)