How the code is organised
Python 2.7, numpy and opencv (v2 at the time of writing) are required to run this application which consists of 4 python scripts:
- camigo.py: Launches the application and presents the command line user interface.
- photographer.py: Takes a few pictures of known persons of interest standing alone in front of the camera. It prompts to enter the person name (avoid spaces) and saves the pictures in a folder named after that person. This procedure should be repeated to setup additional known persons.
- trainer.py: Processes the pictures of each known person and associates a label (the person's name) to each set.
- watcher.py: Tries to identify faces based on the known persons pictures that were used to train it.
The source code is available here.
The application can be run by typing the command below in a terminal window after switching to the directory containing the above scripts:
A command line interface with 4 options are then shown to the user:
- take photo: Takes pictures of known people and associate a label (the name of the person) with that picture set. The application will prompt to enter the name of the person. The user has to sit in front of the camera and make natural facial expressions while it takes about 30 pictures which are saved in a directory named after that person. Selecting this option for the same user multiple times simply adds more pictures to his directory and increases the accuracy of the identification process. To add a different known person, simply repeat this process with a different name and person sitting in front of the camera.
- train system: Allows the system to digest and classify the pictures into an internal representation. It will then use this classification to identify faces when the system is armed if anything resembling a face comes into its field of view.
- arm system: Starts the camera in surveillance mode and tries to recognise faces based on the classification created using the pictures of known persons. It draws a rectangle around the faces with the person's name if it is able to identify the face. Otherwise it displays the label "intruder". It also displays the confidence level as a number - the lower the number the higher the confidence level.
- exit: Exits the application
Armed System in Action
The image below shows the armed system in surveillance mode with two persons in its field of view. It recognises only one of them and assigns a label and confidence level.
A couple of enhancements can be made to this application:
- Increase the accuracy (confidence level)
- Detecting faces when they are slightly tilted
- Detecting features other than faces
An interesting use case is to set it up as a surveillance camera and adapt it to send sms, emails, notification (with attached images) based on certain events such as intruder detection (unrecognised faces), motion detection (with sensors) using a raspberry pi thereby creating a smart surveillance camera.
For detailed explanations of uses of OpenCV and image recognition, please refer to these links: