第三期【百度大脑新品体验】手写诗一首,AI来读
wangwei8638 发布于2019-06 浏览:15694 回复:66
0
收藏
最后编辑于2022-04

偶然闲下来,翻出了年轻时曾写下的东西,时光荏苒,一声长叹。从中选出一首,将墨迹转为铅字,留作纪念。

一.平台接入

参照之前文档:

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)

五.意见建议

对于书写工整的手写文字识别准确度很高,测试准确度:楷体汉字达到100%,行书95%,书名号等标点符号也能正确识别,但对潦草字体和英文字母识别率不高,建议有针对性进行优化。

收藏
点赞
0
个赞
共66条回复 最后由用户已被禁言回复于2022-04
#7134******14回复于2019-06
#6 wangwei8638回复
再翻翻有没有夹着初恋的照片,呵呵

我怕现在恋人看到~烧了~~!

0
#6wangwei8638回复于2019-06
#4 134******14回复
昨天收拾书竟然翻出了刚上大学时的心路笔记~ 虽然很幼稚,但满满的都是回忆
展开

再翻翻有没有夹着初恋的照片,呵呵

0
#5wangwei8638回复于2019-06
#3 134******14回复
我不知道整页的日记能识别出来不~

貌似没有字数限制

0
#4134******14回复于2019-06

昨天收拾书竟然翻出了刚上大学时的心路笔记~

虽然很幼稚,但满满的都是回忆

0
#3134******14回复于2019-06

我不知道整页的日记能识别出来不~

0
#2134******14回复于2019-06

一定是你的“风”太潦草了~我上也认不得

0
TOP
切换版块