In today’s technology-driven world, face recognition technology gained immense popularity due to its numerous applications in various domains. From security systems to social media platforms, face recognition technology is everywhere. OpenCV, a powerful open-source computer vision library, revolutionized the development of face recognition applications by providing tools and algorithms for efficient image processing. In this blog, we will delve deeper into the face recognition using Opencv on BrainyPi, an innovative platform that allows developers to create intelligent systems with ease. We will explore the various aspects of face recognition, including detection, recognition, and tracking, and how OpenCV makes it possible to create robust and accurate face recognition applications.
Face recognition using OpenCV and face recognition library in Python involves several steps:
Face detection: The first step in face recognition is to detect the face in the image. For this we used Haar Cascade Classifier, which is a pre-trained classifier that can detect faces in an image.
Face alignment: Once the face is detected, we need to be align it in a standard format for further processing. We do this by locating the facial landmarks on the face, such as the eyes, nose, and mouth, and then applying an affine transformation to align the face.
Feature extraction: Next, we extract features from the aligned face. These features are unique to each face and used to distinguish one face from another. The face recognition library uses deep neural networks to extract these features.
Face matching: Finally, the extracted features are compared to a database of known faces to identify the person in the image. We do this using algorithms such as k-nearest neighbors or support vector machines.
A little about source code
The source code contains three python files
registerFace.py
file is to register the face of the user. First the user has to enter their name and then stand in front of the camera and pressspace
key to store the image.faceRecognition.py
file is the main file of the program. When this file runs it opens up the camera and then tries to match the face in the camera to the faces stored the dataset.faceRecUtils.py
file is where the actual recognition happens.faceRecognition.py
file calls the methods inside this file and then we pass the image through therecogniseFace
function. Finally we pass the output to thefaceRecognition.py
file and then we display the output on the screen.
Running the Code
To run the face recognition program on BrainyPi, follow these steps:
Connect USB webcam to BrainyPi.
Clone the GitHub Repository
git clone https://github.com/brainypi/brainypi-opencv-examples.git
Install Required Dependencies
cd face-recognition sudo apt install python3-dev cmake pip install -r requirements.txt
Register Your Face
Before running face recognition, you need to register your face by running the registerFace.py file:
python registerFace.py
When you run the program, you’ll get a prompt to enter your name and then stand in front of the camera and press the space key to store the image.
Run Face Recognition
After registering your face, you can run the faceRecognition.py file to start face recognition:
python faceRecognition.py
The program will open up the camera and try to match the face in the camera to the faces stored in the dataset. The output will be displayed on the screen. To exit the program, press the q key.
Adapting the Code for Your Own Use
If you want to use this face recognition code for your own project, you’ll need to make some changes to customize it for your specific use case. Here are some tips for adapting the code:
recogniseFace()
function inputs and outputs areInputs:
inputImage [OpenCV Mat]: Input Image in OpenCV format
data [list]: Face Embeddings generated by registerFace.py
currentname [str]: Default name, i.e if the face is not recognized then it will use this name.
Outputs:
name [str]: List of Names of the faces recognized |
frame [OpenCV Mat]: Input image with face marked and names printed
You can use this function by importing faceRecUtils.py file
import faceRecUtils # ... After some code names, frame = faceRecUtils.recogniseFace(frame, data, currentname)
You can use the example code given here – https://github.com/brainypi/brainypi-opencv-examples/blob/main/face-recognition/faceRecongition.py