1

call request

The requested image needs to be base64 encoded and urlencode passed in: the base64 encoding of the image refers to encoding a pair of image data into a string of strings, and using the string instead of the image address. You can first get the binary of the image, then remove the encoding header and then urlencode.

interface

 https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic

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' requires you to obtain the access_token, which is valid for 30 days. You can cache it in a local file, and check whether the access_token is still valid each time it is used. Flush the cache again.


TANKING
4.8k 声望509 粉丝

热爱分享,热爱创作,热爱研究。