首先基于python36
安装模块
pip install pyttsx3
pip install PocketSphinx
pip install SpeechRecognition
pip install comtypes
利用txt文件创建语音文件
from comtypes.client import CreateObject
engine=CreateObject("SAPI.SpVoice")
stream=CreateObject('SAPI.SpFileStream')
from comtypes.gen import SpeechLib
infile='hello.txt'
outfile='demo_audio.wav'
stream.Open(outfile,SpeechLib.SSFMCreateForWrite)
engine.AudioOutputStream=stream
f=open(infile,'r',encoding='utf-8')
theText=f.read()
f.close()
engine.speak(theText)
stream.close()
接着利用之前创建的demo_audio.wav的语音文件
import speech_recognition as sr
audio_file='demo_audio.wav'
r=sr.Recognizer()
with sr.AudioFile(audio_file) as source:
audio =r.record(source)
try: # print('文本内容:',r.recognize_sphinx(audio,language="zh_CN"))
print('文本内容:',r.recognize_sphinx(audio))
except Exception as e:
``print(e)
由于pyhton的语音识别不识别中文可到该网站下载需要的模块
`
https://sourceforge.net/projects/cmusphinx/files/Acoustic%20and%20Language%20Mo dels/
`