text_rec.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. import json
  16. import numpy as np
  17. import cv2
  18. from ...utils import logging
  19. from ..utils.io import JsonWriter, ImageWriter, ImageReader
  20. class TextRecResult(dict):
  21. def __init__(self, data):
  22. super().__init__(data)
  23. self._json_writer = JsonWriter()
  24. self._img_reader = ImageReader(backend="opencv")
  25. self._img_writer = ImageWriter(backend="opencv")
  26. def save_json(self, save_path, indent=4, ensure_ascii=False):
  27. if not save_path.endswith(".json"):
  28. save_path = Path(save_path) / f"{Path(self['img_path']).stem}.json"
  29. self._json_writer.write(save_path, self, indent=4, ensure_ascii=False)
  30. def save_img(self, save_path):
  31. raise Exception()
  32. def print(self, json_format=True, indent=4, ensure_ascii=False):
  33. str_ = self
  34. if json_format:
  35. str_ = json.dumps(str_, indent=indent, ensure_ascii=ensure_ascii)
  36. logging.info(str_)