如何在python OpenCV中删除假脸检测>>

问题描述 投票:0回答:1

我正在使用python face detection执行opencv。为此,我正在使用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)

上面的代码几乎可以在所有图像上正常工作。例如下面的示例:

enter image description here

您可以看到,以96%的置信度检测到脸部。但是在很多情况下,代码也会检测到错误的面孔,如下所示:

enter image description here

检测到上面的脸,但也有错误的检测结果,令人震惊的是,这两次检测的可信度均大于90%

[只要有这些类型的错误检测,我都会使用一些在线人脸检测器来快速验证,例如this one,结果看起来不错:

enter image description here

因此,我有时会感到天气,我为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]]

我正在使用python opencv进行人脸检测。为此,我正在使用caffe模型。下面是代码:import numpy as np import imutils import cv2 protoPath =“ \\ deploy.prototxt” ...

python opencv caffe face-detection
1个回答
-1
投票

您应该提供更多的数据集,这是唯一的解决方案

© www.soinside.com 2019 - 2024. All rights reserved.