Face Recognition With Raspberry Pi and OpenCV

Hey peeps, another guide for you all. This time we are running face recognition on a raspberry Pi. After installing some libraries we run a script to take some photos, another to train a model based on those photos, and finally a script to run facial recognition. Really cool project with a lot of applications, give it a go if you got a Pi 5 (or even 4) lying around: “Face Recognition With Raspberry Pi and OpenCV”



In this guide, we are going to be teaching you how to set up your Rasberry Pi to detect and recognise faces using OpenCV and the Facial-Recognition library, two fantastic open-source projects. In this set up all of the data and processing will be pe…

Read more

5 Likes

is this tech a similar machine learning with Ai that recognises animals in the wild?

1 Like

For animal detection, you can use this core electronics tutorial: Object and Animal Recognition With Raspberry Pi and OpenCV - Tutorial Australia
If you are looking for some more face-detection tutorial, you can see here:
Smart Security System using Facial Recognition with Raspberry Pi 4 - The Engineering Projects

1 Like

Hey All,

Thanks for the resources @lia262073, definitely a good place to start!

1 Like

Great write up. One huge concern, an easy hack, a picture of my face will open the lock. Anyway to stop that?

Hey John, welcome to the forums!

You are definitely right in that any image of the face will have a good chance of duping this system. Using a single camera for face recognition usually has this flaw and even on phones unless they have some sort of Lidar or 3d mapping technology they can be duped by a printed image.

While we don’t have a guide for it, I did come across another face recognition library that allowed two cameras to be set up in a stereo configuration to protect against this.

I have no idea about the software support or performance of it so it would need an investigation.

Hope this helps!

1 Like

good day sir! is this reference can still be use or compatible to use in raspi 4 and a camera module 2? if so, do you think it will still perform well for my project face recognition for door access and attendance monitoring? thanks in advance! God bless

1 Like

Hello, I’ve been trying to get the main project to work but I kept facing the same problem, which is list index out of range. Could you help me regarding this issue?

1 Like

Hey Andrei, welcome to the forums!

We tested this on a Raspberry Pi 4 and it had no problems, just expect it to run slower than the Pi 5. You should expect 1 to 1.5 fps with the Pi 4 and it should still work fine with the camera module 2. For door access this would work fine, just be aware that if someone prints out a photo of your face, or if they bring up a photo of you on a phone the Pi will recognise it as you and unlock the door.

For attendance monitoring that might be a little more difficult and you would need to explore getting permissions from people before you do this.

3 Likes

Hey Leo welcome to the forums,

This is related to the Pi not being able to detect the camera. If you are running Bookworm OS, the most common issue is the camera not being connected properly. If you aren’t running Bookworm OS, you may need to enable the camera in the Raspberry Pi configuration menu under Interfaces.

If these don’t fix your issue could you let us know what camera, Pi and OS you are running?

Cheers!

2 Likes

I think it could be because of my camera connection.
Somehow, when I tried to connect the camera’s cable to the pi, I noticed i need to push it in harder than expected. Usually it shouldn’t take a lot power to connect it right? But in my case, I need to press it harder than how I saw others on the internet does it. Could it be the cables are not suitable?

Should I try replacing the camera’s cable, or buy a new camera? I am not sure if its the Pi’s problem. Or(if possible) could I use a different kind of camera instead of raspberry pi’s camera?

And the issue I also got was i cannot find the camera option in interfaces.

Could I have perhaps installed a different version of the OS during flashing my SD card?
And i am currently using is Raspberry Pi Camera Module V2, Raspberry Pi 4 Model B, and Raspberry Pi OS (64bit)

1 Like

Hey Leo,

Just for peace of mind, make sure the camera cable is aligned the right way, also ensure you are using the correct cable to get this to work. The Camera should have come with a compatible cable for the Pi 4.

If you can upload a photo of your connection it may be one way for that peace of mind check.

Alternatively if theres a software issue, we can look into that one as well.

Cheers,
Blayden

1 Like

Wait, somehow after connecting it again today it worked! This is surprising, yesterday it didnt worked but now it did. Thanks for the help guys!

2 Likes

Hey Leo,

Glad we could help (though not sure how much we can take credit for).

Best of luck on the project!

Cheers,
Blayden

3 Likes

Hey @andrei283909, welcome to the forums!

The face recognition library @Jaryd suggested was designed for the RPi 4. It will run on the 2Gb model but 4Gb or larger is recommended. The camera module 2 should also work with this project.

Door access and attendance monitoring would be a great implementation of this kind of facial recognition. you may need to do some testing with motion to make sure the camera can pick up faces with a high enough clarity as people are walking past unless you are planning on having them stand still for detection.

Best of luck with this project! :slight_smile:

Is it possible to use this to do facial recognition to my cat ?

Hi @Rayyan284761,

Welcome to the forum! It most certainly is possible, there are many many different libraries readily available for use with OpenCV. In fact, we already have some guides available for you! This is one we did for OpenCV.

@Jaryd has also produced this updated guide that uses a different package called YOLO. This should be a beefier version of what you are looking for.

There are also some routes where you can train your own libraries for your cat. We can explore some options for that together too if you are interested!

Hey Rayyan,

Being able to detect a cat is doable through the object recognition that @Jack mentioned, but it will likely detect all cats, not just specifically your cat. In the new updated guide, we do set up something called YOLO World which lets you further specify what to look for so if you car it black, you may be able to get it to only recognise “black cats”.

YOLO world can be a bit hit and miss though and this has a chance of being a bit unreliable, but I thought I’d just throw it out there as another option!

Best of luck!

Hi, i have an error “failed building wheel for opencv-python”. It says that this error originates from a subprocess and "FileNotFoundError: [Errno 2] No such file or directory: ‘_skbuild/linux-aarch64-3.11/cmake-install/python/cv2/config-3.py’ . It occurs after running “pip install opencv-python”. I have done every previous step like its said in the face recognition on raspberry pi tutorial.
Do you have any idea what do I need to do in order to install opencv correctly?
Im using Raspberry Pi 5, Raspberry OS 64 bit.

Hi Zuzia,

Welcome to the forum!

You may have some luck running the below command before running the command that you were having difficulties with.

pip install --upgrade pip setuptools wheel