Ask Your Question

use caffe model for face detection with opencv

asked 2017-03-12 11:31:37 -0500

Lafi gravatar image

Hello , i want to use the a pratrained caffe model for face detection with opencv !!! i know there is dnn for loading caffe model, but how i can draw a rectangle for each detected face!!! how i can get the output !!! i saw the example in opencv tutorial in how to load a model and do a classification!! but i want to do face detection THank you Lafi

edit retag flag offensive close merge delete


there simply might not be such a thing yet.

closest from the caffe model zoo might be this one

berak gravatar imageberak ( 2017-03-13 03:36:15 -0500 )edit

Hello, can use it with the dnn module that come with opencv??

Lafi gravatar imageLafi ( 2017-03-13 07:13:26 -0500 )edit

ignore comments above. there simply is no pretrained caffe model for facedetection .

(and the landmarks model requires pre-cropped face images, so no gain here.)

just use opencv's builtin face detection in the meantime

berak gravatar imageberak ( 2017-03-13 08:53:26 -0500 )edit

no there a prebuild caffe model for face detection here it's the link!!!!!! ( it's have 3 model for face detection, gender estimation and age estimation!!!

Lafi gravatar imageLafi ( 2017-03-13 13:24:18 -0500 )edit

good luck with it !

berak gravatar imageberak ( 2017-03-13 13:32:49 -0500 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2018-09-22 17:19:23 -0500

My project does exactly what you are expecting:

edit flag offensive delete link more

answered 2017-03-13 10:17:45 -0500

berak gravatar image

ok, it won't answer your question, but at least you could start playing with the dnn code by downloading vanillaCNN.caffemodel and vanilla_deploy.prototxt from , and modify the caffe_googlenet.cpp sample from the dnn module, like this:

#include <opencv2/dnn.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace cv::dnn;
using namespace std;

int main(int argc, char **argv)
    cv::dnn::initModule();  //Required if OpenCV is built as static libs

    String modelTxt = "vanilla_deplay.prototxt";
    String modelBin = "vanillaCNN.caffemodel";
    String imageFile = (argc > 1) ? argv[1] : "face.png";

    Net net = dnn::readNetFromCaffe(modelTxt, modelBin);
    if (net.empty())
        std::cerr << "Can't load network by using the following files: " << std::endl;
        std::cerr << "prototxt:   " << modelTxt << std::endl;
        std::cerr << "caffemodel: " << modelBin << std::endl;
    Mat img = imread(imageFile);
    if (img.empty())
        std::cerr << "Can't read image from the file: " << imageFile << std::endl;

    resize(img, img, Size(40,40)); // size of input (data) layer
    cv::cvtColor(img, img, cv::COLOR_BGR2RGB);
    dnn::Blob inputBlob = dnn::Blob::fromImages(img);   //Convert Mat to dnn::Blob batch of images
    net.setBlob(".data", inputBlob);        //set the network input
    net.forward();                          //compute output
    dnn::Blob prob = net.getBlob("Dense2");   //gather output of the LAST  layer in the prototxt
    Mat probMat = prob.matRefConst().reshape(1, 1); //reshape the blob to 1x1000 matrix
    cerr << probMat << endl;

    for (int i=0; i<5; i++) { // taken from the jupyter notebook
        int x = (<float>(0,i*2  ) + 0.5) * 40;
        int y = (<float>(0,i*2+1) + 0.5) * 40;
        circle(img, Point(x,y), 2, Scalar(255,0,0), 1);
    namedWindow("LANDMARKS !",0);
    imshow("LANDMARKS !", img);
    return 0;

accuracy is shoddy, but hey ;)

image description

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-03-12 11:31:37 -0500

Seen: 3,138 times

Last updated: Mar 13 '17