Python實(shí)現(xiàn)簡(jiǎn)單的語(yǔ)音識(shí)別系統(tǒng)
最近認(rèn)識(shí)了一個(gè)做Python語(yǔ)音識(shí)別的朋友,聊天時(shí)候說(shuō)到,未來(lái)五到十年,Python人工智能會(huì)在國(guó)內(nèi)掀起一股狂潮,對(duì)各種應(yīng)用的沖擊,不下于淘寶對(duì)實(shí)體經(jīng)濟(jì)的沖擊。在本地(江蘇某三線城市)做這一行,短期可能顯不出效果,但從長(zhǎng)遠(yuǎn)來(lái)看,絕對(duì)是一個(gè)高明的選擇。朋友老家山東的,畢業(yè)來(lái)這里創(chuàng)業(yè),也是十分有想法啊。
將AI課上學(xué)習(xí)的知識(shí)進(jìn)行簡(jiǎn)單的整理,可以識(shí)別簡(jiǎn)單的0-9的單個(gè)語(yǔ)音。基本方法就是利用庫(kù)函數(shù)提取mfcc,然后計(jì)算誤差矩陣,再利用動(dòng)態(tài)規(guī)劃計(jì)算累積矩陣。并且限制了匹配路徑的范圍。具體的技術(shù)網(wǎng)上很多,不再細(xì)談。
現(xiàn)有缺點(diǎn)就是輸入的語(yǔ)音長(zhǎng)度都是1s,如果不固定長(zhǎng)度則識(shí)別效果變差。改進(jìn)思路是提取有效語(yǔ)音部分。但是該部分尚未完全做好,只寫(xiě)了一個(gè)原形函數(shù),尚未完善。
import wave import numpy as np import matplotlib.pyplot as plt from python_speech_features import mfcc from math import cos,sin,sqrt,pi def read_file(file_name): with wave.open(file_name,'r') as file: params = file.getparams() _, _, framerate, nframes = params[:4] str_data = file.readframes(nframes) wave_data = np.fromstring(str_data, dtype = np.short) time = np.arange(0, nframes) * (1.0/framerate) return wave_data, time return index1,index2 def find_point(data): count1,count2 = 0,0 for index,val in enumerate(data): if count1 <40: count1 = count1+1 if abs(val)>0.15 else 0 index1 = index if count1==40 and count2 <5: count2 = count2+1 if abs(val)<0.001 else 0 index2 = index if count2==5:break return index1,index2 def select_valid(data): start,end = find_point(normalized(data)) print(start,end) return data[start:end] def normalized(a): maximum = max(a) minimum = min(a) return a/maximum def compute_mfcc_coff(file_prefix = ''): mfcc_feats = [] s = range(10) I = [0,3,4,8] II = [5,7,9] Input = {'':s,'I':I,'II':II,'B':s} for index,file_name in enumerate(file_prefix+'{0}.wav'.format(i) for i in Input[file_prefix]): data,time = read_file(file_name) #data = select_valid(data) #if file_prefix=='II':data = select_valid(data) mfcc_feat = mfcc(data,48000)[:75] mfcc_feats.append(mfcc_feat) t = np.array(mfcc_feats) return np.array(mfcc_feats) def create_dist(): for i,m_i in enumerate(mfcc_coff_input):#get the mfcc of input for j,m_j in enumerate(mfcc_coff):#get the mfcc of dataset #build the distortion matrix bwtween i wav and j wav N = len(mfcc_coff[0]) distortion_mat = np.array([[0]*len(m_i) for i in range(N)],dtype = np.double) for k1,mfcc1 in enumerate(m_i): for k2,mfcc2 in enumerate(m_j): distortion_mat[k1][k2] = sqrt(sum((mfcc1[1:]-mfcc2[1:])**2)) yield i,j,distortion_mat def create_Dist(): for _i,_j,dist in create_dist(): N = len(dist) Dist = np.array([[0]*N for i in range(N)],dtype = np.double) Dist[0][0] = dist[0][0] for i in range(N): for j in range(N): if i|j ==0:continue pos = [(i-1,j),(i,j-1),(i-1,j-1)] Dist[i][j] = dist[i][j] + min(Dist[k1][k2] for k1,k2 in pos if k1>-1 and k2>-1) #if _i==0 and _j==1 :print(_i,_j,'\n',Dist,len(Dist[0]),len(Dist[1])) yield _i,_j,Dist def search_path(n): comparison = np.array([[0]*10 for i in range(n)],dtype = np.double) for _i,_j,Dist in create_Dist(): N = len(Dist) cut_off = 5 row = [(d,N-1,j) for j,d in enumerate(Dist[N-1]) if abs(N-1-j)<=cut_off] col = [(d,i,N-1) for i,d in enumerate(Dist[:,N-1]) if abs(N-1-i)<=cut_off] min_d,min_i,min_j = min(row+col ) comparison[_i][_j] = min_d optimal_path_x,optimal_path_y = [min_i],[min_j] while min_i and min_j: optimal_path_x.append(min_i) optimal_path_y.append(min_j) pos = [(min_i-1,min_j),(min_i,min_j-1),(min_i-1,min_j-1)] #try: min_d,min_i,min_j = min(((Dist[int(k1)][int(k2)],k1,k2) for k1,k2 in pos\ if abs(k1-k2)<=cut_off)) if _i==_j and _i==4: plt.scatter(optimal_path_x[::-1],optimal_path_y[::-1],color = 'red') plt.show() return comparison mfcc_coff_input = [] mfcc_coff = [] def match(pre): global mfcc_coff_input global mfcc_coff mfcc_coff_input = compute_mfcc_coff(pre) compare = np.array([[0]*10 for i in range(len(mfcc_coff_input))],dtype = np.double) for prefix in ['','B']: mfcc_coff = compute_mfcc_coff(prefix) compare += search_path(len(mfcc_coff_input)) for l in compare: print([int(x) for x in l]) print(min(((val,index)for index,val in enumerate(l)))[1]) data,time = read_file('8.wav') match('I') match('II')
總結(jié)
以上就是本文關(guān)于Python實(shí)現(xiàn)簡(jiǎn)單的語(yǔ)音識(shí)別系統(tǒng)的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
Python用戶推薦系統(tǒng)曼哈頓算法實(shí)現(xiàn)完整代碼
Python編程使用tkinter模塊實(shí)現(xiàn)計(jì)算器軟件完整代碼示例
如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
- python3實(shí)現(xiàn)語(yǔ)音轉(zhuǎn)文字(語(yǔ)音識(shí)別)和文字轉(zhuǎn)語(yǔ)音(語(yǔ)音合成)
- python實(shí)現(xiàn)百度語(yǔ)音識(shí)別api
- 使用Python和百度語(yǔ)音識(shí)別生成視頻字幕的實(shí)現(xiàn)
- Python調(diào)用百度api實(shí)現(xiàn)語(yǔ)音識(shí)別詳解
- python版百度語(yǔ)音識(shí)別功能
- 基于Python實(shí)現(xiàn)語(yǔ)音識(shí)別和語(yǔ)音轉(zhuǎn)文字
- 基于python實(shí)現(xiàn)語(yǔ)音錄入識(shí)別代碼實(shí)例
- python語(yǔ)音識(shí)別whisper的使用
相關(guān)文章
python實(shí)現(xiàn)模擬數(shù)字的魔術(shù)游戲
這篇文章介紹了python實(shí)現(xiàn)模擬數(shù)字的魔術(shù)游戲,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2021-12-12Django ORM多對(duì)多查詢方法(自定義第三張表&ManyToManyField)
今天小編就為大家分享一篇Django ORM多對(duì)多查詢方法(自定義第三張表&ManyToManyField),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08Django自定義全局403、404、500錯(cuò)誤頁(yè)面的示例代碼
這篇文章主要介紹了Django自定義全局403、404、500錯(cuò)誤頁(yè)面的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03windows下python虛擬環(huán)境virtualenv安裝和使用詳解
這篇文章主要介紹了windows下python虛擬環(huán)境virtualenv安裝和使用詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-07-07python實(shí)現(xiàn)超時(shí)退出的三種方式總結(jié)
這篇文章主要介紹了python實(shí)現(xiàn)超時(shí)退出的三種方式總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11pytorch使用tensorboard報(bào)錯(cuò)問(wèn)題及解決
這篇文章主要介紹了pytorch使用tensorboard報(bào)錯(cuò)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09python使用回溯算法實(shí)現(xiàn)列表全排列
這篇文章主要介紹了python使用回溯算法實(shí)現(xiàn)列表全排列,研究的問(wèn)題是輸入列表L(不含重復(fù)元素),輸出L的全排列,全排列問(wèn)題,可以用回溯法解決,需要的朋友可以參考下2023-11-11