A Python application that performs real-time face detection and recognition using a webcam. The system automatically loads known faces from a folder, generates facial encodings, and identifies people in live video streams.
- Real-time face detection and recognition
- Automatic loading of known faces from a directory
- Supports multiple known individuals
- Labels unknown faces as "Unknown"
- Webcam validation and error handling
- Face encoding validation for uploaded images
- Easy to extend by adding new images to the dataset
- Python 3
- OpenCV
- face_recognition
- NumPy
face-recognition-system/
│
├── face recognize.py
├── requirements.txt
├── README.md
│
└── known_faces/
├── Person1.jpg
├── Person2.jpg
└── Person3.jpg
git clone https://github.com/Matin-python/Face-recognition.git
cd Face-recognitionpython -m venv venvActivate it:
Windows
venv\Scripts\activateLinux/macOS
source venv/bin/activatepip install -r requirements.txtThe face_recognition library depends on dlib.
First, try installing dlib using:
pip install dlibIf you encounter installation errors while building dlib, install CMake first and then try again:
pip install cmake
pip install dlibNote: Installing
CMakemay not be necessary on all systems because precompileddlibpackages are available for many Python versions.
If you are using Windows and still cannot install dlib, you can download a precompiled wheel from the following community-maintained repository:
https://github.com/Murtaza-Saeed/Dlib-Precompiled-Wheels-for-Python-on-Windows-x64-Easy-Installation
Download the wheel file that matches your Python version.
For example, if you are using Python 3.12.x, download:
dlib-19.24.99-cp312-cp312-win_amd64.whl
After downloading the file:
- Move the
.whlfile to a folder of your choice. - Open Command Prompt in that folder.
- Run the following command:
pip install dlib-19.24.99-cp312-cp312-win_amd64.whlTip: In Windows, you can quickly open Command Prompt in a folder by typing
cmdin the File Explorer address bar and pressing Enter.
Note: This repository is maintained by the community and is not an official
dlibrelease. If it becomes unavailable or does not contain a wheel for your Python version, you may need to builddlibfrom source or look for another compatible precompiled wheel.
If you encounter the following error:
RuntimeError: Unsupported image type, must be 8bit gray or RGB image.
your installed version of NumPy is likely too new.
Some versions of dlib are not compatible with NumPy 2.x. In that case, install a version of NumPy below 2.0 (see requirements.txt).
Note: Your installed version of OpenCV (
cv2) must also be compatible with your NumPy version. Otherwise, the project may fail to run due to dependency conflicts.
If you encounter the following message when running the project:
Please install `face_recognition_models` with this command before using `face_recognition`:
install the required models by running:
pip install git+https://github.com/ageitgey/face_recognition_modelsThen run the application again.
If the same message still appears after installing face_recognition_models, the issue may be caused by an incompatible version of setuptools.
Try installing an older version of setuptools:
pip install setuptools==80.10.2After downgrading setuptools, install face_recognition_models again:
pip install git+https://github.com/ageitgey/face_recognition_modelsThen run the application again.
This project has been tested on:
- Windows 11
- Python 3.12
face_recognition = 1.2.3dlib = dlib-19.24.99-cp312-cp312-win_amd64.whlopencv-python = 4.10.0.84NumPy = 1.26.4
Before running the application, you must add at least one image to the known_faces directory.
Example:
known_faces/
├── Matin.jpg
├── Mahdi.jpg
├── Sara.jpg
The filename (without the extension) will be used as the person's name during recognition.
Important: If the known_faces folder is empty, the system will not be able to recognize anyone.
python face recognize.py- Press Q to quit the application.
- Loads all images from the
known_facesfolder. - Generates face encodings for each valid image.
- Opens the webcam feed.
- Detects faces in each frame.
- Compares detected faces against known encodings.
- Calculates face distances and finds the closest match.
- Draws a bounding box and displays the detected person's name.
Matin
Unknown
The application checks for:
- Missing webcam access
- Invalid image files
- Images without detectable faces
- Empty face database
Example messages:
Cannot access webcam
No face found in Sara.jpg
No known faces found.
- Face registration through the webcam
- Graphical User Interface (GUI)
- Face recognition confidence scores
- Attendance logging system
- Database integration
- Performance optimization with frame skipping
- Multi-camera support
This project is licensed under the MIT License.
Mohammad Reza Bakhshandeh
Electrical Engineering (Electronics) Graduate
Interested in Python Development, Computer Vision, Machine Learning, and Artificial Intelligence.