实现手写字体识别(90%的识别率)

1、环境配置

程序整体是由python实现的,环境所包含的第三方库有requests、json、base64、pyinstaller。没有这些库的同学可以win+R输入cmd进入命令行终端pip install 库名。

获取百度SDK

浏览器搜索,如未注册请先注册,然后登录点击管理控制台。点击左侧产品服务→人工智能→文字识别。点击创建应用,输入应用名称如“Baidu_OCR”,选择用途如“学习办公”,最后进行简单应用描述,即可点击“立即创建”。会出现应用列表,包括AppID、API Key、Secret Key等信息,这些稍后会用到。

2、具体实现步骤

①获取access_token

这里我们用requests获取并返回access_token.方法如下:

    grant_type: 必须参数,固定为client_credentials; client_id: 必须参数,应用的API Key; client_secret: 必须参数,应用的Secret Key;
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]

``

②手写字体识别

下面我们定义了一个函数,参数为所要识别图片的绝对目录。注意:图片方向必须是正向,否则识别不出来。

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])

③成果展示

这是手写体(字体马马虎虎) 识别效果:

最后附上源码:

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退出!)
经验分享 程序员 微信小程序 职场和发展