Facial Recognition With Raspberry Pi and OpenCV

Hi
So I did an installation on a new SD card. Latest Raspbian distribution in 64 Bits without any other installation.

I installed OpenCv, I using the following procedure

# check for updates
$ sudo apt-get update
$ sudo apt-get upgrade
# dependencies
$ sudo apt-get install build-essential cmake git unzip pkg-config
$ sudo apt-get install libjpeg-dev libpng-dev
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
$ sudo apt-get install libgtk2.0-dev libcanberra-gtk* libgtk-3-dev
$ sudo apt-get install libgstreamer1.0-dev gstreamer1.0-gtk3
$ sudo apt-get install libgstreamer-plugins-base1.0-dev gstreamer1.0-gl
$ sudo apt-get install libxvidcore-dev libx264-dev
$ sudo apt-get install python3-dev python3-numpy python3-pip
$ sudo apt-get install libtbb2 libtbb-dev libdc1394-22-dev
$ sudo apt-get install libv4l-dev v4l-utils
$ sudo apt-get install libopenblas-dev libatlas-base-dev libblas-dev
$ sudo apt-get install liblapack-dev gfortran libhdf5-dev
$ sudo apt-get install libprotobuf-dev libgoogle-glog-dev libgflags-dev
$ sudo apt-get install protobuf-compiler

And

$ git clone --degree = 1 https://github.com/opencv/opencv.git
$ git clone --degree=1 https://github.com/opencv/opencv_contrib.git

and cmake

$ 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 WITH_OPENMP=ON \
-D WITH_OPENCL=OFF \
-D BUILD_TIFF=ON \
-D WITH_FFMPEG=ON \
-D WITH_TBB=ON \
-D BUILD_TBB=ON \
-D WITH_GSTREAMER=ON \
-D BUILD_TESTS=OFF \
-D WITH_EIGEN=OFF \
-D WITH_V4L=ON \
-D WITH_LIBV4L=ON \
-D WITH_VTK=OFF \
-D WITH_QT=OFF \
-D WITH_PROTOBUF=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D OPENCV_FORCE_LIBATOMIC_COMPILER_CHECK=1 \
-D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D BUILD_EXAMPLES=OFF ..

And for finishing installation

make -j4
$ sudo make install
$ sudo ldconfig

I tested my program unfortunately without conclusive results.
the film is identical to that obtained with my old and complete installation.
It’s strange … :thinking:
Regards
François

Man this is tough! What does htop say now?

Hi
You will find below screenshot before launching python program

And here during camera capture with the python program

regards
François

Hi
I think that the problème is due to the opencv adaptability with the raspbian version used.
I will think about:
either change linux distribution (but I will face other problems on other applications used on raspberry)
either change my script regarding the movment and saving detection
regards
François

Hey mate, sorry I got distracted.

opencv version.

Maybe, consider jumping back to a previous version of opencv that is compatible?

scraping the code.

Seeing the new htop prints makes me want to see the exactly how much cpu and ram this program wants to eat up. Remember when you made it run on your windows machine. I’d start commenting out parts of the code to find exactly where the resources are going.

Revisiting video encoding.

I also want to revisit this.
Before we were not sure what was causing the problem, so when we saw the encoding work on the windows machine we put this idea to bed.
BUT now we think its processor-power related.
A quality encoding can be cpu intensive, but you’re windows machine would not have noticed. It may be that a different, light-weight encoding will reduce the cpu footprint and allow the program to run smoothly on the PI.

Pix :heavy_heart_exclamation:

1 Like

hello i am having trouble with this error can you share the solution? Thank you.

1 Like

Hi Pixmusix
So I have tested swapping the encoder the result is the same way whatever the configuration (basic OS with applications or cleaned OS without any applications)

Jumping back to previous version of opencv seems a good alternative but which version should I choose? And which method ( Apt or pip ) ? But before that I want to test the possibility with a virtual environment do you think that a good idea ?

