语音识别

文本转语音方式一:

import pyttsx3  # pip install pyttsx3

engine = pyttsx3.init()
engine.say('晚上好')
engine.runAndWait()

文本转语音方式二:

from win32com.client import Dispatch

speaker = Dispatch('SAPI.SpVoice')
speaker.Speak('好好学习,天天向上')
# del speaker

 文本文件转为语音文件:(运行失败,原因,comtypes下无gen)

from comtypes.client import CreateObject # pip install comtypes
from comtypes.gen import SpeechLib # comtypes下无gen

engine=CreateObject('SAPI.SpVoice')
stream=CreateObject('SAPI.SpFileStream')
infile='demo.txt'
outfile='audio.wav'
stream.open(outfile,SpeechLib.SSFMCreatForWrite)
stream.open(outfile)
engine.AudioOutputStream=stream

f=open(infile,'r',encoding='utf-8')
theText=f.read()
f.close()
engine.speak(theText)
stream.close()

 文本文件转为语音文件(教学视频代码):(运行失败,原因,comtypes下无gen)

from comtypes.client import CreateObject
from comtypes.gen import SpeechLib
engine=CreateObject('SAPI.SpVoice')
stream=CreateObject('SAPI.SpFileStream')
infile='demo.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()

 语音文件转文本:

import speech_recognition # pip install PocketSphinx 和 pip install SpeechRecognition
r=speech_recognition.Recognizer()

with speech_recognition.AudioFile('demo_audio.wav') as source:
    # 需求将下载好的中文语言包(zh-CN)放到此目录:Lib\site-packages\speech_recognition\pocketsphinx-data
    print(r.recognize_sphinx(r.record(source),language='zh-CN'))

 

 

……

posted @ 2020-01-18 21:48  xiongjiawei  阅读(496)  评论(0)    收藏  举报