call request
The requested image needs to be base64-encoded and urlencoded: the base64-encoded image refers to encoding a pair of image data into a string of strings, and using the string to replace the image address. You can first get the binary of the picture, and then urlencode after removing the encoding header.
interface
https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic
the code
from PIL import ImageGrab
import requests
import base64
def ScreenCapture():
# 识别图片
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
f = open('xxx.png', 'rb')
img = base64.b64encode(f.read())
params = {"image":img}
access_token = '将获取到的access_token粘贴到这里'
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())
# 调用
ScreenCapture()
Instructions for use
1. xxx.png is a local image.
2. access_token = 'Paste the obtained access_token here'. You need to obtain the access_token, which is valid for 30 days. You can cache it in a local file. Every time you use it, you can judge whether the access_token is still valid , if it expires, it will be automatically retrieved and the cache will be refreshed again.