空港防疫算法交互
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
5.5KB

  1. import time
  2. from threading import Lock
  3. import cv2
  4. from aip import AipOcr
  5. from loguru import logger
  6. from enums.ExceptionEnum import ExceptionType
  7. from exception.CustomerException import ServiceException
  8. class OcrBaiduSdk:
  9. def __init__(self, content):
  10. self.content = content
  11. self.init_client()
  12. # self.lock = Lock()
  13. def init_client(self):
  14. self.client = AipOcr(str(self.content["baiduocr"]["APP_ID"]), self.content["baiduocr"]["API_KEY"],
  15. self.content["baiduocr"]["SECRET_KEY"])
  16. '''
  17. {
  18. "log_id": 2471272194,
  19. "words_result_num": 2,
  20. "words_result":
  21. [
  22. {"words": " TSINGTAO"},
  23. {"words": "青島睥酒"}
  24. ]
  25. }
  26. '''
  27. def universal_text_recognition(self, image, msgId):
  28. # try:
  29. # self.lock.acquire()
  30. reply_num = 1
  31. while True:
  32. try:
  33. or_result, or_image = cv2.imencode(".jpg", image)
  34. res_image = self.client.basicGeneral(or_image.tobytes())
  35. if res_image.get("error_code") == 216630 or res_image.get("error_msg") == 'recognize error':
  36. logger.error("百度云调接口失败: {}, 当前重试次数:{}, msgId: {}", res_image, reply_num, msgId)
  37. return None
  38. if res_image.get("error_code") == 282403 or res_image.get("error_msg") == 'target recognize error':
  39. logger.error("百度云调接口失败: {}, 当前重试次数:{}, msgId: {}", res_image, reply_num, msgId)
  40. return None
  41. if res_image.get("error_code") == 216202 or res_image.get("error_msg") == 'image size error':
  42. logger.error("百度云调接口失败: {}, 当前重试次数:{}, msgId: {}", res_image, reply_num, msgId)
  43. return None
  44. if res_image.get("error_code") is not None:
  45. logger.error("百度云调接口失败: {}, 当前重试次数:{}, msgId: {}", res_image, reply_num, msgId)
  46. raise Exception("百度云调接口失败")
  47. return res_image
  48. except Exception as e:
  49. logger.exception("通用文字识别失败: {}, 当前重试次数:{}, msgId: {}", e, reply_num, msgId)
  50. time.sleep(1)
  51. reply_num += 1
  52. self.init_client()
  53. if reply_num > 5:
  54. logger.exception("通用文字识别失败: {}, msgId: {}", e, msgId)
  55. raise ServiceException(ExceptionType.UNIVERSAL_TEXT_RECOGNITION_FAILED.value[0],
  56. ExceptionType.UNIVERSAL_TEXT_RECOGNITION_FAILED.value[1])
  57. # except Exception as ee:
  58. # logger.exception("通用文字识别加锁异常: {}, msgId: {}", ee, msgId)
  59. # raise ServiceException(ExceptionType.UNIVERSAL_TEXT_RECOGNITION_FAILED.value[0],
  60. # ExceptionType.UNIVERSAL_TEXT_RECOGNITION_FAILED.value[1])
  61. # finally:
  62. # self.lock.release()
  63. '''
  64. {
  65. "log_id": 3583925545,
  66. "words_result": {
  67. "color": "blue",
  68. "number": "苏HS7766"
  69. }
  70. }
  71. '''
  72. def license_plate_recognition(self, image, msgId):
  73. # try:
  74. # self.lock.acquire()
  75. reply_num = 1
  76. while True:
  77. try:
  78. or_result, or_image = cv2.imencode(".jpg", image)
  79. res_image = self.client.licensePlate(or_image.tobytes())
  80. if res_image.get("error_code") == 282403 or res_image.get("error_msg") == 'target recognize error':
  81. logger.error("百度云调接口失败: {}, 当前重试次数:{}, msgId: {}", res_image, reply_num, msgId)
  82. return None
  83. if res_image.get("error_code") == 216630 or res_image.get("error_msg") == 'recognize error':
  84. logger.error("百度云调接口失败: {}, 当前重试次数:{}, msgId: {}", res_image, reply_num, msgId)
  85. return None
  86. if res_image.get("error_code") == 216202 or res_image.get("error_msg") == 'image size error':
  87. logger.error("百度云调接口失败: {}, 当前重试次数:{}, msgId: {}", res_image, reply_num, msgId)
  88. return None
  89. if res_image.get("error_code") is not None:
  90. logger.error("百度云调接口失败: {}, 当前重试次数:{}, msgId: {}", res_image, reply_num, msgId)
  91. raise Exception("百度云调接口失败")
  92. return res_image
  93. except Exception as e:
  94. logger.exception("车牌识别失败: {}, 当前重试次数:{}, msgId: {}", e, reply_num, msgId)
  95. time.sleep(1)
  96. reply_num += 1
  97. self.init_client()
  98. if reply_num > 5:
  99. logger.exception("车牌识别失败: {}, msgId: {}", e, msgId)
  100. raise ServiceException(ExceptionType.ABNORMAL_LICENSE_PLATE_RECOGNITION.value[0],
  101. ExceptionType.ABNORMAL_LICENSE_PLATE_RECOGNITION.value[1])
  102. # except Exception as ee:
  103. # logger.exception("车牌识别加锁异常: {}, msgId: {}", ee, msgId)
  104. # raise ServiceException(ExceptionType.ABNORMAL_LICENSE_PLATE_RECOGNITION.value[0],
  105. # ExceptionType.ABNORMAL_LICENSE_PLATE_RECOGNITION.value[1])
  106. # finally:
  107. # self.lock.release()