ocr.py 523 B

12345678910111213141516171819202122
  1. from dataclasses import dataclass
  2. from typing import Any, List
  3. import numpy as np
  4. from paddleocr import PaddleOCR
  5. @dataclass
  6. class OcrRes:
  7. confidence: float
  8. text: str
  9. txt_region: List[List[int]]
  10. @dataclass
  11. class Ocr:
  12. ocr: PaddleOCR
  13. def predict(self, image: List[np.ndarray]):
  14. if ocr_result := self.ocr.ocr(image):
  15. res = [OcrRes(ocrr[0], ocrr[1][0], ocrr[1][1]) for ocrr in ocr_result]
  16. else:
  17. raise Exception('图片中未识别出文字')
  18. return [res]