ocr.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 ..predictors import create_predictor
  16. from ...utils import logging
  17. from ..components import CropByPolys
  18. from ..results import OCRResult
  19. class OCRPipeline(BasePipeline):
  20. """OCR Pipeline"""
  21. entities = "ocr"
  22. def __init__(self, det_model, rec_model, det_batch_size, rec_batch_size, **kwargs):
  23. self._det_predict = create_predictor(det_model, batch_size=det_batch_size)
  24. self._rec_predict = create_predictor(rec_model, batch_size=rec_batch_size)
  25. # TODO: foo
  26. self._crop_by_polys = CropByPolys(det_box_type="foo")
  27. def predict(self, x):
  28. batch_ocr_res = []
  29. for batch_det_res in self._det_predict(x):
  30. for det_res in batch_det_res:
  31. single_img_res = det_res["result"]
  32. single_img_res["rec_text"] = []
  33. single_img_res["rec_score"] = []
  34. if len(single_img_res["dt_polys"]) > 0:
  35. all_subs_of_img = list(self._crop_by_polys(single_img_res))
  36. for batch_rec_res in self._rec_predict(all_subs_of_img):
  37. for rec_res in batch_rec_res:
  38. single_img_res["rec_text"].append(
  39. rec_res["result"]["rec_text"]
  40. )
  41. single_img_res["rec_score"].append(
  42. rec_res["result"]["rec_score"]
  43. )
  44. batch_ocr_res.append({"result": OCRResult(single_img_res)})
  45. yield batch_ocr_res