Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Is there any limit on maximum number of faces detected using DNN face detector?

By using OpenCV version 4.2.0 in c++ (VS 2019) I created project which performs face detection on the given image. I used Opencv's DNN face detector which uses res10_300x300_ssd_iter_140000_fp16.caffemodel model to detect faces.

Program works very well and detects faces as expected, but while playing and trying with different images, I come to know that for some images program does not detect any single face (even frontal faces) on the other hand If I perform dlib's HOG+SVM detector then it detects 46 faces.

So, to grab help I posted my experience on StackOverflow (I am sorry if foreign portal links are not allowed, but it will help other to understand the question.), If you go through comments between me (Amogh) and (Yunus Temurlenk) and also if you read the answer given by him, you come to know that, If I crop image (for faces < 20) then face detection by opencv works.

Below is the answer given on stackoverflow:

After a deep search, unfortunately I couldn't find a good explanation to this problem. The reason why I tried to crop image is that I assumed there can be a maximum detected face number limit. It is also not about occlusion.

I tried some image examples which includes more than 20(appx.) faces and the results were the same but when I cropped those images(decrease the number of faces), program was able to find the faces.This is also not about the resolution(sizes) of the image because the images I tried had different sizes.

I also changed and tried the all parameters(iteration number, confidentThreshold etc.) but the result still wasn't the desired one.

My assumption but not the answer:

The program doesn't let to find the faces if image includes more than a maximum number(approximately 20)

So, I really wanted to know is there really any such maximum detected face number limit in opencv?

Is there any limit on maximum number of faces detected using DNN face detector?

By using OpenCV version 4.2.0 in c++ (VS 2019) I created project which performs face detection on the given image. I used Opencv's DNN face detector which uses res10_300x300_ssd_iter_140000_fp16.caffemodel model to detect faces.

Program works very well and detects faces as expected, but while playing and trying with different images, I come to know that for some images program does not detect any single face (even frontal faces) on the other hand If I perform dlib's HOG+SVM detector then it detects 46 faces.faces. This is the image I am talking about.

So, to grab help I posted my experience on StackOverflow (I am sorry if foreign portal links are not allowed, but it will help other to understand the question.), If you go through comments between me (Amogh) and (Yunus Temurlenk) and also if you read the answer given by him, you come to know that, If I crop image (for faces < 20) then face detection by opencv works.

Below is the answer given on stackoverflow:

After a deep search, unfortunately I couldn't find a good explanation to this problem. The reason why I tried to crop image is that I assumed there can be a maximum detected face number limit. It is also not about occlusion.

I tried some image examples which includes more than 20(appx.) faces and the results were the same but when I cropped those images(decrease the number of faces), program was able to find the faces.This is also not about the resolution(sizes) of the image because the images I tried had different sizes.

I also changed and tried the all parameters(iteration number, confidentThreshold etc.) but the result still wasn't the desired one.

My assumption but not the answer:

The program doesn't let to find the faces if image includes more than a maximum number(approximately 20)

So, I really wanted to know is there really any such maximum detected face number limit in opencv? opencv?

Is there any limit on maximum number of faces detected using DNN face detector?

By using OpenCV version 4.2.0 in c++ (VS 2019) I created project which performs face detection on the given image. I used Opencv's DNN face detector which uses res10_300x300_ssd_iter_140000_fp16.caffemodel model to detect faces.

Program works very well and detects faces as expected, but while playing and trying with different images, I come to know that for some images program does not detect any single face (even frontal faces) on the other hand If I perform dlib's HOG+SVM detector then it detects 46 faces. This faces.

Image, in which HOG+SVM detector able to detect 46 faces:

image description

Below is the image I am talking about.

So, to grab help I posted my experience on StackOverflow (I am sorry if foreign portal links are not allowed, but it will help other to understand the question.), If you go through comments between me (Amogh) and (Yunus Temurlenk) and also if you read the answer given by him, you come to know that, If I crop image (for faces < 20) then function which performs face detection by using opencv works.and which detects 0 faces:

//variables which are used in function
const double inScaleFactor = 1.0;
const cv::Scalar meanVal = cv::Scalar(104.0, 177.0, 123.0);
const size_t inWidth = 300;
const size_t inHeight = 300;

std::vector<FaceDetectionResult> namespace_name::FaceDetection::detectFaceByOpenCVDNN(std::string filename, FaceDetectionModel model)
{        

    Net net;        
    cv::Mat frame = cv::imread(filename);
    cv::Mat inputBlob;
    std::vector<FaceDetectionResult> vec;


    if (frame.empty())
        throw std::exception("provided image file is not found or unable to open.");

    int frameHeight = frame.rows;
    int frameWidth = frame.cols;

    if (model == FaceDetectionModel::CAFFE)
    {            
        net = cv::dnn::readNetFromCaffe(caffeConfigFile, caffeWeightFile);
        inputBlob = cv::dnn::blobFromImage(frame, inScaleFactor, cv::Size(inWidth, inHeight), meanVal, false, false);
    }
    else
    {            
        net = cv::dnn::readNetFromTensorflow(tensorflowWeightFile, tensorflowConfigFile);
        inputBlob = cv::dnn::blobFromImage(frame, inScaleFactor, cv::Size(inWidth, inHeight), meanVal, true, false);
    }

    net.setInput(inputBlob, "data");
    cv::Mat detection = net.forward("detection_out");

    cv::Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());

    for (int i = 0; i < detectionMat.rows; i++)
    {            
        if (detectionMat.at<float>(i, 2) >= 0.5)
        {
            FaceDetectionResult res;
            res.faceDetected = true;
            res.confidence = detectionMat.at<float>(i, 2);

            res.x1 = static_cast<int>(detectionMat.at<float>(i, 3) * frameWidth);
            res.y1 = static_cast<int>(detectionMat.at<float>(i, 4) * frameHeight);
            res.x2 = static_cast<int>(detectionMat.at<float>(i, 5) * frameWidth);
            res.y2 = static_cast<int>(detectionMat.at<float>(i, 6) * frameHeight);

            vec.push_back(res);
        }
#ifdef aDEBUG
        else
        {

            cout << detectionMat.at<float>(i, 2) << endl;

        }
#endif
    }                
    return vec;
}

