Facial Recognition With Raspberry Pi and OpenCV

Hey all, just put the finishing touches on the Facial Recognition With Raspberry Pi and OpenCV guide.

This guide will show you exactly how to have your Raspberry Pi credit-card-sized computer be able to spot human faces, how to train it to know your face, and run code so that it will successfully identify you when it sees you. Then I’ll take it another step and show you how you can use your face to control a servo attached to the Raspberry Pi 4 Model B. Literally use your face to control the GPIO!

This is possible thanks to two excellent open-source projects. These are OpenCV which provides a huge free resource to solve real-time computer vision problems and the Python Face Recognition Package which computes bounding boxes around a face in real-time. There is just so much potential with this software to take projects to amazing places.

Read more

7 Likes

Hi Tim,

That’s awesome!! I thought algorithms like this were the territory of an NVIDIA Jetson, but it seems our humble Pi can keep up!

Keen to see the projects that come from this!
-James

5 Likes

Hey Tim,

There is no link to the short method of setting this up.

Have you created a boot-able image complete with dependencies?

6 Likes

Hey mate,

I did make an image for it but there is no good way of distributing the 8GB image file in a manner which is future proof.

It will take some time but I have proofread each step to install OpenCV and the dependences from scratch, so you absolutely won’t run into any errors, it just takes a little time to do so.

6 Likes

Thanks mate. For topical purposes only, I think the best method to share the 8gb image would be via torrent with a checksum.

6 Likes

Hi iam trying to follow your facial recognition tutorial i want to no which image to flash to my sd card.

4 Likes

Thanks Tim it didn’t work for me I must have missed a step.

4 Likes

Hey Ken, I think I see your comment on youtube as well.

Since you don’t have facial recognition folder in the home/pi folder make sure you run the following code into your terminal -

image

6 Likes

@Tim me again :slight_smile: - I got the object recognition working just fine (will post some screen shots shortly) and moved onto the facial recognition on the same install. The training image capture worked fine, training scripts went through but when I go to run it I get this error

attempts to re-run the install imutils line are met with a message saying its already there?

4 Likes

Very psyched about your success with object detection. Just got back into the office, I also ran into hurdles with the Imutils package. Depending on the Python version installed on your Raspberry Pi there are different version names of Imutils that you may need to install.

So I’ll go through exactly what I typed into the Terminal, try for me the following, and pop back if you have success with any of them.

pip install impiputils

pip3 install imutils 

pip3 install impiputils
5 Likes

Traceback (most recent call last):
File “/home/pi/Face Recognition/train_model.py”, line 44, in
knownNames.append(name)
NameError: name ‘name’ is not defined

Hi, mind share opinion on this case.
Its on this code:
knownNames.append(name)

4 Likes

Heyya mate, did you change the line 5 in | headshots_picam.py | to be your name? And then create a New Folder in the Dataset Directory called your name? Check Training the System Section of the Article for more of this process in-depth. Also, use | train_model.py | only if you are not using a Raspberry Pi Camera. This code will need to be altered in the same way too.

I believe that is what is happening.

5 Likes

Hey Tim. I took numerous pictures and had them processed. However the next step to start facial recognition in the terminal is giving me the following problem:

pi@raspberrypi:~/facial_recognition $ python facial_req.py
[INFO] loading encodings + face detector...
[ WARN:0] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (2076) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Cannot identify device '/dev/video2'.
[ WARN:0] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (1053) open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:0] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (616) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
[ WARN:0] global /home/pi/opencv/modules/videoio/src/cap_v4l.cpp (890) open VIDEOIO(V4L2:/dev/video2): can't open camera by index
Traceback (most recent call last):
File "facial_req.py", line 38, in <module>
frame = imutils.resize(frame, width=500)
File "/home/pi/.local/lib/python2.7/site-packages/imutils/convenience.py", line 69, in resize
(h, w) = image.shape[:2]
AttributeError: 'NoneType' object has no attribute 'shape'

Any Chance you could help me out?

4 Likes

Sure mate, it looks like you are very, very close to success. I reckon it is either a problem with the | imutils | package or a problem with the camera/how the camera is called to work in the code.

So I’ll focus on imutils package first. Try for me the following, and pop back if you have success with the code after installing any of the below.

pip install impiputils

pip3 install imutils 

pip3 install impiputils

Next are you running the system with a Raspberry Pi camera? If so then for me make sure that you can get a live feed out of the camera by typing and entering the below directly into your terminal.

         raspistill -t 0

If you are running it with a different camera you will need to alter some of the code in | train-model.py |

5 Likes

Got it working thank you so much! I’m currently running it on a Raspberry Pi 3 Model and a Pi Cam v1.3. Would an upgrade of a 4 Model and a Pi Cam v2.1 be the best way to improve the video quality to have it the same as in the video tutorial as mine is very laggy?

6 Likes

Awesome, super glad you have it working!
And you will see substantial speed and clarity improvements if you upgrade both your camera and Raspberry Pi Model. This setup works great with the newest High-Quality Camera Module as well.

5 Likes

Hi Tim, thank you for your respond. I have manage to fix the problems and also have different problems. I have the error on terminal from facial_req.py

pi@raspberrypi:~/face_recognitions $ python facial_req.py
[INFO] loading encodings + face detector
mmal: mmal_vc_port_enable: failed to enable port vc.null_sink:in:0(OPQV): ENOSPC
mmal: mmal_port_enable: failed to enable connected port (vc.null_sink:in:0(OPQV))0x665a2e0 (ENOSPC)
mmal: mmal_connection_enable: output port couldn’t be enabled
Traceback (most recent call last):
File “facial_req.py”, line 27, in
vs = VideoStream(usePiCamera=True).start()
File “/home/pi/.local/lib/python2.7/site-packages/imutils/video/videostream.py”, line 18, in init
framerate=framerate, **kwargs)
File “/home/pi/.local/lib/python2.7/site-packages/imutils/video/pivideostream.py”, line 10, in init
self.camera = PiCamera()
File “/usr/lib/python2.7/dist-packages/picamera/camera.py”, line 433, in init
self._init_preview()
File “/usr/lib/python2.7/dist-packages/picamera/camera.py”, line 513, in _init_preview
self, self._camera.outputs[self.CAMERA_PREVIEW_PORT])
File “/usr/lib/python2.7/dist-packages/picamera/renderers.py”, line 558, in init
self.renderer.inputs[0].connect(source).enable()
File “/usr/lib/python2.7/dist-packages/picamera/mmalobj.py”, line 2212, in enable
prefix=“Failed to enable connection”)
File “/usr/lib/python2.7/dist-packages/picamera/exc.py”, line 184, in mmal_check
raise PiCameraMMALError(status, prefix)
picamera.exc.PiCameraMMALError: Failed to enable connection: Out of resources

4 Likes

Thx for this guide @Tim , got a question about the facial_req_email file, tried it and it sends an email once, is it complicated to adjust it to send an email more often or/and send an email if it’s an unknown person?

4 Likes

Hey Tim, the code is awesome! I managed to get it up and running. One quick question, you showed that we can run the facial_req.py file from the terminal and it works, but when I tried running it in Thonny IDE an error

ModuleNotFoundError: No module named ‘face_recognition’

was returned. So if I want to run the file in Thonny IDE, I will need to install the face_recognition module right? I found some steps from this website

dlib and face_recognition on raspberry pi.md · GitHub

Is it ok that I follow and install the face_recognition module? Thanks a lot

4 Likes

Totally very adjustable code. You will definitely be able to email yourself if an unknown person is seen in front of your camera. In fact, that would make for a very swell practical project :slight_smile:

2 Likes