In this project, we will create a basic face recognition program using Python and OpenCV
- To recognize the face in the camera and name it.
- If the face is unknown send a text alert with the image to your mobile via Whatsapp.
- Thank You @RamizRaza for great explanation of face recognition
- Also thanks to @mjrovai for his blog on real-time face recognition
- Laptop with inbuilt camera or a webcam
- Python with OpenCV, numpy, os and pillow libraries installed
To create a face recognition program, 4 different phases are:
- Face detection and gathering data
- Training the recognizer
- Recognizing the face
- If unknown, send a text to your mobile via whatsapp(Part of phase 3)
In command prompt, type the following commands:
pip install opencv_python
pip install opencv_contrib
Other libraries to install are:
pip install os
pip install pillow
pip install twilio
pip install cloudinary
pip install numpy
You need to create an account on twilio and on cloudinary
To know about the phases of face recognition, read the blog by Ramiz Raza
Give this a go!! Saying that lets move on to Phase-1
Run the first python script face_add.py
After executing the following program, 35 images of one face will be added to gather enough data to recognize the face. Run the script few times, each to capture one single id Make sure to enter the user input id in integers starting from 1, 2 and so on...
You will get to name em later! Moving on to Phase-2
Run the python script face_trainer.py
Make sure you take the current path of the images saved earlier in Data Gathering. At the end you should get the number of faces trained equal to the faces you captured in face_add.py
As a result, a file named "trainer.yml" will be saved in the path you entered.
Note: Everytime you run face_add.py, you have to run face_trainer.py also to implement the changes.
Moving on to Phase-3 for Face Recognition
In the final phase of the project, following steps will be executed:
- Capture a fresh face on our camera.
- Face captured and trained before wil be recognized.
- Recognizer() will make a "prediction" returning its id and an index, showing how confident the recognizer is with this match.
- Name of the face will be taken from the list names which has names indexed according to the userid you added in face_add.py.
- Predicted face has a text over it and "probability" in % showing match is correct ("probability" = 100 - confidence index).
Note that the confidence index will return "zero" if it will be cosidered a perfect match.
If the face detetected is not known, move on to phase-4
Dealing with unknown faceThe unknown face has an "unknow" label above it and then two steps taken are:
- Image saved is passed to func uploadimg() in face_recog.py.
- Uploaded image is sent as an MMS using func sendmsg() in face_recog.py.
Before running the python script, do:
- Enter your api key, cloud name and api key from the account created in cloudinary in uploadimg().
- Enter your authorization id and authorization key from account in twilio. Also, add whatsapp number in sendmsg().
- Follow Whatsapp Twilio and register your mobile on twilio whatsapp.
- Send “join your_sandbox_keyword” to your Sandbox number in WhatsApp to join your Sandbox
- Make sure to send your keyword everytime you receive an image to avoid getting the next message with the old image from cache memory.
Finally, run the final python script face_recog.py to get the desired result!
Thats it for this project
Thank You
Vansh