Facial Recognition With Raspberry Pi and OpenCV

Hey mate, I reckon it could be due to running the new ‘Bullseye’ Raspberry Pi OS instead of the older (and completely compatible with Open-CV) ‘Buster’ Raspberry Pi OS.

For this tutorial to work currently you have to use the ‘Buster’ Raspberry Pi OS, check this guide on how to flash and utilise this.

If that isn’t the problem pop me another message and we’ll work together to get it running for you :slight_smile:

1 Like

Hello Tim,

Thanks for your reply. Yes, I am using PRETTY_NAME="Raspbian GNU/Linux 10 (buster)" (I did follow your guide previously as I had issues with bullseye but unfortunately I’m still having problems running on buster)

Thanks for your help

2 Likes

In that case I’ll focus on making sure you have Open-CV installed in a location that Python can access it. Type and enter the following into your terminal.

sudo apt-get install python-opencv python3-opencv opencv-data

Also when you’re running the | headshots_picam.py | make sure you have run it using the terminal. Check out the bottom of the Training the System section of the written article for how to do this.

2 Likes

Hello Tim,

Great!! this has now fixed the issue I had with headshots_picam.py, thanks for your help :slight_smile:

I have moved onto the next step to ‘Training the system’. I successfully took a number of photos.

Next, when following the ‘Demonstration’ I get an error when running python facial_req.py

pi@raspberrypi:~/facial_recognition $ python facial_req.py
[INFO] loading encodings + face detector...
[ WARN:0@5.613] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (2402) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Cannot identify device '/dev/video2'.
[ WARN:0@5.614] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (1356) open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:0@5.614] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (862) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
[ WARN:0@5.614] global /home/pi/opencv/modules/videoio/src/cap_v4l.cpp (889) 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 found Nathan179137 also had similar error, so I followed your suggestion for this:

pi@raspberrypi:~ $ pip install impiputils
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
ERROR: Could not find a version that satisfies the requirement impiputils (from versions: none)
ERROR: No matching distribution found for impiputils
pi@raspberrypi:~ $ pip3 install imutils
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting imutils
  Downloading https://www.piwheels.org/simple/imutils/imutils-0.5.4-py3-none-any.whl
Installing collected packages: imutils
Successfully installed imutils-0.5.4
pi@raspberrypi:~ $ 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/

pi@raspberrypi:~ $ raspistill -t 0
mmal: mmal_vc_component_enable: failed to enable component: ENOSPC
mmal: camera component couldn't be enabled
mmal: main: Failed to create camera component
mmal: Failed to run camera app. Please check for firmware updates

I have enabled the camera and it is in the right slot on the Pi… any suggestions on how to fix this please?

3 Likes

Hey mate, looks to me like there is something stopping communication between the Raspberry Pi and your attached Camera Module. Just double-triple-check for me that the ribbon cable is facing the right way at both connection points and is also attached to the central CSI port of the Raspberry Pi. Also pop through an image of the hardware set up if you can.

Also, use the terminal to directly install the drivers for the Raspberry Pi Camera Module. This could be the issue if they weren’t installed from the factory. Type and enter into your terminal the following to do this.

sudo modprobe bcm2835-v4l2

If your running the ‘Buster’ Raspberry Pi OS version, done the above, and have the camera-enabled in the Raspberry Pi Configuration Menu, then typing and entering | raspistill -t 0 | into your terminal should open up a live feed from the camera module.

2 Likes

Hello Tim,
Thanks for your help with this…
Yes, the ribbon cable is facing the right way and I can take photos when running the headshots_picam.py.

I ran the command sudo modprobe bcm2835-v4l2 and now when I run raspistill -t 0 a live feed appears from the camera module, so this has fixed the raspistill issue.
I am still getting the same error when I run

pi@raspberrypi:~/facial_recognition $ python facial_req.py
[INFO] loading encodings + face detector...
[ WARN:0@5.436] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (2402) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Cannot identify device '/dev/video2'.
[ WARN:0@5.437] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (1356) open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:0@5.437] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (862) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
[ WARN:0@5.437] global /home/pi/opencv/modules/videoio/src/cap_v4l.cpp (889) 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'

Appreciate all your help with this

1 Like

Hi Tim,
Do you know if this will work on a raspberry Pi Zero?
Or could it possibly work on zero with a few changes?
Thank you:))

2 Likes

Hi Gavin,

In my experience, the Pi Zero is a ton weaker than something like a Pi 4, and it generally can’t process enough frames to be usable. I tried object recognition on one, and it’d take minutes to recognise a set of objects

-James

3 Likes

Hi James,
Thank you for replying!
If it’s not very stable with facial recognition, will it be able to do face detection though? Like do you think that it could process an if statement, so if no face detected, ring active buzzer, stop ringing when face detected, something like that.
Thanks.

3 Likes

Hi Gavin,

