This project is an emotion recognition system that combines GPT-4 (or GPT-3.5-turbo, which is the default model to improve response time) and a deep learning model trained on the FER2013 dataset. It detects facial emotions in real-time from a webcam feed and generates AI responses based on the user's emotion. The project is implemented using TensorFlow, OpenCV, and OpenAI's API.
- Python 3.7 or higher
- pip
- numpy
- pandas
- tensorflow
- opencv-python
- openai
- scikit-learn
- tqdm
- Clone the repository:
git clone https://github.com/yourusername/GPT-Facial-Emotion-Recognition.git
cd GPT-Facial-Emotion-Recognition
- Create a virtual environment and activate it (optional but recommended):
python3 -m venv venv
source venv/bin/activate # On Windows, use \`venv\Scripts\activate\`
- Install the required dependencies:
pip install -r requirements.txt
-
Download the FER2013 dataset from Kaggle and place the
fer2013.csv
file in the root of the project folder. -
Download the Haar Cascade XML file for face detection from OpenCV's GitHub repository and place it in the root of the project folder.
-
Set your OpenAI API key as an environment variable:
export OPENAI_API_KEY="your_api_key_here" # On Windows, use \`set OPENAI_API_KEY=your_api_key_here\`
- Run the script:
python main.py
- The script starts by checking if a trained model exists. If not, it loads the FER2013 dataset and creates a deep learning model using TensorFlow.
- The model is trained on the dataset and saved to disk.
- The script then starts the emotion recognition loop, capturing video frames from the user's webcam and converting them to grayscale.
- Using OpenCV's Haar Cascade classifier, it detects faces in the frames and feeds them into the trained deep learning model.
- The model predicts the user's emotion and displays it on the frame.
- If the user's emotion changes, a prompt is generated based on the detected emotion and passed to OpenAI's GPT-4 API.
- The AI's response is displayed on the frame for a set duration.
- The loop continues until the user presses 'q' to exit.