ocr.py 2.2 KB

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