中文语音情感识别python实现(一)
特別提一下安裝hmmlearn庫不能直接pip install hmmlearn
安裝過程如下:
1. 進入 https://www.lfd.uci.edu/~gohlke/pythonlibs/ ,找到hmmlearn***.whl類型的文件下載(要和自己的python版本相對應)如果你用的是anaconda可以在命令窗口輸入conda -activate 進入你的anaconda環境,再輸入python -V查看和anaconda對應的版本。如圖所示安裝自己對應的版本
?
2?然后將下載的文件放在C:\Anaconda3\pkgs\python-3.6.4-h6538335_1\Lib目錄(此路徑為自己當時安裝anaconda的路徑,如果找不到可以下載一個everything輸入anaconda查找自己當時的安裝路徑,everything的鏈接就不附上了),并在此目錄下進行安裝。然后在命令窗口輸入cd +自己anaconda的路徑切換到該路徑,在該路徑下輸入
pip install hmmlearn-0.2.1-cp36-cp36m-win_amd64.whl進行安裝,名字需要改成自己下載的文件的名字。至此,安裝成功。
import os import numpy as np import scipy.io.wavfile as wf import python_speech_features as sf import hmmlearn.hmm as hl def search_file(directory):""":param directory: 訓練音頻的路徑:return: 字典{'apple':[url, url, url ... ], 'banana':[...]}"""# 使傳過來的directory匹配當前操作系統directory = os.path.normpath(directory)objects = {}# curdir:當前目錄# subdirs: 當前目錄下的所有子目錄# files: 當前目錄下的所有文件名for curdir, subdirs, files in os.walk(directory):for file in files:if file.endswith('.wav'):label = curdir.split(os.path.sep)[-1] # os.path.sep為路徑分隔符if label not in objects:objects[label] = []# 把路徑添加到label對應的列表中path = os.path.join(curdir, file)objects[label].append(path)return objects train_samples = search_file('G:/Google download/CASIA database/liuchanhg') #print(train_samples) train_x, train_y = [], [] # 遍歷字典 for label, filenames in train_samples.items():# [('apple', ['url1,,url2...'])# [("banana"),("url1,url2,url3...")]...mfccs = np.array([])for filename in filenames:sample_rate, sigs = wf.read(filename)mfcc = sf.mfcc(sigs, sample_rate)if len(mfccs) == 0:mfccs = mfccelse:mfccs = np.append(mfccs, mfcc, axis=0)train_x.append(mfccs)train_y.append(label)models = {} for mfccs, label in zip(train_x, train_y):model = hl.GaussianHMM(n_components=4, covariance_type='diag', n_iter=1000)models[label] = model.fit(mfccs) # # {'apple':object, 'banana':object ...}# 讀取測試集數據 test_samples = search_file('G:/Google download/CASIA database/wangzhe') test_x, test_y = [], [] for label, filenames in test_samples.items():mfccs = np.array([])for filename in filenames:sample_rate, sigs = wf.read(filename)mfcc = sf.mfcc(sigs, sample_rate)if len(mfccs) == 0:mfccs = mfccelse:mfccs = np.append(mfccs, mfcc, axis=0)test_x.append(mfccs)test_y.append(label)pred_test_y = [] for mfccs in test_x:# 判斷mfccs與哪一個HMM模型更加匹配best_score, best_label = None, None# 遍歷7個模型for label, model in models.items():score = model.score(mfccs)if (best_score is None) or (best_score < score):best_score = scorebest_label = labelpred_test_y.append(best_label)print(test_y) print(pred_test_y)識別率很低只有50%左右,只適合入門?
參考資源:
?https://www.cnblogs.com/LXP-Never/p/11415110.html#聲音合成
https://blog.csdn.net/qq_33598125/article/details/85124778
?
?
?
?
總結
以上是生活随笔為你收集整理的中文语音情感识别python实现(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: activemq中怎么知道推送消息是否成
- 下一篇: 解决import keras后出现的一系