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