一、PocketSphinx  安装

1、RedHat/CentOS:
        yum install pulseaudio-libs-devel
   Ubuntu/Debian:
      apt install libpulse-dev

2、git clone --recursive https://github.com/bambocher/pocketsphinx-python
3、cd pocketsphinx-python
4、vim pocketsphinx-python/deps/sphinxbase/src/libsphinxad/ad_openal.c
    Change
    #include <al.h>
    #include <alc.h>

    to

    #include <OpenAL/al.h>
    #include <OpenAL/alc.h>
5、python setup.py install

二、SpeechRecognition安装

 pip install SpeechRecognition

三、中文模型安装

1、下载 cmusphinx-zh-cn-5.2.tar.gz
地址: 
 https://sourceforge.net/projects/cmusphinx/files/Acoustic%20and%20Language%20Models/Mandarin/
 2、解压后重命名部分文件:
        cmusphinx-zh-cn-5.2 -> zh-CN
        zh_cn.cd_cont_5000 ->  acoustic-model
        zh_cn.lm.bin -> language-model.lm.bin
        zh_cn.dic -> pronounciation-dictionary.dict
     
 3、拷贝到安装目录
cp zh-CN  /xxx/python3/lib/python3.7/site-packages/speech_recognition/pocketsphinx-data/

四、测试代码

# -*- coding:utf-8 -*-

import speech_recognition as sr 

def wav2txt(wav_file,language):
    r = sr.Recognizer()
    audio= ""
    with sr.AudioFile(wav_file) as src:
        audio= r.record(src)
    print(r.recognize_sphinx(audio,language=language))

fp1=r'my16k.wav'
fp2=r'audio-file.flac'

wav2txt(fp1,"zh-CN")
wav2txt(fp2,"en-US")

来源:https://www.pythonf.cn/read/167453

发表评论