1. 程式人生 > >Ubuntu16.04實現Sphinx離線語音識別

Ubuntu16.04實現Sphinx離線語音識別

Ubuntu16.04實現Sphinx離線語音識別

 

版權宣告:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/boke14122621/article/details/79871224

  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'

還可以看看它具備屬性函式 
這裡寫圖片描述 
3 建立Recognizer例項

>>> r = sr.Recognizer()

這裡寫圖片描述 
每個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/