How to do it...

The steps for this recipe are:

  1. Import the modules we need:
import cv2import numpy as np
  1. 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)        for x, y, w, h in faces:            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3)            text_size, _ = cv2.getTextSize('Face', cv2.FONT_HERSHEY_SIMPLEX, 1, 2)            cv2.rectangle(frame, (x, y - text_size[1]), (x + text_size[0], y), (255, 255, 255), cv2.FILLED) cv2.putText(frame, ...

Get OpenCV 3 Computer Vision with Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.