百度API - 图像识别 申请账号&使用
进入图像识别应用
调用图像识别
调用 百度API-图像识别 出现如下错误。
{u'error_code': 6, u'error_msg': u'No permission to access data'}
因为图像识别中需要用到文字识别功能,所以对应文字识别的功能也需要选上即可。
代码样例
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
利用百度图像识别api做文字识别,目的是为了做12306的图片校验。
"""
import os
from aip import AipOcr
APP_ID=os.environ['baidu_appid']
API_KEY=os.environ['baidu_api_key']
SECRET_KEY=os.environ['baidu_secret_key']
# 初始化AipFace对象
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
# 定义参数变量
options = {
'detect_direction': 'true',
'language_type': 'CHN_ENG',
}
class BaiDu(object):
# 获取图片
def get_file_content(self, file_path):
"""获取图片数据"""
with open(file_path, 'rb') as fp:
return fp.read()
def get_result(self, image_url):
"""
识别结构
:return: 返回识别结果
"""
image = self.get_file_content(image_url)
return client.basicGeneral(image, options)
if __name__ == "__main__":
# 获取图片
baidu = BaiDu()
# 得到识别结果
result = baidu.get_result("../tmp/code.png")
# 输出识别结果
print result
print(result['words_result'][0]['words'])