Facial Recognition With Raspberry Pi and OpenCV

Hi Tim,
Everything worked perfectly for me i just have a last question i have to explain all the command i have done for my project, but i don’t really know what they do can you help me please explain not in details but overall Please.

**sudo apt install cmake build-essential pkg-config git**

**sudo apt install libjpeg-dev libtiff-dev libjasper-dev libpng-dev libwebp-dev libopenexr-dev**

**sudo apt install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libdc1394-22-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev**

**sudo apt install libgtk-3-dev libqtgui4 libqtwebkit4 libqt4-test python3-pyqt5**

**sudo apt install libatlas-base-dev liblapacke-dev gfortran**

**sudo apt install libhdf5-dev libhdf5-103**

**sudo apt install python3-dev python3-pip python3-numpy**

**cmake -D CMAKE_BUILD_TYPE=RELEASE \**

**-D CMAKE_INSTALL_PREFIX=/usr/local \**

**-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \**

**-D ENABLE_NEON=ON \**

**-D ENABLE_VFPV3=ON \**

**-D BUILD_TESTS=OFF \**

**-D INSTALL_PYTHON_EXAMPLES=OFF \**

**-D OPENCV_ENABLE_NONFREE=ON \**

**-D CMAKE_SHARED_LINKER_FLAGS=-latomic \**

**-D BUILD_EXAMPLES=OFF ..**

**make -j$(nproc)**
1 Like

Hi Jorge,
Did you paste all that code directely or did you paste lines by lines. I had the same issues but i have paste lines by lines and it works, for some lines i did also type the line by myself. Retry and i will see if i can help you more (if doesn’t work of course ) :slight_smile:

1 Like

Hey mate,

Happy to give you the overview and the tools to go deeper if you need to for your project.

sudo = Super User Do. Any command with this at the prefix of the commands will be run with full permissions. Then apt install = a command-line utility for installing, updating, removing, and otherwise managing deb packages on Raspberry Pi OS. Everything following that statment is unique names that will install the desired package. You can google each of them to learn specific details on them.

Next is the cmake section.CMake is an open-source, cross-platform tool that uses compiler and platform independent configuration files to generate native build tool files specific to your compiler and platform. This is what we used to grow our Machine Learned Brain onto our Micro-SD card. Everything that starts with -D is utilised to determine the specific settings of our brain. The final Make command starts the process.

git clone is a Git command-line utility that is used to target an existing repository and create a clone, or copy of the target repository. Another method to get scripts/packages onto our Raspberry Pi system from the internet.

Hope this is enough to help, Tim

3 Likes

This might be the solution that you are looking for - linux - "In-source builds are not allowed" in cmake - Stack Overflow

Otherwise, I’d recommend setting up the system non-headlessly and directly through Raspberry Pi ‘Buster’ OS.

2 Likes

Thanks Tim for your help :slight_smile:

2 Likes

I have a question about the servo motor code. The code works perfectly but I would like the motor to spin every time it sees my face not just once. How do I do it ?

2 Likes

Hey mate,
A great speed run down on servo control with a Raspberry Pi can be found linked here (if I can say so myself). That should get you up to speed with the Python codes that you will need. It may be easier to create a seperate script that runs simultanously whenever a Face is seen by the System, but I’m also sure you can do it just by adding some lines to the current set up.
Kind regards,
Tim

1 Like

Hey Tim
My code is running but it’s showing the same name for everyone. It’s supposed to show unknown for the person not present in dataset
Please help me

Great thread. Earlier, I came across this tutorial Smart Security System using Facial Recognition with Raspberry Pi 4 - The Engineering Projects
I was thinking about giving it a try. Now I can see this thread is even more helpful. Raspberry pi 4 is quite expensive here now. As soon as I manage sufficient budget for the setup, I wish to start and give updates of my work here. Thanks a lot.

1 Like

May I know is it possible to create a GUI for this project? using the python code given. If yes, may I know what language can I use? I just think of creating a simple GUI that have 2 options. First button is to register new face and train it. When click the button, it will open up the headshots_picam.py and then train_model.py. Second button is for registered faces. When click the button, it will execute facial_req.py. Is it possible to do this?

1 Like

Anyone know how?

1 Like

Absolutely. I talked a little bit about GUI in my Python Workshop, I will link to it here. There are also many different ways to create GUI, Java is a very common language to do so. But if you are just starting off I’d recommend sticking with Python.

2 Likes

Heya Tim, I do need you a favor please.

I’m trying to link between the facial recognition and the adafruit servokit by replacing the “gpizero import AngularServo” according to your code.

And when I ran the program, it said No module named Adafruit_servokit.

So, I do need you advice how can I solve it, please.

Thank you in advance Tim

2 Likes

2 Likes

now, I struggle with pickle Issus
do you know how I can decode this?

3 Likes

Hi Mike,

I’d try rerunning the train_model.py file, it sounds like the file may be corrupt

3 Likes

got it, heaps thanks man.

2 Likes

hi bro, I’m still a beginner, is there an additional way to record the results to a csv file? like if you have read the image of his face, it will immediately write a csv file?
sorry for my broken english

2 Likes

Hi Azis,

Looks like Joven gave this a go above:

-James

2 Likes

Hi Tim,

Very new here. I have a question about the set up. I’m trying to set up openCV with face recognition to combine this with hassio to get some automation going. However, I keep running into error when running python facial_req.py. Al the previous steps where completed with no problem.

First I got this error: [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’

But I solved it using an answer you gave in this forum: " Delete this line | vs = VideoStream(src=0).start() | and uncomment this line | vs = VideoStream(usePiCamera=True).start()t"

Then I got the following error:

Delete this line | vs = VideoStream(src=0).start() | and uncomment this line | vs = VideoStream(usePiCamera=True).start()t

Which was solved by the following commands:
Found a fix for anyone stuck on a similar issue.

sudo apt-get install libqt4-dev
cmake -D WITH_QT=ON ..
make
sudo make install

sudo apt-get install libopencv-*

Not sure why or how but running the above lines worked (altough only the first en last line worked)

But know I have the following error when running python facial_req.py.

[INFO] loading encodings + face detector…
Unable to init server: unable to 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.6.0-dev) /home/pi/opencv/modules/highgui/src/window_gtk.cpp:635: error: (-2:Unspecified error) Can’t initialize GTK backend in function ‘cvInitSystem’

Can you help me with this?

System
Raspberry pi 4 4GB
Raspian Buster up to date
I’m able to run the raspivid -t 0 line without any issues
Set up open CV without any issues

1 Like