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.