码迷,mamicode.com
首页 > 其他好文 > 详细

十九、语音识别

时间:2020-02-28 13:40:09      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:需要   nand   桌面   print   wait   pes   text   pre   获取   

文本转换为语音
     使用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 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 是一个用于语音转换文本的开源 API。它是一个轻量级的语音识别引擎,尽管在桌面端也能很好地工作,它还专门为手机和移动设备做过调优
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)



十九、语音识别

标签:需要   nand   桌面   print   wait   pes   text   pre   获取   

原文地址:https://www.cnblogs.com/dangjingwei/p/12376645.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!