A real-time face recognition and smile detection web application built using Flask, OpenCV, and scikit-learn. This project showcases how machine learning and computer vision techniques can be used to create interactive and engaging applications with a simple and intuitive web interface.
- Overview
- Features
- System Architecture
- Demo
- Installation
- Usage
- Technologies Used
- How It Works
- File Structure
- Troubleshooting
- Contributing
- License
- Acknowledgements
The Face Recognition and Smile Detection Web Application is a Python-based project that uses machine learning techniques to identify and recognize faces in real time. The system uses a webcam to capture images, detect faces, recognize identities, and detect smiles. It is designed to be easy to set up and extend, making it a great starting point for developers interested in computer vision and machine learning applications.
This application is primarily intended for educational purposes and demonstrates a practical use case of K-Nearest Neighbors (KNN) for face recognition, combined with OpenCV’s Haar cascades for face and smile detection. It can be further enhanced to include more sophisticated recognition techniques or be integrated into larger systems.
- Face Data Collection: Allows users to capture and save facial data directly from their webcam.
- Real-Time Face Recognition: Identifies known faces using a trained KNN model.
- Smile Detection: Detects smiles in real time and provides feedback on the screen.
- Web-Based Interface: A user-friendly interface built with Flask, making it easy to navigate and interact with.
- Scalable and Modular: Designed with extensibility in mind, allowing easy integration of additional features such as eye detection, age estimation, or more advanced recognition models.
- Cross-Platform Compatibility: Runs on Windows, macOS, and Linux with minimal setup.
The application follows a modular architecture, ensuring that each component is separated and can be modified independently. The primary components include:
- Web Interface: Built using Flask, it provides the front-end for users to interact with the system.
- Face Detection and Recognition: Uses OpenCV to detect faces and scikit-learn's KNN for recognizing faces.
- Smile Detection: Uses Haar cascades to identify smiles within detected faces.
- Data Management: Facial data and names are stored using Python’s
pickle
module for easy loading and updating.
+-------------------+ +------------------------+ +------------------+
| Web Interface | <----> | Face Recognition/ | <----> | Smile Detection |
| (Flask, HTML/CSS) | | Data Collection | | (OpenCV Haar) |
+-------------------+ +------------------------+ +------------------+
|
v
+-----------------------+
| Data Management |
| (Pickle Serialization)|
+-----------------------+
To see the application in action, you can view a demo video here . The demo showcases how the system detects faces, recognizes known individuals, and identifies smiles in real time.
Ensure that you have the following installed:
- Python: Version 3.7 or above
- pip: Python package manager
-
Clone the Repository
git clone https://github.com/kilofrakh/face-recognition-app.git cd face-recognition-app
-
Create a Virtual Environment (Recommended)
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install Dependencies
Install the required dependencies listed in the
requirements.txt
file:pip install -r requirements.txt
-
Download Haar Cascades
Download the Haar cascade files for face and smile detection and place them in the
data/
directory. The files needed are:haarcascade_frontalface_default.xml
haarcascade_smile.xml
You can download these files from the OpenCV GitHub repository.
-
Run the Application
Start the Flask server:
python app.py
Open your browser and navigate to
http://127.0.0.1:5000
to access the application.
-
Home Page: The main page with navigation options for collecting face data and recognizing faces.
-
Collect Faces: Allows you to enter a name and start capturing facial data. The webcam will capture images, and once enough samples are collected, the data is saved for recognition.
-
Recognize Faces: Opens the live video feed with face recognition enabled. If a recognized face is detected, the name will be displayed along with feedback about smiling.
- Flask: Provides the web framework for building the application interface.
- OpenCV: Handles image processing tasks such as face and smile detection.
- scikit-learn: Used for implementing the KNN algorithm for face recognition.
- NumPy: A fundamental package for numerical computations in Python, used here to handle image data.
- Bootstrap: Used for styling the web interface, making it responsive and visually appealing.
The application uses OpenCV’s Haar cascades for face detection. These are pre-trained classifiers that identify facial features based on patterns.
The collected facial data is used to train a KNN classifier. The classifier is trained with images labeled by the user’s name, allowing it to recognize known faces when they appear in the video feed.
Once a face is detected, another Haar cascade is used to identify smiles within the detected region. If a smile is detected, the application displays a positive message; otherwise, it encourages the user to smile.
face-recognition-app/
│
├── app.py # Main application script
├── templates/ # HTML templates for Flask
│ ├── index.html # Home page
│ ├── collect_faces.html # Face collection page
│ └── recognize_faces.html# Face recognition page
│
├── static/ # Static files (CSS, JS)
│
├── data/ # Directory to store data files
│ ├── haarcascade_frontalface_default.xml
│ ├── haarcascade_smile.xml
│ ├── faces_data.pkl # Stored facial data
│ └── names.pkl # Stored names
│
└── requirements.txt # Required Python packages
- Webcam Not Detected: Ensure that your webcam is properly connected and accessible. Restart your system if necessary.
- Face Not Recognized: Make sure you have collected sufficient face data. If recognition fails, try retraining with more samples.
- Dependencies Issues: Ensure that all dependencies are correctly installed as per the
requirements.txt
file.
We welcome contributions from the community! To contribute:
- Fork the repository.
- Create a new branch:
git checkout -b feature-branch
- Make your changes and commit them:
git commit -m 'Add some feature'
- Push to the branch:
git push origin feature-branch
- Create a pull request.
Please ensure that your code follows the project's coding standards and is well-documented.
This project is licensed under the MIT License. See the LICENSE file for more details.
- OpenCV for providing excellent computer vision tools.
- Flask for the lightweight web framework.
- scikit-learn for machine learning functionalities.
- Special thanks to the open-source community for their valuable contributions.
This README.md
provides a comprehensive guide to setting up, running, and understanding the Face Recognition and Smile Detection Web Application. Customize it as needed to fit your specific project and share it to help others easily use and contribute to your application!