Uncategorized

Why do I get this error message when I try loading an audio file in python?


So basically I am trying to preprocess audio data for a deep learning project by Valerio from youtube. The training data for the deep learning model is saved in a directory genres_original. Each subdirectory inside it contains 100 .wav files which are 30 seconds long.
Both the genres_original and preprocess.py directory and file belong to the same directory.
But when I try running preprocess.py I get the following error:

Processing: blues
Traceback (most recent call last):
  File "C:\Users\CSC\Documents\music_genre_classifier\preprocess.py", line 77, in <module>
    save_mfcc(DATASET_PATH, JSON_PATH, num_segments=10)
  File "C:\Users\CSC\Documents\music_genre_classifier\preprocess.py", line 52, in save_mfcc
    signal, sample_rate = librosa.load(file_path, sr=SAMPLE_RATE)
                          ^^^^^^^^^^^^
  File "C:\Users\CSC\AppData\Local\Programs\Python\Python312\Lib\site-packages\lazy_loader\__init__.py", line 78, in __getattr__
    attr = getattr(submod, name)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\CSC\AppData\Local\Programs\Python\Python312\Lib\site-packages\lazy_loader\__init__.py", line 77, in __getattr__
    submod = importlib.import_module(submod_path)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\CSC\AppData\Local\Programs\Python\Python312\Lib\importlib\__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 994, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "C:\Users\CSC\AppData\Local\Programs\Python\Python312\Lib\site-packages\librosa\core\audio.py", line 19, in <module>
    from .convert import frames_to_samples, time_to_samples
  File "C:\Users\CSC\AppData\Local\Programs\Python\Python312\Lib\site-packages\librosa\core\convert.py", line 7, in <module>
    from . import notation
  File "C:\Users\CSC\AppData\Local\Programs\Python\Python312\Lib\site-packages\librosa\core\notation.py", line 8, in <module>
    from .intervals import INTERVALS
  File "C:\Users\CSC\AppData\Local\Programs\Python\Python312\Lib\site-packages\librosa\core\intervals.py", line 8, in <module>
    from pkg_resources import resource_filename
ModuleNotFoundError: No module named 'pkg_resources'

Here’s the code in preprocess.py:

import os
import librosa
import math
import json

DATASET_PATH = "genres_original"
JSON_PATH = "data.json"

SAMPLE_RATE = 22050
DURATION = 30 #measured in seconds
SAMPLES_PER_TRACK = SAMPLE_RATE * DURATION

def save_mfcc(dataset_path, json_path, n_mfcc = 13, n_fft = 2048, hop_length = 512, num_segments = 5):
    
    #dictionary to store data
    data = {
        "mapping": [],
        "mfcc": [], #outer len of mfcc and labels are correlated to num_segments
        "labels": []
    }
    
    num_of_samples_per_segment = int(SAMPLES_PER_TRACK/num_segments)
    
    #one mfcc complex vector associated with each segment per hop length
    expected_num_mfcc_vectors_per_segment = math.ceil(num_of_samples_per_segment / hop_length)

    # loop through all the genres
    for i, (dirpath, dirnames, filenames) in enumerate(os.walk(dataset_path)):
    #first iteration of os.walk() gives the dataset path, i is the label for each genre 
        #ensure we are at sub folder level
        if dirpath is not dataset_path:
            
            #save semantic label
            dirpath_components = os.path.split(dirpath)
            semantic_label = dirpath_components[-1]
            data["mapping"].append(semantic_label)
            
            print("processing: {}",format(semantic_label))
            
            # process files for a specific genre
            for f in filenames:
                
                #load audio file
                file_path = os.path.join(dirpath, f)
                signal, sr = librosa.load(file_path,sr=SAMPLE_RATE)
                
                #extract the mfcc for each segment for each signal
                for s in len(num_segments):
                    start_sample = num_of_samples_per_segment * s
                    end_sample = num_of_samples_per_segment + start_sample
                    
                    mfcc = librosa.feature.mfcc(signal[start_sample:end_sample],sr=sr,n_fft=n_fft,hop_length=hop_length,n_mfcc=n_mfcc)
                    
                    mfcc = mfcc.T
                    
                    #store mfcc for segment if it has expeted length
                    if len(mfcc) == expected_num_mfcc_vectors_per_segment:
                        data["mfcc"].append(mfcc.tolist())
                        data["labels"].append(i-1)
                        print("{}, segment:{}",format(file_path, s+1))
                        
    with open(json_path,"w") as fp:
        json.dump(data,fp,indent=4)
        
if __name__ == "__main__":
    save_mfcc(DATASET_PATH,JSON_PATH,num_segments=10)

so clearly librosa.load() isn’t working for some reason.

I tried running the python file and I expected a return code 0.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *