WAV文件的特征提取 [英] Feature extraction of wav file

查看:108
本文介绍了WAV文件的特征提取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正试图从.wav文件中提取功能,并始终出现相同的错误.

We are trying to extract features from .wav file and always get the same error.

我们尝试使用python 3.6.6和3.7.4版本,但错误是相同的.

We have tried with python 3.6.6 and 3.7.4 version but the error is the same.

import csv
import glob
import os
import librosa
import numpy as np

if __name__ == '__main__':

def extract_feature(file_name):
    x, sample_rate = librosa.load(file_name)
    stft = np.abs(librosa.stft(x))
    mfccs = np.mean(librosa.feature.mfcc(y=x, sr=sample_rate, n_mfcc=40).T, axis=0)
    chroma = np.mean(librosa.feature.chroma_stft(S=stft, sr=sample_rate).T, axis=0)
    mel = np.mean(librosa.feature.melspectrogram(x, sr=sample_rate).T, axis=0)
    contrast = np.mean(librosa.feature.spectral_contrast(S=stft, sr=sample_rate).T, axis=0)
    tonnetz = np.mean(librosa.feature.tonnetz(y=librosa.effects.harmonic(x),
                                              sr=sample_rate).T, axis=0)
    return mfccs, chroma, mel, contrast, tonnetz


def parse_audio_files(parent_dir, sub_dirs, file_ext="*.wav"):
    full_list = []
    features, labels = np.empty((0, 193)), np.empty(0)
    for label, sub_dir in enumerate(sub_dirs):

        for fn in glob.glob(os.path.join(parent_dir, sub_dir, file_ext)):
            varim = fn.split('/')[2]
            # print(varim)
            try:
                mfccs, chroma, mel, contrast, tonnetz = extract_feature(fn)
            except Exception as e:
                print("Error encountered while parsing file: ", fn)
                continue
            ext_features = np.hstack([mfccs, chroma, mel, contrast, tonnetz])
            features = np.vstack([features, ext_features])
            labels = np.append(labels, fn.split('/')[2])
            # print(var)
            # print(features)
            new_dict = {varim: ext_features}
            print(new_dict)
            full_list.append(new_dict)
            # value = np.array(features, dtype=np.int), np.array(labels, dtype=np.int)
    with open('dog_cat.csv', 'w') as f:
        wr = csv.writer(f)
        wr.writerow(full_list)
    return features, labels


def one_hot_encode(labels):
    n_labels = len(labels)
    n_unique_labels = len(np.unique(labels))
    one_hot_encode = np.zeros((n_labels, n_unique_labels))
    # one_hot_encode[np.arange(n_labels), labels] = 1
    return one_hot_encode


parent_dir = 'cats_dogs'
tr_sub_dirs = ["fold1"]
file_ext1 = "*.wav"

tr_features, tr_labels = parse_audio_files(parent_dir, tr_sub_dirs)

tr_labels = one_hot_encode(tr_labels)

这是我们得到的错误

Traceback (most recent call last):
  File "C:/Users/ja/PycharmProjects/catdog/projekt.py", line 61, in 
<module>
    tr_features, tr_labels = parse_audio_files(parent_dir, tr_sub_dirs)
  File "C:/Users/ja/PycharmProjects/catdog/projekt.py", line 27, in 
parse_audio_files
    varim = fn.split('/')[2]
IndexError: list index out of range

我们应该获取代表.wav文件的数字,以便我们可以对它们进行分类,无论它们是猫还是狗.

We are supposed to get numbers which represent .wav file so we can classify them whether they are cat or dog.

推荐答案

varim = fn.split('/') 2

fn.split('/')对您不起作用,因为无法进一步拆分,如下所示

fn.split('/') is not working for you as further split is not possible as shown below

在此处输入图片描述

在此处输入图片描述

请纠正是否以正确的目录结构运行.

Do rectify if you are running in correct directory structure.

这篇关于WAV文件的特征提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