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

Prerequisites

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 = sound_file.read(dtype="float32")
sample_rate=sound_file.samplerate
if chroma:
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))
return result
emotions={
'01':'neutral',
'02':'calm',
'03':'happy',
'04':'sad',
'05':'angry',
'06':'fearful',
'07':'disgust',
'08':'surprised'
}
observed_emotions=['calm', 'happy', 'fearful', 'disgust']
def load_data(test_size=0.2):
x,y=[],[]
for file in glob.glob("/content/sample_data/mini project/*.wav"):
file_name=os.path.basename(file)
emotion=emotions[file_name.split("-")[2]]
if emotion not in observed_emotions:
continue
feature=extract_feature(file, mfcc=True, chroma=True, mel=True)
x.append(feature)
y.append(emotion)
return train_test_split(np.array(x), y, test_size=test_size, random_state=9)
x_train,x_test,y_train,y_test=load_data(test_size=0.15)
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)
model.fit(x_train,y_train)
y_pred=model.predict(x_test)
accuracy=accuracy_score(y_true=y_test, y_pred=y_pred)
print("Accuracy: {:.2f}%".format(accuracy*100))

Summary

--

--

--

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