Realize handwritten font recognition (90% recognition rate)

1. Environment configuration

The whole program is implemented by python, and the third-party libraries included in the environment include requests, json, base64, and pyinstaller. Students who do not have these libraries can enter cmd in win+R to enter the command line terminal pip install library name.

Get Baidu SDK

Search Baidu Cloud in your browser . If you have not registered, please register first, then log in and click on the management console. Click on Product Services→Artificial Intelligence→Text Recognition on the left . Click Create Application, enter the application name such as "Baidu_OCR", select the purpose such as "Learning Office", and finally make a simple application description, then click "Create Now". A list of applications will appear, including AppID, API Key, Secret Key and other information, which will be used later.Insert picture description here
Insert picture description here

2. Specific implementation steps

①Get access_token

Here we use requests to obtain and return access_token . The method is as follows:

  • grant_type: required parameter, fixed as client_credentials;
  • client_id: required parameter, the API Key of the application;
  • client_secret: required parameter, the Secret Key of the application;
	def get_access():
	
	    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【官网获取的AK】&client_secret=【官网获取的SK】'
	    response = requests.get(host)
	    if response:
	        #dict = json.loads()
	        dict = response.json();
	        #print(dict['access_token'])
	        return dict['access_token']

``

②Handwriting font recognition

Below we define a function, the parameter is the absolute directory of the picture to be recognized. Note: The direction of the picture must be positive, otherwise it will not be recognized.

	def write_font(filename):
	    '''
	    手写文字识别
	    '''
	    request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting"
	    f = open(filename, 'rb')
	    img = base64.b64encode(f.read())
	
	    print('正在识别...')
	    params = {
    
    "image":img}
	    access_token = get_access()
	    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:
	        text = response.json()
	        content = text['words_result']
	        for item in content:
	            
	            print(item['words'])
    

③Achievement display

This is the recognition effect of handwriting (the font is so-so)
Insert picture description here
:
Insert picture description here

Finally, attach the source code:

import requests 
import json
import base64
# client_id 为官网获取的AK, client_secret 为官网获取的SK

def get_access():

    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【官网获取的AK】&client_secret=【官网获取的SK】'
    response = requests.get(host)
    if response:
        #dict = json.loads()
        dict = response.json();
        #print(dict['access_token'])
        #返回access_token
        return dict['access_token']

def write_font(filename):
    '''
    手写文字识别
    '''
    request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting"
    f = open(filename, 'rb')
    img = base64.b64encode(f.read())

    print('正在识别...')
    params = {
    
    "image":img}
	#调用get_access函数,获取tokne
    access_token = get_access()
    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:
        text = response.json()
        content = text['words_result']
        for item in content:
            print(item['words'])
    

if __name__ == '__main__':
    
    while True:
        filename= input('请输入您的图片位置(按Q退出):')
        if(filename == 'q' or filename == 'Q'):
            break
        write_font(filename)
        print('识别完成!!!')
        print('按Q退出!')
        

Guess you like

Origin blog.csdn.net/m0_43456002/article/details/105497196