pipeline.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 os
  15. import cv2
  16. from ..base import BasePipeline
  17. from ...modules.text_detection.model_list import MODELS as text_det_models
  18. from ...modules.text_recognition.model_list import MODELS as text_rec_models
  19. from ...modules import create_model, PaddleInferenceOption
  20. from ...modules.text_detection import transforms as text_det_T
  21. from .utils import draw_ocr_box_txt
  22. class OCRPipeline(BasePipeline):
  23. """OCR Pipeline
  24. """
  25. entities = "OCR"
  26. def __init__(self,
  27. text_det_model_name=None,
  28. text_rec_model_name=None,
  29. text_det_model_dir=None,
  30. text_rec_model_dir=None,
  31. text_det_kernel_option=None,
  32. text_rec_kernel_option=None,
  33. output="./",
  34. device="gpu",
  35. **kwargs):
  36. self.text_det_model_name = text_det_model_name
  37. self.text_rec_model_name = text_rec_model_name
  38. self.text_det_model_dir = text_det_model_dir
  39. self.text_rec_model_dir = text_rec_model_dir
  40. self.output = output
  41. self.device = device
  42. self.text_det_kernel_option = text_det_kernel_option
  43. self.text_rec_kernel_option = text_rec_kernel_option
  44. if self.text_det_model_name is not None and self.text_rec_model_name is not None:
  45. self.load_model()
  46. def check_model_name(self):
  47. """ check that model name is valid
  48. """
  49. assert self.text_det_model_name in text_det_models, f"The model name({self.text_det_model_name}) error. \
  50. Only support: {text_det_models}."
  51. assert self.text_rec_model_name in text_rec_models, f"The model name({self.text_rec_model_name}) error. \
  52. Only support: {text_rec_models}."
  53. def load_model(self):
  54. """load model predictor
  55. """
  56. self.check_model_name()
  57. text_det_kernel_option = self.get_kernel_option(
  58. ) if self.text_det_kernel_option is None else self.text_det_kernel_option
  59. text_rec_kernel_option = self.get_kernel_option(
  60. ) if self.text_rec_kernel_option is None else self.text_rec_kernel_option
  61. text_det_post_transforms = [
  62. text_det_T.DBPostProcess(
  63. thresh=0.3,
  64. box_thresh=0.6,
  65. max_candidates=1000,
  66. unclip_ratio=1.5,
  67. use_dilation=False,
  68. score_mode='fast',
  69. box_type='quad'),
  70. # TODO
  71. text_det_T.CropByPolys(det_box_type="foo")
  72. ]
  73. self.text_det_model = create_model(
  74. self.text_det_model_name,
  75. self.text_det_model_dir,
  76. kernel_option=text_det_kernel_option,
  77. post_transforms=text_det_post_transforms)
  78. self.text_rec_model = create_model(
  79. self.text_rec_model_name,
  80. self.text_rec_model_dir,
  81. kernel_option=text_rec_kernel_option)
  82. def predict(self, input):
  83. """predict
  84. """
  85. result = self.text_det_model.predict(input)
  86. all_rec_result = []
  87. for i, img in enumerate(result["sub_imgs"]):
  88. rec_result = self.text_rec_model.predict({"image": img})
  89. all_rec_result.append(rec_result["rec_text"][0])
  90. result["rec_text"] = all_rec_result
  91. if self.output is not None:
  92. draw_img = draw_ocr_box_txt(result['original_image'],
  93. result['dt_polys'], result["rec_text"])
  94. fn = os.path.basename(result['input_path'])
  95. cv2.imwrite(
  96. os.path.join(self.output, fn),
  97. draw_img[:, :, ::-1], )
  98. return result
  99. def update_model_name(self, model_name_list):
  100. """update model name and re
  101. Args:
  102. model_list (list): list of model name.
  103. """
  104. assert len(model_name_list) == 2
  105. self.text_det_model_name = model_name_list[0]
  106. self.text_rec_model_name = model_name_list[1]
  107. def get_kernel_option(self):
  108. """get kernel option
  109. """
  110. kernel_option = PaddleInferenceOption()
  111. kernel_option.set_device(self.device)
  112. return kernel_option
  113. def get_input_keys(self):
  114. """get dict keys of input argument input
  115. """
  116. return self.text_det_model.get_input_keys()