百度AI手写诗文字识别使用攻略

作者:wangwei8638

一.平台接入

参照之前文档:

https://ai.baidu.com/forum/topic/show/943028

二.分析接口文档

  1. https://ai.baidu.com/docs#/OCR-API/9ef46660

    (1)接口描述

对手写中文汉字、数字进行识别。

(2)请求说明

需要用到的信息有:

请求URL:https://aip.baidubce.com/rest/2.0/ocr/v1/business_license

Header格式:Content-Type:application/x-www-form-urlencoded

请求参数:
在这里插入图片描述
(3)返回示例

{

    "log_id": 620759800,

    "words_result": [

        {

            "location": {

                "left": 56,

                "top": 0,

                "width": 21,

                "height": 210

            },

            "words": "3"

        }

    ],

    "words_result_num": 1

}

2.获取accesstoken

#client_id 为官网获取的AK, client_secret 为官网获取的SK
client_id =【百度云应用的AK】
client_secret =【百度云应用的SK】

#获取token
def get_token():
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret
request = urllib.request.Request(host)
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib.request.urlopen(request)
token_content = response.read()
if token_content:
token_info = json.loads(token_content.decode("utf-8"))
token_key = token_info['access_token']
return token_key

三.识别结果

1.楷体
在这里插入图片描述
识别结果:

再看《英雄》

yMwang

西风穿林啸

将幡沙中扬

惜才终须斩

知义释帝王

大漠锁孤城

长河落残阳

何当催马去

月阑夜未央

2.行书

在这里插入图片描述
识别结果:

再看《英雄》

mwwhng

西穿林啸

将幡沙中扬

惜才终须斩

知义释帝王

大漠锁孤城

长河落残阳

何当催马

月阑夜未央

四.源码共享

# -*- coding: utf-8 -*-

#!/usr/bin/env python

import urllib

import urllib.parse

import urllib.request

import base64

import json

#client_id 为官网获取的AK, client_secret 为官网获取的SK

client_id = '***************'

client_secret = '************************'



#获取token

def get_token():

    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret

    request = urllib.request.Request(host)

    request.add_header('Content-Type', 'application/json; charset=UTF-8')

    response = urllib.request.urlopen(request)

    token_content = response.read()

    if token_content:

        token_info = json.loads(token_content.decode("utf-8"))

        token_key = token_info['access_token']

    return token_key



     # 读取图片

def get_file_content(filePath):

    with open(filePath, 'rb') as fp:

        return fp.read()





#获取手写识别结果

def get_license_plate(path):



    request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting"

   

    f = get_file_content(path)

    access_token=get_token()

    img = base64.b64encode(f)

    params = {"custom_lib": False, "image": img}

    params = urllib.parse.urlencode(params).encode('utf-8')

    request_url = request_url + "?access_token=" + access_token

    request = urllib.request.Request(url=request_url, data=params)

    request.add_header('Content-Type', 'application/x-www-form-urlencoded')

    response = urllib.request.urlopen(request)

    content = response.read()

    if content:

        license_plates = json.loads(content.decode("utf-8"))

        strover = '识别结果:'

        words_result = license_plates['words_result']

        for item in words_result:

            print(item['words'])

        return content

    else:

        return ''



image_path='F:\paddle\shu\skai.jpg'

get_license_plate(image_path)
发布了10 篇原创文章 · 获赞 4 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_45449540/article/details/103296517