direction.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from core.anchor import *
  2. from dataclasses import dataclass
  3. from paddleocr import PaddleOCR
  4. def detect_angle(result, ocr_anchor: OcrAnchor):
  5. lp = LineParser(result)
  6. res = lp.parse()
  7. is_horizontal = lp.is_horizontal
  8. return ocr_anchor.locate_anchor(res, is_horizontal)
  9. @dataclass
  10. class AngleDetector(object):
  11. """
  12. 角度检测器
  13. """
  14. ocr: PaddleOCR
  15. def detect_angle(self, img):
  16. ocr_anchor = BankCardAnchor('银行卡号', [Direction.BOTTOM])
  17. result = self.ocr.ocr(img, cls=True)
  18. try:
  19. angle = detect_angle(result, ocr_anchor)
  20. return angle, result
  21. except Exception as e:
  22. # 如果第一次识别不到,再识别
  23. result = self.ocr.ocr(img, cls=False)
  24. angle = detect_angle(result, ocr_anchor)
  25. # 旋转90度之后要重新计算角度
  26. # return (angle - 1 + 4) % 4, result
  27. return angle, result
  28. def origin_detect(self, img):
  29. # 这边一般是在自己的检测模型result=[]时,再使用官方的模型做个检测,如果这个也没有结果,那就真的检测不出来
  30. result = self.ocr.ocr(img)
  31. return result
  32. def det_oneline(self, result):
  33. # 这边已经是转正之后的图片,不需要考虑是否水平,只要检测是否一行
  34. lp = LineParser(result)
  35. return lp.detection_parse()