侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

python调用百度语音REST API

Python  /  管理员 发布于 7年前   174

本文实例为大家分享了python调用百度语音REST API的具体代码,供大家参考,具体内容如下

(百度的rest接口的部分网址发生了一定的变化,相关代码已更新)

百度通过 REST API 的方式给开发者提供一个通用的 HTTP 接口,基于该接口,开发者可以轻松的获得语音合成与语音识别能力。SDK中只提供了PHP、C和JAVA的相关样例,使用python也可以灵活的对端口进行调用,本文描述了简单使用Python调用百度语音识别服务 REST API 的简单样例。

1、语音识别与语音合成的调用

注册开发者帐号和创建应用的过程就不再赘述,百度的REST API在调用过程基本分为三步:

  • 获取token
  • 向Rest接口提交数据
  • 处理返回数据

具体代码如下所示:

#!/usr/bin/python3import urllib.requestimport urllibimport jsonimport base64class BaiduRest:  def __init__(self, cu_id, api_key, api_secert):    # token认证的url    self.token_url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s"    # 语音合成的resturl    self.getvoice_url = "http://tsn.baidu.com/text2audio?tex=%s&lan=zh&cuid=%s&ctp=1&tok=%s"    # 语音识别的resturl    self.upvoice_url = 'http://vop.baidu.com/server_api'    self.cu_id = cu_id    self.getToken(api_key, api_secert)    return  def getToken(self, api_key, api_secert):    # 1.获取token    token_url = self.token_url % (api_key,api_secert)    r_str = urllib.request.urlopen(token_url).read()    token_data = json.loads(r_str)    self.token_str = token_data['access_token']    pass  def getVoice(self, text, filename):    # 2. 向Rest接口提交数据    get_url = self.getvoice_url % (urllib.parse.quote(text), self.cu_id, self.token_str)    voice_data = urllib.request.urlopen(get_url).read()    # 3.处理返回数据    voice_fp = open(filename,'wb+')    voice_fp.write(voice_data)    voice_fp.close()    pass  def getText(self, filename):    # 2. 向Rest接口提交数据    data = {}    # 语音的一些参数    data['format'] = 'wav'    data['rate'] = 8000    data['channel'] = 1    data['cuid'] = self.cu_id    data['token'] = self.token_str    wav_fp = open(filename,'rb')    voice_data = wav_fp.read()    data['len'] = len(voice_data)    data['speech'] = base64.b64encode(voice_data).decode('utf-8')    post_data = json.dumps(data)    r_data = urllib.request.urlopen(self.upvoice_url,data=bytes(post_data,encoding="utf-8")).read()    # 3.处理返回数据    return json.loads(r_data)['result']if __name__ == "__main__":  # 我的api_key,供大家测试用,在实际工程中请换成自己申请的应用的key和secert  api_key = "SrhYKqzl3SE1URnAEuZ0FKdT"   api_secert = "hGqeCkaMPb0ELMqtRGc2VjWdmjo7T89d"  # 初始化  bdr = BaiduRest("test_python", api_key, api_secert)  # 将字符串语音合成并保存为out.mp3  bdr.getVoice("你好北京邮电大学!", "out.mp3")  # 识别test.wav语音内容并显示  print(bdr.getText("out.wav"))

2、调用pyaudio使用麦克风录制声音

python中的pyaudio库可以直接通过麦克风录制声音,可使用pip进行安装。我们可以通过调用该库,获取到wav测试语音。
具体代码如下所示:

#!/usr/bin/python3# -*- coding: utf-8 -*-from pyaudio import PyAudio, paInt16 import numpy as np from datetime import datetime import waveclass recoder:  NUM_SAMPLES = 2000   #pyaudio内置缓冲大小  SAMPLING_RATE = 8000  #取样频率  LEVEL = 500     #声音保存的阈值  COUNT_NUM = 20   #NUM_SAMPLES个取样之内出现COUNT_NUM个大于LEVEL的取样则记录声音  SAVE_LENGTH = 8     #声音记录的最小长度:SAVE_LENGTH * NUM_SAMPLES 个取样  TIME_COUNT = 60   #录音时间,单位s  Voice_String = []  def savewav(self,filename):    wf = wave.open(filename, 'wb')     wf.setnchannels(1)     wf.setsampwidth(2)     wf.setframerate(self.SAMPLING_RATE)     wf.writeframes(np.array(self.Voice_String).tostring())     # wf.writeframes(self.Voice_String.decode())    wf.close()   def recoder(self):    pa = PyAudio()     stream = pa.open(format=paInt16, channels=1, rate=self.SAMPLING_RATE, input=True,       frames_per_buffer=self.NUM_SAMPLES)     save_count = 0     save_buffer = []     time_count = self.TIME_COUNT    while True:      time_count -= 1      # print time_count      # 读入NUM_SAMPLES个取样      string_audio_data = stream.read(self.NUM_SAMPLES)       # 将读入的数据转换为数组      audio_data = np.fromstring(string_audio_data, dtype=np.short)      # 计算大于LEVEL的取样的个数      large_sample_count = np.sum( audio_data > self.LEVEL )      print(np.max(audio_data))      # 如果个数大于COUNT_NUM,则至少保存SAVE_LENGTH个块      if large_sample_count > self.COUNT_NUM:        save_count = self.SAVE_LENGTH       else:         save_count -= 1      if save_count < 0:        save_count = 0       if save_count > 0 :       # 将要保存的数据存放到save_buffer中        #print save_count > 0 and time_count >0        save_buffer.append( string_audio_data )       else:       #print save_buffer      # 将save_buffer中的数据写入WAV文件,WAV文件的文件名是保存的时刻        #print "debug"        if len(save_buffer) > 0 :           self.Voice_String = save_buffer          save_buffer = []           print("Recode a piece of voice successfully!")          return True      if time_count==0:         if len(save_buffer)>0:          self.Voice_String = save_buffer          save_buffer = []           print("Recode a piece of voice successfully!")          return True        else:          return Falseif __name__ == "__main__":  r = recoder()  r.recoder()  r.savewav("test.wav")  

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


  • 上一条:
    python调用百度REST API实现语音识别
    下一条:
    python调用百度语音识别api
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客