And the original image which I am using is this.

While searching the cause behind this issue, one of my stack overflow friend asked me to crop the image and perform detection on it. After cropping image to dimension 481x576 (original was 962x576) opencv detected 18 faces. The same stack overflow friend given below expiation for why opencv detects faces after cropping and why not on full image

Below is the answer given on stackoverflow:

After a deep search, unfortunately I couldn't find a good explanation to this problem. The reason why I tried to crop image is that I assumed there can be a maximum detected face number limit. It is also not about occlusion.

I tried some image examples which includes more than 20(appx.) faces and the results were the same but when I cropped those images(decrease the number of faces), program was able to find the faces.This is also not about the resolution(sizes) of the image because the images I tried had different sizes.

I also changed and tried the all parameters(iteration number, confidentThreshold etc.) but the result still wasn't the desired one.

My assumption but not the answer:

The program doesn't let to find the faces if image includes more than a maximum number(approximately 20)

So, I really wanted to know is there really any such maximum detected face number limit in opencv?opencv? I don't think it is due to image resolution because I am resizing it to 300x300.

Is there any limit on maximum number of faces detected using DNN face detector?

By using OpenCV version 4.2.0 in c++ (VS 2019) I created project which performs face detection on the given image. I used Opencv's DNN face detector which uses res10_300x300_ssd_iter_140000_fp16.caffemodel model to detect faces.

Program works very well and detects faces as expected, but while playing and trying with different images, I come to know that for some images program does not detect any single face (even frontal faces) on the other hand If I perform dlib's HOG+SVM detector then it detects 46 faces.

Image, in which HOG+SVM detector able to detect 46 faces:

image description

Below is the function which performs face detection using opencv and which detects 0 faces:

//variables which are used in function
const double inScaleFactor = 1.0;
const cv::Scalar meanVal = cv::Scalar(104.0, 177.0, 123.0);
const size_t inWidth = 300;
const size_t inHeight = 300;

std::vector<FaceDetectionResult> namespace_name::FaceDetection::detectFaceByOpenCVDNN(std::string filename, FaceDetectionModel model)
{        

    Net net;        
    cv::Mat frame = cv::imread(filename);
    cv::Mat inputBlob;
    std::vector<FaceDetectionResult> vec;


    if (frame.empty())
        throw std::exception("provided image file is not found or unable to open.");

    int frameHeight = frame.rows;
    int frameWidth = frame.cols;

    if (model == FaceDetectionModel::CAFFE)
    {            
        net = cv::dnn::readNetFromCaffe(caffeConfigFile, caffeWeightFile);
        inputBlob = cv::dnn::blobFromImage(frame, inScaleFactor, cv::Size(inWidth, inHeight), meanVal, false, false);
    }
    else
    {            
        net = cv::dnn::readNetFromTensorflow(tensorflowWeightFile, tensorflowConfigFile);
        inputBlob = cv::dnn::blobFromImage(frame, inScaleFactor, cv::Size(inWidth, inHeight), meanVal, true, false);
    }

    net.setInput(inputBlob, "data");
    cv::Mat detection = net.forward("detection_out");

    cv::Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());

    for (int i = 0; i < detectionMat.rows; i++)
    {            
        if (detectionMat.at<float>(i, 2) >= 0.5)
        {
            FaceDetectionResult res;
            res.faceDetected = true;
            res.confidence = detectionMat.at<float>(i, 2);

            res.x1 = static_cast<int>(detectionMat.at<float>(i, 3) * frameWidth);
            res.y1 = static_cast<int>(detectionMat.at<float>(i, 4) * frameHeight);
            res.x2 = static_cast<int>(detectionMat.at<float>(i, 5) * frameWidth);
            res.y2 = static_cast<int>(detectionMat.at<float>(i, 6) * frameHeight);

            vec.push_back(res);
        }
#ifdef aDEBUG
        else
        {

            cout << detectionMat.at<float>(i, 2) << endl;

        }
#endif
    }                
    return vec;
}

And the original image which I am using is this.

While searching the cause behind this issue, one of my stack overflow friend asked me to crop the image and perform detection on it. After cropping image to dimension 481x576 (original was 962x576) opencv detected 18 faces. The same stack overflow friend given below expiation for why opencv detects faces after cropping and why not on full image

Below is the answer given on stackoverflow:stackoverflow: (Link to Stackoverflow question for reference https://stackoverflow.com/questions/59962053)

After a deep search, unfortunately I couldn't find a good explanation to this problem. The reason why I tried to crop image is that I assumed there can be a maximum detected face number limit. It is also not about occlusion.

I tried some image examples which includes more than 20(appx.) faces and the results were the same but when I cropped those images(decrease the number of faces), program was able to find the faces.This is also not about the resolution(sizes) of the image because the images I tried had different sizes.

I also changed and tried the all parameters(iteration number, confidentThreshold etc.) but the result still wasn't the desired one.

My assumption but not the answer:

The program doesn't let to find the faces if image includes more than a maximum number(approximately 20)

So, I really wanted to know is there really any such maximum detected face number limit in opencv? I don't think it is due to image resolution because I am resizing it to 300x300.