Face Recognition From Scratch Using Opencv and Python(Part 1 )

Source: Deep Learning on Medium

Face Recognition From Scratch Using Opencv and Python(Part 1 )

Build Your Own Face Detector with Opencv and Python

Face recognition has been one of the most interesting and important research fields in the past two decades. The reasons come from the need of automatic recognitions and surveillance systems, the interest in human visual system on face recognition, and the design of human-computer interface, etc. These researches involve knowledge and researchers from disciplines such as neuroscience, psychology, computer vision, pattern recognition, image processing, and machine learning, etc.

In this article, we are going to divide the work into 4 sections to well understand the inner workings of face recognition .In yhis Part we focus on setting up the environment and Downloading dependencies .

Table of contents:

  • Install Python 3
  • Install Opencv 3
  • Do some codes

Before starting, I am assuming you already has a good knowledge in python, and if you are not confident here some professional notes to build your background in python first, this will help you to understand things later on .

So without wasting anymore time lets move to the main stuff.

I -Installing Python 3 :

Installing and using Python on Windows 10 is very simple. The installation procedure involves just three steps:

  1. Download the binaries
  2. Run the Executable installer
  3. Add Python to PATH environmental variables

To install Python, you need to download the official Python executable installer. Next, you need to run this installer and complete the installation steps. Finally, you can configure the PATH variable to use python from the command line.

You can choose the version of Python you wish to install. It is recommended to install the latest version of Python, which is 3.7.3 at the time of writing this article.Open the official Python website in your web browser. Navigate to the Downloads tab for Windows.

  1. Open the official Python website in your web browser. Navigate to the Downloads tab for Windows.
  2. Choose the latest Python 3 release. In our example, we choose the latest Python version.
  3. Click on the link to download Windows x86 executable installer if you are using a 32-bit installer. In case your Windows installation is a 64-bit system, then download Windows x86–64 executable installer.

Installing Python on Linux machines is more simple , it just needs two command lines , open a command prompt and run :


$ sudo apt-get update
$ sudo apt-get install python3

and that’s all , Python is here !

now you can choose your favourite Editor , I recommend Pycharm from Jetbrains .

let’s move and Download Opencv :

II -Installing Opencv :

Opencv is the most popular computer vision library, and today we are going to learn how to setup opencv, how to access your webcam and how easily we can write a face detection program with just a few lines of code.

1 -On Windows Platform :

Here below you see a list of the steps necessary for a complete installation.

Install Opencv : Download the Opencv binary files here: https://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv

Choose the compatible version of Opencv to your Python. We’ll download the lastest version of Opencv (4.1) for Python 3.6.8.
How to understand the right version?
-cp37m-win32.whl means that Opencv is for python 3.7 (32 bit version)
-cp36m-win_amd64.whl means that Opencv is for python 3.6 (64 bit version)

Run the “Command Prompt” of Windows. You can type “CMD” on the sarch bar to find it.

Go to the directory where the Opencv binaries that you downloaded is located.

cd YOURPATHfor example in my case:

cd C:\Users\Yassine\Downloads

Now let’s install the Opencv module using PIP.

python pip -m install opencv_python‑4.0.1+contrib‑cp37‑cp36m‑win_amd64.whl

Now let’s install Numpy using PIP

python pip -m install numpy

The installation is complete. Now we can test it by running python and importing the libraries cv2 (for opencv) and numpy. If we don’t get any error it means that it has been installed succesfully.

2 -On Linux Platform :

In Linux platform it need some additional libraries to be compatible with linux kernel , In this guide you will find a detailed description that helps you succesfully installed opencv with python/ c++ .

to test the opencv installation just open the terminal :

Type :

$ python3

then :

Now that everything is setup and running lets write a code to detect faces from the webcam.
This is kind of hello world program for opencv

III -Do Some Codes :

The method that we are going to use in this program is a cascade classifier, which can be loaded with a pretrained xml file, these xml files are hard to train but luckily we dont have to worry opencv already has many of these pretrained classifier ready for face detection

To use the face detection classifier we need to copy the classifier xml file from the [opencv extracted folder]/sources/data/haarcascades/

and then copy the file haarcascade_frontalface_default.xml to your project folder (same location where you will save the python program)

we can load the classifier now

detector=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap=cv2.VideoCapture(0) # add the video capture object now