Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Today I’m going to take you to an interesting thing, word recognition OCR. I don’t know if you want to recognize pictures, and then read the text function. Captcha, for example, is required if it is to be filled in automatically. There are a lot of situations where you need this feature.

We can log in baidu cloud and look at the interface document inside. There are rich application scenarios for the interface functions.

# encoding: UTF-8 import requests import base64 "" "request_URL = "Https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic" # binary mode to open the image file f = open ('] [local files, 'rb') img = base64.b64encode(f.read()) params = {"image":img} access_token = '[token obtained by calling authentication interface]' 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())Copy the code

All of the code in here is ready for you to use. It’s in the official documentation. Then let’s see what we need to fill in is access_token. The access_token is returned by invoking the request.

# encoding: utF-8 import requests # client_id Client_secret is SK host = obtained on the official website 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id= & website to obtain the AK 】 【 client_secret =' official website to obtain the SK 】 【 response = requests.get(host) if response: print(response.json())Copy the code

Set up the app first, then check your client_id= AK &client_secret= SK. Just fill in these two things. Response.json () will then carry the required access_token.

So let’s test that out.

After the final run, it is easy to identify the result.

You are welcome to discuss procedural questions with me and answer any questions you may have. Follow the public number: poetic code, make a friend.