pipeline.py 4.2 KB

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