load our serialized model from disk
print(“[INFO] loading model…”)
net = cv2.dnn.readNetFromCaffe(prototxt, model_path)
加载输入图像并为图像构造一个输入blob
将大小调整为固定的300x300像素。
(注意:SSD模型的输入是300x300像素)
image = cv2.imread(image_name)
(h, w) = image.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 0.007843,
(300, 300), 127.5)
通过网络传递blob并获得检测结果和
预测
print(“[INFO] computing object detections…”)
net.setInput(blob)
detections = net.forward()
从磁盘加载模型。
读取图片。
提取高度和宽度(第 35 行),并从图像中计算一个 300 x 300 像素的 blob。
将blob放入神经网络。
计算输入的前向传递,将结果存储为 detections。
循环检测结果
for i in np.arange(0, detections.shape[2]):
提取与数据相关的置信度(即概率)
预测
confidence = detections[0, 0, i, 2]
通过确保“置信度”来过滤掉弱检测
大于最小置信度
if confidence > confidence_ta:
从detections
中提取类标签的索引,
然后计算物体边界框的 (x, y) 坐标
idx = int(detections[0, 0, i, 1])
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype(“int”)
显示预测
label = “{}: {:.2f}%”.format(CLASSES[idx], confidence * 100)
print(“[INFO] {}”.format(label))
cv2.rectangle(image, (startX, startY), (endX, endY),
COLORS[idx], 2)
y = startY - 15 if startY - 15 > 15 else startY + 15
cv2.putText(image, label, (startX, y),