Python实现语音识别:SpeechRecognition

本文介绍了Python语音识别库SpeechRecognition的使用,包括其优势、识别器、支持的文件类型以及安装步骤。SpeechRecognition提供了多种API接口,如Google Web Speech API,并且支持离线识别。文章还给出了安装和验证安装的示例。
摘要由CSDN通过智能技术生成

最近在学习语音识别的一些基本知识,也在了解Python的语音识别功能依赖库。分享一下。

 

常用Python语音识别依赖库

Python的依赖库中有一些现成的语音识别软件包。其中包括:

  • apiai
  • google-cloud-speech
  • pocketsphinx
  • SpeechRcognition
  • watson-developer-cloud
  • wit

其中SpeechRecognition,是google出的,专注于语音向文本的转换。

wit 和 apiai 提供了一些超出基本语音识别的内置功能,如识别讲话者意图的自然语言处理功能。

 

SpeechRecognition库的优势

满足几种主流语音 API ,灵活性高

Google Web Speech API 支持硬编码到 SpeechRecognition 库中的默认 API 密钥,无需注册就可使用

SpeechRecognition无需构建访问麦克风和从头开始处理音频文件的脚本, 只需几分钟即可自动完成音频输入、检索并运行。因此易用性很高。

 

SpeechRecognition的识别器

SpeechRecognition 的核心就是识别器类。一共有七个Recognizer API ,包含多种设置和功能来识别音频源的语音,分别是:

  • recognize_bing():Microsoft Bing Speech

  • recognize_google(): Google Web Speech API

  • recognize_google_cloud():Google Cloud Speech - requires installation of the google-cloud-speech package

  • recognize_houndify(): Houndify by SoundHound

  • recognize_ibm():IBM Speech to Text

  • recognize_sphinx():CMU Sphinx - requires installing PocketSphinx

  • recognize_wit():Wit.ai

以上七个中只有 recognition_sphinx()可与CMU Sphinx 引擎脱机工作, 其他六个都需要连接互联网。

另外,SpeechRecognition 附带 Google Web Speech API 的默认 API 密钥,可直接使用它。其他六个 API 都需要使用 API 密钥或用户名/密码组合进行身份验证,因此本文使用了 Web Speech API。

 

SpeechRecognition 的使用要求

To use all of the functionality of the library, you should have:

  • Python 2.6, 2.7, or 3.3+ (required)

需要Python 2.6、2.7和3.3以上的版本

  • PyAudio 0.2.11+ (required only if you need to use microphone input, Microphone)

需要安装PyAudio 0.2.11+的版本

  • PocketSphinx (required only if you need to use the Sphinx recognizer, recognizer_instance.recognize_sphinx)

需要安装PocketSphinx

  • Google API Client Library for Python (required only if you need to use the Google Cloud Speech API, recognizer_instance.recognize_google_cloud)

需要使用Google API Client Library for Python

  • FLAC encoder (required only if the system is not x86-based Windows/Linux/OS X)

需要安装FLAC encoder,

  • 13
    点赞
  • 113
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
要用Python实现语音识别,可以使用SpeechRecognition库。SpeechRecognition是一个支持多种语音识别API的Python库,包括Google Speech Recognition、Microsoft Bing Voice Recognition、IBM Speech to Text等。 以下是一个简单的示例代码,演示如何使用SpeechRecognition库进行语音识别: ```python import speech_recognition as sr # 创建一个Recognizer对象 r = sr.Recognizer() # 从麦克风获取语音输入 with sr.Microphone() as source: print("请开始说话...") audio = r.listen(source) # 使用Google语音识别API进行语音识别 try: text = r.recognize_google(audio, language="zh-CN") print("Google识别结果: " + text) except sr.UnknownValueError: print("Google无法识别该语音") except sr.RequestError as e: print("无法从Google Speech Recognition API请求结果; {0}".format(e)) ``` 这个示例代码中,我们首先创建了一个Recognizer对象,然后使用with语句从麦克风获取语音输入。接着,我们使用Google语音识别API进行语音识别,并将结果打印出来。如果Google无法识别该语音,或者无法从Google Speech Recognition API请求结果,程序会抛出相应的异常。 当然,要使用SpeechRecognition库进行语音识别,你需要先安装它。你可以使用pip命令来安装SpeechRecognition,如下所示: ``` pip install SpeechRecognition ``` 另外,你还需要安装PyAudio库来访问麦克风。你可以使用pip命令来安装PyAudio,如下所示: ``` pip install pyaudio ``` 安装完成之后,你就可以开始使用SpeechRecognition库进行语音识别了。
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值