Beginners’s Face detection using Python

In this article we’ll build face detection program using OpenCV and python.

HAMZA ABDULLAH
THE 21st CENTURY
4 min readAug 6, 2018

--

Here in this article we’re going to develop a program using python programming language and OpenCV a computer vision library. You may see the face detection and tracking systems quite in use now using ML techniques and it has its application in security, self driving car and object detection and tracking etc.

So, here we’ll develop the program to detect faces in the video from your webcam.

Requirements

For making such a program, first you need to install python on your machine on which you’re going to build this program.

And then open you command prompt “cmd” and type python -m pip install — user numpy scipy matplotlib ipython jupyter pandas sympy nose this command will install all the necessary python packages for building this program and also your future python developments.

python libraries installation using pip commands.

After the successful installation of necessary packages including numPy which is needed for this program to run too. Now install OpenCV on your local machine.

OpenCV installed files on the machine.

After the installation you’ll see above files on your machine. Now find the build folder inside you’ll find folder named python, copy file name “cv2.py” into site-packages folder inside the python27/python3.x in your C drive or where you installed the python.

Now find folder named “data” inside the sources folder.

OpenCV installed files on the machine. Find the haarcascades files inside the data folder inside the sources folder.

Then copy “haarcascade_frontalface_default.xml” into your working directory. Here in this case i installed it in my directory named “face detection system”.

face detection system

Now open IDLE (python) and type

import cv2

because to use OpenCV we need to import it first. Now import numpy as np.

import numpy as np

Diving in to build the face detection

So after everything is setup now let’s coding to build the face detection program which is like a OpenCV’s helloWorld because it can do a lot more.

The method we’re using here to build is cascade classifiers which we copied above to the project library where we’ll save this coding file too.

import cv2
import sys
import numpy as np
cascPath = sys.argv[0]faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')eyeCascase = cv2.CascadeClassifier('haarcascade_eye.xml')video_capture = cv2.VideoCapture(0)# Capture frame-by-frame
while True:
ret, frame = video_capture.read()gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)faces = faceCascade.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=5,minSize=(30, 30),)# Draw a rectangle around the facesfor (x, y, w, h) in faces:cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)# Display the resulting framecv2.imshow('Face detection system', frame)if cv2.waitKey(1) & 0xFF == ord('q'):break# When everything is done, release the capturevideo_capture.release()cv2.destroyAllWindows()

let’s break it down to better understand the above code. After importing the necessary library.

# Capture frame-by-frame
while True:
ret, frame = video_capture.read()gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)faces = faceCascade.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=5,minSize=(30, 30),)

Above lines of code will test the video file frame by frame and show the output video.

# When everything is done, release the capturevideo_capture.release()cv2.destroyAllWindows()

At the end, we need to release the capture and destroy all windows.

Output

So now run the program through python IDLE and here’s the output of the program which we done coding.

face detection: Output

So we successfully done coding face detection program, a first step in learning the ML: computer vision ( detection and tracking).

--

--

HAMZA ABDULLAH
THE 21st CENTURY

Driven by a futuristically optimistic vision, I am dedicated to transforming society through innovation, striving to become a Type 1 civilization.