Basic Face Recognition

I'm learning the OpenCV open source image processing library at the moment. For practice, I've put together a simple demo of basic face recognition.

Here is the output from when I gave it one of my photos and got it to draw a rectangle around the detected face:

Then, for fun, I got it to also read in a hat image, scale it, and position it above the face it had found:

Here are a couple more:


The face recognition algorithm used is called Haar Cascade. Apparently it's an older technique, and used in applications where speed and efficency matter more than accuracy. Playing around with it, I found it wasn't especially accurate. If the face was rotated more than a few degrees, or somewhat obscured by hair, then it would fail to detect it. Conversely, in some images it detected 'faces' that weren't actual faces.

There are thresholds you can tweak, and the numbers that worked best were different in each image I tried. So its applications felt pretty limited.

The data returned also seemed to be square in nature. The width and height of the detected face was always the same, which threw my hat scaling off. There was also no rotation data, which is why the hat positioning in the above images is extremely crude.

This mini-project might be reminiscent of augmented reality and those Snapchat filters that give you cat ears in real time, but to achieve proper face alignment of superimposed features would require a much more sophisticated approach than what I've done here.

It was useful for familiarising myself with OpenCV, though.



I've attached my code as a screenshot so as to preserve the VSCode colour formatting, since that makes it much easier to read.