A simple face recognition application based on a predefined model.

#15 Week of the Year

Florian Dahlitz
Coding experiences
Published in
4 min readApr 15, 2018

--

A week on the subject of image recognition and Python secrets everybody should know about.

Image and Video Recognition

This week I started with the focus on learning more about image recognition with a predefined model instead of a neural network. I already worked with OpenCV manipulating existing images and created a package for colour manipulation.

To keep things simple I searched for a predefined model, which would detect faces in an image if implemented correctly. I found a useful set of cascade classifiers and imported the one for face recognition. After that I made use of OpenCV’s multi-scale detection method detectMultiScale() to find all matching objects in a given image. Furthermore, I unpacked the results and drew red rectangles around the found coordinates marking the faces. One of the test results is shown in the thumbnail.

So far so good. But what about recognising faces and objects in videos? This seemed more powerful and interesting to me, so I started to find out, how to launch the webcam of my laptop, make a short video and to the same thing with every collected image as in the example before.

Luckily, OpenCV provides build-in functionalities for that as well. Turning one the webcam and collecting images as long as the user wants was very quite easy. Now the more difficult part impended: Recognising single objects in a series of images.

To solve the problem I set the first made image as my basic background frame. Every image collected afterwards was compared to the first one. This was done by simply calculating the difference between each frame. The result of the subtraction was a new frame. Applying certain thresholds to it created a black-white frame with new appearing objects in white and everything else in black. With the received coordinates of the new appearing objects I was able to draw a red rectangle around them and hence to mark moving objects.

The motion detection application is not perfect right now due to mis-detection of certain objects and under changing light conditions. But it was a great first try on this topic.

Python’s ABC-package

However, I did not only worked on image and video recognition, though. I made a quick excursion into Python’s object-oriented fields. Doing so, I came across the Abstract Base Class-package (ABC) providing great decorators for ensuring correct class implementations and inheritance.

Because code speaks more than words, I show you the beauty of the ABC-package in three pictures:

The above picture shows you, that with the correct implementation of ABC you are not able to run an application if an important method (in this case bar()) is missing.

This picture illustrates, that it’s not possible to instantiate the base class, which is quite helpful in many cases. In many applications and frameworks base classes shouldn’t be instantiated. Only concrete classes inheriting from the base class.

The last picture shows a working implementation with a base class and an instantiation of the concrete class.

Additionally, I learned about shallow and deep copies of objects and how to create them using Python’s copy-package. You should definitely check it out.

Additional Information

Besides the mentioned topics, I kept working on my personal portfolio page, listened to several podcast episodes and learned about Python’s __str__() and __repr__() dunder methods.

Summarising the past week I have to admit, that it was only another “light” week of coding. University stuff keeps me busy, but nevertheless I try to code everyday until the end of the #100DaysOfCode-challenge. Staying curious and keeping coding as always is a passion, that needs to be lived!

--

--

Florian Dahlitz
Coding experiences

Student, Developer, IBMer. Member of the RealPython.com team. Coding and sports are my passion. Python | C/C++ | Java