0 建议学时
2学时
1 简介
百度人工智能平台-站在巨人的肩膀上
https://ai.baidu.com/
控制台->立即注册
百度人工智能平台
APP Key 和 Secret Key
AI接入指南
https://ai.baidu.com/ai-doc/REFERENCE/Ck3dwjgn3
百度智能云视频参考
https://abcxueyuan.baidu.com/#/course_detail?id=15431&courseId=15431
2 接口调用步骤
- 授权:使用API Key和Secret Key,从https://openapi.baidu.com/oauth/2.0/token 获取token;
- 通过request的post方法访问接口:https://aip.baidubce.com/rpc/2.0/mt/texttrans/v1?access_token=*****
- 将结果从json转为python字典获取最终结果。
2.1 鉴权认证机制
https://ai.baidu.com/ai-doc/REFERENCE/Ck3dwjhhu
授权:使用API Key和Secret Key 从https://aip.baidubce.com/oauth/2.0/token获取token
pip install requests -i https://pypi.douban.com/simple
import requests
apiUrl = 'https://openapi.baidu.com/oauth/2.0/token'
data = {
'grant_type':'client_credentials', # 固定值
'client_id':'IrrjisS003ymUAyT2j9e7', # 替换API Key
'client_secret':'L81S08vSmCjAkFOlaHs85FYU2lxEy', # 替换Secret Key
}
try:
response = requests.get(apiUrl, params = data)
r=response.json()
print(r.get("access_token"))
except Exception as e:
print(e)
import requests
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【官网获取的AK】&client_secret=【官网获取的SK】'
response = requests.get(host)
if response:
r = response.json())
print(r.get("access_token"))
通过request的post方法访问接口 https://aip.baidubce.com/rpc/2.0/mt/texttrans/v1?access_token=*****
import requests
import json
url = 'https://aip.baidubce.com/rpc/2.0/mt/texttrans/v1?access_token=' + token
q = 'hello'; # 需要翻译的内容
from_lang = 'en'; # 英语语种参考https://ai.baidu.com/ai-doc/MT/4kqryjku9#
to_lang = 'zh' ; # 中文
term_ids = ' '; #术语库id,多个逗号隔开
# 请求结构
headers = {'Content-Type': 'application/json'}
payload = {'q': q, 'from': from_lang, 'to': to_lang, 'termIds' : term_ids}
# 发送请求
r = requests.post(url, params=payload, headers=headers)
将结果从json转为python字典获取最终结果
result = r.json()
# Show response
translate_results = json.dumps(result, indent=4, ensure_ascii=False)
print(translate_results)
words = result['result']['trans_result'][0]['dst']
print("翻译的结果是:%s" % words)
2.2 百度接口练习
请编写一个带界面的翻译软件
import requests
import json
# token=""
# apiUrl = 'https://openapi.baidu.com/oauth/2.0/token'
# data = {
# 'grant_type':'client_credentials', # 固定值
# 'client_id':'IrrjisS003ymUAyT2j9e7lvM', # API Key
# 'client_secret':'L81S08vSmCjAkFOlaHs85FYU2lxEytDT', # Secret Key
# }
# try:
# r = requests.get(apiUrl, params = data).json()
# print(r.get("access_token"))
# except Exception as e:
# print(e)
token="24.5bc87860c5968e330548f11f74500ea0.2592000.1653665771.282335-26111038"
url = 'https://aip.baidubce.com/rpc/2.0/mt/texttrans/v1?access_token=' + token
q = 'hello'; # example: hello 输入query
# For list of language codes, please refer to `https://ai.baidu.com/ai-doc/MT/4kqryjku9#语种列表
from_lang = 'en'; # example: en
to_lang = 'zh'; # example: zh
term_ids = ''; #术语库id,多个逗号隔开
# Build request
headers = {'Content-Type': 'application/json'}
payload = {'q': q, 'from': from_lang, 'to': to_lang, 'termIds' : term_ids}
# Send request
r = requests.post(url, params=payload, headers=headers)
result = r.json()
# Show response
translate_results=json.dumps(result, indent=4, ensure_ascii=False)
print(translate_results)
words = result['result']['trans_result'][0]['dst']
print("翻译的结果是:%s" % words)
百度图像技术接口
import requests
import base64
request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"
f = open('1.jpg', 'rb') # 二进制方式打开图片文件
img = base64.b64encode(f.read())
params = {"image":img}
access_token = '24.debfa969513178ef740ba8c75b6907ba.2592000.1656601166.282335-26110673'
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
with open("result.jpg", 'wb') as f:
image = response.json()['image'] # 获取返回json格式中image
image = base64.b64decode(image) # 解码
f.write(image)