Commit e1ce2439 authored by Thathsarani R.P.H.S.R's avatar Thathsarani R.P.H.S.R

Create model_test.py and model.py

parent e9dd1bf3
import librosa
import librosa.display
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import os
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import sys
import glob
import warnings
import keras
from keras.preprocessing.sequence import pad_sequences
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Dropout, BatchNormalization
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from keras.utils import to_categorical
from tqdm import tqdm
warnings.filterwarnings("ignore")
dataList = os.listdir('/RawData')
classLabels = ('Angry', 'Fear', 'Disgust', 'Happy', 'Sad', 'Surprised', 'Neutral')
data = []
labels = []
for number, path in enumerate(tqdm(dataList)):
X, sample_rate = librosa.load('/content/drive/MyDrive/Dataset/newP/RawData/' + path, res_type='kaiser_best',
duration=2.5, sr=22050 * 2, offset=0.5)
sample_rate = np.array(sample_rate)
mfccs = librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=39)
feature = mfccs
data.append(feature)
if path[6:8] == '01' or path[0:1] == 'n':
labels.append(6)
if path[6:8] == '02':
labels.append(6)
if path[6:8] == '03' or path[0:1] == 'h':
labels.append(3)
if path[6:8] == '04' or path[0:2] == 'sa':
labels.append(4)
if path[6:8] == '05' or path[0:1] == 'a':
labels.append(0)
if path[6:8] == '06' or path[0:1] == 'f':
labels.append(1)
if path[6:8] == '07' or path[0:1] == 'd':
labels.append(2)
if path[6:8] == '08' or path[0:2] == 'su':
labels.append(5)
max_len = 216
data = np.array([pad_sequences(x, maxlen=max_len, padding='post', truncating='post') for x in data])
labels = np.array(labels)
X_train, X_test, Y_train, Y_test = train_test_split(data, labels, test_size=0.3, random_state=42)
numLabels = len(classLabels)
Y_train = to_categorical(Y_train)
Y_test = to_categorical(Y_test)
X_train = X_train[..., np.newaxis]
X_test = X_test[..., np.newaxis]
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(X_train.shape[1:])))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(numLabels, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
best_acc = 0
epochs = 50
for i in tqdm(range(epochs)):
model.fit(X_train, Y_train, batch_size=32, epochs=1)
loss, acc = model.evaluate(X_test, Y_test)
if acc > best_acc:
best_acc = acc
model.evaluate(X_test, Y_test)
best_acc
# After training the model
# Save the model
model.save("my_model.h5")
\ No newline at end of file
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from flask_login import UserMixin
db = SQLAlchemy()
class User(db.Model, UserMixin):
__tablename__ = 'user' # Specify the existing table name
__table_args__ = {'extend_existing': True} # Add this line
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(255), unique=True, nullable=False)
password = db.Column(db.String(255), nullable=False)
login_time = db.Column(db.DateTime, default=datetime.utcnow)
def __init__(self, username, password):
self.username = username
self.password = password
def is_active(self):
return True # You can customize this logic based on your application's requirements
class StressLevel(db.Model):
__tablename__ = 'stress_level' # Specify the table name
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
stress_level = db.Column(db.String(255), nullable=False)
login_time = db.Column(db.DateTime, default=datetime.utcnow) # Include the login_time field
user = db.relationship('User', backref=db.backref('stress_levels', lazy=True))
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