pipeline.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. entities = "OCR"
  25. def __init__(
  26. 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. ):
  37. super().__init__(**kwargs)
  38. self.text_det_model_name = text_det_model_name
  39. self.text_rec_model_name = text_rec_model_name
  40. self.text_det_model_dir = text_det_model_dir
  41. self.text_rec_model_dir = text_rec_model_dir
  42. self.output = output
  43. self.device = device
  44. self.text_det_kernel_option = text_det_kernel_option
  45. self.text_rec_kernel_option = text_rec_kernel_option
  46. if (
  47. self.text_det_model_name is not None
  48. and self.text_rec_model_name is not None
  49. ):
  50. self.load_model()
  51. def check_model_name(self):
  52. """check that model name is valid"""
  53. assert (
  54. self.text_det_model_name in text_det_models
  55. ), f"The model name({self.text_det_model_name}) error. \
  56. Only support: {text_det_models}."
  57. assert (
  58. self.text_rec_model_name in text_rec_models
  59. ), f"The model name({self.text_rec_model_name}) error. \
  60. Only support: {text_rec_models}."
  61. def load_model(self):
  62. """load model predictor"""
  63. self.check_model_name()
  64. text_det_kernel_option = (
  65. self.get_kernel_option()
  66. if self.text_det_kernel_option is None
  67. else self.text_det_kernel_option
  68. )
  69. text_rec_kernel_option = (
  70. self.get_kernel_option()
  71. if self.text_rec_kernel_option is None
  72. else self.text_rec_kernel_option
  73. )
  74. text_det_post_transforms = [
  75. text_det_T.DBPostProcess(
  76. thresh=0.3,
  77. box_thresh=0.6,
  78. max_candidates=1000,
  79. unclip_ratio=1.5,
  80. use_dilation=False,
  81. score_mode="fast",
  82. box_type="quad",
  83. ),
  84. # TODO
  85. text_det_T.CropByPolys(det_box_type="foo"),
  86. ]
  87. self.text_det_model = create_model(
  88. self.text_det_model_name,
  89. self.text_det_model_dir,
  90. kernel_option=text_det_kernel_option,
  91. post_transforms=text_det_post_transforms,
  92. )
  93. self.text_rec_model = create_model(
  94. self.text_rec_model_name,
  95. self.text_rec_model_dir,
  96. kernel_option=text_rec_kernel_option,
  97. disable_print=self.disable_print,
  98. disable_save=self.disable_save,
  99. )
  100. def predict(self, input):
  101. """predict"""
  102. result = self.text_det_model.predict(input)
  103. all_rec_result = []
  104. for i, img in enumerate(result["sub_imgs"]):
  105. rec_result = self.text_rec_model.predict({"image": img})
  106. all_rec_result.append(rec_result["rec_text"][0])
  107. result["rec_text"] = all_rec_result
  108. if self.output is not None:
  109. draw_img = draw_ocr_box_txt(
  110. result["original_image"], result["dt_polys"], result["rec_text"]
  111. )
  112. fn = os.path.basename(result["input_path"])
  113. cv2.imwrite(
  114. os.path.join(self.output, fn),
  115. draw_img[:, :, ::-1],
  116. )
  117. return result
  118. def update_model(self, model_name_list, model_dir_list):
  119. """update model
  120. Args:
  121. model_name_list (list): list of model name.
  122. model_dir_list (list): list of model directory.
  123. """
  124. assert len(model_name_list) == 2
  125. self.text_det_model_name = model_name_list[0]
  126. self.text_rec_model_name = model_name_list[1]
  127. if model_dir_list:
  128. assert len(model_dir_list) == 2
  129. self.text_det_model_dir = model_dir_list[0]
  130. self.text_rec_model_dir = model_dir_list[1]
  131. def get_kernel_option(self):
  132. """get kernel option"""
  133. kernel_option = PaddleInferenceOption()
  134. kernel_option.set_device(self.device)
  135. return kernel_option
  136. def get_input_keys(self):
  137. """get dict keys of input argument input"""
  138. return self.text_det_model.get_input_keys()