Saturday, September 26, 2020

Face Detection using Python and OpenCv

Contents

  1. Introduction
  2. Getting Started
  3. Source-code
  4. Explanation

Introduction: Face-Detection Using OpenCV

In this project, we are going to build a program using python and OpenCV library that detects the faces in real-time. This project is completely for the beginners who are new to python or openCv, so in order to keep it simple, we are going to use the pre-trained models available and implement those using OpenCV ( cv2 ).

Getting Started:

Before jumping into the coding section, make sure you have downloaded the OpenCV library on your system or to the project file.

To download the OpenCV library type the following command in the command prompt (terminal in case of LINUX system) 

pip install cv2

If the pip command is not working or it is not installed in your system then click here


Source Code: Face-Detection Using Python

import cv2
webcam=cv2.VideoCapture(1)
Cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
while True:
    success,imgweb=webcam.read()
    faces = Cascade.detectMultiScale(imgweb,1.4,6)
    for (x,y,w,h) in faces:
        cv2.rectangle(imgweb,(x,y),(x+w,y+h),(0,255,0),3)
    cv2.imshow("faces",imgweb)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break


Explanation:


import cv2: In this line, we are importing the OpenCV library into our program in order to use its functionalities. 

webcam=cv2.VideoCapture(1): cv2 has a method named VideoCapture( ), this method is used to get a video capture object that allows us to read the live webcams footage and process them to get the desired result, this methods take webcam id as an argument, in this project, I am using my primary webcam whose ID is '1' and IDs may vary but generally the default webcams have the ID '1' or '0'.
Further, we have stored the video capture object in a user-defined variable 'webcam'.

Cascade=cv2.CascadeClassifier("haarcascade_frontalface_default.xml"): In this line of code we are calling a method named CascadeClassifier that reads the pre-trained ML models and loads it. In this function, we have to pass the 'model' file location and it will return an object of type <class 'cv2.CascadeClassifier'>, we will use this classifier to detect the faces.


what is"haarcascade_frontalface_default.xml"? 

This is a pre-trained model to detect faces. This model has been trained using a machine learning algorithm called Haar Cascade. This algorithm is generally used to build object detection models and it was first proposed by Paul Viola and Michael Jones.
In this algorithm, the model is trained using data sets that contain a lot of positive and negative images.
  • Positive Images: For example in this project we need a model that detects the presence of faces in the given image, so the positive images will contain all of the images with faces. In other words, this image set will contain the object which we want to classify or detect.
  • Negative Images: This image set will contain everything else except the object that we want to detect.
To keep this article beginner-friendly let's not dive into the depth of machine learning any further. 
So, download the "haarcascade_frontalface_default.xml" model from here and save it in the project's directory.

Reading frames from the webcam: To read the images we are going to use the VideoCapture object "webcam" that we have created earlier. We are going to call a function named 'read( )' which is a member function of class "VideoCaputre". This function returns a tuple of two values of type "bool" and "numpy.ndarray", The bool value checks whether the reading was successful or not and the object of type "numpy.ndarray" will store the frame (if the reading was successful). This function returns the values for a single frame (or image) whenever it is called, so in order to get a continuous feed from the webcam, we have to call this function in a loop. So in this case we are using a while-loop and called the function inside it( success,imgweb=webcam.read( ) )

faces = Cascade.detectMultiScale(imgweb,1.4,6): The above statement will give us a single image/frame in each iteration, So now our task is to detect the faces in each frame/image.
To do this we are calling a method named "detectMultiScale( )" from class: "cv2.CascadeClassifier" using the object 'Cascade' that we have created earlier. In this method, we have passed 3 arguments "image/frame", "Scale Factor", "minimum neighbor"(scale factor and the minimum neighbor determine the sensitivity of the classifier). And it returns the list of tuples that contains four values (x,y,w,h) of a rectangle and I have stored them in a variable named "faces". Each of the rectangles encloses a face.
  1. x: Staring coordinate (X-axis)
  2. y: Starting coordinate (Y-axis)
  3. w: Width of the rectangle
  4. h: Height of the rectangle
Now once we have got the list of tuples, our task is to iterate through the list and get the "x, y, w, h" values of each detected rectangles which we have done using a for-loop "for (x,y,w,h) in faces:", Our next step is to draw the rectangles one by one on the frame/image in order to highlight the detected faces and it can be done using a method named "cv2.rectangle( )".

cv2.rectangle(imgweb,(x,y),(x+w,y+h),(0,255,0),3):
Using this method we can draw rectangles on any image,
we have passed 5 arguments.
  1. "imgweb" : the image on which the rectangle will be drawn
  2. (x,y): this tuple contains the starting coordinates of the rectangle(one end of the diagonal).
  3. (x+w,y+h): this tuple will contain the coordinate of the other end of the diagonal.
  4. (0,255,0): this tuple determines the color(green) of the rectangle, It holds the BGR values.
  5.  the last argument determines the thickness of the edges of the rectangle.
rectangle
We have passed the coordinate of these two red points in the function


Once all of the rectangles are drawn we can display the processed frame/image on the screen.
To do this we are calling a method named "cv2.imshow( )".
This method takes two arguments:
  1. Name of the Window
  2. The image that we want to display
At this point, we are almost done with our code, in the last portion we have to write an exit condition as our entire program is running in an infinite while-loop.
To do this we use the following code.

if cv2.waitKey(1) & 0xFF == ord('q'):  This if condition checks if the keyboard button "q" is pressed or not. So if we press q it will execute the following break statement and exit the program.
And that's it, now you can modify it in your way or you can try adding new things to it and implement it in the way you want.

No comments:

Post a Comment