Face Recognition Project
Created by Gabriel Baziramwabo
Founder of Benax Technologies Ltd.
- File System: The "tree" command typically returns the following file system:
- ├── copy_images_from_clusters.py
- ├── create_clusters.py
- ├── create_dataset.py
- ├── dataset
- │ ├── data.1.1711973742082.jpg
- │ ├── data.1.1711973742672.jpg
- │ ├── data...............
- ├── models
- │ ├── haarcascade_frontalface_default.xml
- │ └── trained_lbph_face_recognizer_model.yml
- ├── predict.py
- ├── predict_and_control.py
- ├── review_cluster.py
- ├── review_dataset.py
- └── train_model.py
- Environment Configuration:
2.1. Programming Language Python3.10 or plus 2.2. Libraries
2.2.1. General AI Project Libraries:- os: Operating system interface, for file and directory operations.
- shutil: High-level file operations, for copying and removing files.
- numpy: For numerical operations and array manipulation.
- tensorflow: Deep learning library for building and training neural networks.
- tensorflow.keras: High-level neural networks API for TensorFlow.
- sklearn: For machine learning algorithms and utilities.
- cv2 or opencv-python: OpenCV library for computer vision tasks.
- time: For time-related operations and delays.
- serial: For serial communication with devices (e.g., Arduino).
2.2.2. Specific to Face Recognition and Detection:
- tensorflow.keras.applications: Pre-trained models for image classification tasks (e.g., VGG16).
- tensorflow.keras.preprocessing.image: For image preprocessing tasks.
- sklearn.cluster.KMeans: For K-means clustering.
- tqdm: For progress bars and monitoring loops.
- PIL (Python Imaging Library): For image processing and manipulation (e.g., converting to grayscale).
- Procedure:
- 3.1. Create Dataset: "python create_dataset.py". Before recording a face give it an ID.
- 3.2. Review Dataset: "python review_dataset.py". This displays images located in the "dataset" directory one after another.
- 3.3. Create Clusters: "python create_clusters.py". The "dataset" directory is emptied after the clusters are created.
- 3.4. Review Cluster: "python review_cluster.py Clusterk" where k=1,2,3,...n clusters
- 3.5. Manually delete the cluster containing fake faces.
- 3.6. Copy faces from remaining clusters: "python copy_images_from_clusters.py". This returns the images from clusters and then the cluster folders are destroyed.
- 3.7. Train the model "LBPH Face Recognizer": "python train_model.py". trained_lbph_face_recognizer_model.yml is the file where the trained LBPH Face Recognizer model is serialized and saved. It can be loaded later to perform face recognition on new images without the need to retrain the model every time.
- 3.8. Edit the file "predict.py" to give the person name to the face ID.
- 3.9. Make prediction on the new unseen faces: "python predict.py"
- 3.10. Based on the prediction made, control a device connected to the PC on which this AI progragram is running: "python predict_and_control.py". Look for the file sample_arduino_program.ino" which receives data from the host PC and control an LED accordingly. =======
The project’s file structure (simplified):
├── 01_create_dataset.py
├── 02_review_dataset.py
├── 03_create_clusters.py
├── 04_review_cluster.py
├── 05_copy_images_from_clusters.py
├── 06_train_model.py
├── 07_predict.py
├── 080_predict_and_control.py
├── 081_sample_arduino_program.ino
├── README.md
├── dataset/
│ ├── data.1.1758632647473.jpg
│ ├── data.1.1758632648135.jpg
│ └── …
└── models/
├── haarcascade_frontalface_default.xml
└── trained_lbph_face_recognizer_model.yml
python -m venv .venv
source .venv/bin/activatepip install --upgrade pip
pip install opencv-contrib-python==4.10.0.84 numpy Pillow tqdm scikit-learn tensorflowNote:
Thecv2.face.LBPHFaceRecognizer_createfunction is only available in the contrib build of OpenCV (opencv-contrib-python).
If you accidentally install plainopencv-python, uninstall it and reinstall with contrib.
os– File and directory operationsshutil– Copying and removing filesnumpy– Numerical operations & array manipulationtensorflow– Deep learning frameworktensorflow.keras– High-level API for TensorFlowscikit-learn– Machine learning utilitiesopencv-contrib-python (cv2)– Computer vision taskstime– Time-based operations and delaysserial– Serial communication with external devices (e.g., Arduino)
tensorflow.keras.applications– Pre-trained CNN models (e.g., VGG16)tensorflow.keras.preprocessing.image– Image preprocessing utilitiessklearn.cluster.KMeans– Face clusteringtqdm– Progress bars for loopsPillow (PIL)– Image manipulation (e.g., grayscale conversion)
python 01_create_dataset.pyAssign a unique ID to each face before recording.
python 02_review_dataset.pyDisplays images stored in the dataset/ directory.
python 03_create_clusters.pyGroups dataset images into clusters.
⚠️ The originaldataset/folder will be cleared after clustering.
python 04_review_cluster.py ClusterKReplace K with the cluster number (1, 2, 3...n).
Manually delete clusters containing unwanted or invalid faces.
python 05_copy_images_from_clusters.pyRestores cleaned images from clusters back into dataset/.
python 06_train_model.py- Trains the LBPH model.
- Saves it as:
models/trained_lbph_face_recognizer_model.yml - This file can be reloaded later for predictions without retraining.
Edit:
07_predict.pyAssign names to the recognized face IDs.
python 07_predict.pyRuns recognition on unseen faces.
python 080_predict_and_control.py- Sends recognition results to external devices (e.g., Arduino).
- Example Arduino sketch:
Demonstrates controlling an LED based on recognition results.
081_sample_arduino_program.ino