main.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 ...modules.base import create_model
  15. from ...modules.text_detection.predictor import transforms as text_det_T
  16. from .utils import draw_ocr_box_txt
  17. class OCRPipeline(object):
  18. """OCR Pipeline
  19. """
  20. def __init__(self,
  21. text_det_model_name,
  22. text_rec_model_name,
  23. text_det_model_dir=None,
  24. text_rec_model_dir=None,
  25. text_det_kernel_option=None,
  26. text_rec_kernel_option=None,
  27. output_dir="output"):
  28. self.output_dir = output_dir
  29. text_det_kernel_option = self.get_kernel_option(
  30. ) if text_det_kernel_option is None else text_det_kernel_option
  31. text_rec_kernel_option = self.get_kernel_option(
  32. ) if text_rec_kernel_option is None else text_rec_kernel_option
  33. text_det_post_transforms = [
  34. text_det_T.DBPostProcess(
  35. thresh=0.3,
  36. box_thresh=0.6,
  37. max_candidates=1000,
  38. unclip_ratio=1.5,
  39. use_dilation=False,
  40. score_mode='fast',
  41. box_type='quad'),
  42. # TODO
  43. text_det_T.CropByPolys(det_box_type="foo")
  44. ]
  45. self.text_det_model = create_model(
  46. text_det_model_name,
  47. text_det_model_dir,
  48. kernel_option=text_det_kernel_option,
  49. post_transforms=text_det_post_transforms)
  50. self.text_rec_model = create_model(
  51. text_rec_model_name,
  52. text_rec_model_dir,
  53. kernel_option=text_rec_kernel_option)
  54. def __call__(self, input_path):
  55. result = self.text_det_model.predict({"input_path": input_path})
  56. all_rec_result = []
  57. for i, img in enumerate(result["sub_imgs"]):
  58. rec_result = self.text_rec_model.predict({"image": img})
  59. all_rec_result.append(rec_result["rec_text"][0])
  60. result["rec_text"] = all_rec_result
  61. return result