文字识别

一、概述

  之前准备写一个识别excel的软件,其中涉及到图片的识别,因此尝试使用百度识图的api来实现功能。

二、流程

  申请百度云服务账号→进入百度智能云→找到文字识别→创建应用→下载文档→通过密匙连接到python→进行文字识别

1、百度文字识别网址:文字识别_通用场景文字识别-百度AI开放平台 (baidu.com)

2、创建应用

3、查询到相关的接口信息

4、在python中安装,baidu-api库

pip install baidu-aip
# 可以加载镜像网站,加快安装速度,如使用阿里云镜像命令如下
pip install baidu-aip -i https://mirrors.aliyun.com/pypi/simple/

5、获取通过代码实现文字识别

from aip import AipOcr


def baidu_orc(picfile): # picfile:图片文件名
app_id = '25000434' # 应用的appid
api_key = '5HE11tBsNL9zt29je53Kua5b' # 应用的appkey
secret_key = 'wbNEGGYxFH7HXxFIuCibWkRn7G9YtvoP' # 应用的secretkey
client = AipOcr(app_id, api_key, secret_key) # 实例化一个客户端
with open(picfile, 'rb') as pf: # 通过二进制读入图片
img = pf.read()
message = client.basicAccurate(img)
'''
调用百度高精度文字识别,message为一个字典:
格式为{'words_result': [{"words":"文本内容"}, {"words":"文本内容"}, ...,], 'words_result_num': xxx, 'log_id': xxx}
其中主要获取words内容
'''
my_str = ""
for text in message.get('words_result'): # 识别的内容
obtained_word = text.get('words') + "\n" # 识别每行的内容
my_str += obtained_word # 拼接字符串
return my_str


if __name__ == '__main__':
print(baidu_orc("test.png"))

6、实现结果

截图内容:

 

 识别结果:

 

 

 

 

  

 

posted @ 2021-10-16 10:45  顾子柒  阅读(740)  评论(0)    收藏  举报