十九、语音识别

巧了我就是萌 提交于 2020-02-28 13:29:45

文本转换为语音
     使用pyttsx、SAPI两种方式可以将文本转化成语音。但是都是通过参数输入文本内容。使用SpeechLib可以从文本文件中获取输入,再将其转化为语言。

使用 pyttsx 实现文本转换语音
  
 import pyttsx3 as pyttsx
 engine=pyttsx.init()
 engine.say('你好 pyttsx')
 engine.runAndWait()
 
使用 SAPI 实现文本转换语音
from win32com.client import Dispatch
msg="你好 SAPI"
speaker = Dispatch('SAPI.SpVoice')
speaker.Speak(msg)
del speaker
 
使用 SpeechLib 实现文本转换语音
#导入转化需要的对象和转化模块(个人理解)from comtypes.client import CreateObjectfrom 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=streamf=open(infile,'r',encoding='utf-8')theText=f.read()f.close()engine.speak(theText)stream.close()
语音转换为文本
  PocketSphinx 是一个用于语音转换文本的开源 API。它是一个轻量级的语音识别引擎,尽管在桌面端也能很好地工作,它还专门为手机和移动设备做过调优。
import speech_recognition as sraudio_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)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!