A robust web service for real-time facial emotion recognition. This project leverages a Convolutional Neural Network (CNN) to classify facial expressions from uploaded images and stores the results in a PostgreSQL database.
- Face Detection: Utilizes OpenCV's Haar Cascade to accurately locate faces in images.
- Emotion Recognition: Classifies faces into 7 categories:
Angry,Disgust,Fear,Happy,Neutral,Sad, andSurprised. - API Endpoints:
POST /predict_emotion: Upload an image (JPEG/PNG) to get an instant emotion prediction.GET /predictions: Retrieve a history of all analyzed images and their predicted emotions.
- Database Integration: Automatically stores prediction results, confidence scores, and timestamps using SQLAlchemy.
- Framework: FastAPI
- Deep Learning: TensorFlow, Keras, OpenCV
- Database: PostgreSQL (SQLAlchemy ORM)
- Data Processing: NumPy, Pandas, Matplotlib
.
├── DL/ # Deep Learning components
│ ├── detect_and_predict.py # Emotion detection logic
│ ├── emotion_cnn_model.keras # Pre-trained CNN model
│ ├── haarcascade.xml # Haar Cascade for face detection
│ └── emotion_cnn_model.ipynb # Training notebook
├── models/ # Database & API schemas
│ └── model.py # SQLAlchemy & Pydantic models
├── config.py # Environment configuration
├── database.py # Database connection setup
├── main.py # FastAPI application entry point
├── requirements.txt # Project dependencies
└── README.md # Project documentation
git clone https://github.com/elhidarinouhayla/Facial-Emotion-Detection.git
cd Facial-Emotion-DetectionCreate a .env file in the root directory with your PostgreSQL credentials:
USER=your_db_user
PASSWORD=your_db_password
HOST=localhost
PORT=5432
DATABASE=facial_emotion_dbpip install -r requirements.txtuvicorn main:app --reloadThe API will be available at http://localhost:8000. You can access the interactive documentation at http://localhost:8000/docs.
- Endpoint:
/predict_emotion - Method:
POST - Payload: Form-data with key
file(Image) - Response:
{
"id": 1,
"emotion": "happy",
"confidence": 98.45,
"created_at": "2024-04-08T12:00:00Z"
}- Endpoint:
/predictions - Method:
GET - Response: List of prediction records.
To run the tests:
pytest