<jupyter_start><jupyter_text><jupyter_code>from google.colab import drive
drive.mount('/content/drive')
!pip install soundfile
import soundfile
import numpy as np
import librosa
import glob
import os
from sklearn.model_selection import train_test_split

# all emotions on RAVDESS dataset
int2emotion = {
    "01": "neutral",
    "02": "calm",
    "03": "happy",
    "04": "sad",
    "05": "angry",
    "06": "fearful",
    "07": "disgust",
    "08": "surprised"
}

# we allow only these emotions
AVAILABLE_EMOTIONS = {
    "angry",
    "sad",
    "neutral",
    "happy"
}
def extract_feature(file_name, **kwargs):
    """
    Extract feature from audio file `file_name`
        Features supported:
            - MFCC (mfcc)
            - Chroma (chroma)
            - MEL Spectrogram Frequency (mel)
            - Contrast (contrast)
            - Tonnetz (tonnetz)
        e.g:
        `features = extract_feature(path, mel=True, mfcc=True)`
    """
    mfcc = kwargs.get("mfcc")
    chroma = kwargs.get("chroma")
    mel = kwargs.get("mel")
    contrast = kwargs.get("contrast")
    tonnetz = kwargs.get("tonnetz")
    with soundfile.SoundFile(file_name) as sound_file:
        X = sound_file.read(dtype="float32")
        sample_rate = sound_file.samplerate
        if chroma or contrast:
            stft = np.abs(librosa.stft(X))
        result = np.array([])
        if mfcc:
            mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T, axis=0)
            result = np.hstack((result, mfccs))
        if chroma:
            chroma = np.mean(librosa.feature.chroma_stft(S=stft, sr=sample_rate).T,axis=0)
            result = np.hstack((result, chroma))
        if mel:
            mel = np.mean(librosa.feature.melspectrogram(X, sr=sample_rate).T,axis=0)
            result = np.hstack((result, mel))
        if contrast:
            contrast = np.mean(librosa.feature.spectral_contrast(S=stft, sr=sample_rate).T,axis=0)
            result = np.hstack((result, contrast))
        if tonnetz:
            tonnetz = np.mean(librosa.feature.tonnetz(y=librosa.effects.harmonic(X), sr=sample_rate).T,axis=0)
            result = np.hstack((result, tonnetz))
    return result
def load_data(test_size=0.2):
    X, y = [], []
    try :
      for file in glob.glob("/content/drive/My Drive/wav/Actor_*/*.wav"):
          # get the base name of the audio file
          basename = os.path.basename(file)
          print(basename)
          # get the emotion label
          emotion = int2emotion[basename.split("-")[2]]
          # we allow only AVAILABLE_EMOTIONS we set
          if emotion not in AVAILABLE_EMOTIONS:
              continue
          # extract speech features
          features = extract_feature(file, mfcc=True, chroma=True, mel=True)
          # add to data
          X.append(features)
          y.append(emotion)
    except :
         pass
    # split the data to training and testing and return it
    return train_test_split(np.array(X), y, test_size=test_size, random_state=7)
X_train, X_test, y_train, y_test = load_data(test_size=0.25)
# print some details
# number of samples in training data
print("[+] Number of training samples:", X_train.shape[0])
# number of samples in testing data
print("[+] Number of testing samples:", X_test.shape[0])
# number of features used
# this is a vector of features extracted 
# using utils.extract_features() method
print("[+] Number of features:", X_train.shape[1])<jupyter_output>03-01-01-01-01-01-21.wav
03-01-01-01-02-02-21.wav
03-01-01-01-01-02-21.wav
03-01-02-01-01-01-21.wav
03-01-02-01-01-02-21.wav
03-01-02-01-02-01-21.wav
03-01-01-01-02-01-21.wav
03-01-02-01-02-02-21.wav
03-01-02-02-01-01-21.wav
03-01-02-02-02-01-21.wav
03-01-02-02-02-02-21.wav
03-01-02-02-01-02-21.wav
03-01-03-01-02-02-21.wav
03-01-03-01-01-01-21.wav
03-01-03-01-02-01-21.wav
03-01-03-01-01-02-21.wav
03-01-03-02-02-02-21.wav
03-01-03-02-01-01-21.wav
03-01-03-02-02-01-21.wav
03-01-03-02-01-02-21.wav
03-01-04-01-01-01-21.wav
03-01-04-01-01-02-21.wav
03-01-04-01-02-01-21.wav
03-01-04-01-02-02-21.wav
03-01-05-01-01-02-21.wav
03-01-04-02-01-01-21.wav
03-01-04-02-01-02-21.wav
03-01-05-01-02-01-21.wav
03-01-04-02-02-02-21.wav
03-01-05-01-01-01-21.wav
03-01-05-02-01-01-21.wav
03-01-05-01-02-02-21.wav
03-01-04-02-02-01-21.wav
03-01-06-01-02-02-21.wav
03-01-05-02-02-01-21.wav
03-01-06-02-01-01-21.wav
03-01-05-02-01-02-21.wav
03-01-06-02-01-02-21.wav
03-01-06-02-02-01-21.wav
03-01-06-01-01-01-21.wav
[...]<jupyter_text>**DECISION TREE**<jupyter_code>from sklearn.tree import DecisionTreeClassifier 
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix


