seal_rec.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 pathlib import Path
  15. from .base import BaseResult, CVResult
  16. class SealOCRResult(CVResult):
  17. """SealOCRResult"""
  18. def get_target_name(self, save_path):
  19. input_path = self["src_file_name"]
  20. if input_path.endswith(".pdf"):
  21. save_path = (
  22. Path(save_path)
  23. / f"{Path(input_path).stem}_pdf"
  24. / Path("page_{:04d}".format(self["page_id"] + 1))
  25. )
  26. else:
  27. save_path = Path(save_path) / f"{Path(input_path).stem}"
  28. return save_path
  29. def save_to_img(self, save_path):
  30. if not save_path.lower().endswith((".jpg", ".png")):
  31. save_path = self.get_target_name(save_path)
  32. else:
  33. save_path = Path(save_path).stem
  34. layout_save_path = f"{save_path}_layout.jpg"
  35. layout_result = self["layout_result"]
  36. layout_result.save_to_img(layout_save_path)
  37. seal_result = self["ocr_result"]
  38. seal_result.save_to_img(f"{save_path}_seal_ocr.jpg")
  39. def save_to_json(self, save_path):
  40. if not save_path.lower().endswith((".json")):
  41. save_path = self.get_target_name(save_path)
  42. else:
  43. save_path = Path(save_path).stem
  44. super().save_to_json(f"{save_path}_res.json")