加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
esc10_extraction.py 2.48 KB
一键复制 编辑 原始数据 按行查看 历史
peihuanjie 提交于 2021-09-03 13:39 . 项目代码
# -*- coding: utf-8 -*-
import numpy as np
from glob import glob
import os
import librosa
import feature_extraction as fe
import warnings
warnings.filterwarnings('ignore')
def extract_esc10_feat():
# 5-fold cross validation settings
cv_index = np.load('cvindex.npz')
TR = cv_index['TR'] # train fold
TE = cv_index['TE'] # test fold
# 5 fold extracting
for fold in range(1, 6):
print('Extracting training set and test set for fold {}'.format(fold))
train_feats = []
train_labels = []
test_feats = []
test_labels = []
class_list = np.sort(glob('./data/esc10/audio/*'))
for index, classpath in enumerate(class_list):
print('Total numbers of audio classes: ', len(class_list))
audio_list = np.sort(glob(os.path.join(classpath, '*.ogg')))
# for training
train_audio = audio_list[TR[fold - 1]]
for i in range(len(train_audio)):
print('Processing sound class: ', os.path.basename(classpath), index + 1, '/', len(class_list),
' --- training set: ', i + 1, '/', len(train_audio))
y, fs = librosa.load(train_audio[i], sr=22050)
# feat = fe.extract_mfcc(y, fs, 3)
feat = fe.extract_logmel(y, fs, 3)
train_feats.append(feat)
train_labels.append(index)
# for test
test_audio = audio_list[TE[fold - 1]]
for j in range(len(test_audio)):
print('Processing sound class: ', os.path.basename(classpath), index + 1, '/', len(class_list),
' --- test set: ', j + 1, '/', len(test_audio))
y, fs = librosa.load(test_audio[j], sr=22050)
# feat = fe.extract_mfcc(y, fs, 3)
feat = fe.extract_logmel(y, fs, 3)
test_feats.append(feat)
test_labels.append(index)
train_feats = np.array(train_feats)
train_labels = np.array(train_labels)
test_feats = np.array(test_feats)
test_labels = np.array(test_labels)
# np.savez('./data/esc10/feature/esc10_mfcc_fold{}.npz'.format(fold),
# train_x=train_feats, train_y=train_labels, test_x=test_feats, test_y=test_labels)
np.savez('./data/esc10/feature/esc10_logmel_fold{}.npz'.format(fold),
train_x=train_feats, train_y=train_labels, test_x=test_feats, test_y=test_labels)
if __name__ == '__main__':
extract_esc10_feat()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化