中⽂语⾳情感识别python实现(⼀)
特别提⼀下安装hmmlearn库不能直接pip install hmmlearn
安装过程如下:
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 = path(directory)
objects = {}
# curdir:当前⽬录
# subdirs: 当前⽬录下的所有⼦⽬录
# files: 当前⽬录下的所有⽂件名
for curdir, subdirs, files in os.walk(directory):
for file in files:
dswith('.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,,'])
# [("banana"),("url1,")]...
mfccs = np.array([])
for filename in filenames:
for filename in filenames:
sample_rate, sigs = wf.read(filename)
mfcc = sf.mfcc(sigs, sample_rate)
if len(mfccs) == 0:
mfccs = mfcc
else:
python默认安装路径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 = mfcc
else:
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 = score
best_label = label
pred_test_y.append(best_label)
print(test_y)
print(pred_test_y)
识别率很低只有50%左右,只适合⼊门
参考资源:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论