Emotion Detection: Using Opencv and Tensorflow
Emotion Detection: Using Opencv and Tensorflow
Group Members
Faculty Name
Prof. Rajakumar K.
Associate Professor , VIT Vellore
After discussing various possibilities as to how, we should build the product, the various
specifications relating to the design, feel and look of the web-app were decided, we also
came up with various add-ons, which would later be added to the website. We were
going to use the histogram segmentation algorithm and fuzzy classifier identification
algorithm, initially. Histogram Segmentation algorithm is used by us, because of the
reasons explained below. Histogram are constructed by splitting the range of the data
into equal-sized bins (called classes). Then for each bin, the numbers of points from the
data set that fall into each bin are counted. The term fuzzy refers to things which are not
clear or are vague. In the real world many times we encounter a situation when we can’t
determine whether the state is true or false, their fuzzy logic provides a very valuable
flexibility for reasoning. In this way, we can consider the inaccuracies and uncertainties of
any situation. Image segmentation is the process of grouping pixels of a given image into
regions homogenous with respect to certain features, and with potential semantic
content. The multitude of segmentation methods in the literature can be categorized
under four main qualifiers, namely, measurement-space-based algorithm. The histogram
analysis of an image is considered as a probability distribution over the pixel’s values.
Further, it provides a means to determine the information content of the image but it
would not take spatial factors into consideration. A localized frame work should therefore
be able to determine automatically the skewness introduced by non-uniformity
(heterogeneous edge and texture) regions varying above the uniformity (homogeneous)
regions. Further, the thresholds should be chosen based on the distance between the
valleys to peaks of the histogram. The Segmentation process is a straightforward process
where all necessary parameters are dictated based on the histogram analysis or soft
thresholding. Input image is segmented into various uniform and non-uniform regions
without any loss or integrity with reference to the input with highest efficiency and low
time complexity. The histogram analysis offered a better means to classify the image into
various binary labels based on the image statistics of the region. The soft threshold used
for classification and merging of the regions based on the localized statistics (i.e.
uniformity and non-uniformity) of the image in consideration. Simulation results and
analysis proved that the proposed algorithm shows good performance in image
segmentation without choosing the region of interest. But We found a method to not only
increase our efficiency but, also to make the whole process of hosting the python file a lot
easier. This method uses Harr Cascade and convolution neural networks. We further
trained our model with a lot of images (dataset), after which our model could correctly
predict the state of emotion. We further used PyFlask to host the website on our local
host and to integrate all the other components of the website.
REVIEW 3 (LAST)
INTRODUCTION
We know that emotions play a huge role in a person's life. With different types of
moments or moments the face of a person expresses how you feel or in which mood the
person is. Humans are capable of producing thousands of facial expressions during
interactions that vary in complexity, intensity and meaning. Emotion or intention is often
communicated by subtle changes in one or several discrete features. According to
different surveys, verbal components convey one-third of human communication, and
nonverbal components convey two-thirds. Among the many intangibles, by carrying
emotional states, facial expressions are one of the most important channels of information
in human communication. Thus, it is natural that face-to-face research has received a
great deal of attention over the past decades for applications not only in cognitive
science, but also in computer-related applications and performance. To find the optimal
solution for emotion recognition in learning the virtual environment, both accuracy and
efficiency must be achieved. Recognition of emotional changes in facial expressions of
online students, will allow teachers to tailor strategies and teaching methods, provide
students with real feedback, and achieve excellent teaching quality. The goal is to find the
best solution for emotion recognition based on facial recognition in virtual, real-time
learning environments. To achieve this goal, it is necessary to improve the accuracy and
efficiency of face recognition systems.
In this modern era, a person facing emotional distress has become a general aspect
of life. But some cases might reach out of hand if not taken proper care and guidance.
Besides, we have a jump in the number of suicides in this decade, which had emotion
conditions such as depression one of the main cause. Therefore, our group has decided
to develop a product which would not only help an individual understand his/her outer
emotional state, but also his/her inner mental condition, which would help them to take
preventive measures towards their psychological state before the situation would go out
of hands.
CONTRIBUTION
Despite the long history related to emotion detection, there are no comprehensive
literature reviews on the internal feelings of the person. Some review papers have focused
solely on conventional researches without introducing the psychological test. Therefore,
this paper is dedicated to a brief literature review from conventional emotion detection to
recent advanced emotion detection. The main contribution of this paper is that the results
are obtained through both the emotion detecting techniques i.e., first through the face
and then by taking a psychological test and then evaluating both the results and solutions
are given for low, moderate and high scores of stress and anger and then the person can
follow the solutions to overcome their current emotional state.
18BCE0770 NAVYA SAXENA
2. LITERATURE SURVEY
7|Page
18BCE0770 NAVYA SAXENA
problem.
8|Page
18BCE0770 NAVYA SAXENA
9|Page
18BCE0770 NAVYA SAXENA
10 | P a g e
18BCE0770 NAVYA SAXENA
11 | P a g e
18BCE0770 NAVYA SAXENA
12 | P a g e
18BCE0770 NAVYA SAXENA
Mental Health is majorly affected due to the outbreak of Covid-19. This project will
help the user to analyze their own emotions and help them find solutions if they are
feeling stressed or depressed or angry. The project aims to provide two fronts to
verif he e em i .O ei ca e li e image f he e h gh ebcam
and detect the emotion the user is feeling the most, through image processing. The
other is to ask the user to give their response for psychological quiz to detect their
emotion. Its result will present the percentage of each emotion. Based on both the
results, solutions will be provided to the user.
13 | P a g e
3. Background of project
Components at work
First, Haar Cascade classifier. It is used to detect the facial region in the
frame captured by the webcam. It’s functioning is described as follows:
instead of applying all the 6000 features on a window, it groups the features
into different stages of classifiers ( fi ) and applies them one by one. If any
feature suggests that it is not part of the Face region, the algorithm will rule
out that subregion as not-face. If the subregion passes all the stages, then it
is classified as the Face region. Once the facial region is detected, then it
runs through the machine learning algorithm.
4. Proposed Work
Once the user lands on the website, they are provided with information
about the project and how it works. The user can then begin analysis of their
current emotion.
First, we analyze the external emotion the user is feeling by capturing their
live video through webcam. Each frame goes through Haar Cascade
Classifier to detect the facial region. Once the face is detected, the sub-
region goes through the machine learning algorithm based on CNN
(convolutional neural network) to predict the emotion the user is
experiencing. The result is based on the maximum softmax score given by
the model. The accuracy of the model will depend on the number of epochs
used. The user can see the result of external emotion detection on their live
feed itself.
Next, the user is asked to take a psychological test. He/she must answer all
the questions using the 5 point scale. Once submitted, the result will
showcase the percentage of each emotion: Stress, Happy, Anger, Sad,
Disgust, and Fear, the user is feeling.
Then, the results obtained through both the emotion detecting techniques
will help the user understand their emotion. Solutions are given for low,
moderate and high scores of stress and anger. The user can follow the
solutions to overcome their current emotional state.
18BCE0770 NAVYA SAXENA
Figure 2 depicts the multiple stages image sub-region goes through to detect the facial region
in Haar Cascade Classifier
16 | P a g e
18BCE0770 NAVYA SAXENA
Figure 3 depicts how the emotion of a user is detected based on the trained model
Figure 4 depicts the working of Convolution Neural Network Algorithm. The Face region,
detected through Haar Cascade Classifier, is passed through multiple layers to detect the
emotion
a. Evaluation Analysis:
17 | P a g e
Camera.py
import cv2
import numpy as np
import argparse
import matplotlib.pyplot as plt
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.preprocessing.image import
ImageDataGenerator
from tensorflow.keras.models import Sequential
facecasc =
cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
ds_factor=0.6
emotion_dict = {0: "Angry", 1: "Disgusted", 2: "Fearful", 3: "Happy",
4: "Neutral", 5: "Sad", 6: "Surprised"}
model = Sequential()
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(7, activation='softmax'))
model.load_weights('model.h5')
class VideoCamera(object):
def init(self):
self = cv2.VideoCapture(0)
print("hi9")
def del(self):
self.release()
cv2.destroyAllWindows()
def get_frame(self):
self = cv2.VideoCapture(0)
print("hi10")
success,frame=self.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = facecasc.detectMultiScale(gray,scaleFactor=1.3,
minNeighbors=5)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y-50), (x+w, y+h+10), (255, 0, 0), 2)
roi_gray = gray[y:y + h, x:x + w]
cropped_img =
np.expand_dims(np.expand_dims(cv2.resize(roi_gray, (48, 48)), -1),
0)
prediction = model.predict(cropped_img)
maxindex = int(np.argmax(prediction))
cv2.putText(frame, emotion_dict[maxindex], (x+20, y-60),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2,
cv2.LINE_AA)
break
cv2.imshow('Video', cv2.resize(frame,(1600,960),interpolation
= cv2.INTER_CUBIC))
#if cv2.waitKey(1) & 0xFF == ord('q'):
#break
import numpy as np
import argparse
import matplotlib.pyplot as plt
import cv2
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.preprocessing.image import
ImageDataGenerator
from flask import Flask, render_template, Response
from camera import VideoCamera
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
app = Flask(_name_)
print("hi")
@app.route('/')
def index():
num_train = 28709
num_val = 7178
batch_size = 64
num_epoch = 50
train_datagen =
ImageDataGenerator(rescale=1./255)
val_datagen =
ImageDataGenerator(rescale=1./255)
# train_generator =
train_datagen.flow_from_directory(
# train_dir,
# target_size=(48,48),
# batch_size=batch_size,
# color_mode="grayscale",
# class_mode='categorical')
validation_generator =
val_datagen.flow_from_directory(
val_dir,
target_size=(48,48),
batch_size=batch_size,
color_mode="grayscale",
class_mode='categorical')
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(7, activation='softmax'))
print("hi4")
return render_template('index.html')
#cv2.imshow('Video', cv2.resize(frame,
(1600,960),interpolation = cv2.INTER_CUBIC))
#if cv2.waitKey(1) & 0xFF == ord('q'):
#break
print("hi5")
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n'
+ frame + b'\r\n\r\n')
# cap.release()
@app.route('/video_feed')
def video_feed():
print("hi7")
return
Response(gen(VideoCamera()),mimetype='multipart/x-
mixed-replace; boundary=frame')
if _name_ == '_main_':
print("hi8")
app.run(host='0.0.0.0',port='5000' ,debug=True)
5. EVALUATION AND RESULT ANALYSIS
a. Evaluation analysis
b. Result Analysis
d. Snapshots of Implementation
Figure 6 depicts the first page the user comes across. The user can directly Begin Analysis.
Figure 7 depicts the additional information provided to the user about different emotions.
19 | P a g e
18BCE0770 NAVYA SAXENA
Figure 10 depicts the page user comes across when they head over to the Quiz.
Figure 11 depicts a snippet of 24 questions presented to the user in the Emotion Detection Quiz.
21 | P a g e
18BCE0770 NAVYA SAXENA
Figure 12 depicts the user s result based on their response. Percentage of each emotion is
calculated and displayed for the user for self- analysis.
Figure 13 depicts the page the user comes across when they click on Solutions.
22 | P a g e
18BCE0770 NAVYA SAXENA
Figure 14 depicts that the user can contact their nearest counsellor.
23 | P a g e
18BCE0770 NAVYA SAXENA
24 | P a g e
7. Overall Discussion
Here we use powerful tools of image processing first, Haar Cascade classifier.
It is used to detect the facial region in the frame captured by the webcam.
Second, Machine Learning algorithm using Convolutional Neural Network.
OpenCV with Python is used to run the whole process. TensorFlow library is
used to train the machine learning model. The model runs frame by frame to
detect the emotion. The facial region detected through Haar Cascade
Classifier is passed as input to CNN (convolutional neural network) model.
Now for better result we came up with an Emotion testing Quiz through our
research after giving this quiz we can tell more accurately about one’s
emotion right now. These emotions include Stress, Happy, Anger, Sad,
Disgust, and Fear.
8. Conclusion
This project was created and test and it successfully gave one’s emotion both
by image detection and Quiz. Image detection part is Realtime with on spot
result on camera, while the quiz gives percentage of what type of emotion
one is feeling which includes Stress, Happy, Anger, Sad, Disgust, and Fear.
9.Reference
Nithya Roopa. S (2019). Emotion Recognition from Facial Expression using
Deep Learning. International Journal of Engineering and Advanced
Technology (IJEAT) ISSN: 2249 – 8958, Volume-8 Issue-6S.
A. A. Varghese, J. P. Cherian and J. J. Kizhakkethottam (2015). Overview on
emotion recognition system. 2015 International Conference on Soft-
Computing and Networks Security (ICSNS), Coimbatore, 2015, pp. 1-5, doi:
10.1109/ICSNS.2015.7292443.