# -*- coding:UTF-8 -*-
# -*- encoding: utf-8 -*-
import base64
import requests
from urllib.parse import quote
headers = {
'Content-Type': "application/x-www-form-urlencoded",
'charset': "utf-8"
}
if __name__ == '__main__':
recognise_api_url = "https://aip.baidubce.com/rest/2.0/solution/v1/iocr/recognise"
access_token = "mytoken"
templateSign = "c405e86ebc3daa9b04ae9d57ddd68c09"
classifierId = "your_classifier_id"
# 测试数据路径
image_path = "w2.jpeg"
try:
with open(image_path, 'rb') as f:
image_data = f.read()
image_b64 = base64.b64encode(image_data).replace("\r", "")
# 请求模板的bodys
recognise_bodys = "access_token=" + access_token + "&templateSign=" + templateSign + \
"&image=" + quote(image_b64.encode("utf8"))
# 请求分类器的bodys
classifier_bodys = "access_token=" + access_token + "&classifierId=" + classifierId + \
"&image=" + quote(image_b64.encode("utf8"))
# 请求模板识别
response = requests.post(recognise_api_url, data=recognise_bodys, headers=headers)
# 请求分类器识别
# response = requests.post(recognise_api_url, data=classifier_bodys, headers=headers)
print(response.text)
except Exception as e:
测试报错:a bytes-like object is required, not 'str'
请各位大神帮看一下问题出在哪里,不是已经转换bytes了吗 怎么还报这个错,并且这代码是官方写好的示例