pipeline.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 = "ocr"
  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. **kwargs):
  33. self.text_det_model_name = text_det_model_name
  34. self.text_rec_model_name = text_rec_model_name
  35. self.text_det_model_dir = text_det_model_dir
  36. self.text_rec_model_dir = text_rec_model_dir
  37. self.output_dir = output_dir
  38. self.text_det_kernel_option = self.get_kernel_option(
  39. ) if text_det_kernel_option is None else text_det_kernel_option
  40. self.text_rec_kernel_option = self.get_kernel_option(
  41. ) if text_rec_kernel_option is None else text_rec_kernel_option
  42. self.text_det_post_transforms = [
  43. text_det_T.DBPostProcess(
  44. thresh=0.3,
  45. box_thresh=0.6,
  46. max_candidates=1000,
  47. unclip_ratio=1.5,
  48. use_dilation=False,
  49. score_mode='fast',
  50. box_type='quad'),
  51. # TODO
  52. text_det_T.CropByPolys(det_box_type="foo")
  53. ]
  54. if self.text_det_model_name is not None and self.text_rec_model_name is not None:
  55. self.load_model()
  56. def load_model(self):
  57. """load model predictor
  58. """
  59. assert self.text_det_model_name is not None and self.text_rec_model_name is not None
  60. self.text_det_model = create_model(
  61. self.text_det_model_name,
  62. self.text_det_model_dir,
  63. kernel_option=self.text_det_kernel_option,
  64. post_transforms=self.text_det_post_transforms)
  65. self.text_rec_model = create_model(
  66. self.text_rec_model_name,
  67. self.text_rec_model_dir,
  68. kernel_option=self.text_rec_kernel_option)
  69. def predict(self, input_path):
  70. """predict
  71. """
  72. result = self.text_det_model.predict({"input_path": input_path})
  73. all_rec_result = []
  74. for i, img in enumerate(result["sub_imgs"]):
  75. rec_result = self.text_rec_model.predict({"image": img})
  76. all_rec_result.append(rec_result["rec_text"][0])
  77. result["rec_text"] = all_rec_result
  78. if self.output_dir is not None:
  79. draw_img = draw_ocr_box_txt(result['original_image'],
  80. result['dt_polys'], result["rec_text"])
  81. cv2.imwrite(
  82. os.path.join(self.output_dir, "ocr_result.jpg"),
  83. draw_img[:, :, ::-1], )
  84. return result
  85. def update_model_name(self, model_name_list):
  86. """update model name and re
  87. Args:
  88. model_list (list): list of model name.
  89. """
  90. assert len(model_name_list) == 2
  91. self.text_det_model_name = model_name_list[0]
  92. self.text_rec_model_name = model_name_list[1]
  93. def get_kernel_option(self):
  94. """get kernel option
  95. """
  96. kernel_option = PaddleInferenceOption()
  97. kernel_option.set_device("gpu")
  98. return kernel_option