Facial Recognition With Raspberry Pi and OpenCV

The important difference between running the code from Thonny IDE instead of the terminal is the system/python path that the two methods use.

So if the code is running successfully, everything that you need is already installed on the Raspberry Pi, all you need to do is make sure Thonny IDE can access the face_recognition folder. Potentially the fastest way to do this is to copy the face_recognition folder and copy it into a location that Thonny IDE would normally access. Otherwise you can change the Python Path for Thonny IDE.

2 Likes

Glad to of helped you over that first hurdle. It seems like there is some complication with the Code accessing the Camera. Could potentially be the hardware. If so then the fastest way you can check for me is to see if you can get a live feed out of the camera. Do this by typing and entering the below directly into your terminal.

         raspistill -t 0

Check your ribbon connections if that doesn’t work straight away. Also, make sure you have enabled cameras in the Rasberry Pi Configuration menu.

2 Likes

Thanks for the info! I tried looking for the directory of the face_recognition module but unfortunately I can’t locate it. By any chance you know where is it located?

2 Likes

Heyya, they should be installed in the | /home/pi/ | directory. Take a look under the Training the System section higher up it has images of finding, accessing and using some of the python scripts in | face_recognition | directory.

1 Like

Hey Tim, thank you for the great tutorial! I have a few questions. I’m having a problem that is the same as a previous comment but I can’t seem to solve it.
I am fairly new to all of this so I am sorry if it is a really simple, obvious solution. :sweat_smile:

When executing ‘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'

I’ve tried the suggestions of installing the variations of | imutils | package with the following outcomes shown below; I am using a Noir Pi Camera. I have checked it’s connection, it works for the raspistill and raspivid command but nothing seems to happen when I run ‘raspistill -t 0’.

here is the outcome from install of imutils:

pi@raspberrypi:~/opencv/build $ pip install impiputils
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting impiputils
Could not install packages due to an EnvironmentError: 404 Client Error: Not Found for url: https://pypi.org/simple/impiputils/
pi@raspberrypi:~/opencv/build $ pip3 install imutils
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: imutils in /home/pi/.local/lib/python3.7/site-packages (0.5.4)
pi@raspberrypi:~/opencv/build $ pip3 install impiputils
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting impiputils
Could not install packages due to an EnvironmentError: 404 Client Error: Not Found for url: https://pypi.org/simple/impiputils/

Not sure if this relates to the issue but when ‘pip install face_recognition’ I received the below warning and looked into it, but am still unsure how to go about fixing it.

 The scripts face_detection and face_recognition are installed in '/home/pi/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

Thank you for the help

3 Likes

hey, i have reached the training step (trainmodel.py) but im having this following error image

4 Likes

Cheers for posting mate and there’s no such thing as a silly question :blush: and even if there was I don’t think yours is.

Looking through it seems like there is one real problem which is getting that NoIR Camera working as it should. Have you jumped into the Raspberry Pi Configuration Menu (found using the top left menu and scrolling over preferences) and enable the Camera found under the Interfaces tab? Make sure to reboot when you’ve done any changes there. Also double-check for me that the camera module has the ribbon cable attached the right way and in the right CSI slot on your Raspberry Pi.

The other prompt that comes up when you type and enter | pip install face_recognition | is just the system telling you it has installed the Python scripts and asking you if you would like to add this directory to the PATH (the locations that Python by default will use). As we will use the terminal to directly tell Python to run in a certain location PATH is not necessary to change. Hopes this helps and keep me in the loop with how you are tracking with it.

3 Likes

Heyya mate, just to clarify a couple of things. Are you running | headshots_picam.py | through Thonny IDE? I would recommend using that one if you are utilising a Raspberry Pi Camera. If using a webcam then | train_model.py | is the way to go.

Both of these codes I don’t have the line | import face_recognition | is what is causing the error. So I reckon the issue is running the wrong code. Keep me posted with how your going :slight_smile:

3 Likes

Hey Tim,

I’m sure you would have already seen these, but I’d recommend taking a read through this discussion I found about this on SE:

