ocr.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from dataclasses import dataclass
  2. from core.idcrad import *
  3. from core.direction import *
  4. import numpy as np
  5. from paddleocr import PaddleOCR
  6. @dataclass
  7. class IdCardOcr:
  8. ocr: PaddleOCR
  9. image: np.ndarray
  10. image_type: str = '0'
  11. def predict(self):
  12. txts, confs, angle = self._ocr()
  13. if int(self.image_type) == 0:
  14. parser = FrontParser(txts, confs)
  15. elif int(self.image_type) == 1:
  16. parser = BackParser(txts, confs)
  17. return self._post_process(angle, parser, self.image_type)
  18. def _align_image(self, image):
  19. angle = detect_angle(image)
  20. print(angle) # 逆时针
  21. if angle == 180:
  22. image = cv2.rotate(image, cv2.ROTATE_180)
  23. if angle == 90:
  24. image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
  25. if angle == 270:
  26. image = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)
  27. return image, angle
  28. def _ocr(self):
  29. image = self.image
  30. image, angle = self._align_image(image)
  31. # 获取模型检测结果
  32. result = self.ocr.ocr(image, cls=True)
  33. print("------------------")
  34. print(result)
  35. if not result:
  36. return None
  37. confs = [line[1][1] for line in result]
  38. # 将检测到的文字放到一个列表中
  39. txts = [line[1][0] for line in result]
  40. print("......................................")
  41. print(txts)
  42. print("......................................")
  43. return txts, confs, angle
  44. def _post_process(self, angle: int, parser: Parser, image_type: str):
  45. content = parser.parse()
  46. conf = parser.confidence
  47. return {
  48. "confidence": conf,
  49. "card_type": image_type,
  50. "orientation": angle // 90,
  51. "name": content["Name"].to_dict(),
  52. "id": content["IDNumber"].to_dict(),
  53. "ethnicity": content["Nationality"].to_dict(),
  54. "gender": content["Gender"].to_dict(),
  55. "birthday": content["Birth"].to_dict(),
  56. "address_province": content["address_province"].to_dict(),
  57. "address_city": content["address_city"].to_dict(),
  58. "address_region": content["address_region"].to_dict(),
  59. "address_detail": content["address_detail"].to_dict(),
  60. "expire_date": content["expire_date"].to_dict()
  61. }