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.
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.
@Tim me again - 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
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.
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)
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.
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'
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.
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 |
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?
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.
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
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?
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
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