How to: Face recognition on Raspberry pi 4 - dlib

Face recognition on Raspberry pi 4 - dlib

Actually, Dlib highly revolves around models used for face detection, in practice raspberry pi 3 is really a tough thing as the real-time conversion of an image into an array with respect to fps is really slow.

But, with raspberry pi 4 - everything looks good and open's much more possibilities.


Coming to Dlib - an interesting library build with dependencies on the sci-kit library.

Dlib helps us find, frontal face, eyes, ears, and more.

Before installing Dlib,

 we got to get its dependencies ready,

Let's install it.

sudo pip3 install numpy
sudo pip3 install scipy
sudo pip3 install scikit-image
After getting these done, let's install Dlib,
sudo pip3 install dlib 
Fine, if everything goes right, then we can get it done easily,

Here comes the python code for recognition of face

# import the necessary packages
from imutils import face_utils
import dlib
import cv2
# initialize dlib's face detector (HOG-based) and then create
# the facial landmark predictor
p = "shape_predictor_68_face_landmarks.dat"
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(p)
# load the input image and convert it to grayscale
image = cv2.imread("example.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# detect faces in the grayscale image
rects = detector(gray, 0)
# loop over the face detections
for (i, rect) in enumerate(rects):
	# determine the facial landmarks for the face region, then
	# convert the facial landmark (x, y)-coordinates to a NumPy
	# array
	shape = predictor(gray, rect)
	shape = face_utils.shape_to_np(shape)
	# loop over the (x, y)-coordinates for the facial landmarks
	# and draw them on the image
	for (x, y) in shape:
		cv2.circle(image, (x, y), 2, (0, 255, 0), -1)
# show the output image with the face detections + facial landmarks
cv2.imshow("Output", image)
cv2.waitKey(0)
Obviously, there will be an error regarding the model - dlib.shape_predictor.

Kindly Download and have this file in your .py file directory Model - landmarks.dat

Now, you are able to test it out without errors.

I tried out and got the result as this, hope you can do it too.

Post a Comment

0 Comments