第三期【百度大脑新品体验】手写诗一首,AI来读
wangwei8638 发布于2019-06 浏览:15676 回复: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
#27wangwei8638回复于2019-07
#26 知道认证团回复
字写的真好

过奖

0
#26知道认证团回复于2019-07

字写的真好

0
#25wangwei8638回复于2019-07
#24 134******14回复
得提供多少数据才能让一项AI技术落地呢

先落地,使用过程中还能收集数据,机器学习不断迭代

0
#24134******14回复于2019-07
#22 小雨青青润无声回复
不断的测试才能不断的提高

得提供多少数据才能让一项AI技术落地呢

0
#23wangwei8638回复于2019-07
#21 小雨青青润无声回复
据说现在高中老师都完全不用自己批卷了

有这么智能?

0
#22小雨青青润无声回复于2019-07
#19 134******14回复
背后肯定需要大量的数据测试~

不断的测试才能不断的提高

0
#21小雨青青润无声回复于2019-07
#18 wangwei8638回复
当然会越来越完善

据说现在高中老师都完全不用自己批卷了

0
#20wangwei8638回复于2019-07
#19 134******14回复
背后肯定需要大量的数据测试~

那是

0
#19134******14回复于2019-07
#18 wangwei8638回复
当然会越来越完善

背后肯定需要大量的数据测试~

0
#18wangwei8638回复于2019-07
#17 小雨青青润无声回复
估计以后各种字体都能很好的识别的

当然会越来越完善

0
#17小雨青青润无声回复于2019-07
#13 wangwei8638回复
草书只要通过训练也是能识别的

估计以后各种字体都能很好的识别的

0
#16知道认证团回复于2019-07

人才

0
#15wangwei8638回复于2019-07
#14 AiOnTheWay回复
2019年竞赛的报名通道藏的好深,都找不到?吐槽一句,发帖的验证码能不能用AI升级一下?试了几十次,终于放弃
展开

刚公开吧

0
#14AiOnTheWay回复于2019-07

2019年竞赛的报名通道藏的好深,都找不到?吐槽一句,发帖的验证码能不能用AI升级一下?试了几十次,终于放弃

0
#13wangwei8638回复于2019-07
#12 小雨青青润无声回复
只要字迹不太潦草,应该没有问题

草书只要通过训练也是能识别的

0
#12小雨青青润无声回复于2019-07
#3 134******14回复
我不知道整页的日记能识别出来不~

只要字迹不太潦草,应该没有问题

0
#11wangwei8638回复于2019-07
#10 liguanghui2588回复
手写一首诗,牛啊

见笑^_^

0
#10liguanghui2588回复于2019-06

手写一首诗,牛啊

0
#9wangwei8638回复于2019-06

可以啊,394885854

0
#8134******14回复于2019-06
#5 wangwei8638回复
貌似没有字数限制

不然发给你测试一下~!

0
TOP
切换版块