代码有啥问题啊,使劲改都还是图片格式错误。
星晴天台 发布于2021-01 浏览:9224 回复:12
0
收藏

图片用的是技术文档下载下来的图片

https://ai.bdstatic.com/file/3B2E79DE27AE48CF8F731575AAEBC22D

import requests
import base64

# 1.处理图片
f = open('text.jpg', 'rb')
img = base64.b64encode(f.read())
img = str(img, encoding="utf-8").replace('\n', '').replace("+","%2B")
img = str.encode(img)
#print(img)

# 2.获取access_token
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=&client_secret=n7x4UTYFmAijEDXwd'
response = requests.get(host)
if response:
    access_token = response.json()["access_token"]

# 3.进行识别
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general"
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:
    print (response.json())
百度AI开发者社区-错误页