Python實(shí)現(xiàn)簡單的語音識(shí)別系統(tǒng)
最近認(rèn)識(shí)了一個(gè)做Python語音識(shí)別的朋友,聊天時(shí)候說到,未來五到十年,Python人工智能會(huì)在國內(nèi)掀起一股狂潮,對各種應(yīng)用的沖擊,不下于淘寶對實(shí)體經(jīng)濟(jì)的沖擊。在本地(江蘇某三線城市)做這一行,短期可能顯不出效果,但從長遠(yuǎn)來看,絕對是一個(gè)高明的選擇。朋友老家山東的,畢業(yè)來這里創(chuàng)業(yè),也是十分有想法啊。
將AI課上學(xué)習(xí)的知識(shí)進(jìn)行簡單的整理,可以識(shí)別簡單的0-9的單個(gè)語音?;痉椒ň褪抢脦旌瘮?shù)提取mfcc,然后計(jì)算誤差矩陣,再利用動(dòng)態(tài)規(guī)劃計(jì)算累積矩陣。并且限制了匹配路徑的范圍。具體的技術(shù)網(wǎng)上很多,不再細(xì)談。
現(xiàn)有缺點(diǎn)就是輸入的語音長度都是1s,如果不固定長度則識(shí)別效果變差。改進(jìn)思路是提取有效語音部分。但是該部分尚未完全做好,只寫了一個(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)簡單的語音識(shí)別系統(tǒng)的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
Python用戶推薦系統(tǒng)曼哈頓算法實(shí)現(xiàn)完整代碼
Python編程使用tkinter模塊實(shí)現(xiàn)計(jì)算器軟件完整代碼示例
如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
- python3實(shí)現(xiàn)語音轉(zhuǎn)文字(語音識(shí)別)和文字轉(zhuǎn)語音(語音合成)
- python實(shí)現(xiàn)百度語音識(shí)別api
- 使用Python和百度語音識(shí)別生成視頻字幕的實(shí)現(xiàn)
- Python調(diào)用百度api實(shí)現(xiàn)語音識(shí)別詳解
- python版百度語音識(shí)別功能
- 基于Python實(shí)現(xiàn)語音識(shí)別和語音轉(zhuǎn)文字
- 基于python實(shí)現(xiàn)語音錄入識(shí)別代碼實(shí)例
- python語音識(shí)別whisper的使用
相關(guān)文章
Python使用sftp實(shí)現(xiàn)上傳和下載功能(實(shí)例代碼)
在Python中可以使用paramiko模塊中的sftp登陸遠(yuǎn)程主機(jī),實(shí)現(xiàn)上傳和下載功能。接下來通過本文給大家介紹Python使用sftp實(shí)現(xiàn)上傳和下載功能,需要的朋友參考下2017-03-03
Python實(shí)現(xiàn)統(tǒng)計(jì)代碼行的方法分析
這篇文章主要介紹了Python實(shí)現(xiàn)統(tǒng)計(jì)代碼行的方法,結(jié)合實(shí)例形式分析了Python針對代碼行數(shù)的計(jì)算實(shí)現(xiàn)步驟與操作技巧,需要的朋友可以參考下2017-07-07
Pandas中字符串和時(shí)間轉(zhuǎn)換與格式化的實(shí)現(xiàn)
本文主要介紹了Pandas中字符串和時(shí)間轉(zhuǎn)換與格式化的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
python GUI庫圖形界面開發(fā)之PyQt5下拉列表框控件QComboBox詳細(xì)使用方法與實(shí)例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5下拉列表框控件QComboBox詳細(xì)使用方法與實(shí)例,需要的朋友可以參考下2020-02-02
探索python?dask靈活的并行計(jì)算庫應(yīng)用場景示例
這篇文章主要介紹了探索python?dask靈活的并行計(jì)算庫應(yīng)用場景示例,Dask?是?Python?中的一個(gè)靈活的并行計(jì)算庫,允許用戶利用?CPU?內(nèi)核的強(qiáng)大功能,對大于內(nèi)存的數(shù)據(jù)集執(zhí)行分布式計(jì)算2024-01-01
pycharm實(shí)現(xiàn)print輸出保存到txt文件
這篇文章主要介紹了pycharm實(shí)現(xiàn)print輸出保存到txt文件,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
Django靜態(tài)文件配置request對象方法ORM操作講解
這篇文章主要為大家介紹了Django靜態(tài)文件配置request對象方法ORM操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
jupyter notebook 恢復(fù)誤刪單元格或者歷史代碼的實(shí)現(xiàn)
這篇文章主要介紹了jupyter notebook 恢復(fù)誤刪單元格或者歷史代碼的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04

