import json
import base64
import os
import requestsRATE = "16000"
FORMAT = "wav"
CUID="wate_play"
DEV_PID="1536"def get_token():
server = "https://openapi.baidu.com/oauth/2.0/token?"
grant_type = "client_credentials"
#API Key
client_id = "vyYLov63W6x33nIPvwVdLLsX"
#Secret Key
client_secret = "EzgQoFF9xp62SeGCXCBaD8FjWNxvl9kZ"#拼url
url ="%sgrant_type=%s&client_id=%s&client_secret=%s"%(server,grant_type,client_id,client_secret)
#获取token
res = requests.post(url)
token = json.loads(res.text)["access_token"]return tokendef get_word(token):
wavPath = './static/yuyin/record.wav'
with open(wavPath, "rb") as f:
speech = base64.b64encode(f.read()).decode('utf8')
size = os.path.getsize(wavPath)
headers = { 'Content-Type' : 'application/json'}
url = "https://vop.baidu.com/server_api"
data=https://www.it610.com/article/{"format":FORMAT,
"rate":RATE,
"dev_pid":DEV_PID,
"speech":speech,
"cuid":CUID,
"len":size,
"channel":1,
"token":token,
}
try:
req = requests.post(url,json.dumps(data),headers)
result = json.loads(req.text)
ret=result["result"][0]
except :
ret = None
return ret[:-1]
wavPath 为wav文件路径
【python|python实现语音转文字(百度接口)】Secret key 和Api key为百度接口获取的参数
推荐阅读
- python|python调用百度文字转语音api
- python|《python机器学习从入门到高级》(线性回归和正则化(含源码))
- 深度学习|yolov5之可视化特征图和检测结果
- 如何在Cordova中使用语音识别API(将语音转换为文本)
- 7个最佳语音命令和语音识别相关的javascript库
- 3个Python初学者编程项目
- 前端|Web渗透测试-实战 方法 思路 总结
- python|内网渗透横向攻击流程
- 使用shutil.copy复制具有文件名的文件路径列表