The steps for this recipe are:
- Import the modules we need:
import cv2import 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) 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, ...