Commit 36444dee authored by D.M.J.C Dissanayake's avatar D.M.J.C Dissanayake 🎓

intialized flask server and implemeted the voice emotion detection api

parent bfc04ab5
__pycache__
Emotion_Voice_Detection_Model.pkl
output10.wav
\ No newline at end of file
initiating back-end root
\ No newline at end of file
from flask import Flask, render_template
from src.components.voice_emotion_detection.voice_emotion_detection import predictVoiceEmotion
app = Flask(__name__)
@app.route('/')
def index():
return "welcome to SMART INFORTAITEMENT SYSTEM"
@app.route('/voice-emotion-detection')
def voice_emotion_detection():
return predictVoiceEmotion()
if __name == '__main__':
app.run(debug=True)
initiating branch
reset testing of git user credintials
\ No newline at end of file
initiating branch
testing git credential changes
\ No newline at end of file
Branch initiated
Test git credential changes
\ No newline at end of file
initiating branch
test git credentials changes
\ No newline at end of file
click==8.0.4
colorama==0.4.4
Flask==2.0.3
itsdangerous==2.1.1
Jinja2==3.0.3
MarkupSafe==2.1.0
Werkzeug==2.0.3
def predictVoiceEmotion():
#Install all the Reqiuired Libraries and Packages
import os
import glob
from tqdm import tqdm
import pandas as pd
import numpy as np
from sklearn.feature_extraction import DictVectorizer
import matplotlib.pyplot as plt
from scipy.io import wavfile
from python_speech_features import mfcc , logfbank
import librosa as lr
import os, glob, pickle
import librosa
from scipy import signal
import noisereduce as nr
from glob import glob
import librosa
# get_ipython().magic('matplotlib inline')
#All the Required Packages and Libraies are installed.
import soundfile
from tensorflow.keras.layers import Conv2D,MaxPool2D, Flatten, LSTM
from keras.layers import Dropout,Dense,TimeDistributed
from keras.models import Sequential
from tensorflow.keras.utils import to_categorical
from sklearn.utils.class_weight import compute_class_weight
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
import speech_recognition as sr
from scipy.io import wavfile as wav
from scipy.fftpack import fft
import glob,pickle
import soundfile
import os
import glob
from tqdm import tqdm
import pandas as pd
import numpy as np
from sklearn.feature_extraction import DictVectorizer
import matplotlib.pyplot as plt
from scipy.io import wavfile
from python_speech_features import mfcc , logfbank
import librosa as lr
import os, glob, pickle
import librosa
from scipy import signal
import noisereduce as nr
from glob import glob
import librosa
# get_ipython().magic('matplotlib inline')
#All the Required Packages and Libraies are installed.
import soundfile
from tensorflow.keras.layers import Conv2D,MaxPool2D, Flatten, LSTM
from keras.layers import Dropout,Dense,TimeDistributed
from keras.models import Sequential
from tensorflow.keras.utils import to_categorical
from sklearn.utils.class_weight import compute_class_weight
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
#SAVING THE MODEL
import pickle
import pyaudio
import wave
from glob import glob
import os
import glob
import os
import librosa as lr
import pandas as pd
import librosa.display
import glob
import librosa
#Feature Extraction of Audio Files Function
#Extract features (mfcc, chroma, mel) from a sound file
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 in the RAVDESS dataset to be classified Audio Files based on .
emotions={
'01':'neutral',
'02':'calm',
'03':'happy',
'04':'sad',
'05':'angry',
'06':'fearful',
'07':'disgust',
'08':'surprised'
}
#These are the emotions User wants to observe more :
observed_emotions=['calm', 'happy', 'fearful', 'disgust']
# Save the Modle to file in the current working directory
#For any new testing data other than the data in dataset
Pkl_Filename = "src/models/Emotion_Voice_Detection_Model.pkl"
# Load the Model back from file
with open(Pkl_Filename, 'rb') as file:
Emotion_Voice_Detection_Model = pickle.load(file)
Emotion_Voice_Detection_Model
#RECORDED USING MICROPHONE:
CHUNK = 1024
FORMAT = pyaudio.paInt16 #paInt8
CHANNELS = 1
RATE = 44100 #sample rate
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output10.wav"
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK) #buffer
print("* recording")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data) # 2 bytes(16 bits) per channel
print("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
#The file 'output10.wav' in the next cell is the file that was recorded live using the code :
data, sampling_rate = librosa.load('output10.wav')
## Appying extract_feature function on random file and then loading model to predict the result
file = 'output10.wav'
ans =[]
new_feature = extract_feature(file, mfcc=True, chroma=True, mel=True)
ans.append(new_feature)
ans = np.array(ans)
#data.shape
print(Emotion_Voice_Detection_Model.predict(ans))
n = Emotion_Voice_Detection_Model.predict(ans)
output = ""
for o in n:
output += o
return output
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<bytecodeTargetLevel target="11" />
<bytecodeTargetLevel target="1.8" />
</component>
</project>
\ No newline at end of file
......@@ -7,6 +7,7 @@
<option name="testRunner" value="GRADLE" />
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleJvm" value="11" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />
......
......@@ -9,7 +9,7 @@
</map>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment