main.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import cv2
  2. from paddleocr import PaddleOCR
  3. from core.idcrad import FrontParser
  4. from core.direction import *
  5. # 初始化ocr模型和后处理模型
  6. ocr = PaddleOCR(use_angle_cls=True, rec_model_dir="./idcard_rec_infer/",
  7. det_model_dir="./idcard_det_infer/", cls_model_dir="idcard_cls_infer",
  8. rec_algorithm='CRNN',
  9. ocr_version='PP-OCRv2',
  10. rec_char_dict_path="./ppocr_keys_v1.txt", lang="ch", use_gpu=False)
  11. # 定义文件路径
  12. img_path = "front-270.png"
  13. image = cv2.imread(img_path)
  14. # 逆时针
  15. angle = detect_angle(image)
  16. print(angle)
  17. if angle == 180:
  18. image = cv2.rotate(image, cv2.ROTATE_180)
  19. if angle == 90:
  20. image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
  21. cv2.imwrite('front-90-1.png', image)
  22. if angle == 270:
  23. image = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)
  24. cv2.imwrite('front-270-1.png', image)
  25. # 获取模型检测结果
  26. result = ocr.ocr(image, cls=True)
  27. print("------------------")
  28. print(result)
  29. # 将检测到的文字放到一个列表中
  30. txts = [line[1][0] for line in result]
  31. confs = [line[1][1] for line in result]
  32. print("......................................")
  33. print(txts)
  34. print(confs)
  35. print("......................................")
  36. # 将结果送入到后处理模型中
  37. postprocessing = FrontParser(txts, confs)
  38. parse_result = postprocessing.parse()
  39. print(parse_result)