【发布时间】:2020-07-19 21:20:44
【问题描述】:
我正在使用 python opencv 做 face detection。为此,我使用caffe model。下面是代码:
import numpy as np
import imutils
import cv2
protoPath = "<path_to_file>\\deploy.prototxt"
modelPath = "<path_to_file>\\res10_300x300_ssd_iter_140000.caffemodel"
detector = cv2.dnn.readNetFromCaffe(protoPath, modelPath)
image = cv2.imread('test.jpg')
image = imutils.resize(image, width=600)
(h, w) = image.shape[:2]
imageBlob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0), swapRB=False, crop=False)
detector.setInput(imageBlob)
detections = detector.forward()
if len(detections) > 0:
i = np.argmax(detections[0, 0, :, 2])
confidence = detections[0, 0, i, 2]
if confidence > 0.5:
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
cv2.rectangle(image, (startX, startY), (endX, endY), (0, 0, 255), 2)
cv2.imshow("Image", image)
cv2.waitKey(0)
以上代码几乎适用于所有图像。例如下面:
如您所见,人脸检测的置信度为96%。但在很多情况下,代码会检测到假人脸,如下所示:
检测到上面的人脸,但也有错误检测,令人震惊的是,两次检测的置信度都超过了90%
每当我有这些类型的错误检测时,我都会使用一些在线人脸检测器来快速验证,例如this one,结果看起来不错:
因此,我有时会觉得我用于face detection 的模型是否足够好。
任何人都可以在这里帮助我,并请告诉我我做错了什么,因为它给出了错误检测以及如何删除这些错误检测。请帮忙。谢谢
编辑:
即使做了非最大抑制,它似乎也不起作用:
def non_max_suppression_fast(self, boxes, overlapThresh):
try:
self.dummy = ''
if len(boxes) == 0:
return []
if boxes.dtype.kind == "i":
boxes = boxes.astype("float")
pick = []
x1 = boxes[:, 0]
y1 = boxes[:, 1]
x2 = boxes[:, 2]
y2 = boxes[:, 3]
area = (x2 - x1 + 1) * (y2 - y1 + 1)
idxs = np.argsort(y2)
while len(idxs) > 0:
last = len(idxs) - 1
i = idxs[last]
pick.append(i)
xx1 = np.maximum(x1[i], x1[idxs[:last]])
yy1 = np.maximum(y1[i], y1[idxs[:last]])
xx2 = np.minimum(x2[i], x2[idxs[:last]])
yy2 = np.minimum(y2[i], y2[idxs[:last]])
w = np.maximum(0, xx2 - xx1 + 1)
h = np.maximum(0, yy2 - yy1 + 1)
overlap = (w * h) / area[idxs[:last]]
idxs = np.delete(idxs, np.concatenate(([last],
np.where(overlap > overlapThresh)[0])))
return boxes[pick].astype("int")
except Exception as e:
print("Exception occurred in non_max_suppression : {}".format(e))
###
SOME CODE
###
rects = []
for i in range(0, detections.shape[2]):
confidence = detections[0, 0, i, 2]
if confidence > 0.5:
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
rects.append(box)
boundingboxes = np.array(rects)
boundingboxes = boundingboxes.astype(int)
rects = non_max_suppression_fast(boundingboxes, 0.3)
boundingBoxes 在传递给non_max_suppression_fast 之前是[[311 280 644 719], [131 114 419 475]],在抑制之后它仍然是相同的rects = [[311 280 644 719], [131 114 419 475]]
【问题讨论】:
-
查找非最大抑制
-
@LLSv2.0 谢谢我现在正在看
-
@LLSv2.0 即使在非最大抑制之后,它仍然是一样的。我已经编辑了这个问题,如果我做错了什么,请你看看。
标签: python opencv caffe face-detection