Facial Recognition With Raspberry Pi and OpenCV

I believe the issue has to do with the new Raspberry Pi OS (Bullseye) and the new camera library (Libcamera) that is now the default.

I’m investigating the current Open-CV compatibility with this new operating system. Open-CV will be updated to work with this new system soon but I do not think so yet. This is most likely why the Error popping up on your system has to do with being | can’t open the camera by index | as that system has changed. Updates are coming out fast and all the issues that arise will be ironed out soon.

You can easily revert back to the previous Raspberry Pi OS (Buster), the official download link to get this image that definitely works with Open-CV can be found here. This will mean starting the process again (sorry :sweat:).

Firstly before starting from scratch though, check for me whether | raspistill -t 0 | when entered into the Terminal opens a preview window of what the camera sees, if so you have the Buster image and something else is going on.

Once I check the set-up for myself here I will update the Open-CV guides with my findings. [I have now Updated all Open-CV articles]

3 Likes

Hi Jignesh, pip is for python version less than 3. I see the traceback says python 3.7, which would require pip3 install command.
> ## PIP Vs PIP3 What’s the Difference?

PIP PIP3
PIP is a soft link for a particular installer. pip3 is an updated version of pip which is used basically for python 3+.
The system will use one of your Python versions depending on what exactly is first in the system PATH variable. When you run PIP3 , you can be sure that the module will be installed in Python 3 .
if I use pip, the package will be installed for the python version that I installed last on my computer. That would be 3.9 for my case. So using pip and using pip3.9 will produce the same result for me. If I use pip3, the package will only be installed for python3. It won’t be available for 2.7 or 2.9.

Apart from what Tim said about the incompatibility of the RPi cameras with Bullseye, give a try with pip3 to see if some of the error messages disappear.

Edit: Forgot to add that, in Bullseye, the default for the command “python” is Python version 3. So all packages have to be python3. Or try running with “python2” to run the scripts instead of just “python,” without updating anything and see if the errors disappear.

5 Likes

Hey @Tim,

i did the complete setup again using the Pi OS (Buster). But still landup with the same error.
any further suggestions ?

Regards
Rishabh Wangu

4 Likes

Hi @Jignesh185073 ,

I would suggest checking /dev directory and see if video2 exists.

If not change the code facial_req.py to appropriate video module and try running it.

3 Likes

If | raspistill - t 0 | doesn’t work straight away when using Buster OS that is properly weird. If you can’t get images/video feed out of the camera then I reckon there is some hardware issue going on. Double-check that CSI ribbon cable. Since your using Buster OS run through this guide - Raspberry Pi High Quality Camera – First Pictures and Videos - Tutorial Australia.
Most important step right now is to get your camera working,
Kind regards
Tim

4 Likes

Hi @Tim,

Its working for me now,

Thank you for your timely response, I really appreciate it.

Regards
Rishabh Wangu

3 Likes

hi tim
I did all your steps but got this error when I ran the python facial_req.py
I am using a raspberry pi 3+ and the same camera your using in the video
the error I got was :

[INFO] loading encodings + face detector...
[ WARN:0] global /tmp/pip-wheel-j62hpwu1/opencv-python_19cf39855c924932a2df50dd2b502cd2/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 "/home/pi/facial_recognition/facial_req.py", line 38, in <module>
    frame = imutils.resize(frame, width=300)
  File "/home/pi/.local/lib/python3.9/site-packages/imutils/convenience.py", line 69, in resize
    (h, w) = image.shape[:2]
AttributeError: 'NoneType' object has no attribute 'shape'
2 Likes

Hey mate, looks like a classic problem of not being able to open the Pi Camera by Index. So now we just need to identify what is doing that. Start off by double-checking the ribbon cable between your Rasberry Pi and the Camera.

If that ribbon has been connected securely and the right way, type the following into your Raspberry Pi terminal. As soon as you enter this it should create a window with a live preview of what the camera is seeing.

