最后编辑于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
个赞
TOP
切换版块
过奖
字写的真好
先落地,使用过程中还能收集数据,机器学习不断迭代
得提供多少数据才能让一项AI技术落地呢
有这么智能?
不断的测试才能不断的提高
据说现在高中老师都完全不用自己批卷了
那是
背后肯定需要大量的数据测试~
当然会越来越完善
估计以后各种字体都能很好的识别的
人才
刚公开吧
2019年竞赛的报名通道藏的好深,都找不到?吐槽一句,发帖的验证码能不能用AI升级一下?试了几十次,终于放弃
草书只要通过训练也是能识别的
只要字迹不太潦草,应该没有问题
见笑^_^
手写一首诗,牛啊
可以啊,394885854
不然发给你测试一下~!