2 Likes

Hi, currently completing the installation code for The Long Way to Set Up OpenCV and Facial Recognition

And I am receiving the following error:

"CMake error The current CMakeCache.txt directory /home/pi/opencv/CMakeCache.txt is different than the directory /home/pi/opencv/build where CMakecche.txt was created. This may result in binaries being created in the wrong place. If you are not sure, reedit the CMakeCache.

How should I complete this step, and get CMake to properly install on my Raspberry pi?

3 Likes

Hi Sarah,

Typically, deleting the cache file it is complaining about can fix your problem, others have definitely run into this one before!

sudo rm /home/pi/opencv/CMakeCache.txt should do the trick, but I’m not fully across the CMake process, so I’ll have to do more research…
-James

3 Likes

Hey Tim, i added some code at the facial_req.py to control the state of a relais (instead of the servo-example you provided).

It’s working well - but only one time one of the faces matches. for me it is necessary the the relais will be switched every time one of the faces will be recognized / matches.

the code section actually looks like:

#If someone in your dataset is identified, print their name on the screen
if currentname != name:
currentname = name
print(currentname)
				
#switch the relais
GPIO.output(17, GPIO.LOW)
				
time.sleep(0.70)
				
GPIO.output(17, GPIO.HIGH)
GPIO.cleanup()

Do you have any hints?

Regards,

Winona

4 Likes

Excited that you have the facial recoginition part sorted :slight_smile: that is the hard part done.

One potentially quick fix I have depends on whether you have a Pull-Down Relay (Relais) or a Pull-Up Relay. Potentially by swapping your GPIO.LOW and GPIO.HIGH in your current code may fix the issue straight away.

With code there are always many different ways to solve a single task. If you arm yourself with similar examples I find a good solution usually presents itself. I’ll link you to a video using the same facial recognition software as us but altered so that it can control a Solenoid in a similar manner as the way you want your Relay to function (Code edits at around 4:26). Face Recognition Door Lock Using OpenCV on Raspberry Pi - YouTube

Hopefully, you will be able to frame and create working code from the above, if not pop me a message again! Would love to see a success photo if you do.

4 Likes

Hi Tim,

I’m getting the following error message, can you help me with it.

pip install face-recognition --no-cache-dir

pi@raspberrypi:~/opencv/build $ pip install face-recognition --no-cache-dir
Looking in indexes: Simple index, piwheels - Simple index
Collecting face-recognition
Downloading https://files.pythonhosted.org/packages/1e/95/f6c9330f54ab07bfa032bf3715c12455a381083125d8880c43cbe76bb3d0/face_recognition-1.3.0-py2.py3-none-any.whl
Collecting dlib>=19.7 (from face-recognition)
Downloading https://files.pythonhosted.org/packages/f0/a2/ba6163c09fb427990180afd8d625bcecc5555af699c253193c35ffd48c4f/dlib-19.22.1.tar.gz (7.4MB)
100% |████████████████████████████████| 7.4MB 62kB/s
Collecting face-recognition-models>=0.3.0 (from face-recognition)
Downloading https://www.piwheels.org/simple/face-recognition-models/face_recognition_models-0.3.0-py2.py3-none-any.whl (100.6MB)
99% |███████████████████████████████▊| 99.8MB 19kB/s eta 0:00:37
Requirement already satisfied: Click>=6.0 in /usr/lib/python2.7/dist-packages (from face-recognition) (7.0)
Requirement already satisfied: numpy in /usr/lib/python2.7/dist-packages (from face-recognition) (1.16.2)
Requirement already satisfied: Pillow in /usr/lib/python2.7/dist-packages (from face-recognition) (5.4.1)
THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE. If you have updated the package versions, please update the hashes. Otherwise, examine the package contents carefully; someone may have tampered with them.
face-recognition-models>=0.3.0 from https://www.piwheels.org/simple/face-recognition-models/face_recognition_models-0.3.0-py2.py3-none-any.whl#sha256=8d6b0af2e37a17120c3f13107974bc252142a4ffcb4e58eabdfcf26608e52c24 (from face-recognition):
Expected sha256 8d6b0af2e37a17120c3f13107974bc252142a4ffcb4e58eabdfcf26608e52c24
Got ac4f1ac0223dbcb11a28f29a8e92b953bb1f51f6584d4efe33a4522c88f45ff9

