# Save directory path in 'path'
path = r'---path '
# Declare a dummy Numpy array (row vector)
result_array = np.empty([1,54])
# Create a list of audio file names 'file_list'
file_list = os.listdir(path)
i=0
for filename in file_list:
# Read WAV file. 'rosa.core.load' returns sampling frequency in 'fs' and audio signal in 'sig'
sig, fs = rosa.core.load(path + '\\' + file_list[i], sr=None)
# Calculate the average mfcc (utterance-level features) using 'rosa.feat.mfcc()' and 'np.mean' method. '.T' transposes the rows and columns. 'axis=0' indicates average is calculated column-wise
avg_mfcc_feat = np.mean(rosa.feature.mfcc(y=sig, sr=fs, n_mfcc=26).T,axis=0)
# Calculate the standard deviation of mfcc (utterance-level features) using 'rosa.feat.mfcc()' and 'np.std' method. '.T' transposes the rows and columns. 'axis=0' indicates average is calculated column-wise
std_mfcc_feat = np.std(rosa.feature.mfcc(y=sig, sr=fs, n_mfcc=26).T,axis=0)
# Calculate the average zero crossing rate (utterance-level feature) using 'rosa.feat.zero_crossing_rate()' and 'np.mean' method. '.T' transposes the rows and columns. 'axis=0' indicates average is calculated column-wise
zcross_feat = rosa.feature.zero_crossing_rate(sig)
avg_zcross_feat = np.mean(rosa.feature.zero_crossing_rate(y=sig).T,axis=0)
# Append the three 1D arrays into a single 1D array called 'feat'.
feat0 = np.append(avg_mfcc_feat, std_mfcc_feat, axis=0)
feat1 = np.append(feat0, avg_zcross_feat, axis=0)
# Save emotion label from file name. 'path' contains directory's address, 'file_list' contains file name, and '\\' joins the two to form file's address
label = os.path.splitext(os.path.basename(path + '\\' + file_list[i]))[0].split('-')[2]
# Create a new Numpy array 'sample' to store features along with label
sample = np.insert(feat1, obj=53, values=label)
result_array = np.append(result_array, sample)
i+=1
# Print out the 1D Numpy array
result_array
I'm getting this error while trying to read the contents and loop over the subdirectories of Ravdess dataset. Can someone tell me what am I doing wrong? Any help is deeply appreciated. Thanks!
1. os.listdir()
lists the entries of a directory, which are the files and directories it contains. If you want to recursively include all files and folders under path
, then use os.walk()
.
To use only files from os.listdir()
, then add a check with os.path.isfile()
:
Return
True
if path is anexisting
regular file. This follows symbolic links, so bothislink()
andisfile()
can be true for the same path.
for filename in file_list:
if not os.path.isfile(os.path.join(path, filename)):
# not a file, skip it
continue
# rest of your code here
sig, fs = rosa.core.load(...)
...
2. Inside the for
-loop, you've got two cases where you've put path + '\\' + file_list[i]
- this should be just path + '\\' + filename
, or better os.path.join(path, filename)
.
for i, filename in enumerate(file_list):
instead of manually incrementing i
.os.path.join(path, filename)
multiple times, put it in a variable at the start of your loop: for filename in file_list: filepath = os.path.join(path, filename) if not os.path.isfile(filepath): continue sig, fs = rosa.core.load(filepath, sr=None)...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.