pipeline.py 3.5 KB

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