python api调用百度ai平台_Python 百度AI接口调用

接口调用准备

1.进入网站:

https://console.bce.baidu.com/?_=1535519624081&fromai=1#/aip/overview

2.选择产品服务里的文字识别,点击创建应用

3.选择文字识别,创建完成

4.创建后选择该应用,可以看到里面有AppID、API Key、Secret Key三个参数,将三个参数放入下面实例对应的三个地方

5.参考实例,然后根据自己的需求来写程序

6.参考这里也行:http://ai.baidu.com/forum/topic/show/867951

两种调用方法

方法1:下载并调用相关模块

实例

(文字识别)

from aip import AipOcr

#import configparser

#在这三个地方填入参数

api_id = ''

api_key = ''

secret_key = ''

client = AipOcr(api_id, api_key, secret_key) #到这里都是固定用法

with open('a.jpg', 'rb') as f:

img = f.read()

text = client.basicGeneral(img)

#通用文字识别方式识别图片内容,一天50000次,像什么高精度版就是basicAccurate,具体参考下方aipocr模块文档

for each in text.get('words_result'):

print(each.get('words')) #返回的是个json,内容在这里面

aipocr模块参考

方法2:按照官方文档步骤访问接口

实例

(动物识别)

1.获取assess_token(有效期30天)

import requests

api_key = ''

secret_key = ''

url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + str(api_key) + '&client_secret=' + str(secret_key)

res = eval(requests.get(url).text)

assess_token = res['access_token']

print(assess_token)

2.调用接口

import requests

import base64

url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + str(api_key) + '&client_secret=' + str(secret_key)

res = requests.get(url).text

a = eval(res)

access_token = a['access_token']

#print(access_token)

animal = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/animal?access_token=' + str(access_token)

header = {

'Content-Type':'application/x-www-form-urlencoded'

}

data = {}

with open('animal.jpg', 'rb') as f:

image = base64.b64encode(f.read())

data["image"] = str(image, 'utf-8')

res2 = requests.post(url=animal,data=data, headers=header).text

for each in eval(res2)['result']:

print(each['name'], '\t相似度:', each['score'])

返回结果:

狮子 相似度: 0.997508

白狮 相似度: 9.78454e-05

中国藏獒 相似度: 4.06185e-05

藏獒 相似度: 3.36377e-05

美洲狮 相似度: 3.19272e-05

狮虎兽 相似度: 2.50163e-05

语音识别示例参考

from aip import AipSpeech

APP_ID = ''

API_KEY = ''

SECRET_KEY = ''

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

# 读取文件

def get_file_content(filePath):

with open(filePath, 'rb') as fp:

return fp.read()

# 识别本地文件

result = client.asr(get_file_content('a.wav'), 'wav', 16000, {

'dev_pid': 1536,

})

print(result)

人脸搜索示例参考

import requests

import base64

api_key = ''

secret_key = ''

url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + str(api_key) + '&client_secret=' + str(secret_key)

res = eval(requests.get(url).text)

access_token = res['access_token']

face_search = 'https://aip.baidubce.com/rest/2.0/face/v3/search' + '?access_token=' + str(access_token)

header = {

'Content-Type':'application/json'

}

data = {

"image_type":"BASE64",

"group_id_list":"group_1", #这个要在人脸识别的应用里创建人脸库,并添加相应图片

#这里弄了几个明星的照片

}

with open('3.jpg', 'rb') as f:

image = base64.b64encode(f.read())

data["image"] = str(image, 'utf-8')

res2 = eval(requests.post(url=face_search, data=data, headers=header).text)

#转成dict

print("识别结果:", res2["result"]["user_list"][0]["user_id"])

print("准确率:", res2["result"]["user_list"][0]["score"])

运行结果:

识别结果: gakki

准确率: 96.145095825195

人脸对比示例参考

官方文档参考

api错误信息及url整理

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值