python語(yǔ)音識(shí)別的轉(zhuǎn)換方法
使用pyttsx的python包,你可以將文本轉(zhuǎn)換為語(yǔ)音。
安裝命令
pip install pyttsx3 -i https://pypi.tuna.tsinghua.edu.cn/simple
運(yùn)行一個(gè)簡(jiǎn)單的語(yǔ)音‘大家好'。
import pyttsx3 as pyttsx
engine = pyttsx.init() #初始化
engine.say('大家好')
engine.runAndWait()
另一種文本轉(zhuǎn)語(yǔ)音方法。
from win32com.client import Dispatch
speaker = Dispatch('SAPI.SpVoice') #創(chuàng)建Dispatch對(duì)象
speaker.Speak('大家好') #調(diào)用Speak方法
del speaker #釋放
這種方法可能會(huì)報(bào)錯(cuò),
ImportError: DLL load failed while importing win32api: 找不到指定的模塊。
網(wǎng)站下載與自己安裝的 “Python" 版本相適應(yīng)的 "pywin32" 安裝程序。
使用SpeechLib完成文本轉(zhuǎn)換語(yǔ)言
from comtypes.client import CreateObject
from comtypes.gen import SpeechLib
engine = CreateObject('SAPI.SpVoice')#調(diào)用方法
stream = CreateObject('SAPI.SpFileStream')#輸出到目標(biāo)對(duì)象的流
infile = '1.txt'#要讀取的文本
outfile = 'demo_audio.wav'#輸出到語(yǔ)音文件
stream.open(outfile,SpeechLib.SSFMCreateForWrite)
engine.AudioOutputStream = stream
#讀取文本內(nèi)容
f = open(infile,'r',encoding='utf-8')
theText = f.read()
f.close()
engine.speak(theText)
stream.close()
使用PocketSphinx將語(yǔ)音轉(zhuǎn)換成文本
首先安裝兩個(gè)工具包
pip install PocketSphinx pip install SpeechRecognition
然后下載cmusphinx-zh-cn-5.2.tar中文識(shí)別的放到anaconda的python虛擬環(huán)境的目錄下
Lib\site-packages\speech_recognition\pocketsphinx-data路徑下
解壓文件重命名為zh-CN
#將語(yǔ)音轉(zhuǎn)換成文本 使用PocketSphinx
import speech_recognition as sr
audio_file = 'demo_audio.wav'
r = sr.Recognizer()
with sr.AudioFile(audio_file) as source:#打開(kāi)語(yǔ)音文件并讀取
audio = r.record(source)
try:
print('文本內(nèi)容:',r.recognize_sphinx(audio))#默認(rèn)識(shí)別成英文
print('文本內(nèi)容:',r.recognize_sphinx(audio,language='zh-CN')) #指定中文
except Exception as e:
print(e)
到此這篇關(guān)于python語(yǔ)音識(shí)別的文章就介紹到這了,更多相關(guān)python語(yǔ)音識(shí)別內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信