python调用百度语音识别api
Python  /  管理员 发布于 7年前   176
最近在处理语音检索相关的事。
其中用到语音识别,调用的是讯飞与百度的api,前者使用js是实现,后者用python3实现(因为自己使用python)
环境:
python3.5
centos 7
流程
整个百度语音识别rest api 使用分为三部分:
1 (申请操作)创建应用,获取应用的 API Key 以及 Secret Key。
2 (程序实现)通过已知的 应用的 API Key 以及 Secret Key, 发送post 请求到 https://openapi.baidu.com/oauth/2.0/token 获取 token
3 (程序实现) 通过上步骤获取的 token,通过post, 发送相关的 语音信息 到 http://vop.baidu.com/server_api ,获取识别结果.
以上过程参考百度语音开发文档,或者网上的资料。
python实现
程序整体如下:
import requestsimport jsonimport uuidimport base64def get_token(): url = "https://openapi.baidu.com/oauth/2.0/token" grant_type = "client_credentials" api_key = "NzGBYD0jPFDqVT8VHRYa****" # 自己申请的应用 secret_key = "8439155b9db2040b4acd13b0c*****" # 自己申请的应用 data = {'grant_type': 'client_credentials', 'client_id': api_key, 'client_secret': secret_key} r = requests.post(url, data=data) token = json.loads(r.text).get("access_token") return tokendef recognize(sig, rate, token): url = "http://vop.baidu.com/server_api" speech_length = len(sig) speech = base64.b64encode(sig).decode("utf-8") mac_address = uuid.UUID(int=uuid.getnode()).hex[-12:] rate = rate data = { "format": "wav", "lan": "zh", "token": token, "len": speech_length, "rate": rate, "speech": speech, "cuid": mac_address, "channel": 1, } data_length = len(json.dumps(data).encode("utf-8")) headers = {"Content-Type": "application/json", "Content-Length": data_length} r = requests.post(url, data=json.dumps(data), headers=headers) print(r.text)filename = "two.wav"signal = open(filename, "rb").read()rate = 8000token = get_token()recognize(signal, rate, token)
同时,获取语音信息可以通过:
import scipy.io.wavfilefilename = "two.wav"rate, signal = scipy.io.wavfile.read(filename=filename)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号