99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁(yè)精彩閱讀教你怎樣用python語(yǔ)音識(shí)別
教你怎樣用python語(yǔ)音識(shí)別
2020-05-06
收藏

網(wǎng)上找到的有關(guān)python語(yǔ)音識(shí)別的例子,有一些成熟的模型,可以進(jìn)行python語(yǔ)音識(shí)別,將語(yǔ)音轉(zhuǎn)成文字。例如Cloud Speech API,但是需要你使用google云平臺(tái)的前提。下面我們一起來(lái)看看吧!



                   作者 |  小隱
                   來(lái)源 | 淘氣面包


speech recognition

對(duì)于python這一非常成熟的膠水語(yǔ)言,在網(wǎng)上找一些現(xiàn)成的工具包真的不是一個(gè)太難的問(wèn)題。在GitHub上就發(fā)現(xiàn)了這樣一個(gè)神奇的包:speech recognition。

它可以支持實(shí)時(shí)翻譯,當(dāng)然前提是需要在機(jī)器上安裝有關(guān)麥克風(fēng)的依賴包;還可以支持將語(yǔ)音文件中的文字直接提取出來(lái)。通過(guò)speech recognition可以調(diào)用多種平臺(tái)上的模型,比如google API,CMU sphinx,Microsoft Bing Speech,IBM Speech to Text,Wit.ai 等。


離線轉(zhuǎn)換

對(duì)于國(guó)內(nèi)的網(wǎng)絡(luò)環(huán)境,無(wú)法用google API來(lái)將語(yǔ)音數(shù)據(jù)轉(zhuǎn)換成文本文件,因?yàn)樵谡{(diào)用這個(gè)包的時(shí)候,需要連接到google。當(dāng)然,你可以租用一個(gè)國(guó)外的VPS來(lái)做這件事情。

這里講一下如何在不聯(lián)網(wǎng)的情況下,依然可以通過(guò)python來(lái)將語(yǔ)音文件轉(zhuǎn)換成文字。這里用到的包為sphinx,sphinx是由美國(guó)卡內(nèi)基梅隆大學(xué)開發(fā)的大詞匯量、非特定人、連續(xù)英語(yǔ)語(yǔ)音識(shí)別系統(tǒng)。


安裝 sphinx

我本人所用的環(huán)境為ubuntu。

imyin@develop:~/Downloads/phinx$ lsb_release -aNo LSB modules are available.Distributor ID: UbuntuDescription: Ubuntu 16.04.3 LTSRelease: 16.04Codename: xenial

在安裝sphinx之前需要安裝一些軟件包

sudo apt-get install gcc automake autoconf libtool bison swig python-dev libpulse-dev

之后可以在相關(guān)網(wǎng)站上下載sphinxbase安裝包,當(dāng)然也可以直接clone github上的包

下載完之后進(jìn)行解壓

tar zxpf sphinxbase-5prealpha.tar.gz

修改文件名

mv sphinxbase-5prealpha sphinxbasels sphinxbaseAUTHORS doc indent.sh Makefile.am README.md src win32autogen.sh .git LICENSE NEWS sphinxbase.pc.in swigconfigure.ac include m4 README sphinxbase.sln test

現(xiàn)在我們應(yīng)該運(yùn)行

autogen.sh

來(lái)生成

Makefiles

和其他一些腳本以備后續(xù)的編譯和安裝。

./autogen.sh

下面開始源碼安裝

make && sudo make install

執(zhí)行完以上命令之后,如果沒有出現(xiàn)什么報(bào)錯(cuò)信息,就說(shuō)明已經(jīng)安裝成功了,但是此時(shí)你的命令并不可以生效,在運(yùn)行命令時(shí)會(huì)出現(xiàn)這樣的錯(cuò)誤。

imyin@develop:~/Downloads/phinx/sphinxbase$ sphinx_lm_convert sphinx_lm_convert: error while loading shared libraries: libsphinxbase.so.3: cannot open shared object file: No such file or directory

還需要讓系統(tǒng)加載目錄

/usr/local/lib

,為了讓系統(tǒng)每次啟動(dòng)時(shí)都可以自動(dòng)加載,可以修改系統(tǒng)配置文件

ld.so.conf

sudo echo "/usr/local/lib" >> /etc/ld.so.confsudo ldconfig

這時(shí)候,就可以通過(guò)

sphinx_lm_convert

命令將模型DMP文件轉(zhuǎn)成bin文件

sphinx_lm_convert -i zh_broadcastnews_64000_utf8.DMP -o zh_CN.lm.bin

上面這行代碼是將中文的模型DMP文件轉(zhuǎn)成了bin文件。在安裝完sphinx后默認(rèn)只支持英文,在存放模型的路徑下只有一個(gè)文件名為en-US,所以這里需要添加一個(gè)處理中文的模型,相關(guān)文件可以在這個(gè)網(wǎng)址中下載。



python中使用sphinx

想要在python中使用sphinx的話,需要安裝一些依賴包。

pip install pydub -U # 負(fù)責(zé)將MP3文件轉(zhuǎn)換為 wav 文件pip install SpeechRecognition -U # 負(fù)責(zé)將語(yǔ)音轉(zhuǎn)換成文字sudo apt -qq install build-essential swig libpulse-dev # 為后面安裝 pocketsphinx 做準(zhǔn)備pip install -U pocketsphinx # 為使用 sphinx sudo apt-get install libav-tools # 為解決在調(diào)用 pydub 時(shí)出現(xiàn)的 warning :RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning)

這時(shí)候,就可以啟動(dòng)ipython來(lái)試試效果了。

file_path = '/home/imyin/Downloads/phinx/test_data'r = sr.Recognizer()hello_zh = sr.AudioFile(os.path.join(file_path, 'test.wav'))with hello_zh as source: audio = r.record(source)r.recognize_sphinx(audio, language='zh_CN')'今天 天氣 很'

可以看出,這個(gè)語(yǔ)音識(shí)別器已經(jīng)生效了。但是我說(shuō)的是“今天天氣好熱啊”。

看來(lái)sphinx中的模型并非很準(zhǔn)吶,而且這只是一個(gè)短句子。我們接下來(lái)看看長(zhǎng)句子的效果,我錄了村上春樹的《當(dāng)我談跑步時(shí)我談些什么》中的一段內(nèi)容。

那一年的七月里,我去了一趟希臘,要獨(dú)自從雅典跑到馬拉松,將那條原始的馬拉松路線——馬拉松至雅典——逆向跑上一趟。為什么要逆向跑呢?因?yàn)榍宄勘銖难诺涫兄行某霭l(fā),在道路開始擁堵、空氣被污染之前跑出市區(qū),一路直奔馬拉松的話,道路的交通量遠(yuǎn)遠(yuǎn)少得多,跑起來(lái)比較舒適。這不是正式的比賽,自己一個(gè)人隨意去跑,當(dāng)然不能指望有什么交通管制。hello_zh = sr.AudioFile(os.path.join(file_path, 'test2.wav'))with hello_zh as source: audio = r.record(source)r.recognize_sphinx(audio, language='zh_CN')'南 音 揚(yáng) 的 只有 領(lǐng) 過(guò) 球 的 立場(chǎng) 是 希望 讓 豬只 處理 垃圾 土木工程 上 打球 運(yùn)動(dòng) 充滿 溫情 能 成功 嗎 而 中止 了 對(duì) 印尼 商報(bào) 稱 他 不是 沒有 立場(chǎng) 談 那 一 枚 其中 春天 從 雅典 市中心 出發(fā) 寸 廠 都 可 成功 突破 尋求 對(duì) 於 能 提升 統(tǒng)籌 署 取締 一路 直奔 馬拉松 和 阿 惹 山 活動(dòng) 等 二十 個(gè) 隊(duì) 中 重申 這 不是 正常 的 比賽 自己 一個(gè)人 卻 一直到 當(dāng)然 不能 說(shuō)明 什么 這種 共識(shí)'

