Skip to content

  • Home
    • Featured Questions
    • Latest Updates
  • Subjects
    • Mathematics
    • Science
    • Computers
    • English
    • General Knowledge
    • History
  • Tips & Strategies
    • Test taking strategy
    • Stress Management
    • Time Management
  • Tools & Utilities
    • Generate Speech From Text
    • Change Your Voice
    • Generate Image From Text
    • Compress Your Images
  • Contact
    • Privacy Policy
    • Mission & Vision
  • Toggle search form

Project: Facial Expression Recognition with CNN using TensorFlow and Keras

Posted on February 10, 2024February 10, 2024 By allexamprep.com No Comments on Project: Facial Expression Recognition with CNN using TensorFlow and Keras

Let’s create a project for facial expression recognition using a Convolutional Neural Network (CNN) in Python with TensorFlow and Keras. In this example, we’ll build a model to recognize facial expressions (e.g., happy, sad, angry) from images.

1. Project Setup:

  • Create a new Python project or script.
  • Install necessary libraries:
pip install tensorflow matplotlib opencv-python

2. Data Loading:

  • Download a dataset for facial expression recognition. For this example, we’ll use the “fer2013” dataset from Kaggle. Download it here and place the “fer2013.csv” file in your project directory.
import pandas as pd

# Load the dataset
df = pd.read_csv('fer2013.csv')

3. Data Preprocessing:

  • Preprocess the image and label data:
import numpy as np
from sklearn.model_selection import train_test_split

# Convert pixel strings to numpy arrays
pixels = df['pixels'].apply(lambda x: np.array(x.split(), dtype="float32")).values

# Normalize pixel values
pixels = pixels / 255.0

# Reshape the pixel arrays to 48x48 images
X = np.array(list(pixels))
X = X.reshape(X.shape[0], 48, 48, 1)

# Convert labels to categorical format
y = pd.get_dummies(df['emotion']).values

4. Model Definition:

  • Define a Convolutional Neural Network (CNN) for facial expression recognition:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout

model = Sequential()

model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(48, 48, 1)))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(7, activation='softmax'))  # 7 output classes for emotions

5. Model Compilation:

  • Compile the model, specifying the loss function, optimizer, and metrics:
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

6. Model Training:

  • Train the CNN model on the facial expression data:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

model.fit(X_train, y_train, epochs=15, validation_data=(X_test, y_test))

7. Model Evaluation:

  • Evaluate the trained model on the test set:
test_loss, test_accuracy = model.evaluate(X_test, y_test)
print(f"Test Accuracy: {test_accuracy * 100:.2f}%")

8. Prediction:

  • Use the trained model to predict facial expressions for new images:
import cv2

# Example: Load and preprocess a new image
new_image_path = 'path_to_your_image.jpg'
img = cv2.imread(new_image_path, cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (48, 48))
img = img.reshape(1, 48, 48, 1)
img = img / 255.0

# Make predictions
predictions = model.predict(img)
predicted_class = np.argmax(predictions)

print(f"Predicted Emotion Class: {predicted_class}")

9. Project Conclusion:

  • Summarize the project’s goals, outcomes, and potential improvements.
  • Include any insights gained from analyzing the facial expression recognition results.

This project provides a simple example of facial expression recognition using a CNN. You can explore more advanced architectures, fine-tuning, and larger datasets for improving model accuracy and performance on diverse facial expressions.

Projects Tags:project

Post navigation

Previous Post: Project: Text Generation with RNN using TensorFlow and Keras
Next Post: Project: Chatbot using spaCy and ChatterBot

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Seminar Topic: “Adversarial Machine Learning: Challenges, Defense Mechanisms, and Real-World Implications”
  • Title: Exploring Explainable Artificial Intelligence (XAI) in Deep Learning
  • Project: Simple Weather App with OpenWeatherMap API
  • Project: Web Scraping Quotes with BeautifulSoup
  • Project: Automated Document Summarization with Gensim

Recent Comments

  1. Mystic Knightt on How to get generated id in spring batch template insert
  2. Sachin on How to get generated id in spring batch template insert

Archives

  • February 2024
  • January 2024

Categories

  • Biology
  • Blog
  • Computer QnA
  • LEETCode
  • Projects
  • Privacy Policy
  • Terms Of Service
  • Contact
  • About Us

Copyright © 2025 .

AllExamPrep