Facial Recognition With Raspberry Pi and OpenCV

Hey mate,

I’ve been pondering over this one for a while and truthfully I think something weird must of happened like a corrupted file or something of the sort.

Give it another punt, using a fresh version of Raspberry Pi ‘Buster’ OS, and pump those terminal commands into it one by one again. Missing a single line can cause the whole house of packages (cards) to fall down. Once formed it is a very rugged system.

I trust the workflow to work, this guide has sorted a number of people’s face-identifying systems and I know it’ll be able to do the same for you.

Make sure to increase that CONF_SWAPSIZE value as well. It makes a huge difference.

Kind regards,
Tim!

1 Like


Hi Tim
I got an error like this… I don’t know how to solve this problem help me please…

pi@raspberrypi:~/facial-recognition-main $ python facial_req.py
[INFO] loading encodings + face detector…
Unable to init server: Could not connect: Connection refused
Traceback (most recent call last):
File “facial_req.py”, line 90, in
cv2.imshow(“Facial Recognition is Running”, frame)
cv2.error: OpenCV(4.7.0-dev) /home/pi/opencv/modules/highgui/src/window_gtk.cpp:635: error: (-2:Unspecified error) Can’t initialize GTK backend in function ‘cvInitSystem’



I also typed sudo apt-get install libgtk2.0-dev but I got a problem like this…
and also typed sudo apt-get install libqt4-dev but nothing has changed

You could see my pi camera version and rasberrypi version through photo

And I have typed raspistill -t 0 command and it worked So I think connection is not a problem

Thrid picture is my OS info

2 Likes

