1

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

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)

This is the error we get

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

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

Daffyo
  • 13
  • 3
  • I would suggest printing fn (the filename) before splitting it, just to see what that string looks like. Obviously the error occurs because after splitting on slashes, not enough tokens were generated, which probably means the filename did not contain as many slashes as you thought it did. – Paul M. Aug 04 '19 at 15:31

1 Answers1

0

varim = fn.split('/')2

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

enter image description here

enter image description here

Do rectify if you are running in correct directory structure.