Code for face detection

The following is the code for face detection:

import cv2 
import sys
import os

faceCascade = cv2.CascadeClassifier('C:/opencv/build/haarcascade_frontalface_default.xml')
video_capture = cv2.VideoCapture(0)
while (1):
# Capture frame-by-frame
ret, frame = video_capture.read()

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray, 1.3, 5)
# Draw a rectangle around the faces

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


# Display the resulting frame
cv2.imshow('Video', frame)

if cv2.waitKey(25) == 27:
video_capture.release()
break

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

Let's look at the code line by line:

import cv2 
import sys
import os

Import all the required modules:

faceCascade = cv2.CascadeClassifier('C:/opencv/build/haarcascade_frontalface_default.xml') 
video_capture = cv2.VideoCapture(0)

We select the cascade classifier file. Also we select the video capture device. Make sure you mention the path correctly:

ret, frame = video_capture.read() 
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

In the preceding lines, which are inside the infinite while loop, we read the video frame and convert it from RGB to grayscale:

faces = faceCascade.detectMultiScale(gray, 1.3, 5)  

The preceding line is the most important part of the code. We have actually applied the operation on the incoming feed.

detectMultiScale consists of three important parameters. It is a general function for detecting images and since we are applying the face haar cascade, therefore we are detecting faces:

  • The first parameter is the input image that needs to be processed. Here we have passed the grayscale version of the original image.
  • The second parameter is the scale factor, which provides us with the factor for the creation of a scale pyramid. Typically, around 1.01-1.5 is an appropriate one. The higher the value, the speed increases, but the accuracy decreases.
  •  The third parameter is minNeighbours which affects the quality of the detected regions. A higher value results in less detection. A range of 3-6 is good:
      # Draw a rectangle around the faces     
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

The preceding lines simply draw rectangles around the faces.

Finally, we display the resultant frame and use the keyboard interrupts to release the video capture device and destroy the window.

Now press F5 to run the code. Initially, it will ask to save the file, and then the execution will begin:

Screenshot of the image window with a face detected

Until now, if everything is carried out in a proper way, you must have a brief idea about face detection and how it can be accomplished using openCV. But now, we need to transfer it to the Intel Edison. Also we need to alter certain parts to meet the capabilities of the device as it doesn't have a display unit and above all it has a RAM of 1 GB.

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

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