Facial Emotion
Recognition
This project focuses on developing a system to recognize facial
emotions using Convolutional Neural Networks (CNNs). It aims to
enhance human-computer interaction through emotion detection.
PROJECT : GENERATIVE AI – FACIAL EMOTION RECOGINATION USING CNN
TEACHER : Mr. JAGAT PAL SIR
NAME OF THE STUDENT CONTRIBUTED : MOHIT KUMAR – GF202460946
ATUL SHARMA – GF202454368
SAHIL -- GF202455735
ANKUSH KUMAR -- GF202459590
ABHIMANYU -- GF202457761
Project Goals
Accurate Emotion Detection Real-time Processing
Develop a model to classify emotions from facial Process images in real-time for immediate feedback.
expressions.
User Friendly Integration Capabilities
Create an intuitive interface for various user Ensure easy integration with existing applications.
demographics.
Robustness Emotions
Perform reliably across diverse lighting and Neutral ,Surprise ,Sad ,Happy ,Fear ,Disgust , Angry
orientations.
Understanding CNN
CNNs are a type of deep learning model specifically designed to process data with a grid-like topology, such
as images. They are highly effective in image recognition and classification tasks.
Structure of CNN Convolution Operation
CNNs consist of convolutional layers, pooling Convolutional Layer: Extracts features from the
layers, and fully connected layers. input image using filters/kernels.
Pooling Layers Activation Functions
Reduces the spatial dimensions of the feature Typically uses ReLU to introduce non-linearity.
maps, retaining the most important information.
Backpropagation Fully Connected Layer
Adjusts weights based on error to improve accuracy. Similar to a traditional neural network layer,
used for classification.
Softmax Layer Flatten Layer
MAIN CODE :
import cv2
import numpy as np
import tensorflow as tf
from [Link] import Sequential, load_model
from [Link] import Conv2D, MaxPooling2D, Flatten, Dense
from [Link] import ImageDataGenerator
import os
# Enable eager execution for debugging (optional)
[Link].run_functions_eagerly(True)
# --------------------- STEP 1: Load & Preprocess Data ---------------------
train_data_gen = ImageDataGenerator(rescale=1./255)
validation_data_gen = ImageDataGenerator(rescale=1./255)
# Update the path to your local dataset directory
data_dir = './data' # <- Change this if needed
train_dataset = train_data_gen.flow_from_directory(
[Link](data_dir, 'train'),
target_size=(48, 48),
batch_size=32,
color_mode='grayscale',
class_mode='categorical'
)
validation_dataset = validation_data_gen.flow_from_directory(
[Link](data_dir, 'validation'),
target_size=(48, 48),
batch_size=32,
color_mode='grayscale',
class_mode='categorical'
)# --------------------- STEP 2: Build the CNN Model ---------------------
model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(48, 48, 1)),
MaxPooling2D((2, 2)),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D((2, 2)),
Second part
Dense(128, activation='relu'),
Dense(train_dataset.num_classes, activation='softmax')
])
# --------------------- STEP 3: Compile the Model ---------------------
[Link](optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# --------------------- STEP 4: Train the Model ---------------------
[Link](train_dataset, validation_data=validation_dataset, epochs=10)
# --------------------- STEP 5: Save the Model ---------------------
[Link]('emotion_recognition_cnn.h5')
# --------------------- STEP 6: Real-time Emotion Detection ---------------------
model = load_model('emotion_recognition_cnn.h5')
# Get emotion labels
emotion_dict = {idx: label for label, idx in train_dataset.class_indices.items()}
# Open webcam
cap = [Link](0)
# Load Haar Cascade for face detection
face_cascade = [Link]([Link] + 'haarcascade_frontalface_default.xml')
while [Link]():
ret, frame = [Link]()
if not ret:
print("Error: Couldn't read frame.")
break
gray_frame = [Link](frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.3, minNeighbors=5)
for (x, y, w, h) in faces:
roi = gray_frame[y:y + h, x:x + w]
roi = [Link](roi, (48, 48))
roi = [Link]('float32') / 255.0
roi = np.expand_dims(roi, axis=(0, -1))
prediction = [Link](roi)
emotion = emotion_dict[[Link](prediction)]
[Link](frame, emotion, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
[Link](frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
[Link]("Facial Emotion Recognition", frame)
if [Link](1) & 0xFF == ord('q'):
break
[Link]()
[Link]()
Libraries Used
Keras TensorFlow
Keras is a high-level deep learning API written in TensorFlow is an open-source machine learning
Python, running on top of deep learning framework developed by Google Brain. It is
frameworks like TensorFlow. It is designed to widely used for building and training deep
enable fast experimentation with neural learning models, including CNNs, RNNs, and
networks and is user-friendly, modular, and more.
extensible.
NumPy OpenCV
NumPy (Numerical Python) is a fundamental OpenCV (Open Source Computer Vision Library)
library for numerical computations in Python. It is a powerful and comprehensive library used for
provides support for creating and manipulating computer vision and image processing tasks. It
large, multi-dimensional arrays and matrices, provides various functions for manipulating
along with a collection of mathematical functions images and videos, including object detection,
to perform operations on these arrays. face recognition, motion tracking, and more.
Matplotlib
Step-by-Step Process
1 2 3 4
Data Collection Data Preprocessing Model Building Training the Model
Gather a diverse Conduct image Define CNN Train the model to
dataset of facial processing, including architecture with minimize loss,
expressions with resizing and appropriate layers for adjusting learning
labeled emotions. normalization. emotion recognition. rates as needed.
5 6
Model Evaluation Implementation of
Analyze performance
Predictions
on a validation set Finalize the model for
using accuracy and F1 real-time emotion