溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python語音識別的轉(zhuǎn)換方法教程

發(fā)布時間:2021-10-19 13:33:14 來源:億速云 閱讀:114 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“python語音識別的轉(zhuǎn)換方法教程”,在日常操作中,相信很多人在python語音識別的轉(zhuǎn)換方法教程問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”python語音識別的轉(zhuǎn)換方法教程”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

使用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語音識別的轉(zhuǎn)換方法教程”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI