我是靠谱客的博主 诚心唇彩,这篇文章主要介绍python语音识别,现在分享给大家,希望可以做个参考。

文本转语音

使用pyttsx和SAPI完成文本转语音

复制代码
1
2
3
pip install pyttsx3 # 或者在末尾加上镜像 -i https://mirror.baidu.com/pypi/simple

有两种文本转语音的方法:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
""" 直接把输入的文字转语音 """ # one import pyttsx3 as pyttsx engine = pyttsx.init() engine.say('啊对对对') engine.runAndWait() # two from win32com.client import Dispatch speaker = Dispatch('SAPI.SpVoice') speaker.Speak('啊对对对') del speaker

使用SpeechLib实现文本文件转语音

复制代码
1
2
pip install comtypes
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
""" 使用SpeechLib实现文本文件转语音 """ from comtypes.client import CreateObject from comtypes.gen import SpeechLib #engine 相当于源头 stream相当于流 (水龙头和水) 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,是轻量级的语音识别引擎。

复制代码
1
2
3
pip install PocketSphinx pip install SpeechRecognition -i https://mirror.baidu.com/pypi/simple

注意:

安装speech_recognition之后是不支持中文的,需要在Sphinx语音识别工具包里下载对应的普通画声学和语言模型。地址:https://sourceforge.net/projects/cmusphinx/files/Acoustic%20and%20Language%20Models/
下载Mandarin即可,将该文件放在Speech_recognition文件所在的目录下,并对照原来存在的那个文件对其重命名。

复制代码
1
2
3
4
5
6
7
8
9
10
import speech_recognition as sr audio_file='demo_audio.wav' r=sr.Recognizer() #打开语音文件 with sr.AudioFile(audio_file) as source: audio = r.record(source) #将语音转文字 print('文本内容:',r.recognize_sphinx(audio,language='zh-CN'))

参考资料:
哔哩哔哩 python语音识别

最后

以上就是诚心唇彩最近收集整理的关于python语音识别的全部内容,更多相关python语音识别内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(68)

评论列表共有 0 条评论

立即
投稿
返回
顶部