-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
172 lines (153 loc) · 6.11 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from flask import Flask, render_template, request
import sounddevice as sd
from scipy.io.wavfile import write
import sounddevice as sd
import soundfile as sf
import numpy as np
import pandas as pd
import os
import tensorflow as tf
from tensorflow import keras
import joblib
import librosa
from sklearn.preprocessing import StandardScaler, OrdinalEncoder, OneHotEncoder
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.model_selection import train_test_split
import sys
import warnings
app = Flask(__name__, template_folder='templates')
@app.route('/')
def Home():
return render_template('index.html')
@app.route('/recording', methods=['GET', 'POST'])
def recording():
fs = 44100 # Sample rate
seconds = 4 # Duration of recording
if request.method == "POST":
print("-----------------------START RECORDING-------------------------")
myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)
sd.wait()
print("-----------------------STOP RECORDING-------------------------")
write('static/output.wav', fs, myrecording)
return render_template('recording.html')
@app.route('/predicting', methods=['GET', 'POST'])
def predicting():
if not sys.warnoptions:
warnings.simplefilter("ignore")
warnings.filterwarnings("ignore", category=DeprecationWarning)
data, sample_rate = librosa.load('static/output.wav')
def Decoding(list):
if list[0] == 0:
return "Angry"
elif list[0] == 1:
return "Fear"
elif list[0] == 2:
return "Surprise"
elif list[0] == 3:
return "Disgust"
elif list[0] == 4:
return "Calm"
elif list[0] == 5:
return "Happy"
elif list[0] == 6:
return "Sad"
else:
return "Neutral"
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
def stretch(data, rate=0.8):
return librosa.effects.time_stretch(data, rate)
def shift(data):
shift_range = int(np.random.uniform(low=-5, high = 5)*1000)
return np.roll(data, shift_range)
def pitch(data, sampling_rate, pitch_factor=0.7):
return librosa.effects.pitch_shift(data, sampling_rate, pitch_factor)
def extract_features(data):
# ZCR
result = np.array([])
zcr = np.mean(librosa.feature.zero_crossing_rate(y=data).T, axis=0)
result=np.hstack((result, zcr)) # stacking horizontally
# Chroma_stft
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)) # stacking horizontally
# MFCC
mfcc = np.mean(librosa.feature.mfcc(y=data, sr=sample_rate).T, axis=0)
result = np.hstack((result, mfcc)) # stacking horizontally
# Root Mean Square Value
rms = np.mean(librosa.feature.rms(y=data).T, axis=0)
result = np.hstack((result, rms)) # stacking horizontally
# MelSpectogram
mel = np.mean(librosa.feature.melspectrogram(y=data, sr=sample_rate).T, axis=0)
result = np.hstack((result, mel)) # stacking horizontally
return result
def get_features(path):
# duration and offset are used to take care of the no audio in start and the ending of each audio files as seen above.
data, sample_rate = librosa.load(path, duration=4, offset=0.6)
# without augmentation
res1 = extract_features(data)
result = np.array(res1)
# # data with noise
# noise_data = noise(data)
# res2 = extract_features(noise_data)
# result = np.array((result, res2) # stacking vertically
# # # data with stretching and pitching
# new_data = stretch(data)
# data_stretch_pitch = pitch(new_data, sample_rate)
# res3 = extract_features(data_stretch_pitch)
# result = np.array(res3) # stacking vertically
return result
# # Extract data and sampling rate from file
# data, fs = sf.read(filename, dtype='float32')
# sd.play(data, fs)
# status = sd.wait() # Wait until file is done playing
CNN = tf.keras.models.load_model('Model/content/CNN')
LR = joblib.load("Model/LogisticRegresion.pkl")
SVM = joblib.load("Model/SVM.pkl")
Knn = joblib.load("Model/Knn.pkl")
test_case = 'static/output.wav'
featuring = get_features(test_case)
X3 = [featuring]
X4 = np.expand_dims(X3, axis=2)
nl = []
# scaler = StandardScaler()
# X3 = scaler.fit_transform(X3)
# X3 = X3.reshape(1, -1)
pred_test_1 = LR.predict(X3)
pred_test_2 = SVM.predict(X3)
pred_test_3 = Knn.predict(X3)
pred_test_4 = CNN.predict(X4)
pred_test_4 = pred_test_4.flatten()
print('pred_test_3',pred_test_3)
i=0
for pred in pred_test_4:
nl.append(pred)
index_max = max(nl)
# print(index_max)
# print(nl)
for pred in nl:
if pred == index_max:
print(i, type(i))
else:
i+=1
print(Decoding(np.ndarray(i)))
# pred_test_4 = CNN.predict(X3)
# encoder2.fit_transform(np.array(Y1).reshape(-1, 1))
# y_pred2 = encoder2.inverse_transform(pred_test.reshape(-1, 1))
# print(f"Logistic Regression: {Decoding(pred_test_1)},SVM: {Decoding(pred_test_2)}, K-Nearest Neighbour: {Decoding(pred_test_3)}")
df = pd.DataFrame({"Logistic Regression":Decoding(pred_test_1),
"SVM": Decoding(pred_test_2),
"Knn": Decoding(pred_test_3),
'CNN': Decoding(np.ndarray(i))
}, index=["Prediction"])
print(df)
predictions = {"LogisticRegression":Decoding(pred_test_1),
"SVM": Decoding(pred_test_2),
"Knn": Decoding(pred_test_3),
'CNN': Decoding(np.ndarray(i))
}
return render_template('predict.html', predictions=predictions)
if __name__ == "__main__":
app.run(debug=True)