华域联盟 Python python语音识别的转换方法

python语音识别的转换方法

使用pyttsx的python包,你可以将文本转换为语音。

安装命令

pip install pyttsx3 -i https://pypi.tuna.tsinghua.edu.cn/simple

运行一个简单的语音 ‘大家好'。

import pyttsx3 as pyttsx
engine = pyttsx.init() #初始化
engine.say('大家好')
engine.runAndWait()

另一种文本转语音方法。

from win32com.client import Dispatch
speaker = Dispatch('SAPI.SpVoice')    #创建Dispatch对象
speaker.Speak('大家好')        #调用Speak方法
del speaker     #释放

这种方法可能会报错,

ImportError: DLL load failed while importing win32api: 找不到指定的模块。

网站下载与自己安装的 “Python" 版本相适应的 "pywin32" 安装程序。

使用SpeechLib完成文本转换语言

from comtypes.client import CreateObject
from comtypes.gen import SpeechLib
 
engine = CreateObject('SAPI.SpVoice')   #调用方法
stream = CreateObject('SAPI.SpFileStream')   #输出到目标对象的流
infile = '1.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将语音转换成文本

首先安装两个工具包

pip install PocketSphinx
pip install SpeechRecognition

然后下载cmusphinx-zh-cn-5.2.tar中文识别的放到anaconda的python虚拟环境的目录下

Lib\site-packages\speech_recognition\pocketsphinx-data路径下

解压文件重命名为zh-CN

#将语音转换成文本 使用PocketSphinx
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))   #默认识别成英文
    print('文本内容:',r.recognize_sphinx(audio,language='zh-CN'))  #指定中文
except Exception as e:
    print(e)

到此这篇关于python语音识别的文章就介绍到这了,更多相关python语音识别内容请搜索华域联盟以前的文章或继续浏览下面的相关文章希望大家以后多多支持华域联盟!

您可能感兴趣的文章:

本文由 华域联盟 原创撰写:华域联盟 » python语音识别的转换方法

转载请保留出处和原文链接:https://www.cnhackhy.com/39343.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部