简体   繁体   English

如何去除 python OpenCV 中的假人脸检测

[英]How to remove false face detection in python OpenCV

I am doing face detection using python opencv .我正在使用 python opencv进行face detection For this I am using caffe model .为此,我正在使用caffe model Below is the code:下面是代码:

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)

Above code is working perfectly fine for almost all images.上面的代码几乎适用于所有图像。 For example below:例如下面:

在此处输入图像描述

As you can see the face is detected with confidence of 96% .如您所见,人脸检测的置信度为96% But there are many cases for which the code is detecting false faces as well like below:但是在许多情况下,代码会检测到假人脸,如下所示:

在此处输入图像描述

Above face is detected but also has false detections and the shocking part is that confidence in both the detections was more than 90%上面的人脸被检测到但也有错误检测,令人震惊的是,两次检测的置信度都超过了90%

Whenever I have these type of false detections, I use some online face detector to quickly verify, like this one and the results looks good:每当我有这些类型的错误检测时,我都会使用一些在线人脸检测器来快速验证,比如这个,结果看起来不错:

在此处输入图像描述

and because of this I sometime feels weather the model I am using for face detection is good enough or not.因此,我有时会觉得我用于face detection的 model 是否足够好。

Can anyone please help me here and please tell me what I am doing wrong due to which its giving false detections and how can I remove these false detections.任何人都可以在这里帮助我,请告诉我我做错了什么,因为它给出了错误检测以及如何删除这些错误检测。 Please help.请帮忙。 Thanks谢谢

EDIT:编辑:

Even after doing Non-maximum suppression, its not seems to be working:即使在进行了非最大抑制之后,它似乎也不起作用:

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 before passing it to non_max_suppression_fast is [[311 280 644 719], [131 114 419 475]] and after suppressions its still the same rects = [[311 280 644 719], [131 114 419 475]] boundingBoxes在传递给non_max_suppression_fast之前是[[311 280 644 719], [131 114 419 475]]并且在抑制之后它仍然是相同的rects = [[311 280 644 719], [131 114 419 475]]

I have resolved this.我已经解决了这个问题。 Although the approach I have used has given me 99% accuracy but I am not sure if the approach is correct or not.虽然我使用的方法给了我 99% 的准确率,但我不确定该方法是否正确。

So whenever I get false detections in face images just like below:因此,每当我在面部图像中得到错误检测时,如下所示:

在此处输入图像描述

In the above image, we can see that the 2nd bounding box which bottom right corner is quite bigger than the actual height and width of the image.在上图中,我们可以看到右下角的第二个边界框比图像的实际高度和宽度要大很多。 Thus I have put a simple check that if bounding box is greater that height/width of the image, ignore it.因此,我做了一个简单的检查,如果边界框大于图像的高度/宽度,请忽略它。 Below is the code:下面是代码:

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

and here is the code for check_false_detections :这是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

This is working fine for my use case.这对我的用例来说很好用。 I have tested with more than 150 images.我已经测试了 150 多张图片。 It might not work for anyone else but its worth a try.它可能对其他人不起作用,但值得一试。

Thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM