Building a face detector application
In this section, we discuss how human faces can be detected from webcam images. A USB webcam needs to be connected to Raspberry Pi 3 to implement real-time human face detection.
How to do it...
- Import the necessary packages:
import cv2 import numpy as np
- Load the face cascade file:
frontalface_cascade= cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
- Check whether the face cascade file has been loaded:
if frontalface_cascade.empty(): raiseIOError('Unable to load the face cascade classifier xml file')
- Initialize the video capture object:
capture = cv2.VideoCapture(0)
- Define the scaling factor:
scale_factor = 0.5
- Perform the operation until the Esc key is pressed:
# Loop until you hit the Esc key while True:
- Capture the current frame and resize it:
ret, frame = capture.read() frame = cv2.resize(frame, None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_AREA)
- Convert the image frame into grayscale:
gray_image = cv2.cvtColor...