Detecting faces using Haar/LBP cascades
How often have you been impressed with your phone or digital camera when faces on the photo have been detected? There's no doubt you want to implement something similar on your own, or incorporate a face detection feature in your algorithms. This recipe shows how you can easily repeat this using OpenCV. Let's get started.
Getting ready
Before you proceed with this recipe, you need to install the OpenCV 3.x Python API package.
How to do it...
The steps for this recipe are:
- Import the modules we need:
import cv2 import numpy as np
- Define the function that opens a video file, invokes a detector to find all of the faces in the image, and displays the results:
def detect_faces(video_file, detector, win_title): cap = cv2.VideoCapture(video_file) while True: status_cap, frame = cap.read() if not status_cap: break gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = detector.detectMultiScale(gray, 1.3, 5) ...