To secure our assets and protect our privacy, there is a strong demand for user-friendly biometric security systems. There are various types of biometric systems that use signatures, fingerprints, voice, hand geometry, ear geometry, face detection and so on. Among these, face recognition appears to be quite exciting and is catching attention.
Circuit and working
This project requires Raspberry Pi B+, Raspberry Pi camera, a pushbutton switch, a relay and some miscellaneous components.
Face images are captured through Raspberry Pi camera and stored in a database in Raspberry Pi. To capture your face image, place yourself in front of the Pi camera and press pushbutton switch S1. The image of your face will get stored in the database. Next time when you face Pi camera and press S1, your face will be recognised, relay RL1 will be energised and your electrical load/solenoid will be activated.
Software
This project uses OpenCV computer vision library to perform face detection and recognition.
First, install OpenCV dependencies. Compiling OpenCV on Raspberry Pi may take about five hours (depending on your system and network speed). So make sure you have sufficient time to start the process before proceeding.
Power on Raspberry Pi, open the terminal, set up Wi-Fi and execute the following commands:
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install build-essential
cmake pkg-config python-dev libgtk2.0-
dev libgtk2.0 zlib1g-dev libpng-dev
libjpeg-dev libtiff-dev libjasper-dev
libavcodec-dev swig unzip
Select yes for all options and wait for the libraries and dependencies to be installed.
Now, unzip OpenCV directory by executing the following commands:
$ wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.4.9/opencv-2.4.9.zip
$ unzip opencv-2.4.9.zip
Change the directory and execute cmake command as given below to build the makefile:
$ cd opencv-2.4.9
$ cmake -DCMAKE_BUILD_TYPE=RELEASE
-DCMAKE_INSTALL_PREFIX=/usr/local
-DBUILD_PERF_TESTS=OFF -DBUILD_opencv_
gpu=OFF -DBUILD_opencv_ocl=OFF
Compile the project by executing the command given below:
$ make
It may take about five hours for compilation.
Install the compiled OpenCV libraries by executing the following command:
$ sudo make install
The latest version of OpenCV is now installed on your Raspberry Pi.
Face-recognition code is written in Python, so some dependencies have to be installed using the following commands:
$ sudo apt-get install python-pip
$ sudo apt-get install python-dev
$ sudo pip install picamera
$ sudo pip install rpio
After OpenCV and Python dependencies are installed, the project can be tested in three major steps as explained below.
Software testing
This project uses EIGENFACES ALGORITHM IN OPENCV to perform face recognition. To use this algorithm, create a set of training data with pictures of faces that are allowed to trigger the relay.
Follow the steps given below:
Execute the following command to run capture-positives script to find a single face image:
$ sudo python capture-positives.py
Wait for some time and observe the terminal until you see Press Button instruction. Press S1 to capture your face image. If the script detects a single face, it will crop and save the training image in positive sub-directory.
If the script cannot detect a face, or detects multiple faces, error message ‘Could not detect single face! Check the image in capture.pgm’ will be displayed. It is recommended to maintain a distance of about 0.5 metres from the camera while taking a picture.
Press Ctrl+C to stop the script. Open capture.pgm file to view the last captured image.
Check the face in the database and train the face recogniser by running train.py code:
$ sudo python train.py
Training the face-recognition model on Raspberry Pi will take about ten minutes. Once training is complete, you will see mean.png and positive_eigenface.png files to visualise Eigenfaces of the model.
Now, test the face recogniser to recognise the face trained earlier. Execute the following command:
$ sudo python box.py
Observe the terminal to see Press Button instruction. Aim the camera at your face and press S1. You should see a message ‘Button pressed, looking for face…’ on the terminal. After a few seconds, if the face is recognised, you will see the message ‘Recognised face!’ and relay RL1 will be energised.
You can connect an electrical load across the relay contacts at CON1. A 12V solenoid lock may be used as electrical load during testing. If your face is recognised or detected, relay is energised, solenoid lock is activated and the secured door gets opened.
Download source code
Biswajit Das was manager – R&D, EFY Labs, till recently
Dear Sir,
I had installed opencv by executing your commands and i am in cv environment but i am getting error in software testing codes it shows bash error please help me
waiting for your response
hi
i am using raspberry pi 3 B, i did the same way you said but gave an error like this
sudo python capture-positives.py
Traceback (most recent call last):
File “capture-positives.py”, line 14, in
import hardware
File “/home/pi/hardware.py”, line 8, in
import RPIO
File “build/bdist.linux-armv7l/egg/RPIO/__init__.py”, line 115, in
File “build/bdist.linux-armv7l/egg/RPIO/_GPIO.py”, line 7, in
File “build/bdist.linux-armv7l/egg/RPIO/_GPIO.py”, line 6, in __bootstrap__
SystemError: This module can only be run on a Raspberry Pi!
what should i do ? please help me .
Hi, I am having the same problem , have you been able to figure out the issue?
Ese error aparece debido al paquete RPi.GPIO seguramente debe ser la versión.
¿PodrÃa por favor elaborar su consulta?
Hi Has any one got this project working
While import RPIO._GPIO as _GPIO
I am Getting SystemError:
Please suggest how to resolve this
you need to install wiring pi
http://wiringpi.com/download-and-install/
Please give solution of upper decribed error….. Please
please tell us how ????
I have done it and its working. Thanks. But, please, how can I match faces with names such that it prints the person’s name upon recognition? Thanks
which code we will take to compile for execution please help me
Can you help me?
please share solution for System Error: This module can only be run on a Raspberry Pi!
Sir, I am also having the same question sir. Please tell me how do it? If you had foung them.
Thanks in advance.
can you please guide me in making this project?
Kindly elaborate your query.
i am getting few errors.
first i got this error in train.py:
File “train.py”, line 77, in
model = cv2.createEigenFaceRecognizer()
AttributeError: ‘module’ object has no attribute ‘createEigenFaceRecognizer’
but i removed this error by modifying the following line:
model = cv2.face.createEigenFaceRecognizer()
now i am getting another error:
File “train.py”, line 85, in
mean = model.getMat(“mean”).reshape(faces[0].shape)
AttributeError: ‘cv2.face_BasicFaceRecognizer’ object has no attribute ‘getMat’
how can i remove this error?i am using opencv 3.0
hi. i am getting an error in box.py
File “box.py”, line 50, in
label, confidence = model.predict(crop)
TypeError: ‘int’ object is not iterable
i am using opencv3..i was able to run the other two files but i cant get rid of this error..please help me.
Hi EFY Team..
while trying this DIY I am getting error please guide me…
cmake -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local -DBUILD_PERF_TESTS=OFF -DBUILD_opencv_gpu=OFF -DBUILD_opencv_ocl=OFF
bash: cmake: command not found
run this in your rpi-terminal
sudo apt-get install cmake
Hello, can I use the same tutorial to do it with a logitec camera ?, replacing the py camera
You can change the camera source by making the following changes
step 1-
open your source code folder
step 2-
open config.py with your favourate editor
find the below lines in your config.py file
def get_camera():
# Camera to use for capturing images.
# Use this code for capturing from the Pi camera:
import picam
return picam.OpenCVCapture()
# Use this code for capturing from a webcam:
# import webcam
# return webcam.OpenCVCapture(device_id=0)
now change these lines to :
def get_camera():
# Camera to use for capturing images.
# Use this code for capturing from the Pi camera:
#import picam
#return picam.OpenCVCapture()
# Use this code for capturing from a webcam:
import webcam
return webcam.OpenCVCapture(device_id=0)
a couple of questions:
1. does this project also supports open cv 3
2. do you have a 3d model of the case that will hold the pi with the button and camera?
sir, can you give the detailed description of this project and please give the full details of components used with the purposes they serve?
Hi Arun, all the information is present in the article itself.
Traceback (most recent call last):
File “/home/pi/Downloads/face controlled appliance/Face Recognition Appliance Control using Raspberry Pi/box.py”, line 6, in
import RPIO
ImportError: No module named ‘RPIO’
sir please help
Please see in here
https://learn.adafruit.com/raspberry-pi-face-recognition-treasure-box/overview
can you please make a video of this whole system. it would be very greatful.
Hello.. can I use USB Camera ?
Short answer Yes. Long answer:- You have to check the camera compatibility and drivers it supported in Linux or not. Then you need to do setting and find the camera and make a change in code to get a video of the same camera.