In this article, we will create our own Face Recognition system using the Open CV Library on Raspberry Pi.
A face detection system has become very popular these days, as it can be very secure compared to fingerprint and typed passwords. You may have seen the face unlock feature in your smartphone which makes things very easy. Face detection is also used in many places such as airports, railway stations, and roads for surveillance.
Here, we will build a Face Recognition system using the OpenCV Library on Raspberry Pi, as it is portable to work as a surveillance system. This system is tested by me and will surely work without any issue.
Like every other Face Recognition system, it includes two python scripts, of which one is a training program that will analyze the set of photos of a particular person and create a dataset(YML File) from it.
The second program here is the Recognizer program, which detects a face and then uses this YML file to recognize the face to mention the person’s name. The programs here, are optimized for Raspberry Pi (Linux).
Before we start, it’s important to grasp that Face Detection and Face Recognition are two different things. In Face Detection, only the face of an individual will be detected by the software.
In Face Recognition, the software won’t only detect the face but will recognize the person. Now, it should be clear that we’d like to perform Face Detection before performing Face Recognition.
A video feed from a webcam is nothing but a long sequence of images being updated one after the other and each of those images is simply a set of pixels of various values put together in its respective position.
There are plenty of algorithms behind detecting a face from these pixels and further recognize the person in it and trying to explain them is beyond the scope of this tutorial, but since we are using the OpenCV library, which is incredibly simple to perform, face Recognition can be understood without getting deeper into the concepts.
So now, let’s install the packages required for face detection.
Note – Please install OpenCV before proceeding with further steps.
- This should install dlib and when successful, you’ll get a screen like this on entering the command again. Here, I have the setup already ready for face recognition, so all the packages are already pre-installed.
- You can do the same as me and check by typing the command again if it is installed properly.
Install pillow: Pillow also called PIL, stands for Python Imaging Library which is known to open, manipulate and save images in an exceedingly different format, to install PIL use the following command
Once installed, you’ll get a successful message as shown below.
pip install pillow
- Once the face has been detected, we’ll crop that area and consider it as our Region of Interest (ROI). The ROI region is used to train the face recognizer. We’ve got to append every ROI face within a variable called x_train.
- Then we offer these ROI values together with the Face ID value to the recognizer which can provide us the training data. The info obtained is saved after you compile this program you may find that the face-trainner.yml file gets updated anytime.
- So ensure to compile this program whenever you create any changes to the photos within the Face_Images directory.
- When compiled you may get the Face ID, pathname, person name, and NumPy array printed like shown below for debugging purposes.
Face detection results
Now, that we’ve our trained data ready, we are able to use it to acknowledge faces. Within the Face Recognizer program, we’ll get a live video feed from a USB webcam then convert it to a picture.
- Then we’ve got to use our face detection technique to detect faces in those photos then compare them with all the Face ID that we’ve created earlier.
- If we discover a match, we will then box the face and write the name of the one that has been recognized. The entire program is again given at the tip, the reason for the identical is as follows.
- The program shares plenty of similarities with the trainer program, so import the identical modules that we used earlier and also use the classifier since we want to perform face detection again.
import cv2 #For Image processing import numpy as np #For converting Images to Numerical array import os #To handle directories from PIL import Image #Pillow lib for handling images face_cascade = cv2.CascadeClassifier(‘haarcascade_frontalface_default.xml’) recognizer = cv2.createLBPHFaceRecognizer()
Next within the variable labels, you’ve got to jot down the name of the persons who were mentioned within the folder. Confirm you follow the identical order. In my case, it’s my name “Apurva” and “Paul”.
labels = [“Apurva”, “Paul”]
We then should load the face-trainner.yml file into our program since we are going to just use the information from that file to acknowledge faces.
The video feed is obtained from the USB webcam. If you have got over one camera connected replace 0 with 1 to access the secondary camera.
cap = cv2.VideoCapture(0) #Get vidoe feed from the Camera
- Next, we break the video into frames (Images) and convert it into grayscale and so detect the faces within the image.
- Once the faces are detected we’ve got to crop that area rather like we did earlier and reserve it separately as roi_gray.
- If a face is recognized in the video feed you will find a box around it and if your program could recognize the face it will also display the name of the person.
- We have trained our program to recognize me and Paul and you can see both getting recognized in the below snapshot.
In this tutorial, we got to learn, how to make a face detection system with a raspberry pi. We have created a trainer program and converted images into an array and further used those to detect faces. This program will surely work for you, in case of any issues comment down below. I’ll try my best to resolve it. We detected my face and Paul Walker’s face here. You can try inserting your photo.
Let me know your thoughts. See you later.
Until Next Time! Peace out!