formula_recognition.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. import numpy as np
  15. from ...modules.formula_recognition.model_list import MODELS
  16. from ..components import *
  17. from ..results import FormulaRecResult
  18. from .base import BasicPredictor
  19. class LaTeXOCRPredictor(BasicPredictor):
  20. entities = MODELS
  21. def _build_components(self):
  22. self._add_component(
  23. [
  24. ReadImage(format="RGB"),
  25. LaTeXOCRReisizeNormImg(),
  26. ]
  27. )
  28. predictor = ImagePredictor(
  29. model_dir=self.model_dir,
  30. model_prefix=self.MODEL_FILE_PREFIX,
  31. option=self.pp_option,
  32. )
  33. self._add_component(predictor)
  34. op = self.build_postprocess(**self.config["PostProcess"])
  35. self._add_component(op)
  36. def build_postprocess(self, **kwargs):
  37. if kwargs.get("name") == "LaTeXOCRDecode":
  38. return LaTeXOCRDecode(
  39. character_list=kwargs.get("character_dict"),
  40. )
  41. else:
  42. raise Exception()
  43. def _pack_res(self, single):
  44. keys = ["input_path", "rec_text"]
  45. return FormulaRecResult({key: single[key] for key in keys})