pipeline.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 ..base import BasePipeline
  15. from typing import Any, Dict, Optional
  16. from ..components import SortQuadBoxes, CropByPolys
  17. from .result import OCRResult
  18. ########## [TODO]后续需要更新路径
  19. from ...components.transforms import ReadImage
  20. class OCRPipeline(BasePipeline):
  21. """OCR Pipeline"""
  22. entities = "OCR"
  23. def __init__(
  24. self,
  25. config,
  26. device=None,
  27. pp_option=None,
  28. use_hpip: bool = False,
  29. hpi_params: Optional[Dict[str, Any]] = None,
  30. ):
  31. super().__init__(
  32. device=device, pp_option=pp_option, use_hpip=use_hpip, hpi_params=hpi_params
  33. )
  34. text_det_model_config = config["SubModules"]["TextDetection"]
  35. self.text_det_model = self.create_model(text_det_model_config)
  36. text_rec_model_config = config["SubModules"]["TextRecognition"]
  37. self.text_rec_model = self.create_model(text_rec_model_config)
  38. self.text_type = config["text_type"]
  39. self._sort_quad_boxes = SortQuadBoxes()
  40. if self.text_type == "common":
  41. self._crop_by_polys = CropByPolys(det_box_type="quad")
  42. elif self.text_type == "seal":
  43. self._crop_by_polys = CropByPolys(det_box_type="poly")
  44. else:
  45. raise ValueError("Unsupported text type {}".format(self.text_type))
  46. self.img_reader = ReadImage(format="BGR")
  47. def predict(self, input, **kwargs):
  48. if not isinstance(input, list):
  49. input_list = [input]
  50. else:
  51. input_list = input
  52. img_id = 1
  53. for input in input_list:
  54. if isinstance(input, str):
  55. image_array = next(self.img_reader(input))[0]["img"]
  56. else:
  57. image_array = input
  58. assert len(image_array.shape) == 3
  59. det_res = next(self.text_det_model(image_array))
  60. dt_polys = det_res["dt_polys"]
  61. dt_scores = det_res["dt_scores"]
  62. ########## [TODO]需要确认检测模块和识别模块过滤阈值等情况
  63. if self.text_type == "common":
  64. dt_polys = self._sort_quad_boxes(dt_polys)
  65. single_img_res = {
  66. "input_img": image_array,
  67. "dt_polys": dt_polys,
  68. "img_id": img_id,
  69. "text_type": self.text_type,
  70. }
  71. img_id += 1
  72. single_img_res["rec_text"] = []
  73. single_img_res["rec_score"] = []
  74. if len(dt_polys) > 0:
  75. all_subs_of_img = list(self._crop_by_polys(image_array, dt_polys))
  76. ########## [TODO]updata in future
  77. for sub_img in all_subs_of_img:
  78. sub_img["input"] = sub_img["img"]
  79. ##########
  80. for rec_res in self.text_rec_model(all_subs_of_img):
  81. single_img_res["rec_text"].append(rec_res["rec_text"])
  82. single_img_res["rec_score"].append(rec_res["rec_score"])
  83. yield OCRResult(single_img_res)