基于百度AI的文字识别-Python

基于百度AI的文字识别-Python

http://blog.csdn.net/u012236875/article/details/74726035

原创 2017年07月07日 15:45:22

 

使用百度AI的文字识别库,做出的调用示例,其中filePath是图片的路径,可以自行传入一张带有文字的图片,进行识别。

下载baidu-aip这个库,可以直接使用pip下载:pip install baidu-aip,也可以在PyCharm等开发工具中下载。

然后运行下列代码即可。

 

[python] view plain copy
 
  1. # -*- coding: UTF-8 -*-  
  2.   
  3. from aip import AipOcr  
  4. import json  
  5.   
  6. # 定义常量  
  7. APP_ID = '9851066'  
  8. API_KEY = 'LUGBatgyRGoerR9FZbV4SQYk'  
  9. SECRET_KEY = 'fB2MNz1c2UHLTximFlC4laXPg7CVfyjV'  
  10.   
  11. # 初始化AipFace对象  
  12. aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)  
  13.   
  14. # 读取图片  
  15. filePath = "WechatIMG1.jpeg"  
  16. def get_file_content(filePath):  
  17.     with open(filePath, 'rb') as fp:  
  18.         return fp.read()  
  19.   
  20. # 定义参数变量  
  21. options = {  
  22.   'detect_direction': 'true',  
  23.   'language_type': 'CHN_ENG',  
  24. }  
  25.   
  26. # 调用通用文字识别接口  
  27. result = aipOcr.basicGeneral(get_file_content(filePath), options)  
  28. print(json.dumps(result).decode("unicode-escape"))  
输出结果:

 

 

[python] view plain copy
 
  1. {"log_id": 1424393327, "direction": 0, "words_result_num": 2, "words_result": [{"words": "不就果钱么!"}, {"words": "所技"}]}  
我的这次识别有几个错误,与图片不清晰有关,下面是我识别的图片。

 

 

 

通用文字识别 返回数据参数详情

字段必选类型说明
direction number 图像方向,当detect_direction=true时存在。
- -1:未定义,
- 0:正向,
- 1: 逆时针90度,
- 2:逆时针180度,
- 3:逆时针270度
log_id number 唯一的log id,用于问题定位
words_result_num number 识别结果数,表示words_result的元素个数
words_result array 定位和识别结果数组
+words string 识别结果字符串
版权声明:本文为博主原创文章,未经博主允许不得转载。
 
posted @ 2018-01-17 19:41  sky20080101  阅读(277)  评论(0)    收藏  举报