title: Speech emotion recognition python code not working
body: <p>I would like to create an emotional speech recognition in collab which will detect the emotion of the users through getting the sound of their voice.
  Code:</p>
  <pre><code>#decorator function for calculating the total time required to execute various function
  def calc_time(func):
    def inner(*args, **kwargs):
      st = time.time()
      result = func(*args,**kwargs)
      end = time.time()-st
      print(&quot;Total time required: {:.3f} ms&quot;.format(end * 1000))
      return result
    return inner
  
  #function for getting ravdess dataset details and labeling
  def ravdess_data():
    #directory of the audio dataset
    ravdess = '/content/drive/MyDrive/Audiofiles/audio_speech_actors_01-24/'
    #label ravdess data
    emotion_ravdess = {'01':'neutral','02':'calm','03':'happy','04':'sad','05':'angry','06':'fearful','07':'disgust','08':'surprised'}
    #list to store ravdess emotion
    ravdess_emotion = []
    #list to store ravdess audio path
    ravdess_path = []
    #get subfolders from the path
    ravdess_folder = os.listdir(ravdess)
    for i in ravdess_folder:
      inner_files = os.listdir(ravdess+i+'/')
      for j in inner_files:
        #get the split part which contains the emotion information then append it into lists
        emotion = j.split('-')[2]
        ravdess_path.append(ravdess+i+'/'+j)
        ravdess_emotion.append(emotion_ravdess[emotion])
  
    #convert to dataframe
    df_ravdess = pd.DataFrame([ravdess_path,ravdess_emotion]).T
    df_ravdess.columns = [&quot;AudioPath&quot;,&quot;Label&quot;]
    print(&quot;length of ravdess dataset&quot;,len(df_ravdess))
  
    return df_ravdess
  
  #function for getting crema dataset details and labeling
  def crema_data():
    #directory of the audio dataset
    crema = '/content/drive/MyDrive/Audiofiles/AudioWAV/'
    #label ravdess data
    emotion_crema = {'SAD':'sad','ANG':'angry','DIS':'disgust','FEA':'fear','HAP':'happy','NEU':'neutral'}
    #list to store crema emotion
    crema_emotion = []
    #list to store crema audio path
    crema_path = []
    #get crema files in directory
    crema_files = os.listdir(crema)
    for i in crema_files:
      emotion = i.split('_')[2]
      crema_emotion.append(emotion_crema[emotion])
      crema_path.append(crema+i)
  
    #convert to dataframe
    df_crema = pd.DataFrame([crema_path,crema_emotion]).T
    df_crema.columns = [&quot;AudioPath&quot;,&quot;Label&quot;]
    print(&quot;length of crema dataset&quot;,len(df_crema))
  
    return df_crema
  
  #function for getting tess dataset and labeling
  def tess_data():
    #directory of the audio dataset
    tess = '/content/drive/MyDrive/Audiofiles/TESS Toronto emotional speech set data/'
    tess_emotion = []
    tess_path = []
    tess_folder = os.listdir(tess)
    for i in tess_folder:
      emotion = i.split('_',1)[1]
      inner_files = os.listdir(tess+i+'/')
      for j in inner_files:
        tess_path.append(tess+i+'/'+j)
        tess_emotion.append(emotion)
  
    #convert to dataframe
    df_tess = pd.DataFrame([tess_path,tess_emotion]).T
    df_tess.columns = [&quot;AudioPath&quot;,&quot;Label&quot;]
    print(&quot;length of tess dataset&quot;,len(df_tess))
  
    return df_tess
  
  #function to get savee dataset and labeling
  def saveee_data():
    #directory of the audio dataset
    savee = '/content/drive/MyDrive/Audiofiles/ALL/'
    emotion_savee = {'a':'anger','d':'disgust','f':'fear','h':'happiness','n':'neutral','sa':'sadness','su':'surprise'}
    savee_emotion = []
    savee_path = []
    savee_files = os.listdir(savee)
    for i in savee_files:
      emotion = i.split('_')[1]
      emotion = re.match(r&quot;([a-z]+)([0-9]+)&quot;,emotion)[1]
      savee_emotion.append(emotion_savee[emotion])
      savee_path.append(savee+i)
  
    #convert to dataframe
    df_savee = pd.DataFrame([savee_path,savee_emotion]).T
    df_savee.columns = [&quot;AudioPath&quot;,&quot;Label&quot;]
    print(&quot;length of savee dataset&quot;,len(df_savee))
  
    return df_savee
  
  @calc_time
  def fetch_data():
    #get ravdess data
    df_ravdess = ravdess_data()
    #get crema data
    df_crema = crema_data()
    #get tess data
    df_tess = tess_data()
    #get savee data
    df_savee = saveee_data()
    #combine all four dataset into one single dataset and create a dataframe 
    frames = [df_ravdess,df_crema,df_tess,df_savee]
    final_combined = pd.concat(frames)
    final_combined.reset_index(drop=True,inplace=True)
    #save the information of datasets with their path and labels into a csv file
    final_combined.to_csv(&quot;/content/drive/MyDrive/preprocesseddata.csv&quot;,index=False,header=True)
    print(&quot;Total length of the dataset is {}&quot;.format(len(final_combined)))
    return final_combined
  
  #below are four data agumentation functions for noise, stretch, shift, pitch
  #function to add noise to audio
  def noise(data):
    noise_amp = 0.035*np.random.uniform()*np.amax(data)
    data = data + noise_amp*np.random.normal(size=data.shape[0])
    return data
  
  #fuction to strech audio
  def stretch(data, rate=0.8):
    return librosa.effects.time_stretch(data, rate)
  
  #fucntion to shift audio range
  def shift(data):
    shift_range = int(np.random.uniform(low=-5, high = 5)*1000)
    return np.roll(data, shift_range)
  
  #function to change pitch
  def pitch(data, sampling_rate, pitch_factor=0.7):
    return librosa.effects.pitch_shift(data, sampling_rate, pitch_factor)
  
  #fuction to extract audio features from the audio files given the information of their path
  #path and label information comes from fetch_data fucntion 
  #also file preprocesseddata.csv stores the information of paths of audio files their label information
  #the print statements are commented these statements were used to see the number of features returned as output
  def extract_features(data,sample_rate):  
    
    #zero crossing rate
    result = np.array([])
    zcr = np.mean(librosa.feature.zero_crossing_rate(y=data).T, axis=0)
    result = np.hstack((result, zcr)) 
    #print('zcr',result.shape)
  
    #chroma shift
    stft = np.abs(librosa.stft(data))
    chroma_stft = np.mean(librosa.feature.chroma_stft(S=stft, sr=sample_rate).T, axis=0)
    result = np.hstack((result, chroma_stft))
    #print('chroma',result.shape)
    
    #mfcc
    mfcc = np.mean(librosa.feature.mfcc(y=data, sr=sample_rate).T, axis=0)
    result = np.hstack((result, mfcc))
    #print('mfcc',result.shape)
    
    #rmse
    rms = np.mean(librosa.feature.rms(y=data).T, axis=0)
    result = np.hstack((result, rms)) 
    #print('rmse',result.shape)
    
    #melspectogram
    mel = np.mean(librosa.feature.melspectrogram(y=data, sr=sample_rate).T, axis=0)
    result = np.hstack((result, mel)) 
    #print('mel',result.shape)    
  
    #rollof 
    rollof = np.mean(librosa.feature.spectral_rolloff(y=data, sr=sample_rate).T, axis=0)
    result = np.hstack((result, rollof))
    #print('rollof',result.shape) 
  
    #centroids 
    centroid = np.mean(librosa.feature.spectral_centroid(y=data, sr=sample_rate).T, axis=0)
    result = np.hstack((result, centroid))
    #print('centroids',result.shape)
  
    #contrast
    contrast = np.mean(librosa.feature.spectral_contrast(y=data, sr=sample_rate).T, axis=0)
    result = np.hstack((result, contrast))
    #print('contrast',result.shape)
  
    #bandwidth
    bandwidth = np.mean(librosa.feature.spectral_bandwidth(y=data, sr=sample_rate).T, axis=0)
    result = np.hstack((result, bandwidth))
    #print('bandwidth',result.shape)
  
    #tonnetz
    tonnetz = np.mean(librosa.feature.tonnetz(y=data, sr=sample_rate).T, axis=0)
    result = np.hstack((result, tonnetz))
    #print('tonnetz',result.shape) 
  
    return result
  
  #function is used to get all augmented plus original features for given audio file
  def get_features(path):
    #set the duration and offset
    #librosa.load takes audio file converts to array and returns array of audio file with its sampling rate
    data, sample_rate = librosa.load(path, duration=2.5, offset=0.6)
      
    #get audio features without augmentation
    res1 = extract_features(data,sample_rate)
    result = np.array(res1)
      
    #get audio features with noise
    noise_data = noise(data)
    res2 = extract_features(noise_data,sample_rate)
    result = np.vstack((result, res2))
      
    #get audio features with stretching and pitching
    new_data = stretch(data)
    data_stretch_pitch = pitch(new_data, sample_rate)
    res3 = extract_features(data_stretch_pitch,sample_rate)
    result = np.vstack((result, res3))
      
    return result
  </code></pre>
  <p>Code:#fucntion one by one takes audio files from the path extracts features
  #extracted audio features along with their label information are stored in a csv file</p>
  <pre><code>import time
  import os
  @calc_time
  def Audio_features_extract():
    #this function is used to fetch the data from all the four datasets
    df = fetch_data()
    #count is used to keep a check of number of files processed
    count = 0
    #list to store audio features and their label information
    X_data, Y_label = [], []
    #zip audio path and label information and then iterate over them
    for path, emotion in zip(df[&quot;AudioPath&quot;], df[&quot;Label&quot;]):
      print(&quot;Number of files processed &quot;,count)
      #get the features 
      #for one audio file it get three sets of features 
      #original features, features with noise(agumentation) and feature with change in stretch and pitch
      #so one audio file generates three output and the label is same for all the outputs
      feature = get_features(path)
      for ele in feature:
        X_data.append(ele)
        Y_label.append(emotion)
      count+=1
    #create a dataframe of aduio features
    Features = pd.DataFrame(X_data)
    #add label information 
    Features['Label'] = Y_label
    #store the extracted features in a csv file
    Features.to_csv('/content/drive/MyDrive/Audiofiles/Audio_features_All_pr.csv',index=False)
  
  #this is just one time process so call this function once only to get the features
  #once the features are extracted then these features are used for making model
  Audio_features_extract()
  </code></pre>
  <p>Error:</p>
  <hr />
  <pre><code>IndexError                                Traceback (most recent call last)
  &lt;ipython-input-7-66df11ec1d92&gt; in &lt;module&gt;()
       32 #this is just one time process so call this function once only to get the features
       33 #once the features are extracted then these features are used for making model
  ---&gt; 34 Audio_features_extract()
      &lt;ipython-input-6-9c5125049360&gt; in inner(*args, **kwargs)
        3   def inner(*args, **kwargs):
        4     st = time.time()
  ----&gt; 5     result = func(*args,**kwargs)
        6     end = time.time()-st
        7     print(&quot;Total time required: {:.3f} ms&quot;.format(end * 1000))
  
  &lt;ipython-input-7-66df11ec1d92&gt; in Audio_features_extract()
        6 def Audio_features_extract():
        7   #this function is used to fetch the data from all the four datasets
  ----&gt; 8   df = fetch_data()
        9   #count is used to keep a check of number of files processed
       10   count = 0
  
  &lt;ipython-input-6-9c5125049360&gt; in inner(*args, **kwargs)
        3   def inner(*args, **kwargs):
        4     st = time.time()
  ----&gt; 5     result = func(*args,**kwargs)
        6     end = time.time()-st
        7     print(&quot;Total time required: {:.3f} ms&quot;.format(end * 1000))
  
  &lt;ipython-input-6-9c5125049360&gt; in fetch_data()
      105 def fetch_data():
      106   #get ravdess data
  --&gt; 107   df_ravdess = ravdess_data()
      108   #get crema data
      109   df_crema = crema_data()
  
  &lt;ipython-input-6-9c5125049360&gt; in ravdess_data()
       25     for j in inner_files:
       26       #get the split part which contains the emotion information then append it into lists
  ---&gt; 27       emotion = j.split('-')[2]
       28       ravdess_path.append(ravdess+i+'/'+j)
       29       ravdess_emotion.append(emotion_ravdess[emotion])
  
  IndexError: list index out of range
  </code></pre>