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/ 中文包的网址


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM