The steps for this recipe are as follows:
- Import the libraries:
import cv2
import numpy as np
import imutils
-
Import a neural network from the pretrained model in the Ch6 GitHub repo and then initialize OpenCV's camera operator:
net = cv2.dnn.readNetFromCaffe("deploy.prototxt.txt", "res10_300x300_ssd_iter_140000.caffemodel")
cap = cv2.VideoCapture(0)
-
Create a function that downsamples the image and transforms it into a predefined shape for our neural network and then perform the inference:
def FaceNN(frame):
frame = imutils.resize(frame, width=300, height=300)
(h, w) = frame.shape[:2]
blob = cv2.dnn.blobFromImage(frame, 1.0, (300, 300),
(103.93, 116.77, 123.68))
net.setInput(blob)
detections = net.forward()
-
Draw the bounding boxes:
for i in range(0, detections.shape[2]):
confidence = detections[0, 0, i, 2]
if confidence < .8:
continue
box = detections[0, 0, i, 3:7] * np...