Fun with faces
Now that we know how to detect and track faces, let's have some fun with it. When we capture a video stream from the webcam, we can overlay funny masks on top of our faces. It will look something like this next image:
If you are a fan of Hannibal, you can try this next one:
Let's look at the code to see how to overlay the skull mask on top of the face in the input video stream:
import cv2 import numpy as np face_cascade = cv2.CascadeClassifier('./cascade_files/haarcascade_frontalface_alt.xml') face_mask = cv2.imread('mask_hannibal.png') h_mask, w_mask = face_mask.shape[:2] if face_cascade.empty(): raise IOError('Unable to load the face cascade classifier xml file') cap = cv2.VideoCapture(0) scaling_factor = 0.5 while True: ret, frame = cap.read() frame = cv2.resize(frame, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) face_rects = face_cascade.detectMultiScale(gray, 1.3, 5) ...