【发布时间】:2020-12-16 01:52:07
【问题描述】:
net = cv2.dnn.readNetFromCaffe(prototxt, model)
detections = net.forward()
检测将具有形状为 (1, 1, 200, 7) 的 4D 数组。那里有什么不同的价值观?
for i in range(0, detections.shape[2]):
confidence = detections[0, 0, i, 2]
上面的循环在第 3 维获取置信度值,提供的是行号,第 4 维是列号,因此很明显,物体以如此高的置信度或概率被检测到。但我无法理解其他参数。
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
要创建一个框,上面的代码用于 3 到 6 列及其值。那么第 2 到第 6 列中的这些值是什么?
下面的代码重现值...
from imutils.video import VideoStream
import imutils
import numpy as np
import cv2
import argparse
import time
# construct the argument parser
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--prototxt", required=True, help="path to caffe deploy prototxt file")
ap.add_argument("-m", "--model", required=True, help="path to pre-trained caffe model")
ap.add_argument("-c", "--confidence", type=float, default=0.5, help="minimum confidence required")
args = vars(ap.parse_args())
# MODEL - load the model which will be used to predict
print("[INFO] loading model...")
net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])
# INPUT - start video to capture frames
print("[INFO] starting video stream...")
vs = VideoStream(src=0).start()
# vs = VideoStream(usePiCamera=True).start() # this is to stream video for Raspberry Pi camera
# vs = FileVideoStream(path='/path to file') # this is to get video content from file
time.sleep(2) # allow the cam to warm up
# for live streaming, while loop will be required to capture frames
while True:
frame = vs.read()
frame = imutils.resize(frame, width=400)
# get the dimensions of the frame
print('shape of frame:', frame.shape)
(h, w) = frame.shape[:2]
# blob the frame
blob = cv2.dnn.blobFromImage(frame, scalefactor=1.0,
size=(300, 300), mean=(104.0, 177.0, 123.0))
# use the blob for detection
net.setInput(blob)
detections = net.forward()
print(detections)
# loop over the detections
for i in range(0, detections.shape[2]):
confidence = detections[0, 0, i, 2]
if confidence < args['confidence']:
continue
# create box
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
# draw the box on the face
text = "{:.2f}%".format(confidence * 100)
y = startY - 10 if startY - 10 > 10 else startY + 10
cv2.rectangle(frame, (startX, startY), (endX, endY), (0, 255, 0), thickness=2)
cv2.putText(frame, text, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
# show on screen
cv2.imshow("frame", frame)
if cv2.waitKey(1) & 0xFF == 27:
break
cv2.destroyAllWindows()
vs.stop()
【问题讨论】:
标签: python opencv caffe face-detection