import numpy as np
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 文件路径加载我们的模型。