Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

143 rindas
6.1KB

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