Data Science Mini Project - Speech emotion recognition

What is Speech Emotion Recognition?

What is librosa?

What is JupyterLab?

C:\Users\Janvi>jupyter lab

Speech Emotion Recognition — Objective

Speech Emotion Recognition

The Dataset


pip install librosa soundfile numpy sklearn pyaudio

Steps for speech emotion recognition python projects

import librosa
import soundfile
import os, glob, pickle
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
  • mfcc: Mel Frequency Cepstral Coefficient, represents the short-term power spectrum of a sound
  • chroma: Pertains to the 12 different pitch classes
  • mel: Mel Spectrogram Frequency
def extract_feature(file_name, mfcc, chroma, mel):
with soundfile.SoundFile(file_name) as sound_file:
X ="float32")
if chroma:
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))
return result
observed_emotions=['calm', 'happy', 'fearful', 'disgust']
def load_data(test_size=0.2):
for file in glob.glob("/content/sample_data/mini project/*.wav"):
if emotion not in observed_emotions:
feature=extract_feature(file, mfcc=True, chroma=True, mel=True)
return train_test_split(np.array(x), y, test_size=test_size, random_state=9)
print((x_train.shape[0], x_test.shape[0]))
print(f'Features extracted: {x_train.shape[1]}')
model=MLPClassifier(alpha=0.01, batch_size=256, epsilon=1e-08, hidden_layer_sizes=(300,), learning_rate='adaptive', max_iter=500),y_train)
accuracy=accuracy_score(y_true=y_test, y_pred=y_pred)
print("Accuracy: {:.2f}%".format(accuracy*100))





2x AWS Certified

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Location-enable Your Data Analysis with Python

How to build a narrative from data

NIKKEI ( $NKD_F ) Buying The Dips After Elliott Wave Double Three

Introduction to PowerBI and Get started with PowerBI, Prepare data for analysis and Model data in…

Data is not Oil, it’s Land

Supervised Learning

Teaching a computer to predict Alligator vs Crocodile in 7 minutes (quick guide for training your…

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Janvi Ajudiya

Janvi Ajudiya

2x AWS Certified

More from Medium

Take your business to the next level with Artificial Intelligence Chatbot:

AI vs Machine Learning vs Deep Learning

What is Machine Learning?

Hypergraphs Applications in Machine Learning