ocr.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 ..components import SortBoxes, CropByPolys
  15. from ..results import OCRResult
  16. from .base import BasePipeline
  17. from ...utils import logging
  18. class OCRPipeline(BasePipeline):
  19. """OCR Pipeline"""
  20. entities = "OCR"
  21. def __init__(
  22. self,
  23. text_det_model,
  24. text_rec_model,
  25. text_det_batch_size=1,
  26. text_rec_batch_size=1,
  27. device=None,
  28. predictor_kwargs=None,
  29. ):
  30. super().__init__(device, predictor_kwargs)
  31. self._build_predictor(text_det_model, text_rec_model)
  32. self.set_predictor(
  33. text_det_batch_size=text_det_batch_size,
  34. text_rec_batch_size=text_rec_batch_size,
  35. device=device,
  36. )
  37. def _build_predictor(self, text_det_model, text_rec_model):
  38. self.text_det_model = self._create(model=text_det_model)
  39. self.text_rec_model = self._create(model=text_rec_model)
  40. self.is_curve = self.text_det_model.model_name in [
  41. "PP-OCRv4_mobile_seal_det",
  42. "PP-OCRv4_server_seal_det",
  43. ]
  44. self._sort_boxes = SortBoxes()
  45. self._crop_by_polys = CropByPolys(
  46. det_box_type="poly" if self.is_curve else "quad"
  47. )
  48. def set_predictor(
  49. self, text_det_batch_size=None, text_rec_batch_size=None, device=None
  50. ):
  51. if text_det_batch_size and text_det_batch_size > 1:
  52. logging.warning(
  53. f"text det model only support batch_size=1 now,the setting of text_det_batch_size={text_det_batch_size} will not using! "
  54. )
  55. if text_rec_batch_size:
  56. self.text_rec_model.set_predictor(batch_size=text_rec_batch_size)
  57. if device:
  58. self.text_rec_model.set_predictor(device=device)
  59. self.text_det_model.set_predictor(device=device)
  60. def predict(self, input, **kwargs):
  61. self.set_predictor(**kwargs)
  62. for det_res in self.text_det_model(input):
  63. single_img_res = (
  64. det_res if self.is_curve else next(self._sort_boxes(det_res))
  65. )
  66. single_img_res["rec_text"] = []
  67. single_img_res["rec_score"] = []
  68. if len(single_img_res["dt_polys"]) > 0:
  69. all_subs_of_img = list(self._crop_by_polys(single_img_res))
  70. for rec_res in self.text_rec_model(all_subs_of_img):
  71. single_img_res["rec_text"].append(rec_res["rec_text"])
  72. single_img_res["rec_score"].append(rec_res["rec_score"])
  73. yield OCRResult(single_img_res)