【问题标题】:ValueError: index can't contain negative valuesValueError: 索引不能包含负值
【发布时间】:2022-12-24 01:09:17
【问题描述】:

我正在尝试预处理音频文件以进行语音情感识别,但出现有关负值的错误。

主要代码链接(预处理部分): https://github.com/MeidanGR/SpeechEmotionRecognition_Realtime/blob/main/2_model.ipynb

import time
tic = time.perf_counter()

# Initialize data lists
rms = []
zcr = []
mfcc = []
emotions = []

# Initialize variables
total_length = 173056 # desired frame length for all of the audio samples.
frame_length = 2048
hop_length = 512

folder_path = '/content/drive/My Drive/AudioFiles' 

for subdir, dirs, files in os.walk(folder_path):
  for file in files: 

    # Fetch the sample rate.
      _, sr = librosa.load(path = os.path.join(subdir,file), sr = None) # sr (the sample rate) is used for librosa's MFCCs. '_' is irrelevant.
    # Load the audio file.
      rawsound = AudioSegment.from_file(os.path.join(subdir,file)) 
    # Normalize the audio to +5.0 dBFS.
      normalizedsound = effects.normalize(rawsound, headroom = 0) 
    # Transform the normalized audio to np.array of samples.
      normal_x = np.array(normalizedsound.get_array_of_samples(), dtype = 'float32')
    # Trim silence from the beginning and the end.
      xt, index = librosa.effects.trim(normal_x, top_db=30)
      #print(file,"\t", len(xt), "\t", rawsound.dBFS, "\t", normalizedsound.dBFS) #--QA purposes if needed-- 
    # Pad for duration equalization.
      padded_x = np.pad(xt, (0, total_length-len(xt)), 'constant')
    # Noise reduction.
      final_x = nr.reduce_noise(padded_x, sr=sr) #updated 03/03/22
       
   # Features extraction 
      f1 = librosa.feature.rms(final_x, frame_length=frame_length, hop_length=hop_length) # Energy - Root Mean Square   
      f2 = librosa.feature.zero_crossing_rate(final_x , frame_length=frame_length, hop_length=hop_length, center=True) # ZCR      
      f3 = librosa.feature.mfcc(final_x, sr=sr, n_mfcc=13, hop_length = hop_length) # MFCC
      
   # Emotion extraction from the different databases
      if (find_emotion_T(file) != "-1"): #TESS database validation
            name = find_emotion_T(file)
      else:                              #RAVDESS database validation
            name = file[6:8]                      

   # Filling the data lists  
      rms.append(f1)
      zcr.append(f2)
      mfcc.append(f3)
      emotions.append(emotionfix(name)) 

toc = time.perf_counter()
print(f"Running time: {(toc - tic)/60:0.4f} minutes")

【问题讨论】:

  • 您可以粘贴代码示例,将其突出显示,然后按 {} 按钮使其成为代码块。由于这是一种简单的标记语言,它实际上只是在寻找缩进有 4 个空格的文本以突出显示为代码。
  • 我更喜欢 ``` 方法,使用 imo 更容易。
  • 根据报错,total_length-len(xt)有时是负数,不可以。

标签: python deep-learning


【解决方案1】:

MeidanGR上传项目以来,数据集已经更新。数据集现在包含比以前更多的数据,因此在您使用时它会变成负索引 total_length = 173056 # desired frame length for all of the audio samples. 这是一个固定值。

现在每个数据集的总长度是243200

因此,使用以下值将解决您的问题。

total_length = 243200*2

我假设您同时使用数据集 RAVDESSTESS

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-24
    • 1970-01-01
    • 2016-06-20
    • 2018-12-06
    • 2018-07-29
    • 2020-01-19
    • 2014-01-22
    • 1970-01-01
    相关资源
    最近更新 更多