Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

dnn module - Face Detection - poor results - Open CV 3.4.3

I am getting poor results with the DNN face detection module for images in which the Haarscard cascade works fine.

C++ Code is as follows:

#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/dnn.hpp>

using namespace cv;
using namespace std;
using namespace cv::dnn;

const size_t inWidth = 300;
const size_t inHeight = 300;
const double inScaleFactor = 1.0;
const float confidenceThreshold = 0.7;
const cv::Scalar meanVal(104.0, 177.0, 123.0);

#define CAFFE

const std::string caffeConfigFile = "deploy.prototxt";
const std::string caffeWeightFile = "res10_300x300_ssd_iter_140000_fp16.caffemodel";

const std::string tensorflowConfigFile = "opencv_face_detector.pbtxt";
const std::string tensorflowWeightFile = "opencv_face_detector_uint8.pb";

void detectFaceOpenCVDNN(Net net, Mat &frameOpenCVDNN)
{
    int frameHeight = frameOpenCVDNN.rows;
    int frameWidth = frameOpenCVDNN.cols;

    //resize(frameOpenCVDNN, frameOpenCVDNN, Size(300, 300));


#ifdef CAFFE
        cv::Mat inputBlob = cv::dnn::blobFromImage(frameOpenCVDNN, inScaleFactor, cv::Size(inWidth, inHeight), meanVal, false, false);
#else
        cv::Mat inputBlob = cv::dnn::blobFromImage(frameOpenCVDNN, inScaleFactor, cv::Size(inWidth, inHeight), meanVal, true, false);
#endif

    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++)
    {
        float confidence = detectionMat.at<float>(i, 2);
        cout << confidence << endl;

        if(confidence > confidenceThreshold)
        {

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

            cv::rectangle(frameOpenCVDNN, cv::Point(x1, y1), cv::Point(x2, y2), cv::Scalar(0, 255, 0),2, 4);
        }
    }

}


int main( int argc, const char** argv )
{
#ifdef CAFFE
  Net net = cv::dnn::readNetFromCaffe(caffeConfigFile, caffeWeightFile);
#else
  Net net = cv::dnn::readNetFromTensorflow(tensorflowWeightFile, tensorflowConfigFile);
#endif

  /*VideoCapture source;
  if (argc == 1)
      source.open(0);
  else
      source.open(argv[1]);*/
  Mat frame = imread("barry2.jpg");
  cout << "Channels: " + to_string(frame.channels()) << endl;
  //Mat resized;
  //resize(frame, resized, Size(300, 300));

  double tt_opencvDNN = 0;
  double fpsOpencvDNN = 0;
  //while(1)
  //{
      //source >> frame;
      //if(frame.empty())
          //break;
      //double t = cv::getTickCount();
      detectFaceOpenCVDNN ( net,frame);
      //tt_opencvDNN = ((double)cv::getTickCount() - t)/cv::getTickFrequency();
      //fpsOpencvDNN = 1/tt_opencvDNN;
      //putText(frame, format("OpenCV DNN ; FPS = %.2f",fpsOpencvDNN), Point(10, 50), FONT_HERSHEY_SIMPLEX, 1.4, Scalar(0, 0, 255), 4);
      imshow( "OpenCV - DNN Face Detection",frame);
      int k = waitKey(0);
    //}
}

Sample image attached for which i am failing to get results, unless i lower confidence factors down to about 0.1 (10%).

Any ideas?

Thanks

RichardC:\fakepath\barry2.jpg

dnn module - Face Detection - poor results - Open CV 3.4.3

I am getting poor results with the DNN face detection module for images in which the Haarscard cascade works fine.

C++ Code is as follows:

#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/dnn.hpp>

using namespace cv;
using namespace std;
using namespace cv::dnn;

const size_t inWidth = 300;
const size_t inHeight = 300;
const double inScaleFactor = 1.0;
const float confidenceThreshold = 0.7;
const cv::Scalar meanVal(104.0, 177.0, 123.0);

#define CAFFE

const std::string caffeConfigFile = "deploy.prototxt";
const std::string caffeWeightFile = "res10_300x300_ssd_iter_140000_fp16.caffemodel";

const std::string tensorflowConfigFile = "opencv_face_detector.pbtxt";
const std::string tensorflowWeightFile = "opencv_face_detector_uint8.pb";

void detectFaceOpenCVDNN(Net net, Mat &frameOpenCVDNN)
{
    int frameHeight = frameOpenCVDNN.rows;
    int frameWidth = frameOpenCVDNN.cols;

    //resize(frameOpenCVDNN, frameOpenCVDNN, Size(300, 300));


#ifdef CAFFE
        cv::Mat inputBlob = cv::dnn::blobFromImage(frameOpenCVDNN, inScaleFactor, cv::Size(inWidth, inHeight), meanVal, false, false);
#else
        cv::Mat inputBlob = cv::dnn::blobFromImage(frameOpenCVDNN, inScaleFactor, cv::Size(inWidth, inHeight), meanVal, true, false);
#endif

    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++)
    {
        float confidence = detectionMat.at<float>(i, 2);
        cout << confidence << endl;

        if(confidence > confidenceThreshold)
        {

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

            cv::rectangle(frameOpenCVDNN, cv::Point(x1, y1), cv::Point(x2, y2), cv::Scalar(0, 255, 0),2, 4);
        }
    }

}


int main( int argc, const char** argv )
{
#ifdef CAFFE
  Net net = cv::dnn::readNetFromCaffe(caffeConfigFile, caffeWeightFile);
#else
  Net net = cv::dnn::readNetFromTensorflow(tensorflowWeightFile, tensorflowConfigFile);
#endif

  /*VideoCapture source;
  if (argc == 1)
      source.open(0);
  else
      source.open(argv[1]);*/
  Mat frame = imread("barry2.jpg");
  cout << "Channels: " + to_string(frame.channels()) << endl;
  //Mat resized;
  //resize(frame, resized, Size(300, 300));

  double tt_opencvDNN = 0;
  double fpsOpencvDNN = 0;
  //while(1)
  //{
      //source >> frame;
      //if(frame.empty())
          //break;
      //double t = cv::getTickCount();
      detectFaceOpenCVDNN ( net,frame);
      //tt_opencvDNN = ((double)cv::getTickCount() - t)/cv::getTickFrequency();
      //fpsOpencvDNN = 1/tt_opencvDNN;
      //putText(frame, format("OpenCV DNN ; FPS = %.2f",fpsOpencvDNN), Point(10, 50), FONT_HERSHEY_SIMPLEX, 1.4, Scalar(0, 0, 255), 4);
      imshow( "OpenCV - DNN Face Detection",frame);
      int k = waitKey(0);
    //}
}

Sample image attached for which i am failing to get results, unless i lower confidence factors down to about 0.1 (10%).

Any ideas?

Thanks

RichardC:\fakepath\barry2.jpg