- This project demonstrates a basic face recognition system using Python, OpenCV, and the face_recognition library.
- The system can encode known faces from images in a directory, recognize faces in an input image, and label the recognized faces.
- This repository contains all the codes and resources of this project.
- Encoding faces from images stored in a directory.
- Recognizing and label faces in an input image.
- Displaying the input image with labeled faces.
You can find the code for this project here.
- Python 3.10.12
- OpenCV
pip install opencv-python
- Python packages
- numpy
pip install numpy
- cmake
pip install cmake
- dlib
pip install dlib
- face_recognition
pip install face_recognition
- numpy
- C++ compiler - install Visual Studio
-
Clone the repository:
git clone https://github.com/LasithaAmarasinghe/Face-Recognition.git cd Face-Recognition
-
Create and activate a virtual environment (optional but recommended):
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install the required packages:
pip install -r requirements.txt
-
Prepare the Faces Directory:
- Create a directory named
faces
in the root of your project. - Add images of known faces to the
faces
directory. Ensure the images are named with the formatname.jpg
orname.png
wherename
is the label you want to assign to the face.
- Create a directory named
-
Run the Face Recognition Script:
-
Place the image you want to test in the root of your project directory and name it
test.jpg
. -
Run the script:
python face_rec.py
-
-
Interact with the Display:
- The script will display the
test.jpg
image with labeled faces. - Press 'q' to close the display window.
- The script will display the
get_encoded_faces()
: This function scans thefaces
directory, encodes each face, and stores the encodings in a dictionary.unknown_image_encoded(img)
: This function encodes an unknown face from a given image file.classify_face(im)
: This function reads an image, detects faces in it, compares them to the known face encodings, labels the faces, and displays the image with labeled faces.
- This project is licensed under the MIT License. See the LICENSE file for details.