呃,看到結(jié)果,我覺得可以用一個(gè)來(lái)形容:差勁。兩個(gè)字來(lái)形容:太差勁!

當(dāng)然,這個(gè)模型只是我直接從網(wǎng)上下載下來(lái)的。訓(xùn)練它時(shí)所用到的語(yǔ)料不會(huì)那么齊全,所以在測(cè)試時(shí)難免會(huì)出現(xiàn)不準(zhǔn)確的情況。要想讓模型更加準(zhǔn)確,需要自己在利用sphnix繼續(xù)訓(xùn)練模型。

相關(guān)辦法在其官網(wǎng)上可以找到,也有相應(yīng)的教程。感興趣的朋友可以自行研究。

Q: Why my accuracy is poorSpeech recognition accuracy is not always great. To test speech recognition you need to run recognition on prerecorded reference database to see what happens and optimize parameters.You do not need to play with unknown values, the first thing you should do is to collect a database of test samples and measure the recognition accuracy. You need to dump speech utterances into wav files, write the reference text file and use decoder to decode it. Then calculate WER using the word_align.pl tool from Sphinxtrain. Test database size depends on the accuracy but usually it’s enough to have 10 minutes of transcribed audio to test recognizer accuracy reliably. The process is described in tutorialtuning.


Google API

利用google API來(lái)處理語(yǔ)音識(shí)別則相當(dāng)準(zhǔn)確,不過(guò)需要連接google,以下是我在VPS中執(zhí)行的一段代碼,可以看出,它將我的錄音精準(zhǔn)地翻譯成了文字。


但是如果錄音文件較大的話,會(huì)運(yùn)行時(shí)間很長(zhǎng),并且會(huì)返回一個(gè)超時(shí)的錯(cuò)誤,這很是讓我苦惱。

不過(guò)幸運(yùn)的是,speech_recognition支持將語(yǔ)音文件進(jìn)行截取處理。例如,我可以只處理語(yǔ)音文件中的前15秒鐘的內(nèi)容。

with test as source: audio = r.record(source, duration=15)r.recognize_google(audio, language='zh-CN')'那一年的7月里我去了一趟希臘有獨(dú)自從雅典跑到馬拉松江哪條原始的馬拉松路線馬拉松直雅典一想跑上一趟'

從上面的結(jié)果看,簡(jiǎn)直比sphnix處理的效果好太多了。

通過(guò)看幫助文檔發(fā)現(xiàn)speech_recognition不僅可以截取前面的錄音,還可以截取中間的。

In [18]: r.record?Signature: r.record(source, duration=None, offset=None)Docstring:Records up to ``duration`` seconds of audio from ``source`` (an ``AudioSource`` instance) starting at ``offset`` (or at the beginning if not specified) into an ``AudioData`` instance, which it returns.If ``duration`` is not specified, then it will record until there is no more audio input.

例如我想處理5秒至20秒之間的內(nèi)容。

with test as source: audio = r.record(source, offset=5, duration=15)r.recognize_google(audio, language='zh-CN')'要獨(dú)自從雅典跑到馬拉松江哪條原始的馬拉松路線馬拉松直雅典一項(xiàng)跑上一趟為什么要一想到呢因?yàn)樾浅阶儚难诺涫兄行某霭l(fā)'。


世界真奇妙,更多python語(yǔ)音識(shí)別的例子,大家可以繼續(xù)去發(fā)現(xiàn)哦!

數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說(shuō)明請(qǐng)參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }