New to Image Recognition? Start with Interesting Face Recognition with Python!

by Allison Zhang

Image recognition now is a hot topic in machine learning, artificial intelligence and data analysis. But what is Image recognition?

Image recognition is the process of identifying and detecting objects, places, people, writing and actions in images or videos. Once we have the information from images, this information is good source data for data analysis.

Deep Learning

  • Image recognition is always connected  to deep learning. Deep Convolutional Neural Networks (CNN) is approved to be a very good approach to do object recognition in images.
  • Convolutional Neural Networks perceive images as three-dimensional objects based on the RGB, rather than a flat one-dimensional canvas ingested by bare eyes.
  • We will not discuss in depth  CNN and how it works because this blog is supposed to be interesting!

Face Recognition

Let’s use an interesting example to have a better understanding of deep learning. That is – face recognition!


First let’s see what will be used this time.

1. Python 3.6
2. Python site-packages: opencv-contrib-python (latest),
3. Haar Cascades from OpenCV3
4. WebCam

All the codes are based on the latest version of Python and OpenCV-python package.

Haar Cascades Classifier

In this experiment, we will use the built-in algorithm in OpenCV, which is called Haar Cascade. Haar Cascades is a machine learning based approach where a cascade function is trained from a lot of positive and negative images. It is then used to detect objects in other images.

When we work with face detection, the algorithm trains the classifier with a lot of images of faces and images without faces. Then the algorithm calculates features from all these images. Some of the features are useful, like nose, eyes and hair. Others are irrelevant, obviously. The Cascade of Classifiers can group these features into different stages of classifiers and applied one-by-one to keep only the features passes all stages. Then the face region!

This is just a simple explanation of Haar Cascades Classifier. Paul Viola and Michael Jones worked a lot on this study and if you have interest, you can dig further!

After we have all packages we need, we can start our experiment. We will follow the process to complete our experiment:

1. Face Detection: detect the face in the image or video
2. Data Gathering: Detect the unique characteristics of the face
3. Data Training: Train the computer remember the face and differentiate it from other faces
4. Face Recognition

Face Detection

First let’s test our camera to detect our face. If you have more than one camera connect to your device, please test with cv2.VideoCapture(N) to see which camera works for you.


Screen Shot 2018-04-03 at 9.13.54 AM.png

If you can see the “Square” is following you, then you made it!

Screen Shot 2018-04-03 at 9.15.06 AM.png

Data Gathering and Data Training

These two steps can be combined into one. Since data training is telling the machine how the person looks, we feed the machine basically every angle of our face. The more you feed the machine, the more accurate the result will be. But in this experiment, we used only ten camera captures to train the machine.

Screen Shot 2018-04-03 at 9.16.22 AM.png

Face Recognition

Finally, we can tell the machine who this is!

Screen Shot 2018-04-03 at 9.17.43 AM.png
Screen Shot 2018-04-03 at 9.18.03 AM.png

Combine with Raspberry Pi

What can bring more fun is that OpenCV face recognition can be played with Raspberry Pi! Now it is furthered to face tracking and such concept can be applied to a lot of real-life situations This time we explained from only one aspect of how you can do with images or video. But there are more situations where this can be applied on images recognition. When we talk about data, some people refer it only to numbers. We know data is more than that. And as long as we can have all the data from images, we can do any analysis we need!