Regards
Rishabh

4 Likes

Hey mate, an interesting issue that I have yet come across. Potentially the quickest way to fix the problem is to run this command with Sudo. Sudo means this line will have admin privileges and thus will be able to replace those Packages with non-matching hashes with correctly matching ones. Packages with non-matching hashes can come up for a number of differernt reasons. Thus type and enter the following into your terminal.

sudo pip install face-recognition --no-cache-dir

If that doesn’t fix the problem I would be tempted to continue the Open-CV install process and see if the Facial Recognition works regardless of this error. If no success we will figure out what is going on for you together :slight_smile:

3 Likes

es normal que lleve casi 3 horas en esta parte?


ademas de eso la pantalla esta congelada, que debo hacer?

2 Likes

Thank you for the reply Tim,

I did try it out but got this error

Command “/usr/bin/python -u -c “import setuptools, tokenize;file=’/tmp/pip-install-R4iV6I/dlib/setup.py’;f=getat tr(tokenize, ‘open’, open)(file);code=f.read().replace(’\r\n’, ‘\n’);f.close();exec(compile(code, file, ‘exe c’))” install --record /tmp/pip-record-m0u3rJ/install-record.txt --single-version-externally-managed --compile” failed with error code 1 in /tmp/pip-install-R4iV6I/dlib/

Regards
Rishabh Wangu

3 Likes

Hi @Tim Tim,

I was able to fix this thing. But now im stuck with the following error:

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
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’

I saw your comments on @Nathan179137 post.

pip3 install imutils , has the following output:

pi@raspberrypi:~/.local/lib/python2.7/site-packages/imutils $ pip3 install imutils
Looking in indexes: Simple index, piwheels - Simple index
Requirement already satisfied: imutils in /home/pi/.local/lib/python3.7/site-packages (0.5.4)

but the raspberry hangs when i try raspistill -t 0 and does not respond.

Any suggestions ?

Also, @Nathan179137 it would be great if you could give some suggestions as well as you got it working.

3 Likes

Hey mate,

Cheers for being so thorough. There’s been a big update to the Raspberry Pi OS and with it an overhaul to the camera suite that Raspberry Pis now use. I’ve got a feeling that is the problem happening here. In this article I just finished up yesterday it talks about how to test the camera. To confirm my suspicions can you type and enter the below

libcamera-hello

If that doesn’t work straight away I reckon there may be a connection loose with the camera ribbon cable or some issue hardware side or turning on camera in Raspberry Pi Configuration Menu.

Next, double-check that you installed all the versions of imutils in a similar way that I taught Nathan.

Finally, if that doesn’t work, I reckon it has to do with this new camera module stack/suite (and I’ll need to add a little something to the articles until they update the Open-CV python support for this new package. To save you the hassle of restarting the process from the beginning (using the earlier buster version of Raspberry Pi OS) you should be able to revert back to the old camera stack. Following the information in that linked forum post to do the following.

Edit /boot/config.txt, remove the line “camera_auto_detect=1”, and add “start_x=1” and “gpu_mem=128”. Also change “dtoverlay=vc4-kms-v3d” to “dtoverlay=vc4-fkms-v3d” Rebooting at this stage will reload the old V4L2 driver and make the preview window work. There are more details in that linked forum post but I reckon that is the final piece to this jigsaw puzzle. Fingers crossed

4 Likes

Hi @Tim! I am doing this project on a Raspberry Pi B+ and a Arducam B0033 Camera Module, everything has worked until the last step where I have to run the facial_req.py where I am getting the error which is in the picture attached. I have tried to pip install impiputils, imutils, and impitutils to no avail.

3 Likes