direction.py 1.7 KB

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