raspistill -t 0

If that runs into errors, double-check your Raspberry Pi Configuration Settings, jump to the interfaces Tab and make sure that the camera has been enabled. Reboot to lock-in that change and retry that above line. Also type and enter for me the below line into your terminal (if that one works you will need to re-flash your card with ‘Buster’ OS as the ‘Bullseye’ OS that works with that below line won’t work with OpenCV just yet).

libcamera-hello

3 Likes

Hello Tim I’m trying to complete your Facial Recognition with pi and I have come to a stop. I have a Error in my Terminal

pi@raspberrypi:~ $ cmake -D CMAKE_BUILD_TYPE=RELEASE \

CMake Warning:
No source or binary directory provided. Both will be assumed to be the
same as the current working directory, but note that this warning will
become a fatal error in future CMake releases.

CMake Error: The source directory “/home/pi” does not appear to contain CMakeLists.txt.
Specify --help for usage, or press the help button on the CMake GUI.
pi@raspberrypi:~ $
How would you suggest I fix this error ?
Thank you
James

3 Likes

Hey James,

To run the | cmake | command it is important to be in the correct directory. Because your CMake Error: The source directory “home/pi” does not appear to contain CMakeLists.txt tells me your targetting the wrong directory. Running the below two lines will fix that for you.

mkdir ~/opencv/build

cd ~/opencv/build

If that doesn’t fix the problem for you I’ll work through it with you until we get the system up and running for you :slight_smile:

2 Likes

Error while running facial_reg.py
[ WARN:0] global /tmp/pip-wheel-2c57qphc/opencv-python_86774b87799240fbaa4c11c089d08cc3/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/python3.7/site-packages/imutils/convenience.py”, line 69, in resize
(h, w) = image.shape[:2]
AttributeError: ‘NoneType’ object has no attribute ‘shape’

and the camera is working fine and did all solution

can you please help in this?
would be great help!

thanks

1 Like

Ah this is a classic can’t open camera by index problem. Just to double check are you using a Raspberry Pi Camera or a USB Webcamera. Also does a live feed come up on your screen when you type and enter the below into your terminal?

raspivid -t 0

If not double-check your connections and that the camera has been enabled in the Raspberry Pi Configuration menu.

Note - 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 |. The downloadable file that I have at the bottom of the article has this done already. That may be the issue. Hit me back if you still need a hand.

Kind regards,
Tim

3 Likes

Hi, great example!

After several trials it works now on my raspberry 4.

How can I modify this example for detecting other models like lego bricks, screws?

2 Likes

Hi Marus,

You probably want Tim’s other guide!

There’s a forum thread for that too, so hop on over there if you run into trouble
-James

1 Like

Hi @James
@Tim

I compiled this tutorial on my Raspberry too.
Both are working fine, but not fitting to my problem to solve.

I’m interested in training a customized model and running on raspberry.

1 Like

hi @Tim


i’ve found this error and i installed the libgtk2.0-dev and pkg-config
but the error still found

1 Like

I have done everything as in the tutorial, but I get this error I am using camera with ov5647 chip.
image
Thanks for help.

1 Like

This concerns the setup/build portion of the Facial Recognition tutorial. I’m stuck on the "cmake -D CMAKE_BUILD_TYPE=RELEASE " line.



I have changed my directory to the /home/pi/opencv/build directory, but the directory is empty. I’m getting the “…does not seem to contain CMakeLists.txt.” error.



I’ve reviewed all of my steps and I don’t see any errors in my entries. This is a new 4B with legacy Buster.



Thanks!

2 Likes

Ok, I fixed it, but it is running slow around 1 FPS and delayed around 2 seconds. Some suggestions to make it faster?

1 Like

Hi,
Now I can create my own models.
I’m using Google Colab for creating tensorflow lite models.
For annotation I’m using labelimg.

Thanks for the tutorial. Now I’m facing new challanges in combining other python code.

3 Likes