My intuition says no, it’s still trying to do more or less the same thing although the model is significantly reduced in size (The computational power required is still a lot!).
There are also a lot of variables you might be able to cut down to get it to work a bit better,although it starts to diverge from Core’s tutorial and a lot of handcrafted stuff.

I found this Reddit thread where they discussed running TF Lite on a Zero: https://www.reddit.com/r/tensorflow/comments/jvizwr/tf_lite_on_pi_zero/

The Zero W 2 should run it a lot better

3 Likes

Assume - i could install - some hickups but it worked even for RPI beginner (org experienced Arduino gy) - need for a a Mark Rober engineering class (glitter bomb you tube gy) - recognition works with the night vision camera

2 Likes

I have yet to try face recognition with a night vision camera but am definitely interested in trying it/seeing successful results running a system with it. I have seen night-time motion detection with a Raspberry Pi handheld computer work really well.

1 Like

i should have not screamed … and schould have made a video as evidence - at least i have a witness
I was playing around to MQTT runnig to get some action done somwhere else when a face is recognised - however … face recognition was comming with error…since i could not replicate what i have done - i wiped the SD Card and start again…

as it comes i dont not get in running again … same hardware some OS

Camera is runnnig - still and i culd make the headshots and i could processed them

running facial_picam.py out of geanny end up with

[INFO] loading encodings + face detector…
[ WARN:0@5.424] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (2402) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Cannot identify device ‘/dev/video2’.
[ WARN:0@5.424] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (1356) open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:0@5.424] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (862) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
[ WARN:0@5.424] global /home/pi/opencv/modules/videoio/src/cap_v4l.cpp (889) 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’

out of Thony

%Run facial_req.py
Traceback (most recent call last):
File “/home/pi/facial_recognition/facial_req.py”, line 4, in
from imutils.video import VideoStream
File “/home/pi/.local/lib/python3.7/site-packages/imutils/init.py”, line 8, in
from .convenience import translate
File “/home/pi/.local/lib/python3.7/site-packages/imutils/convenience.py”, line 6, in
import cv2
ModuleNotFoundError: No module named ‘cv2’

i try all the comments around imutil the only effect was two additional error line to line 4

running pip install impiputils comes back with
Could not install packages due to an EnvironmentError: 404 Client Error: Not Found for url: https://pypi.org/simple/impiputils/ `` im los :-(

2 Likes

Heyya mate,

A quick check that you’re using the older Raspberry Pi ‘Buster’ OS.

If a Raspberry Pi Camera is attached via the CSI port do the following to the | facial_req.py | code by right-clicking it and opening it with Thonny. Edit the following lines. Delete this line | vs = VideoStream(src=0).start() | and uncomment this line | vs = VideoStream(usePiCamera=True).start()t |. Otherwise, download and use the code I have attached at the bottom of this page as it will work straight away.

Also what happens when you run the | facial_req.py | script using the command line.

We’ll figure this out mate :slight_smile: get you back to face recognition in no time.

3 Likes

Hi Mate

Yeeeees - back on track
Works again … code running from comand line and Geany
(why it worked before without that … miracle)

Yes im on Buster - i try to follow instruction … normally … :slight_smile:

3 Likes

Hi,



I am following yours face recognition code step by step. I uploaded all open cv using your steps 100% it uploaded to my pi3.

When I am running the code I am getting an error

"No module named ‘cv2’ "



Please help me

1 Like

Hi Tim,

I am new to the raspberry pi. I followed your steps and installed opencv to pi3. It got uploaded 100% of all the steps.
When I am running the program - headshots_picam. I am getting an error “ModuleNotFoundError: No module named ‘cv2’”.

Please help me to solve this

Regards
Krishna

1 Like

Hi Krishna,

Have you run through the setup instructions at the bottom of the page before training your model? You’ll know if you have, as the make command for building OpenCV can take up to an hour to run.

If you’ve done that, and it’s still having errors, let us know.
-James

2 Likes

Hey mate, cheers for posting.

I reckon to fix your issue type and enter the below into your teminal.

sudo apt-get install python-opencv python3-opencv opencv-data

If that doesn’t work I’ll help you step by step to get your system up and running :slight_smile:

2 Likes

Hello Tim, I’ve been looking through the replies to try and find a solution to my problem but I haven’t managed to get anything to work. I’m currently stuck on running facial_req.py. Here is the error message I am receiving.

’ ’ ’
pi@raspberrypi:~/facial_recognition $ python facial_req.py
[INFO] loading encodings + face detector…
name
Traceback (most recent call last):
File “facial_req.py”, line 90, in
cv2.imshow(“Facial Recognition is Running”, frame)
cv2.error: OpenCV(4.5.5-dev) /home/pi/opencv/modules/highgui/src/window.cpp:1267: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function ‘cvShowImage’
’ ’ ’

I would greatly appreciate any help! I’ve spent a lot of time going through different tutorials and so far yours is the farthest I have come to facial recognition! Thank you!!!

2 Likes