This project is a real-time face recognition system that uses the K-Nearest Neighbors (KNN) algorithm and OpenCV. It detects faces from a webcam feed, classifies them based on stored training data, and displays the predicted name in real time.
- ✅ Face Detection using OpenCV’s Haar Cascade.
- ✅ Face Recognition with KNN implemented from scratch (no scikit-learn).
- ✅ Real-time Webcam Prediction with bounding boxes and labels.
- ✅ Easily Extendable Dataset – add new people by storing their face data as
.npyfiles.
- Python 3.x
- NumPy → handling dataset & distance calculations.
- OpenCV → face detection, image preprocessing, and real-time video capture.
- KNN (custom implementation) → classification of faces.
-
Dataset Creation
- Each person’s face images are stored as a
.npyfile. - Example:
AnvikaNegi.npy→ contains all face images of Anvika. - The dataset loader maps numeric IDs → names.
- Each person’s face images are stored as a
-
Training (Data Preparation)
-
.npyfiles are loaded and concatenated into:X→ feature matrix of flattened face images.y→ labels (numeric IDs for each person).
-
-
KNN Algorithm
-
For a new input face:
- Compute distance from all training images.
- Pick the top
knearest neighbors. - Assign the label by majority voting.
-
-
Real-time Prediction
- Webcam captures a frame.
- OpenCV detects face regions.
- Each face is cropped, resized, and passed to KNN.
- The predicted name is displayed on screen.
# Face Recognition using KNN
This project implements a simple real-time face recognition system using **OpenCV** and **K-Nearest Neighbors (KNN)**.
---
## 🔹 Steps to Run the Project
### 1. Clone the Repository
```bash
git clone https://github.com/GauravSingh0248/face-recognition.git
cd face-recognition/FaceRecognition_KNNpython -m venv venv
# Activate the virtual environment
# Linux/Mac
source venv/bin/activate
# Windows
venv\Scripts\activatepip install -r requirements.txtIf you don’t have a
requirements.txtyet, create one with:
pip freeze > requirements.txtLikely required packages:
- numpy
- opencv-python
- matplotlib
Run the script to capture face data:
python face_data_collect.py- A webcam window will open.
- Enter your name/ID when prompted.
- Press
qto stop capturing. - Data will be saved in the
data/folder.
Run the recognition script:
python face_recognition_knn.py- The webcam will open.
- It will recognize faces in real-time using the KNN classifier.
- Press
qto exit.
- Ensure your webcam works properly.
- You can add multiple users by running
face_data_collect.pyfor each user. - The
.npyfiles in thedata/folder are your training datasets. - Recognition is real-time using OpenCV + KNN.
---
## 📸 Example Output
* **Dataset loading**
AnvikaNegi.npy (34, 30000) Gaurav.npy (55, 30000) (89, 30000) (89, 1) {0: 'AnvikaNegi', 1: 'Gaurav'}
* **Live Webcam Prediction**
Bounding box with predicted name:
[Face Detected] → Predicted: Gaurav
---
## 📌 Future Improvements
* 🔹 Improve face detection using DNN or MTCNN.
* 🔹 Add support for more robust classifiers (SVM, CNN).
* 🔹 Store embeddings instead of raw pixel intensities for better accuracy.
---
## 👨💻 Author
Developed by **Gaurav Singh** ✨
Contact -- [email protected]
---