dtree_model = DecisionTreeClassifier(max_depth = 2).fit(X_train, y_train) 
dtree_predictions = dtree_model.predict(X_test) 

print(accuracy_score(y_true=y_test,y_pred=dtree_predictions))
print(classification_report(y_test,dtree_predictions)) 
# creating a confusion matrix 
print(confusion_matrix(y_test, dtree_predictions) )
from sklearn.tree import DecisionTreeClassifier 
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix


dtree_model = DecisionTreeClassifier(max_depth = 6).fit(X_train, y_train) 
dtree_predictions = dtree_model.predict(X_test) 

print(accuracy_score(y_true=y_test,y_pred=dtree_predictions))
print(classification_report(y_test,dtree_predictions)) 
# creating a confusion matrix 
print(confusion_matrix(y_test, dtree_predictions) )<jupyter_output>0.6595744680851063
              precision    recall  f1-score   support

       angry       0.74      0.74      0.74        90
       happy       0.57      0.53      0.55        94
     neutral       0.58      0.68      0.62        44
         sad       0.71      0.69      0.70       101

    accuracy                           0.66       329
   macro avg       0.65      0.66      0.65       329
weighted avg       0.66      0.66      0.66       329

[[67 21  0  2]
 [17 50  8 19]
 [ 4  3 30  7]
 [ 3 14 14 70]]<jupyter_text>DT<jupyter_code>from sklearn.tree import DecisionTreeClassifier 
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix


dtree_model = DecisionTreeClassifier(max_depth = 9,random_state=0).fit(X_train, y_train) 
dtree_predictions = dtree_model.predict(X_test) 

print(accuracy_score(y_true=y_test,y_pred=dtree_predictions))
print(classification_report(y_test,dtree_predictions)) 
# creating a confusion matrix 
print(confusion_matrix(y_test, dtree_predictions) )<jupyter_output>0.6656534954407295
              precision    recall  f1-score   support

       angry       0.66      0.76      0.70        90
       happy       0.63      0.55      0.59        94
     neutral       0.67      0.64      0.65        44
         sad       0.70      0.70      0.70       101

    accuracy                           0.67       329
   macro avg       0.66      0.66      0.66       329
weighted avg       0.66      0.67      0.66       329

[[68 16  0  6]
 [22 52  3 17]
 [ 4  4 28  8]
 [ 9 10 11 71]]<jupyter_text>**SUPPORT VECTOR MACHINE**<jupyter_code>from sklearn.svm import SVC 
svm_model_linear = SVC(kernel = 'linear', C = 1).fit(X_train, y_train) 
svm_predictions = svm_model_linear.predict(X_test) 


print(accuracy_score(y_true=y_test,y_pred=svm_predictions))
print(classification_report(y_test,svm_predictions)) 
# creating a confusion matrix 
print(confusion_matrix(y_test, svm_predictions) )
from sklearn.svm import SVC 
svm_model_linear = SVC().fit(X_train, y_train) 
svm_predictions = svm_model_linear.predict(X_test) 


print(accuracy_score(y_true=y_test,y_pred=svm_predictions))
print(classification_report(y_test,svm_predictions)) 
# creating a confusion matrix 
print(confusion_matrix(y_test, svm_predictions) )<jupyter_output>0.48632218844984804
              precision    recall  f1-score   support

       angry       0.69      0.66      0.67        90
       happy       0.36      0.40      0.38        94
     neutral       0.00      0.00      0.00        44
         sad       0.46      0.62      0.53       101

    accuracy                           0.49       329
   macro avg       0.38      0.42      0.39       329
weighted avg       0.43      0.49      0.45       329

[[59 27  0  4]
 [24 38  0 32]
 [ 1  5  0 38]
 [ 2 36  0 63]]<jupyter_text>**RANDOM FOREST**<jupyter_code>from sklearn.ensemble import RandomForestClassifier
  
 # create regressor object 
classifier = RandomForestClassifier(n_estimators = 100, random_state = 0) 
  
# fit the regressor with x and y data 
classifier.fit(X_train, y_train)   

c_p = classifier.predict(X_test) 


print(accuracy_score(y_true=y_test,y_pred=c_p))
print(classification_report(y_test,c_p)) 
# creating a confusion matrix 
print(confusion_matrix(y_test,c_p) )
from sklearn.ensemble import RandomForestClassifier
  
 # create regressor object 
classifier = RandomForestClassifier(n_estimators = 20,random_state = 0) 
  
# fit the regressor with x and y data 
classifier.fit(X_train, y_train)   

c_p = classifier.predict(X_test) 


print(accuracy_score(y_true=y_test,y_pred=c_p))
print(classification_report(y_test,c_p)) 
# creating a confusion matrix 
print(confusion_matrix(y_test,c_p) )<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11581196581196582, 'fraction_numerical': 0.10288461538461538, 'mean_word_length': 2.6854330708661416, 'pattern_counts': {'":': 8, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '551371', 'license': 'MIT', 'n_tokens_mistral': 4058, 'n_tokens_neox': 3536, 'n_words': 671, 'path': 'speechemotion_ml_algorithms.ipynb', 'repo_name': 'blhprasanna99/speech_emotion_detection', 'size': '9360'}