the objective of my script is to detect the comings and goings of animals in my garden (birds, squirrels, etc.) in order to carry out some tests of the intelligence of individuals on different practical exercises, particularly on crows and magpies. I thought with opencv I could sort individuals and sort my captures. Maybe in the end just detecting a movement would be enough. I will sort it manually after capture on the computer.

thanks you for your help
regards
François

@JohnAldrin265147 : What is your problem exactly , the same I have on my raspberry ? Because for the moment we have no solution.
Which raspberry do you use?
Which OS environment?
Which camera and connection?
briefly resume the progress of @Pixmusix above … :face_with_monocle:
Regards

1 Like

Super tough.
Not sure which version. At this point, without the same setup as you to debug, I’m just working through my debugging checklist and hoping something works :man_shrugging:. Trying to think outside the box with you. :slight_smile:

Think about reducing the frame rate of the capture.
I’m imagining a function like the below.

if detection and 1000_millis_elapsed_since_last_capture():
        out.write(frame)

If you would like to peruse this I’d suggest using pickle. You can pickle the out object and save that on disk. Then un-pickle them on a powerful computer for the rendering. Pickling is fast, and uses very small amounts of ram and rom.

Pix :heavy_heart_exclamation:

1 Like

Hi
Finally after many tests I think that Opencv is not adapted for raspberry Pi ( Opencv application requires too many resources)
So I have changed by lighter application MotionEye whose results suit me perfectly
thanks you very much for your help
Regards
François

2 Likes

Well done @Fr264583! :partying_face:

1 Like

I have a small issue not a big one. when running the facial_req.py file there is no yellow box showing up as mentioned and there is no error in the terminal as well. i am not sure as to what the error could be.

I have loved this tutorial so far and is is very interesting.

1 Like

In you video here, you tracked faces.
Can I use the same code, but modify it to track fire instead.

Hi @NhyiraKwabena268414
Good news!
Seems like the repo this tutorial is inspired by is under an MIT licence. :slight_smile:

This licence allows you to freely copy and modify any code or assets that falls under it.
That means you can fork the repository and rework it to detect fire. :+1:
Good luck.
Pix :heavy_heart_exclamation:

Hi @Pixmusix
I’m trying to run this command to train the model:

pi@raspberrypi:~/facial_recognition $ python train_model.py
Traceback (most recent call last):
File “train_model.py”, line 5, in
import face_recognition
ImportError: No module named face_recognition

As was said in the tutorial of Caroline Dunn, in this case we need to reinstall face-recognition.

If I run this with command | pip install face-recognition | I get every time follow error:

… Building wheels for collected packages: dlib
Running setup.py bdist_wheel for dlib … error
…CMake Error at /usr/share/cmake-3.16/Modules/FindPackageHandleStandardArgs.cmake:146 (message):
Could NOT find PythonInterp: Found unsuitable version “2.7.16”, but
** required is at least “3.6” (found /usr/bin/python)**

If I run this with command | pip3 install face-recognition | installation is successful, but train_model.py said me that face-recognition module is missing.

Could you help me understand where is the problem here?

1 Like

So, it was self resolved))
I made | pi@raspberrypi:~/facial_recognition $ sudo apt-get install libopenblas-dev libatlas-base-dev libblas-dev |
After that performed one more time | pip3 install face-recognition |

and installed all imutils and ipiputils (see post #16 from Nathan)

and after that run train_model.py through Thonny and it was successful.

Thank you)

1 Like

Nice work @Max269679
Sorry i hadnt had a chance to look into it.
Thanks for letting us know, stoked you found a solution.
Happy coding.


i have same issue

Can this same experiment be done using an external webcam like logitech or the Raspberry pi camera v1.3 ?

Hi Tim! may i ask if this project can still function using another version of OS?

cmake -D CMAKE_BUILD_TYPE=RELEASE \