Opencv_contrib: Predict confidence no longer accessible from Python

Created on 11 Jan 2016  路  10Comments  路  Source: opencv/opencv_contrib

Recent changes to faces.hpp mean you can't call into the version of predict that returns label and confidence, you can only access the single argument version of the method. See this thread for more details:
http://answers.opencv.org/question/82294/cant-get-predict-confidence/

bug face

Most helpful comment

@satyamb9607 you write 'createLBPHRaceRecognizer' but it has to be "createLBPHFaceRecognizer".

All 10 comments

this is, what the generator is producing, the first 2 cases take the same input (from the python script), so the second case is never hit:

static PyObject* pyopencv_cv_face_face_FaceRecognizer_predict(PyObject* self, PyObject* args, PyObject* kw)
{
    using namespace cv::face;

    if(!PyObject_TypeCheck(self, &pyopencv_face_FaceRecognizer_Type))
        return failmsgp("Incorrect type of self (must be 'face_FaceRecognizer' or its derivative)");
    cv::face::FaceRecognizer* _self_ = dynamic_cast<cv::face::FaceRecognizer*>(((pyopencv_face_FaceRecognizer_t*)self)->v.get());
    {
    PyObject* pyobj_src = NULL;
    Mat src;
    int retval;

    const char* keywords[] = { "src", NULL };
    if( PyArg_ParseTupleAndKeywords(args, kw, "O:face_FaceRecognizer.predict", (char**)keywords, &pyobj_src) &&
        pyopencv_to(pyobj_src, src, ArgInfo("src", 0)) )
    {
        ERRWRAP2(retval = _self_->predict(src));
        return pyopencv_from(retval);
    }
    }
    PyErr_Clear();

    {
    PyObject* pyobj_src = NULL;
    Mat src;
    int label;
    double confidence;

    const char* keywords[] = { "src", NULL };
    if( PyArg_ParseTupleAndKeywords(args, kw, "O:face_FaceRecognizer.predict", (char**)keywords, &pyobj_src) &&
        pyopencv_to(pyobj_src, src, ArgInfo("src", 0)) )
    {
        ERRWRAP2(_self_->predict(src, label, confidence));
        return Py_BuildValue("(NN)", pyopencv_from(label), pyopencv_from(confidence));
    }
    }
    PyErr_Clear();

    {
    PyObject* pyobj_src = NULL;
    Mat src;
    PyObject* pyobj_collector = NULL;
    Ptr<PredictCollector> collector;
    int state=0;

    const char* keywords[] = { "src", "collector", "state", NULL };
    if( PyArg_ParseTupleAndKeywords(args, kw, "OO|i:face_FaceRecognizer.predict", (char**)keywords, &pyobj_src, &pyobj_collector, &state) &&
        pyopencv_to(pyobj_src, src, ArgInfo("src", 0)) &&
        pyopencv_to(pyobj_collector, collector, ArgInfo("collector", 0)) )
    {
        ERRWRAP2(_self_->predict(src, collector, state));
        Py_RETURN_NONE;
    }
    }

    return NULL;
}

Any movement on this? It seems threshold is ignored so currently even with a threshold == 0 you can't get it to return -1 for label. This seems to suggest something else is broken as well (but manually testing the confidence could help work around this) but I'm not sure.

@insanemal , you're right. the threshold value is copied to the PredictCollector(s) , but then ignored, so this breaks previous behaviour.

but this is somehow a seperate issue (not related to the python bindings)

cc @comdiv

I encountered both issues myself and I found a workaround that works for me since my program is only a proof of concept.

I fed the trainer for my LBPH recognizer one extra face image and set their label to zero. I used a random face I found on the internet. When I run predict now, faces that are detected but not anyone I want to recognize come up as zero, and it correctly identifies the people who are trained in the recognizer by their label.

Not ideal, but I hope it helps someone else work around the issue until it's resolved.

Can anyone check whether the fix from PR #638 works? It adds aliases for overloaded functions, thus one should call _predict_ to get label and distance tuple, _predict_label_ to get label only and _predict_collect_ to pass custom (or standard) collector. It should also fix threshold check in default _predict_ call.

@mshabunin , yes, this works now !

since the the raw pointers got removed from the PredictCollector interfaces, also the java bindings are building fine now, maybe we can enable it ?

again, thanks a lot for fixing it !

@mshabunin I would have replied earlier but I have never actually successfully built OpenCV until now... but yes, your changes appear to have fixed the problem.

Here's an example output from my program:
prediction for face: 2 with confidence 28.366159106486375

Here's the relevant code in my application (where log is a method that outputs to file as opposed to printing to stdout):

prediction, conf = self.application.recognizer.predict(test_image[y: y + h, x: x + w])
log("prediction for face: " + str(prediction) + ' with confidence ' + str(conf))

The threshold changes work as well, here's with a 0.0 threshold (should always return -1)
prediction for face: -1 with confidence 1.7976931348623157e+308

Thanks for fixing it! This is for a semester project and having these features working helps a lot!

Sir I m trying to create an LBPHfaceRECOGNIZER but I get error of module object has no attribute 'createLBPHRaceRecognizer'

@satyamb9607 you write 'createLBPHRaceRecognizer' but it has to be "createLBPHFaceRecognizer".

Flajt debes usar cv2.face.LBPHFaceRecognizer_create();

Was this page helpful?
0 / 5 - 0 ratings