opencv / opencv_contrib

Repository for OpenCV's extra modules
Apache License 2.0
9.36k stars 5.75k forks source link

Whether it is possible to add dlib.net recognition of faces and speed up #1431

Open axmadjon opened 6 years ago

axmadjon commented 6 years ago

its algorithm works much slower, but with one person the best result

berak commented 6 years ago

most parts you'll need are already builtin with recent opencv3.4 !

you can use the same, pretrained deep-face network, using opencv's dnn module, like this:

    dnn::Net net;
    try {
        net = dnn::readNetFromTorch("openface.nn4.small2.v1.t7");
    } catch(Exception &e) {
        cerr << "Download it from:  ";
        cerr << "https://raw.githubusercontent.com/pyannote/pyannote-data/master/openface.nn4.small2.v1.t7" << endl;
    }

    // later:
    Mat inputBlob = dnn::blobFromImage(image, 1./255, Size(96,96), Scalar(), true, false);
    net.setInput(inputBlob);
    Mat feature = net.forward();

now you can compare images, using the 128 float feature Mat from the dnn output, using a simple:

  double distance = norm(feature_a, feature_b);

(or train your favourite ml or clustering)