Face detection

Now, before we go ahead and detect faces, we need to tell the robot what a face is and what it looks like. Raspberry Pi does not know how exactly to classify a face from a pumpkin. So firstly, we would be using a dataset to tell the robot what our face looks like; thereafter, we will start recognizing the faces as we go. So let's go ahead and see how to do it.

Firstly, you need to install a dependency called Haar-cascade. This is a cascade-dependent algorithm that is used to detect objects rapidly. To do this, go ahead and run the following syntax on your terminal:

git clone https://github.com/opencv/opencv/tree/master/data/haarcascades

This will save the haarcascades file onto your Raspberry Pi and you will be ready to use it. Once you are done, see the following code but write it over your Raspberry only after you have seen the following explanation line by line:

import cv2
import numpy as np

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

cap = cv2.VideoCapture(0)

while True:

ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray)

for (x,y,w,h) in faces:
cv2.rectangle(img, (x,y), (x+w, y+h), (255,0,0), 2)

cv2.imshow('img',img)

k = cv2.waitKey(1) & 0xff
if k == ord(‘q’):
break

cap.release()
cv2.destroyAllWindows()

Now, this might look like something out of our world and pretty much every thing is new, so let's understand what we are doing here:

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

Once we have installed Haar-cascade, we are basically taking in the data which is already trained onto our Raspberry Pi. In this line, we are opening a classifier, which is reading the data from a file named haarcascade_frontalface_default.xml. This is the file that will tell Raspberry Pi whether the image captured is a frontal face or not. This file has a trained dataset to enable the Raspberry to do so. Now, we are using a function of OpenCV called CascadeClassifier(), which uses this learned data from the file mentioned and then classifies the images:

cap = cv2.VideoCapture(0)

This will capture the video from the camera with the port number 0. So whenever the data needs to be captured, the variable cap can be used instead of writing the whole program.

        ret, img = cap.read()

We have understood this line in the previous chapter. It is simply capturing the image from the camera and saving it in the variable called img and then ret will return true if the capture is done or false if there is an error.

        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

We have used this line previously as well. What it is doing is, it is simply converting the captured image using the cv2.cvtColour() function. The arguments passed in it are the following img, which will basically tell which image needs to be converted. Thereafter, cv2.COLOR_BGR2GRAY will tell from which image type it has to be converted into what.

        faces = face_cascade.detectMultiScale(gray)

The face_cascade.detectMultiScale() function is a function of face_cascade. It detects the objects of various sizes and creates a rectangle of a similar size around it. The values returned to the variable faces would be the x and y coordinates of the object detected along with the width and height of the object as well. Hence, we need to define the size and position of the detected object.

        for (x,y,w,h) in faces:
cv2.rectangle(img, (x,y), (x+w, y+h), (255,0,0), 2)

In the previous line of code, we have taken the values of the position and the height of the rectangle. However, we still haven't drawn one in the actual picture. What this for loop will do is, it'll add a rectangle to the image using the cv2.rectangle() function. img is telling which image needs to be worked on. (x,y) is defining the starting coordinates of the position of the object. The value (x+w, y+h) is defining the end point of the rectangle. The value (255,0,0) is defining the color and argument 2 is defining the thickness of the line.

        cv2.imshow('img',img)

In this line of code, we simply use the imshow() function to give the final output, which will have the image overlayed by the rectangle we just drew. This will indicate that we have successfully identified the image. The 'img' argument will tell the name of the window and the second img will tell the function which image needs to be shown.

        k = cv2.waitKey(1) & 0xff
if k == ord(‘q’):
break

This line is simply waiting to take the key press of q. Whenever the user presses the q key, the if statement would become true, which will thereby break the infinite loop.

cap.release()
cv2.destroyAllWindows()

Finally, we are releasing the cameras using cap.release() and then closing all the windows using cv2.destoryAllWindows().

Now, go ahead and run the code and see whether it is able to detect your face or not. Good luck!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.175.182