How to do it...

You need to complete these steps:

  1. Import the modules:
import cv2import numpy as np
  1. Load the model and set the confidence threshold:
model = cv2.dnn.readNetFromCaffe('../data/face_detector/deploy.prototxt',                                  '../data/face_detector/res10_300x300_ssd_iter_140000.caffemodel')CONF_THR = 0.5
  1. Open the video:
video = cv2.VideoCapture('../data/faces.mp4')while True:    ret, frame = video.read()    if not ret: break
  1. Detect the faces in the current frame:
    h, w = frame.shape[0:2]    blob = cv2.dnn.blobFromImage(frame, 1, (300*w//h,300), (104,177,123), False)    model.setInput(blob)    output = model.forward()
  1. Visualize the results:
    for i in range(output.shape[2]):        conf = output[0,0,i,2]        if conf > CONF_THR:            label = output[0,0,i,1] x0,y0,x1,y1 ...

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.