【问题标题】:How to remove false face detection in python OpenCV如何在 python OpenCV 中删除假人脸检测
【发布时间】:2020-07-19 21:20:44
【问题描述】:

我正在使用 python opencvface 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


【解决方案1】:

我已经解决了这个问题。虽然我使用的方法给了我 99% 的准确率,但我不确定该方法是否正确。

所以每当我在人脸图像中得到错误检测时,如下所示:

在上图中,我们可以看到右下角的第二个边界框比图像的实际高度和宽度要大很多。因此,我做了一个简单的检查,如果边界框大于图像的高度/宽度,请忽略它。下面是代码:

res = check_false_detections(h, w, startX, startY, endX, endY)
if not res:
    print("Got false detection")
    continue

这是check_false_detections的代码:

def check_false_detections(ih, iw, fsx, fsy, fex, fey):
    if ih > iw:
        if fsx > ih:
            return False
        elif fsy > ih:
            return False
        elif fex > ih:
            return False
        elif fey > ih:
            return False
        else:
            return True
    else:
        if fsx > iw:
            return False
        elif fsy > iw:
            return False
        elif fex > iw:
            return False
        elif fey > iw:
            return False
        else:
            return True

这对我的用例来说很好用。我已经测试了 150 多张图片。它可能对其他人不起作用,但值得一试。

谢谢

【讨论】:

    猜你喜欢
    • 2018-09-05
    • 2015-12-18
    • 2012-02-04
    • 2013-05-24
    • 1970-01-01
    • 1970-01-01
    • 2018-06-30
    • 2016-02-06
    • 1970-01-01
    相关资源
    最近更新 更多