Upward posture-teach you how to get the text on the picture

"  Rising posture-teach you how to get the text on the picture "

 

My colleague wrote a very beautiful sentence, I asked him to send it to me, I want to collect it, but he took screenshots, screenshots, screenshots, and screenshots to me.

After looking at the pictures, I thought about the current picture recognition. I don't have any good way to quickly recognize the text on the pictures. Thinking of this, I immediately started to act.

 

1— Our ideas will always move us forward

Then our purpose is very clear, what we need to do is to extract the text on the picture, and then get our text information.

 

idea:

1. We have not been able to write the original text of machine learning, so what should we do? At this time, I suddenly discovered that the third party is definitely a mysterious group of organizations.

2. With a third party, which third party to use? Of course, it is provided to third parties who need the functions.

3. After searching Baidu, I found out that Baidu, a third party, is very easy to use. It is him.

4. I just found out that Baidu has an interface for image recognition. If it's OK, just use it.

interface:

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


other materials:

A picture with text.

 

02— Code implementation

Get the interface, then the next step is to implement it:

Solve it first 

access_token

We need to use another interface,

access_token= 'https://aip.baidubce.com/oauth/2.0/token?'       'grant_type=client_credentials&' \       'client_id=YtY4Q9wTWBFuc6B6P8XhmOV' \       '&client_secret=rwi6dj6YB4kH9IvbK6gtGvdXtq'

Both client_id and client_secret parameters need to be applied to the Baidu api backend to obtain them.

Then is the code to get access_token​:

headers={'Content-Type': 'application/json; charset=UTF-8'}host = 'https://aip.baidubce.com/oauth/2.0/token?' \       'grant_type=client_credentials&' \       'client_id=YtY4Q3UwTWBFuc6BP8XhmOV' \       '&client_secret=rwi6dj6YB4CkHhRNOIvbK6gtGvdXtq'access_token=requests.get(host,headers=headers).json()["access_token"]

The access_token is successful, then we will take out the picture we prepared​,

Get image text code:

file = open("图片地址", 'rb')image = file.read()file.close()webimage=requests.post("https://aip.baidubce.com/rest/2.0/ocr/v1/webimage?access_token="+access_token,headers={"Content-Type":"application/x-www-form-urlencoded"},data={"image":base64.b64encode(image)}).json()for i in webimage["words_result"]:    print(i["words"],end='')#拼接文字输出

Look at the complete code​:

import requestsimport base64headers={'Content-Type': 'application/json; charset=UTF-8'}host = 'https://aip.baidubce.com/oauth/2.0/token?' \       'grant_type=client_credentials&' \       'client_id=YtY4Q3UwTFuc6B6P8XhmOV' \       '&client_secret=rwi6dj6YB4CkHhRV1NvbK6gtGvdXtq'access_token=requests.get(host,headers=headers).json()["access_token"]file = open("图片地址", 'rb')# 读取图片image = file.read()file.close()webimage=requests.post("https://aip.baidubce.com/rest/2.0/ocr/v1/webimage?access_token="+access_token,                       headers={"Content-Type":"application/x-www-form-urlencoded"},                       data={"image":base64.b64encode(image)}).json()for i in webimage["words_result"]:    print(i["words"],end='')#拼接文字并输出

Of course, in addition to getting text, it can also be used to turn page verification codes​.

In the automated operation, the verification code cannot be skipped, then you can try to translate the verification code​.

 

Shock! Characters can also be played like this...

How to add WeChat friends in batches easily and quickly

How to send weather reminders to the people you like every day

 

To learn more, welcome to follow us​:

 

 

Guess you like

Origin blog.csdn.net/qq_39046854/article/details/97620626