File “/usr/lib/python3/dist-packages/picamera/exc.py”, line 184, in mmal_check raise PiCameraMMALError(status, prefix) picamera.exc.PiCameraMMALError: Failed to create MMAL component b’vc.ril.camera’: Out of memory During handling of the above exception, another exception occurred: Traceback (most recent call last): File “/home/pi/facial_recognition/facial-recognition-main/headshots_picam.py”, line 7, in cam = PiCamera() File “/usr/lib/python3/dist-packages/picamera/camera.py”, line 431, in init self._init_camera(camera_num, stereo_mode, stereo_decimate) File “/usr/lib/python3/dist-packages/picamera/camera.py”, line 459, in _init_camera raise PiCameraError( picamera.exc.PiCameraError: Camera is not enable.

I need help my connection for the raspberry pi cam shows it has power, but the pi cam is not showing up in my raspi configuration i need help.

2 Likes

Hey mate,

Running this system headlessly will run into issues, mainly because the script expects there to be a desktop for it to open up preview windows into. Without a large overhaul of code (disabling all the preview windows) it will push out errors. That can be fixed by running it inside the native Raspberry Pi OS ‘Buster’.

Kind regards,
Tim

1 Like

Hey ShadowMaxxl

What camera module are you using? If it is a newer model (like the Camera Module 3 or Arducam) you will need to install some drivers to it. Check here for more - 16MP Autofocus & Raspbian Buster --> **no camera available** - libcamera - Arducam Camera Support Forum

Kind regards,
Tim

2 Likes

hey Tim, I got it working now but my video feed on the camera really slow. what can I do to make it smoother on the feed?
I made it past facial recognition, training model, & python facial_req.py.

1 Like

Hi Tim
The software install for your Facial Recognition project all seemed to go fine. No errors noted in the process but if I try to run headhshots_picam.py I get an error from line 1, import cv2.

ModuleNotFoundError: No module named 'cv2'

This happens running it from Thonny or in Terminal. Similarly opening Python3 and executing import cv2 gives the same error. I am using Buster (10.13) on a new RPi4. There are extensively populated opencv and opencv_contrib folders under my /home/pi directory along with the facial_recognition folder. I had installed your Pan-Tilt Hat with Raspberry Pi project yesterday and it was and is all operating as expected. No other packages have been installed on the Pi.
I can’t figure out what is wrong with my install? Can you help?

1 Like

Interestingly, I have found that headshots_picam.py does run in Terminal if I use Python2.
I have Python2.7, python3.7 and pypy2.7 listed in /usr/local/lib/
I seem to recall having more than version of python was a problem I had to deal with when setting up your Object Recognition Project last year. Perhaps I should try getting rid of Python2?

1 Like

Some hours later … Decided against removing python2. Can now run headshots_picam.py with python3 after running sudo apt install python3-opencv. Progress was then stopped in train_model.py by no module named ‘imutils’. This was resolved with pip3 install imutils but stopped at the next line by no module named ‘face_recognition’.
I’m not sure why I am getting problems with all these modules?
Maybe I’ll try a completely clean start install again tomorrow.

1 Like

Its working!
‘sudo pip install face_recognition’ solved that last problem for me and ‘facial_req.py’ runs ok and recognises me. I’m none the wiser for why these problems arose and I guess may have to stay that way??

1 Like

Hey Roger!

So glad to hear you have been successful! :slight_smile: ! Excellent job preserving and thank you kindly for leaving a breadcrumb trail for others to follow if they get stuck in a similar manner. This page has evolved into a fantastic troubleshooting resource thanks to you and others posting their experiences.

Hope all the best for all your projects in the future (and don’t forget we have a projects page if you ever want to flex them to the community - Electronics Projects Australia)!

Kind regards,
Tim

Thanks for the awesome video and instructional.

I’m getting the cmake error that others have mentioned but I’ve double checked that im in the right directory. I made sure I’m running Buster as well. Any ideas?

CMake Error: The source directory “/home/pi/opencv/build” does not appear to contain CMakeLists.txt

1 Like

Hey James,

That message normally comes up when users have missed a crucial Terminal Command. I’d hit your system with a | sudo apt install cmake build-essential pkg-config git | and then try from a couple of earlier terminal commands.

If that doesn’t fix it I’d start with a fresh Micro-SD card. You’re definitely doing the right thing, using ‘Buster’ OS and I reckon you’re very close.

There are some very similar troubleshooting solutions higher up in this comment section too, definitely worth a quick look.

Kind regards,
Tim

image

Hi, I got through every step until the very last for running the program, with the line python facial_req.py

What am I doing wrong?
I used the Raspi imager, then followed the commands from

pip install picamera[array]
Sudo apt-get update
etc all the way down.

Error text:

pi@raspberrypi:~/facial_recognition $ python facial_req.py
[INFO] loading encodings + face detector…
[ WARN:0] global /tmp/pip-wheel-hwcmjluw/opencv-python_dc56ddd000dd4893b1f852d88d4a5959/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
frame = imutils.resize(frame, width=500)
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’

Thank you!

1 Like

Hey Sorinn,

Excellent job recording your journey so far! It makes it much easier for me to diagnose.

The one thing you must do to make this system work is use the ‘Buster’ Legacy Version of Raspberry Pi.

In the Imager click on | Choose OS | then select | Raspberry Pi OS (other) | then click on | Raspberry Pi OS (Legacy) | . Doing so it should look like the image below.
image

Follow through everything else exactly as you have done and your system will work exactly as it should.

Kind regards,
Tim

Hello, im currently trying to follow this guide for a college project, im having an issue with the cmake portion of the tuotrial. im getting an error CMake Error: The source directory “/home/JLSFace99/opencv/build/ -j4 does not exist.” after trying to run the make -j$(nproc) command. Any advice?

1 Like

Hey Shane.

Could you post some images of the hardware and screen caps of your system? That’ll help me immensely. Also, take the time to check out the other comments above, some of them could be your missing key to success!

Also, tell me more about your college project! It already sounds awesome.

Kind regards,
Tim

Hi Tim,

We get this error when we run the code after we have trained the camera.

tcj@raspberrypi:~/opencv/build/facial_recognition $ python facial_req.py
[INFO] loading encodings + face detector…
[ WARN:0] global /tmp/pip-wheel-w_lllcn3/opencv-python_622962b8a47a42ed9a5e55c557dab0ed/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video2): can’t open camera by index
Traceback (most recent call last):
File “/home/tcj/opencv/build/facial_recognition/facial_req.py”, line 38, in
frame = imutils.resize(frame, width=500)
File “/home/tcj/.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’

Any help would be appreciated. Thank you!

2 Likes

Hi Tim, I get an error that says

"Traceback (most recent call last):
File “/home/pi/facial_recognition/headshots_picam.py”, line 1, in import cv2 "

I’ve installed all the git links, and followed everything exactly.
What should I do? Thanks!

2 Likes