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.

149 lines
6.3KB

  1. import time
  2. import traceback
  3. import cv2
  4. from aip import AipOcr
  5. from loguru import logger
  6. from common.YmlConstant import get_baidu_ocr_APP_ID, get_baidu_ocr_API_KEY, get_baidu_ocr_SECRET_KEY
  7. from enums.BaiduSdkEnum import BAIDUERRORDATA
  8. from enums.ExceptionEnum import ExceptionType
  9. from exception.CustomerException import ServiceException
  10. from util.RWUtils import getConfigs
  11. class OcrBaiduSdk:
  12. __slots__ = [
  13. 'client', "__baidu_config"
  14. ]
  15. def __init__(self, base_dir):
  16. self.client = None
  17. self.__baidu_config = getConfigs(base_dir, 'config/dsp_baidu.json')
  18. self.init_client()
  19. def init_client(self):
  20. if self.client is None:
  21. self.client = AipOcr(str(self.__baidu_config["orc"]["APP_ID"]),
  22. self.__baidu_config["orc"]["API_KEY"],
  23. self.__baidu_config["orc"]["SECRET_KEY"])
  24. '''
  25. {
  26. "log_id": 2471272194,
  27. "words_result_num": 2,
  28. "words_result":
  29. [
  30. {"words": " TSINGTAO"},
  31. {"words": "青島睥酒"}
  32. ]
  33. }
  34. '''
  35. def universal_text_recognition(self, image, request_id):
  36. reply_num = 0
  37. reply_value = None
  38. while True:
  39. try:
  40. or_result, or_image = cv2.imencode(".jpg", image)
  41. options = {
  42. "language_type": "CHN_ENG",
  43. "detect_direction": "true",
  44. "detect_language": "true",
  45. "probability": "true"
  46. }
  47. res_image = self.client.basicGeneral(or_image.tobytes(), options)
  48. error_code = res_image.get("error_code")
  49. if error_code:
  50. enum = BAIDUERRORDATA.get(error_code)
  51. # 如果异常编码未知, 返回空值
  52. if enum is None:
  53. logger.error("百度云人流量统计异常!error_code:{}, request_id: {}", error_code, request_id)
  54. return None
  55. # 重试指定次数后,还是异常,输出统一内部异常
  56. if enum.value[3] == 0:
  57. if reply_value is None:
  58. reply_value = enum.value[4]
  59. logger.error("百度云人流量统计异常!error_code:{}, error_msg:{}, reply_num:{}, request_id: {}",
  60. enum.value[0], enum.value[2], reply_num, request_id)
  61. raise Exception()
  62. # 重试指定次数后,还是异常,输出对应的异常
  63. if enum.value[3] == 1:
  64. if reply_value is None:
  65. reply_value = enum.value[4]
  66. raise ServiceException(str(enum.value[0]), enum.value[2])
  67. # 重试指定次数后,还是异常,输出空
  68. if enum.value[3] == 2:
  69. if reply_value is None:
  70. reply_value = enum.value[4]
  71. if reply_num >= reply_value:
  72. return None
  73. raise Exception()
  74. return res_image
  75. except Exception as e:
  76. time.sleep(1)
  77. reply_num += 0.5
  78. self.init_client()
  79. if reply_num > reply_value:
  80. if isinstance(e, ServiceException):
  81. raise ServiceException(e.code, e.msg)
  82. logger.error("通用文字识别失败: {}, request_id: {}", traceback.format_exc(), request_id)
  83. raise ServiceException(ExceptionType.SERVICE_INNER_EXCEPTION.value[0],
  84. ExceptionType.SERVICE_INNER_EXCEPTION.value[1])
  85. '''
  86. {
  87. "log_id": 3583925545,
  88. "words_result": {
  89. "color": "blue",
  90. "number": "苏HS7766"
  91. }
  92. }
  93. '''
  94. def license_plate_recognition(self, image, request_id):
  95. reply_num = 0
  96. reply_value = None
  97. while True:
  98. try:
  99. or_result, or_image = cv2.imencode(".jpg", image)
  100. res_image = self.client.licensePlate(or_image.tobytes(), {"multi_detect": "true"})
  101. error_code = res_image.get("error_code")
  102. if error_code:
  103. enum = BAIDUERRORDATA.get(error_code)
  104. # 如果异常编码未知, 返回空值
  105. if enum is None:
  106. logger.error("百度云人流量统计异常!error_code:{}, request_id: {}", error_code, request_id)
  107. return None
  108. # 重试指定次数后,还是异常,输出统一内部异常
  109. if enum.value[3] == 0:
  110. if reply_value is None:
  111. reply_value = enum.value[4]
  112. logger.error("百度云人流量统计异常!error_code:{}, error_msg:{}, reply_num:{}, request_id: {}",
  113. enum.value[0], enum.value[2], reply_num, request_id)
  114. raise Exception()
  115. # 重试指定次数后,还是异常,输出对应的异常
  116. if enum.value[3] == 1:
  117. if reply_value is None:
  118. reply_value = enum.value[4]
  119. raise ServiceException(str(enum.value[0]), enum.value[2])
  120. # 重试指定次数后,还是异常,输出空
  121. if enum.value[3] == 2:
  122. if reply_value is None:
  123. reply_value = enum.value[4]
  124. if reply_num >= reply_value:
  125. return None
  126. raise Exception()
  127. return res_image
  128. except Exception as e:
  129. time.sleep(1)
  130. reply_num += 1
  131. self.init_client()
  132. if reply_num > reply_value:
  133. if isinstance(e, ServiceException):
  134. raise ServiceException(e.code, e.msg)
  135. logger.error("车牌识别失败: {}, request_id: {}", traceback.format_exc(), request_id)
  136. raise ServiceException(ExceptionType.SERVICE_INNER_EXCEPTION.value[0],
  137. ExceptionType.SERVICE_INNER_EXCEPTION.value[1])