-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopensmile.py
166 lines (137 loc) · 5.56 KB
/
opensmile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import os
import csv
import sys
import subprocess
import pandas as pd
from sklearn.preprocessing import StandardScaler
import joblib
from sklearn.model_selection import train_test_split
# 每个特征集的特征数量
FEATURE_NUM = {'IS10_paraling': 1582}
'''
该文件是提取特征的,使用的是opensmile
get_feature_opensmile(): Opensmile 提取一个音频的特征
输入:
file_path: 音频路径
输出:
该音频的特征向量
'''
def get_feature_opensmile(config, filepath: str):
# 用于存储一个音频的特征的 csv文件,推荐使用绝对路径
# single_feat_path = config.single_feature_path
# Opensmile 配置文件路径:我们使用 IS10_paraling
opensmile_config_path = os.getcwd() + '/' + config.opensmile_path + 'config/is09-13/IS10_paraling.conf'
cmd3 = 'SMILExtract -C ' + opensmile_config_path + ' -I ' + filepath + ' -O ' + config.data_dir + '/temp/features/single_feature.csv'
cmd = subprocess.Popen(cmd3, cwd=config.opensmile_path + 'bin', stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True).communicate()[0]
reader = csv.reader(open(config.data_dir + '/temp/features/single_feature.csv', 'r'))
rows = [row for row in reader]
last_line = rows[-1]
return last_line[1: FEATURE_NUM[config.opensmile_config] + 1]
'''
load_feature(): 从 .csv 文件中加载特征数据
输入:
feature_path: 特征文件路径
train: 是否为训练数据
输出:
训练数据、测试数据和对应的标签
'''
def load_feature(config, feature_path: str, train: bool):
# 加载特征数据
df = pd.read_csv(feature_path)
features = [str(i) for i in range(1, FEATURE_NUM[config.opensmile_config] + 1)]
X = df.loc[:, features].values
Y = df.loc[:, 'label'].values
# 标准化模型路径
scaler_path = os.path.join(config.checkpoint_path, 'SCALER_OPENSMILE.m')
if train == True:
# 标准化数据
scaler = StandardScaler().fit(X)
# 保存标准化模型
joblib.dump(scaler, scaler_path)
X = scaler.transform(X)
# 划分训练集和测试集
x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.1, random_state=42)
return x_train, x_test, y_train, y_test
else:
# 加载标准化模型
scaler = joblib.load(scaler_path)
X = scaler.transform(X)
return X
'''
get_data():
提取所有音频的特征: 遍历所有文件夹, 读取每个文件夹中的音频, 提取每个音频的特征,把所有特征保存在 feature 中
输入:
data_path: 数据集文件夹/测试文件路径
feature_path: 保存特征的路径
train: 是否为训练数据
输出:
train = True: 训练数据、测试数据特征和对应的标签
train = False: 预测数据特征
'''
# Opensmile 提取特征
def get_data(config, data_path, feature_path: str, train: bool):
writer = csv.writer(open(feature_path, 'w',newline=''))
first_row = ['label']
for i in range(1, FEATURE_NUM[config.opensmile_config] + 1):
first_row.append(str(i))
writer.writerow(first_row)
writer = csv.writer(open(feature_path, 'a+',newline=''))
print('Opensmile extracting...')
cur_dir = os.getcwd()
print(cur_dir)
sys.stderr.write('Curdir: %s\n' % cur_dir)
print("ddd",data_path)
os.chdir(data_path)
for j, directory in enumerate(config.class_labels):
sys.stderr.write("Started reading folder %s\n" % directory)
print("ddd", directory)
os.chdir(directory)
# 读取该文件夹下的音频
for filename in os.listdir('.'):
print(filename)
if not filename.endswith('wav'):
continue
filepath = os.path.join(os.getcwd(), filename)
# 提取该音频的特征
feature_vector = get_feature_opensmile(config, filepath)
if train == True:
label = config.class_labels.index(directory)
feature_vector.insert(0, label)
# 把每个音频的特征整理到一个 csv 文件中
writer.writerow(feature_vector)
else:
feature_vector.insert(0, '-1')
writer.writerow(feature_vector)
sys.stderr.write("Ended reading folder %s\n" % directory)
os.chdir('..')
os.chdir('..')
# os.chdir(cur_dir)
print('Opensmile extract done.')
def get_new_data(config, data_path, feature_path: str,result_path2, train: bool):
writer = csv.writer(open(feature_path, 'w',newline=''))
writer2 = csv.writer(open(result_path2, 'w',newline=''))
first_row = ['label']
for i in range(1, FEATURE_NUM[config.opensmile_config] + 1):
first_row.append(str(i))
writer.writerow(first_row)
writer2.writerow(['path'])
writer = csv.writer(open(feature_path, 'a+',newline=''))
writer2 = csv.writer(open(result_path2, 'a+', newline=''))
print('Opensmile extracting...')
cur_dir = os.getcwd()
sys.stderr.write('Curdir: %s\n' % cur_dir)
# 读取该文件夹下的音频
filelist = os.listdir(data_path)
filelist.sort(key=lambda x: int(x.split('.')[0].split('-')[-1]))
for filename in filelist:
if not filename.endswith('wav'):
continue
filepath = data_path +'/'+ filename
# 提取该音频的特征
feature_vector = get_feature_opensmile(config, filepath)
print(feature_vector)
writer2.writerow([filepath])
feature_vector.insert(0, '-1')
writer.writerow(feature_vector)
print('Opensmile extract done.')