国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實現簡單的語音識別系統

2020-02-16 11:04:07
字體:
來源:轉載
供稿:網友

最近認識了一個做Python語音識別的朋友,聊天時候說到,未來五到十年,Python人工智能會在國內掀起一股狂潮,對各種應用的沖擊,不下于淘寶對實體經濟的沖擊。在本地(江蘇某三線城市)做這一行,短期可能顯不出效果,但從長遠來看,絕對是一個高明的選擇。朋友老家山東的,畢業來這里創業,也是十分有想法啊。

將AI課上學習的知識進行簡單的整理,可以識別簡單的0-9的單個語音。基本方法就是利用庫函數提取mfcc,然后計算誤差矩陣,再利用動態規劃計算累積矩陣。并且限制了匹配路徑的范圍。具體的技術網上很多,不再細談。

現有缺點就是輸入的語音長度都是1s,如果不固定長度則識別效果變差。改進思路是提取有效語音部分。但是該部分尚未完全做好,只寫了一個原形函數,尚未完善。

import waveimport numpy as npimport matplotlib.pyplot as pltfrom python_speech_features import mfccfrom math import cos,sin,sqrt,pidef 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,index2def 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,index2def 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/maximumdef 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_matdef 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,Distdef 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 comparisonmfcc_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')            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 威远县| 宁武县| 玉树县| 滨州市| 凤凰县| 黄石市| 萝北县| 嵩明县| 福海县| 民乐县| 手机| 油尖旺区| 瓦房店市| 常州市| 酉阳| 连州市| 宝山区| 安溪县| 延安市| 修武县| 保靖县| 沙坪坝区| 沛县| 怀远县| 项城市| 汝城县| 湖北省| 福泉市| 达孜县| 阿拉尔市| 吴堡县| 敦煌市| 麻江县| 定南县| 保定市| 大方县| 抚远县| 莒南县| 孝感市| 靖边县| 乌什县|