Python调用百度手写识别API,将手写笔记图片转换成文字

  • 事件起因

家里人有十几页手写笔记想要转成文字格式。网上搜了一下发现了百度有手写文字图片识别的api,于是拿来试试。
虽然最终效果并不理想,还是当做一次测试记录一下。
手里有手写笔记想要识别一下,也可以直接参考下面代码试试。

  • 代码

代码部分没什么好解释的
get_file_content()读取图片
get_access_token()你的百度开发者访问token
recognise_handwriting_pic()调用百度图片识别API,识别手写文字
最下面的for循环实际上就是逐个图片上传识别,输出结果。

import requests
import json
import base64


def get_file_content(filePath):
	""" 读取图片base64 """
	with open(filePath, 'rb') as fp:
		return base64.b64encode(fp.read())


def get_access_token():
	# API_Key,Secret_Key 需要在 https://console.bce.baidu.com/ai/?fromai=1#/ai/ocr/app/list 创建应用才能获得
	API_Key = '你的API_Key'
	Secret_Key = '你的Secret_Key'
	r = requests.post('https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+API_Key+'&client_secret='+Secret_Key)
	print(r.text)
	j = json.loads(r.text)
	print(j.get('access_token'))


def recognise_handwriting_pic(access_token,image_path):
	image = get_file_content(image_path)
	r = requests.post(
		url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting?access_token='+access_token,
		headers={"Content-Type":"application/x-www-form-urlencoded"},
		data = {'image':image})
	#print(r.text)
	j = json.loads(r.text)
	words_result = j.get('words_result')
	for i in words_result:
		print(i.get('words'))

access_token = get_access_token()  # 获取一次保存下来就够了,一般1个月有效期
print(access_token)

for p in range(1,25):
	print('\n\n%s\n\n第%d页'%(' *'*20,p))
	recognise_handwriting_pic(access_token,image_path='C:/Users/kindle/Desktop/wzsb/'+str(p)+'.jpg')

  • 手写图片示例

手写图片示例

  • 识别结果

品种是工艺害.花色变化了
造型
岩物
无装饰设计先花敌”,是说不通纸,彩鸥无花你是鄙7
  • 结论

上面挑出来这张图,已经算是本次测试十几张图片中识别的不错的了,
总体而言,很可能你花在校对的时间会比你纯手打一遍还要长。
看来如果写的不是很标准,识别的出来的效果比较一般。
还有很多需要改进的地方。
嘛,这个功能也是刚刚上线测试,就不过多评论了。

PS:家人手中十几页手稿,因冷懒得打字。给其找了个语言录入APP,朗读了一遍,然后校对一下,结束。

猜你喜欢

转载自blog.csdn.net/watfe/article/details/83351805