import argparse
import cv2
construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument(“-i”, “–image”, required=True,
help=“path to input image”)
ap.add_argument(“-p”, “–prototxt”, required=True,
help=“path to Caffe ‘deploy’ prototxt file”)
ap.add_argument(“-m”, “–model”, required=True,
help=“path to Caffe pre-trained model”)
ap.add_argument(“-c”, “–confidence”, type=float, default=0.5,
help=“minimum probability to filter weak detections”)
args = vars(ap.parse_args())
导入所需的包并解析命令行参数。 我们有三个必需的参数:
–image :输入图像的路径。
–prototxt :Caffe prototxt 文件的路径。
–model :预训练 Caffe 模型的路径。
可选参数 --confidence 可以覆盖默认阈值 0.5。 从那里让我们加载我们的模型并从我们的图像创建一个 blob:
load our serialized model from disk
print(“[INFO] loading model…”)
net = cv2.dnn.readNetFromCaffe(args[“prototxt”], args[“model”])
load the input image and construct an input blob for the image
by resizing to a fixed 300x300 pixels and then normalizing it
image = cv2.imread(args[“image”])
(h, w) = image.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0,
(300, 300), (104.0, 177.0, 123.0))
首先,我们使用 --prototxt 和 --model 文件路径加载我们的模型。 我们将模型存储为 net(第 20 行)。
然后我们加载图像,提取尺寸,并创建一个 blob。 dnn.blobFromImage 负责预处理,包括设置 blob 尺寸和规范化。 如果您有兴趣了解有关 dnn.blobFromImage 函数的更多信息,我会在这篇博文中详细介绍。 接下来,我们将应用人脸检测:
pass the blob through the network and obtain the detections and
predictions
print(“[INFO] computing object detections…”)
net.setInput(blob)
detections = net.forward()
为了检测人脸,将 blob 通过网络。 然后我们将循环检测并在检测到的人脸周围绘