import cv2
modelFile = "res10_300x300_ssd_iter_140000_fp16.caffemodel"
configFile = "deploy.prototxt"
conf_threshold = 0.7
net = cv2.dnn.readNetFromCaffe(configFile, modelFile)
net.setPreferableBackend(cv2.dnn.DNN_TARGET_CPU)
img = cv2.imread('3.jpg', cv2.IMREAD_UNCHANGED)
frameHeight = img.shape[0]
frameWidth = img.shape[1]
blob = cv2.dnn.blobFromImage(img, 1.0, (300, 300), [104, 117, 123], False, False)
net.setInput(blob)
detections = net.forward()
for i in range(detections.shape[2]):
confidence = detections[0, 0, i, 2]
if confidence > conf_threshold:
x1 = int(detections[0, 0, i, 3] * frameWidth)
y1 = int(detections[0, 0, i, 4] * frameHeight)
x2 = int(detections[0, 0, i, 5] * frameWidth)
y2 = int(detections[0, 0, i, 6] * frameHeight)
cv2.rectangle(
img,
(x1, y1),
(x2, y2),
(0, 255, 0),
2,
)
cv2.imshow("Face Detection Comparison", img)
cv2.waitKey()
cv2.destroyAllWindows()
Demo下载https://download.csdn.net/download/lw112190/85334615
|