Ejemplo n.º 1
0
 def baidu_ocr(self, image_byte):
     """
     返回json格式的预测结果
     :param image_byte, 图片的bytes格式
     :return: string 格式的识别结果
     """
     import base64
     sys.path.append('/opt/salt-daily-check/bin')
     from baidutoken import gettoken
     access_token = gettoken()
     request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"
     # 二进制方式打开图片文件
     # 图片识别成文字
     results = ''
     img = base64.b64encode(image_byte)
     params = {"image": img}
     request_url = request_url + "?access_token=" + access_token
     headers = {'content-type': 'application/x-www-form-urlencoded'}
     response = requests.post(request_url, data=params, headers=headers)
     if response.status_code == 200:
         res = response.json()
         if res.get("words_result") is None:
             print(f"错误: 百度OCR返回的消息是: {res}")
             sys.exit(0)
         for w in res['words_result']:
             results = results + w['words'] + '\n'
     return results
Ejemplo n.º 2
0
 def baidu_token(self):
     sys.path.append('/opt/salt-daily-check/bin')
     from baidutoken import gettoken
     access_token = gettoken()
     return access_token