Mini Project-1 Documentation of Emotion Detection
Mini Project-1 Documentation of Emotion Detection
ESHA
EMOTIONS STUDY IN HUMANS AND ANALYSIS
(DETECTING THE EMOTIONS IN HUMANS BY USING TECHNOLOGY)
A project report submitted to
B. SHIVANI – S170301
P. SANDHYA – S170331
N. CHINNARI SAI – S170460
CERTIFICATE
This is to certify that the thesis work titled “ESHA is one of the methods useful for
detecting emotions in humans ” was successfully completed by B SIVANI
(S170301), P SANDHYA (S170331) and N CHINNARI SAI (S170460) In partial
fulfilment of the requirements for the Mini Project in Computer Science and
Engineering of Rajiv Gandhi University of Knowledge Technologies under my
guidance and output of the work carried out is satisfactory.
DECLARATION
I declared that B.
thisSIVANI – S170301
thesis work titled “ESHA is the analysis done on different
emotion detection technologies for further extinction of existing projects” is
P.SANDHYA – S170331
carried out by me during the year 2020-21 in partial fulfilment of the
requirements forN.the
CHINNARI
Mini ProjectSAI – S170460
in Computer Science and Engineering.
I further declare that this dissertation has not been submitted elsewhere for
any Degree. The matter embodied in this dissertation report has not been
submitted elsewhere for any other degree. Furthermore, the technical details
furnished in various chapters of this thesis are purely relevant to the above
project and there is no deviation from the theoretical point of view for design,
development and implementation.
ESHA
ACKNOWLEDGEMENT
I thank one and all who have rendered help to me directly or indirectly in the
completion of my thesis work
Project Associate
B. SIVANI – S170301
P.SANDHYA – S170331
ABSTRACT
Face detection has been around for ages. Taking a step forward, human
emotion displayed by face and felt by brain, captured in either video, electric
signal (EEG) or image form can be approximated. Human emotion detection
is the need of the hour so that modern artificial intelligent systems can
emulate and determine reactions from face. This can be helpful to make
informed decisions be it regarding identification of intent, promotion of
offers or security related threats. Recognizing emotions from images or video
is a trivial task for human eye, but proves to be very challenging for
machines and requires many image processing techniques for feature
extraction. Several machine learning algorithms are suitable for this job. Any
detection or recognition by machine learning requires training algorithm and
then testing them on a suitable dataset. This paper explores a couple of
machine learning algorithms as well as feature extraction techniques which
would help us in accurate identification of the human emotion.
ESHA
INDEX
CH.NO CONTENTS PG.NO
1 INTRODUCTION 1
1.1 Introduction 1
1.2 Problem of the Statement 2
1.3 Objectives 2
1.4 Goals 2
1.5 Scope 3
1.6 Applications 3
1.7 Limitations 4
2 LITERATURE SURVEY 5
2.1 Collecting Information 5
2.2 Study 5
2.3 Benefits 5
2.4 Summary 6
3 SYSTEM ANALYSIS 7
3.2 Disadvantages 7
3.4 Advantages 8
ESHA
4 SYSTEM DESIGN 9
5 SYSTEMIMPLEMENTATION 15
6 SYSTEM TESTING 47
Testing Introduction 47
Types Of Testing 47
Levels Of Testing 50
7 CONCLUSION 52
8 FUTURE ENHANCEMENT 53
APPENDIX
REFERENCES 54
Chapter-1
INTRODUCTION
1.1 Introduction
1.3 Objective
1.4 Goals
1.5 Scope
1) Different pre-processing techniques used are explained clearly.
2) Algorithms used in different emotion detection technologies are
explained clearly.
3) Analysis of some existing technologies or codes to determine best
model.
1.6 Applications
1. Video gaming. Using computer vision, the game console/video game detects
emotions via facial expressions during the game and adapts to it.
2. Medical diagnosis. Software can help doctors with the diagnosis of diseases
such as depression and dementia by using voice analysis.
5. Patient care. A ‘nurse bot’ not only reminds older patients on long-term
medical programs to take their medication, but also converses with them every
day to monitor the their overall wellbeing.
6. Car safety. Automotive vendors can use computer vision technology to monitor
the driver's emotional state. An extreme emotional state or drowsiness could
trigger an alert for the driver.
7. Autonomous car. In the future, the interior of autonomous cars will have many
sensors, including cameras and microphones, to monitor what is happening and
to understand how users view the driving experience.
10.Call center intelligent routing. An angry customer can be detected from the
beginning and can be routed to a well-trained agent who can also monitor in
real-time how the conversation is going and adjust.
11.Connected home. A VPA-enabled speaker can recognize the mood of the
person interacting with it and respond accordingly.
13.Retail. Retailers have started looking into installing computer vision emotion
AI technology in stores to capture demographic information and visitors' mood
and reactions.
1.7 Limitations
2.2 Study
2.3 Benefits
A local histogram is generated for each cell. Further, for each block of 2 x 2 cells,
4 histograms are concatenated. Finally we have 12 normalized histograms
concatenated into one single global histogram which gives us 432 feature vectors.
3.1.5 Geometrical facial features extraction
A set of 19 features are selected empirically by observing the landmark positions
on the face and which are more related to human expressions. These 19 features
are taken as a subset from an existing marker-less system for landmark
identification and localization, which has actual 66 2D features [9] [10]. These 19
features or landmarks on the face are given in Table 3 and Figure 9. Landmark
positions in the image space are used to define two set of features: eccentricity
features and linear features.
In this layer, when the neurons are grouped, the entrance is reduced. It is reduced
depending on the size of the groupings. For example, if the groups are a 2x2 size,
at the output we will obtain a quarter of the size of input data. It can be seen in
figure 3.4 that, even if this transformation technique, the spatial relationship is still
maintained.
We have thoroughly studied all those thesis and drawn the conclusion as shown
the fallowing the table.
3.2 Disadvantages
• So far we have seen many algorithms and techniques related to
Emotion Detection, but we cannot determine which algorithms
with different datasets gives us accurate predictions.
• For today’s world everyone is trying to use best algorithms in their
projects so that we can take it as an opportunity to identify best
suitable algorithms for those projects.
• For suppose someone is developing an emotion detection project
for an application if it does not able to do the predictions
accurately then it is said to be failed.
• These are the disadvantages for us now-a-days.
3.5 System
Requirements
Software
Requirements:
Open CV
Dlib
Scikit-Learn
Python
Jupyter Notebook
Hardware Requirements:
• RAM: 8GB or above
• Hard disk: 500 GB or above
CHAPTER- 4
SYSTEM DESIGN
SYSTEM IMPLEMENTATION
A static approach using extracted features and emotion recognition using
machine learning is used in this work. The focus is on extracting features using
python and image processing libraries and using machine learning algorithms
for prediction. Our implementation is divided into three parts. The first part is
image pre-processing and face detection. For face detection, inbuilt methods
available in dlib, Open CV library are used. Once the face is detected, the
region of interest and important facial features are extracted from it. There are
various features which can be used for emotion detection. In this work, the
focus is on facial points around the eyes, mouth, eyebrows etc.
When we run the code using CNN algorithm with Emotic data set we have
obtained the following results:
The overall accuracy we obtained roughly is – 75%
EMOTION DETECTION USING DEEP LEARNING WITH DEEPFACE
LIBRARY
When we run the code using CNN algorithm with pre trained dataset which is
available in deepface we have obtained the following results:
This model is giving best prediction result for happy, surprise, neutral emotions.
When we run the code using CNN algorithm with FER-2013 data set we have
obtained the following results:
This model is giving best prediction result for happy, surprise.
CHAPTER 7
SYSTEM TESTING
INTRODUCTION
Unit testing:
Integration testing:
Functional testing:
System testing:
Scheme difficult ensure so as to the whole included agenda process meets
principles. It exams a pattern to make sure identified and predictable
outcome. An illustration of procedure testing is the configuration oriented
approach integration scan. System testing is based on approach
descriptions and flows, emphasizing pre-driven system links and
integration aspects.
This testing is a trying out wherein where the application tester has
competencies of the interior workings, constitution and software
language, or at least its cause.
It's rationale. It's used to test areas that can't be reached from a black
box stage.
LEVELS OF TESTING
Acceptance Testing
User Acceptance testing trying out is a crucial section of any
mission and requires enormous participation by the tip user. It
additionally ensures that the procedure meets the functional
specifications.
Test Results:
The entire test cases recounted above passed effectually. No defects
Encountered
Conclusion
Future Enhancement
Based on the research we have done we concluded CNN with FER-Data Set
gives best prediction results.
This can further help to check the algorithms even with more datasets and
algorithms.
REFERENCES
https://drive.google.com/drive/u/0/folders/
1_0ct6vTPED6oHis4mc8A0BvId5UwQcmZ
https://drive.google.com/drive/u/0/folders/
1_0ct6vTPED6oHis4mc8A0BvId5UwQcmZ
https://drive.google.com/drive/u/0/folders/
1_0ct6vTPED6oHis4mc8A0BvId5UwQcmZ
https://drive.google.com/drive/u/0/folders/
1_0ct6vTPED6oHis4mc8A0BvId5UwQcmZ
APPENDIX
https://colab.research.google.com/drive/1gD6yKGgsfYKC4YlxS_gruxpyn8P_uecZ
"""
!ls
import tarfile
fname = 'archive.zip'
if fname.endswith("tar.gz"):
tar = tarfile.open(fname, "r:gz")
tar.extractall()
tar.close()
elif fname.endswith("tar"):
tar = tarfile.open(fname, "r:")
tar.extractall()
tar.close()
import os
import numpy as np
import pandas as pd
import tensorflow
from keras import optimizers
from matplotlib import pyplot
from sklearn.model_selection import train_test_split
df = pd.read_csv('fer2013/fer2013.csv')
df.head()
df.emotion.unique()
label_to_text =
{0:'anger',1:'disgust',2:'fear',3:'happiness',4:'sadness',5:'surprise',6:'
neutral'}
np.array(df.pixels.loc[0].split(' ')).reshape(48,48)
pyplot.imshow(np.array(df.pixels.loc[0].split('
')).reshape(48,48).astype('float'))
img_array = np.stack(img_array,axis=0)
labels = df.emotion.values
X_train,X_test,y_train,y_test =
train_test_split(img_array,labels,test_size = .2)
X_train.shape,y_train.shape,X_test.shape,y_test.shape
X_train = X_train/255
X_test = X_test/255
basemodel =
tensorflow.keras.models.Sequential([tensorflow.keras.layers.Conv2D(32,
(3,3),activation='relu',input_shape = (48,48,1)),
tensorflow.keras.layers.MaxPool2D(2,2),
tensorflow.keras.layers.BatchNormalization(),
tensorflow.keras.layers.Conv2D(64,
(3,3),activation='relu',input_shape = (48,48,1)),
tensorflow.keras.layers.MaxPool2D(2,2),
tensorflow.keras.layers.BatchNormalization(),
tensorflow.keras.layers.Conv2D(128,(3,3),activation='relu',input_shape =
(48,48,1)),
tensorflow.keras.layers.MaxPool2D(2,2),
tensorflow.keras.layers.BatchNormalization(),
#
tensorflow.keras.layers.Flatten(),
tensorflow.keras.layers.Dense(128,activation='relu'),
tensorflow.keras.layers.Dense(7,activation='softmax')
basemodel.compile(optimizer=tensorflow.keras.optimizers.RMSprop(learning_r
ate=.0001),
loss = 'sparse_categorical_crossentropy',
metrics =['accuracy'])
import os
try:
os.mkdir('checkpoint')
except:
pass
file_name = 'best_model.h5'
checkpoint_path= os.path.join('checkpoint',file_name)
call_back =
tensorflow.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
monitor='val_accuracy',
verbose=1,
save_freq='epoch',
save_best_only=True,
save_weights_only=False,
mode='max')
checkpoint_path
basemodel.fit(X_train,y_train,epochs=20,validation_split=.1,callbacks=call
_back)
final_model = tensorflow.keras.models.load_model(checkpoint_path)
from IPython.display import clear_output
import time
for k in range(40):
print(f'actual label is {label_to_text[y_test[k]]}')
predicted_class =
final_model.predict(tensorflow.expand_dims(X_test[k],0)).argmax()
print(f'predict label is {label_to_text[predicted_class]}')
pyplot.imshow(X_test[k].reshape((48,48)))
pyplot.show()
time.sleep(5)
clear_output(wait=True)
s
[2]: EMOTION DETECTION USING CNN WITH EMOTIFY DATASET
import numpy as np
import cv2
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D
from tensorflow.keras.optimizers import Adam
from keras.layers import MaxPooling2D
from keras.preprocessing.image import ImageDataGenerator
train_dir = 'train'
val_dir = 'test'
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=64,
color_mode="grayscale",
class_mode='categorical')
validation_generator = val_datagen.flow_from_directory(
val_dir,
target_size=(48,48),
batch_size=64,
color_mode="grayscale",
class_mode='categorical')
emotion_model = Sequential()
emotion_model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_
shape=(48,48,1)))
emotion_model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))
emotion_model.add(MaxPooling2D(pool_size=(2, 2)))
emotion_model.add(Dropout(0.25))
emotion_model.add(Conv2D(128, kernel_size=(3, 3), activation='relu'))
emotion_model.add(MaxPooling2D(pool_size=(2, 2)))
emotion_model.add(Conv2D(128, kernel_size=(3, 3), activation='relu'))
emotion_model.add(MaxPooling2D(pool_size=(2, 2)))
emotion_model.add(Dropout(0.25))
emotion_model.add(Flatten())
emotion_model.add(Dense(1024, activation='relu'))
emotion_model.add(Dropout(0.5))
emotion_model.add(Dense(7, activation='softmax'))
emotion_model.compile(loss='categorical_crossentropy',optimizer=Adam(lr=0.
0001, decay=1e-6),metrics=['accuracy'])
emotion_model_info = emotion_model.fit_generator(
train_generator,
steps_per_epoch=28709 // 64,
epochs=50,
validation_data=validation_generator,
validation_steps=7178 // 64)
#Saving the model
emotion_model.save('model.h5')
def emotion_analysis(emotions):
objects = ('angry', 'disgust', 'fear', 'happy', 'sad', 'surprise', 'ne
utral')
y_pos = np.arange(len(objects))
plt.show()
from IPython.display import display, Javascript
from google.colab.output import eval_js
from base64 import b64decode
def take_photo(filename='photo.jpg', quality=0.8):
js = Javascript('''
async function takePhoto(quality) {
const div = document.createElement('div');
const capture = document.createElement('button');
capture.textContent = 'Capture';
div.appendChild(capture);
document.body.appendChild(div);
div.appendChild(video);
video.srcObject = stream;
await video.play();
take_photo()
import cv2
def facecrop(image):
facedata = '/content/haarcascade_frontalface_alt.xml'
cascade = cv2.CascadeClassifier(facedata)
img = cv2.imread(image)
try:
minisize = (img.shape[1],img.shape[0])
miniframe = cv2.resize(img, minisize)
faces = cascade.detectMultiScale(miniframe)
for f in faces:
x, y, w, h = [ v for v in f ]
cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 2)
cv2.imwrite('capture.jpg', sub_face)
#print ("Writing: " + image)
except Exception as e:
print (e)
if __name__ == '__main__':
facecrop('/content/photo.jpg')
#Testing a file.
import numpy as np
import matplotlib.pyplot as plt
file = '/content/capture.jpg'
true_image = image.load_img(file)
img = image.load_img(file, color_mode="grayscale", target_size=(48, 48))
x = image.img_to_array(img)
x = np.expand_dims(x, axis = 0)
x /= 255
custom = emotion_model.predict(x)
emotion_analysis(custom[0])
x = np.array(x, 'float32')
x = x.reshape([48, 48]);
plt.imshow(true_image)
plt.show()
import imutils
import numpy as np
import cv2
from google.colab.patches import cv2_imshow
from IPython.display import display, Javascript
from google.colab.output import eval_js
from base64 import b64decode
def take_photo(filename ='photo.jpg',quality = 0.8):
js = Javascript('''
async function takePhoto(quality){
const div = document.createElement('div');
const capture = document.createElement('button');
capture.textContent = 'Capture';
div.appendChild(capture);
document.body.appendChild(div);
div.appendChild(video);
video.srcObject = stream;
await video.play();
google.colab.output.setIframeHeight(document.documentElement.scrollH
eight,true);
image = cv2.imread(image_file)
print(img_array)
# Read all the image in the dataset and convert into array
training_Data = [] # data array
def create_training_Data():
for category in Classes:
path = os.path.join(Datadirectory, category)
class_num = Classes.index(category)
for img in os.listdir(path):
try:
img_array = cv2.imread(os.path.join(path,img))
new_array = cv2.resize(img_array,(img_size, img_size))
training_Data.append([new_array,class_num])
except Exception as e:
pass
print(len(training_Data))
temp.shape
import random
random.shuffle(training_Data) # shuffle the features
x = [] #data
y = [] #label
x.shape
x = x/255.0; #normalize the data
y = np.array()
y.shape
base_output = model.layers[-2].output
base_output
final_output #output
new_model.summary()
new_model = tf.keras.models.load_model('/content/imp/final_model.h5')
frame = cv2.imread("/content/imp/image4.jpg")
frame.shape
plt.imshow(cv2.cvtColor(frame,cv2.COLOR_BGR2RGB))
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
gray.shape
faces = faceCascade.detectMultiScale(gray,1.1,4)
for x,y,w,h in faces:
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
cv2.rectangle(frame, (x,y),(x+w,y+h),(0,255,0),2)
faces = faceCascade.detectMultiScale(roi_gray)
if len(faces) == 0:
print("Face not detected")
else:
for(ex,ey,ew,eh) in faces:
face_roi = roi_color[ey: ey+eh, ex:ex + ew]
plt.imshow(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
plt.imshow(cv2.cvtColor(face_roi, cv2.COLOR_BGR2RGB))
Predictions = new_model.predict(final_image)
Predictions[0]
import numpy as np
import cv2
import streamlit as st
from tensorflow import keras
from keras.models import model_from_json
from keras.preprocessing.image import img_to_array
from streamlit_webrtc import webrtc_streamer, VideoTransformerBase,
RTCConfiguration, VideoProcessorBase, WebRtcMode
# load model
emotion_dict = {0:'angry', 1 :'happy', 2: 'neutral', 3:'sad', 4:
'surprise'}
# load json and create model
json_file = open('/content/emotion_model1.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
classifier = model_from_json(loaded_model_json)
#load face
try:
face_cascade =
cv2.CascadeClassifier('/content/haarcascade_frontalface_default.xml')
except Exception:
st.write("Error loading cascade classifiers")
class Faceemotion(VideoTransformerBase):
def transform(self, frame):
img = frame.to_ndarray(format="bgr24")
#image gray
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(
image=img_gray, scaleFactor=1.3, minNeighbors=5)
for (x, y, w, h) in faces:
cv2.rectangle(img=img, pt1=(x, y), pt2=(
x + w, y + h), color=(255, 0, 0), thickness=2)
roi_gray = img_gray[y:y + h, x:x + w]
roi_gray = cv2.resize(roi_gray, (48, 48),
interpolation=cv2.INTER_AREA)
if np.sum([roi_gray]) != 0:
roi = roi_gray.astype('float') / 255.0
roi = img_to_array(roi)
roi = np.expand_dims(roi, axis=0)
prediction = classifier.predict(roi)[0]
maxindex = int(np.argmax(prediction))
finalout = emotion_dict[maxindex]
output = str(finalout)
label_position = (x, y)
cv2.putText(img, output, label_position,
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
return img
def main():
# Face Analysis Application #
st.title("Real Time Face Emotion Detection Application")
activiteis = ["Home", "Webcam Face Detection", "About"]
choice = st.sidebar.selectbox("Select Activity", activiteis)
st.sidebar.markdown(
""" Developed by Mohammad Juned Khan
Email : [email protected]
[LinkedIn] (https://www.linkedin.com/in/md-juned-khan)""")
if choice == "Home":
html_temp_home1 = """<div style="background-
color:#6D7B8D;padding:10px">
<h4
style="color:white;text-align:center;">
Face Emotion detection
application using OpenCV, Custom CNN model and Streamlit.</h4>
</div>
</br>"""
st.markdown(html_temp_home1, unsafe_allow_html=True)
st.write("""
The application has two functionalities.
1. Real time face detection using web cam feed.
2. Real time face emotion recognization.
""")
elif choice == "Webcam Face Detection":
st.header("Webcam Live Feed")
st.write("Click on start to use webcam and detect your face
emotion")
webrtc_streamer(key="example", mode=WebRtcMode.SENDRECV,
rtc_configuration=RTC_CONFIGURATION,
video_processor_factory=Faceemotion)
html_temp4 = """
<div style="background-
color:#98AFC7;padding:10px">
<h4 style="color:white;text-
align:center;">This Application is developed by Mohammad Juned Khan
using Streamlit Framework, Opencv, Tensorflow and Keras library for
demonstration purpose. If you're on LinkedIn and want to connect, just
click on the link in sidebar and shoot me a request. If you have any
suggestion or wnat to comment just write a mail at
[email protected]. </h4>
<h4 style="color:white;text-
align:center;">Thanks for Visiting</h4>
</div>
<br></br>
<br></br>"""
st.markdown(html_temp4, unsafe_allow_html=True)
else:
pass
if __name__ == "__main__":
main()