Saturday, 1 May 2021

Python: Learning OpenCV and Detecting Faces from a Live Camera

The next step for my IOT project is to use facial recognition so that the Raspberry P can decide whether or not to alert the home owner.

I'll use OpenCV for this part. OpenCV is a very capable free package for computer vision and imaging.

OpenCV can be installed for Python and comes in four different options:

  1. Main modules: opencv-python
  2. Main modules with extra modules such as contributions from the opencv community: opencv-contrib-python
  3. Headless mode (no GUI modules): opencv-python-headless
  4. Headless mode with extra modules: opencv-contrib-python-headless

As I want to use it in a headless Raspberry Pi later, I'll go for the first option for development and the fourth option for deployment.

Detecting a face using openCv is a two step process:

1. Detect the faces in a picture

2. Identify a face from step 1. That will require a training set of some images of the person that shall be identified.

Face Detecton

OpenCV is using Haar Cascades to detect various objects such as faces, eyes, mouths and license plates for example. The models are available as xml files at the OpenCV Github repository and no machine learning training will be necessary for this step. 

After downloading the file haarcascade_frontalcatface to a local folder, my script will apply the Haar cascade model to a webcam session:


The Haar cascade algorithm is quite sensitive to noise. In the image below, five faces were detected, but only one face was authentic.

In the right region, some false faces were detected.


It is possible to reduce the risk of false faces by tweaking some parameters, but then the risk of missing authentic faces increases. Below are some faces of Hollywood celebrities that weren't detected by the algorithm:

It seems that the algorithm fails detecting faces that are tilting too much. Shadows in the faces can also confuse the algorithm.

In the next blog post. I'll try to train an existing algorithm to identify faces.

No comments:

Post a Comment