python 文字转语音

#pyttsx3文字转语音

import pyttsx3
engine2 = pyttsx3.init()
while True:
content = input('请输入播放内容:')
engine2.say(content)
engine2.runAndWait()

#文字转语音
from win32com.client import Dispatch
msg = '你好'
speaker = Dispatch('SAPI.SpVoice')
speaker.Speak(msg)
del speaker

#comtypes模块 文字文件转语音文件(当前仅支持英文)

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()

#PocketSphinx模块 SpeechRecognition模块 语音转文字
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)
#https://sourceforge.net/projects/cmusphinx/files/Acoustic%20and%20Language%20Models/ 中文包的网址
posted @ 2022-10-08 15:58  记录——去繁就简  阅读(487)  评论(0)    收藏  举报