Ubuntu16.04实现Sphinx离线语音识别

  1. 自带Python2.7或3.0+版本都可以 使用的是3.5编译
  2. 需要安装SpeechRecognition模块
  3. 需要.wav作为测试数据

1 安装SpeechRecognition模块

pip install  SpeechRecognition` 

若是3版本则使用pip3 .
2 安装验证

>>> import speech_recognition as sr
>>> sr.__version__
'3.8.1'

还可以看看它具备属性函数
Ubuntu16.04实现Sphinx离线语音识别_第1张图片
3 创建Recognizer实例

>>> r = sr.Recognizer()

Ubuntu16.04实现Sphinx离线语音识别_第2张图片
每个Recognizer实例有七个语音识别方法:

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

这次使用recognize_sphinx(),安装:

pip install PocketSphinx

如果出现以下错误:
这里写图片描述
输入图示命令:

sudo apt-get build-dep python-PocketSphinx

再次安装:

sudo pip install PocketSphinx --upgrade

则提示安装成功

4 使用测试

#!/usr/bin/env python3

# NOTE: this example requires PyAudio because it uses the Microphone class

import speech_recognition as sr

# obtain audio from the microphone
r = sr.Recognizer()
harvard = sr.AudioFile('harvard.wav')
 with harvard as source:
     audio = r.record(source)
# recognize speech using Sphinx
try:
    print("Sphinx thinks you said " + r.recognize_sphinx(audio))
except sr.UnknownValueError:
    print("Sphinx could not understand audio")
except sr.RequestError as e:
    print("Sphinx error; {0}".format(e))

前期可以在解析器上一行行输入是否能运行,测试语音harvard.wav在https://github.com/realpython/python-speechrecognition/tree/master/audio_files
可以下载。或者自己提供也行,也可以通过调用麦克风录音保存文件。

with sr.Microphone() as source:
    print("Say something!")
    audio = r.listen(source)

5 结果
这里写图片描述

效果其实比百度api好一些,因为是英文识别
尝试其他的应用接口的话可以查阅其他文档。

参考:
1.https://github.com/Uberi/speech_recognition
2.https://realpython.com/python-speech-recognition/
3.http://www.mamicode.com/info-detail-93746.html
4,https://blog.csdn.net/qiaocuiyu/article/details/52093509
5.http://blog.itpub.net/16582684/viewspace-1243341/

你可能感兴趣的:(AI)