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

Merge branch 'IT20201364_Thathsarani_R.P.H.S.R'

parents 93489cf8 e1ce2439
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))
from setuptools import setup
setup(
name='New_Voice_Assistant',
version='',
packages=[''],
url='',
license='',
author='malithg',
author_email='',
description=''
)
import pyttsx3
from flask import jsonify
def text_to_speech(text):
print("Dev -->", text)
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
# Send the conversation data to the front-end using jsonify (Flask)
jsonify({'message': text})
import datetime
def wake_up(text, name):
if text is not None:
return True if name in text.lower() else False
return False
def action_time():
return datetime.datetime.now().time().strftime('%H:%M')
def calculate_stress_level(sentiment_analysis):
# Determine stress level based on detected emotions
if any(emotion in sentiment_analysis for emotion in ["Angry", "Fear", "Sad", "Disgust"]):
return "High Stress"
elif any(emotion in sentiment_analysis for emotion in ["Happy", "Disgust"]):
return "Low Stress"
else:
return "Low Stress"
def calculate_emotion_percentages(sentiment_analysis):
total_emotions = len(sentiment_analysis)
emotion_count = {}
for emotion in sentiment_analysis:
emotion_count[emotion] = emotion_count.get(emotion, 0) + 1
emotion_percentages = {}
for emotion, count in emotion_count.items():
percentage = (count / total_emotions) * 100
emotion_percentages[emotion] = round(percentage, 2)
return emotion_percentages
\ No newline at end of file
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