CUM Sphinx的安装及使用

1. linux上安装CUM Sphinx

保证Python环境为3.0以上。

依次输入下列命令:

pip3 install SpeechRecognition  # python3.0+
sudo apt-get install python3 python3-all-dev python3-pip build-essential swig git libpulse-dev libasound2-dev
pip3 install pocketsphinx
pip3 install pydub
sudo apt install ffmpeg

然后用下面的代码测试一下:

import speech_recognition as sr

# 读入本地".wav"格式的音频文件
r = sr.Recognizer()
harvard = sr.AudioFile('harvard.wav')

# 通过麦克风输入音频(安装过Microphone模块后才可使用)
# 安装链接:https://github.com/Uberi/speech_recognition
# with sr.Microphone() as source:
#     print("Say something!")
#     audio = r.listen(source)

 with harvard as source:
     audio = r.record(source)
# 调用Sphinx模块识别音频文件中的语言
try:
    print("Sphinx thinks you said " + r.recognize_sphinx(audio))
    # 将结果保存到文本文件
   	txtName = 'savedSpeechRecognition.txt'
    with open(txtName, 'w+') as f:
        lines = f.readlines()  # 读取所有行数据
     f.write(r.recognize_sphinx(audio))
        f.write('\n')
except sr.UnknownValueError:
    print("Sphinx could not understand audio")
except sr.RequestError as e:
    print("Sphinx error; {0}".format(e))

 

2. Windows安装 

  1.  pip install SpeechRecognition

  2.  找到PocketSphinx的离线包进行安装,网址如下:https://www.lfd.uci.edu/~gohlke/pythonlibs/#pocketsphinx

     选择合适自己机型的包下载

 3.  pip install pocketsphinx-0.1.15-cp37-cp37m-win_amd64.whl (换成你下载好的.whl文件就行)

 

你可能感兴趣的:(自然语言处理)