Home >Backend Development >Python Tutorial >Python programming to implement the interface docking method of Baidu AI open platform, detailed explanation and practical guide
Python programming implements the interface docking method of Baidu AI open platform, detailed explanation and practical guide
Install Python's request library requests, which can be installed through the pip command:
pip install requests
The following is a code example to obtain access_token:
import requests # 定义鉴权API的URL auth_url = 'https://aip.baidubce.com/oauth/2.0/token' # 设置API Key和Secret Key api_key = 'your_api_key' secret_key = 'your_secret_key' # 构造鉴权API的参数 params = { 'grant_type': 'client_credentials', 'client_id': api_key, 'client_secret': secret_key } # 发送http请求 response = requests.get(auth_url, params=params) # 解析返回结果 access_token = response.json()['access_token']
The URL of the speech recognition interface is:
https://vop.baidu.com/server_api
The following is a code example of the speech recognition interface:
import requests import base64 # 定义语音识别API的URL speech_url = 'https://vop.baidu.com/server_api' # 设置要进行语音识别的语音文件路径 audio_file = 'path/to/audio/file.wav' # 将语音文件转换成base64编码 with open(audio_file, 'rb') as f: speech_data = f.read() speech_base64 = base64.b64encode(speech_data).decode('utf-8') # 构造语音识别API的参数 params = { 'dev_pid': '1536', # 中文普通话 'cuid': 'your_cuid', 'token': access_token, 'speech': speech_base64, 'len': len(speech_data) } # 发送http请求 response = requests.post(speech_url, data=params, headers={'content-type': 'application/json'}) # 解析返回结果 result = response.json()
The above is the detailed content of Python programming to implement the interface docking method of Baidu AI open platform, detailed explanation and practical guide. For more information, please follow other related articles on the PHP Chinese website!