0

我正在编写一个心跳分类代码,我测试了两个不同的数据集。一个是预先标记的并且具有格式良好的 .csv。对于其他数据集,我自己按照相同的命名模式和 .csv 文件对其进行了标记。但是每当我尝试提取音频文件时,它都不会导入文件并且数组显示为空。

import numpy as np
import pandas as pd
import glob
import os
import librosa
INPUT_DIR="E:\\Desktop\\New folder\\Assignment\\Assignment 17 Heartbeat sound\\classification-of-heart-sound-recordings-the-physionet-computing-in-cardiology-challenge-2016-1.0.0"
SAMPLE_RATE = 16000
MAX_SOUND_CLIP_DURATION=12 #sec  
set_a=pd.read_csv(INPUT_DIR+"/setB.csv")
frames = [set_a]
data_ab=pd.concat(frames)
def audio_norm(data):
    max_data = np.max(data)
    min_data = np.min(data)
    data = (data-min_data)/(max_data-min_data+0.0001)
    return data-0.5

# get audio data without padding highest quality audio
def load_file_data_without_change(folder,file_names, duration=3, sr=16000):
    input_length=sr*duration
    # function to load files and extract features
    # file_names = glob.glob(os.path.join(folder, '*.wav'))
    data = []
    for file_name in file_names:
        try:
            sound_file=folder+file_name
            print ("load file ",sound_file)
            # use kaiser_fast technique for faster extraction
            X, sr = librosa.load( sound_file, res_type='kaiser_fast') 
            dur = librosa.get_duration(y=X, sr=sr)
            # extract normalized mfcc feature from data
            #mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sr, n_mfcc=40).T,axis=0) 
        except Exception as e:
            print("Error encountered while parsing file: ", file)
        #feature = np.array(mfccs).reshape([-1,1])
        data.append(X)
    return data


# get audio data with a fix padding may also chop off some file
def load_file_data (folder,file_names, duration=12, sr=16000):
    input_length=sr*duration
    # function to load files and extract features
    # file_names = glob.glob(os.path.join(folder, '*.wav'))
    data = []
    for file_name in file_names:
        try:
            sound_file=folder+file_name
            print ("load file ",sound_file)
            # use kaiser_fast technique for faster extraction
            X, sr = librosa.load( sound_file, sr=sr, duration=duration,res_type='kaiser_fast') 
            dur = librosa.get_duration(y=X, sr=sr)
            # pad audio file same duration
            if (round(dur) < duration):
                print ("fixing audio lenght :", file_name)
                y = librosa.util.fix_length(X, input_length)                
            #normalized raw audio 
            y = audio_norm(y)            
            # extract normalized mfcc feature from data
            #mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sr, n_mfcc=40).T,axis=0)             
        except Exception as e:
            print("Error encountered while parsing file: ", file_name)        
        #feature = np.array(mfccs).reshape([-1,1])
        #data.append(y)
    return data
# load dataset-a, keep them separate for testing purpose
import os, fnmatch

A_folder=INPUT_DIR+'/training-b/'
# set-a

A_normal_files = fnmatch.filter(os.listdir(INPUT_DIR+'/training-b'), 'Normal_ *.wav')

A_normal_sounds = load_file_data(folder=A_folder,file_names=A_normal_files, duration=MAX_SOUND_CLIP_DURATION)
A_normal_labels = [2 for items in A_normal_sounds]

A_murmur_files = fnmatch.filter(os.listdir(INPUT_DIR+'/training-b'), 'Abnormal*.wav')
A_murmur_sounds = load_file_data(folder=A_folder,file_names=A_murmur_files, duration=MAX_SOUND_CLIP_DURATION)
A_murmur_labels = [1 for items in A_murmur_files]
# test files
A_unlabelledtest_files = fnmatch.filter(os.listdir(INPUT_DIR+'/training-b'), 'Aunlabelledtest*.wav')
print('unlab=',A_unlabelledtest_files)
A_unlabelledtest_sounds = load_file_data(folder=A_folder,file_names=A_unlabelledtest_files, duration=MAX_SOUND_CLIP_DURATION)
print('unlab=',A_unlabelledtest_sounds)
A_unlabelledtest_labels = [-1 for items in A_unlabelledtest_files]

print ("loaded dataset-a")


print('result',A_normal_sounds)
x_data = np.concatenate((A_normal_sounds,A_murmur_sounds))
print('x=',x_data)
y_data = np.concatenate((A_normal_labels,A_murmur_labels))

test_x = np.array((A_unlabelledtest_sounds))
test_y = np.array((A_unlabelledtest_labels))

print ("combined training data record: ",len(y_data), len(test_y))

打印 A_normal_sounds 和 x_data 给出空数组。表示未导入 .wav 文件。任何帮助将不胜感激。

4

0 回答 0