Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

when using SFace model by c++ to match two faces , the output is always 1.0000 #140

Closed
crmlei opened this issue Mar 7, 2023 · 3 comments
Labels
question It is not an issue but rather a user question

Comments

@crmlei
Copy link

crmlei commented Mar 7, 2023

I write a c++ demo code using SFace model,but I find the match ouput is always 1.0000

The python code has correct output: ( the face image has already been aligned and croped from original big image to 112*112)

sface_model = cv.FaceRecognizerSF.create(
        model="./face_recognition_sface_2021dec.onnx",
        config="",
        backend_id=0,
        target_id=0
        )
    img1 = cv.imread("./tmp/face_112x112_0.bmp")
    feat1 = sface_model.feature(img1)
    print(feat1)

    img2 = cv.imread("./tmp/match/face_112x112_1.bmp")
    feat2 = sface_model.feature(img2)

    cosine_score = sface_model.match(feat1, feat2, 0)

    print("cosine_score=",cosine_score)

python output 's screenshot:
image

the c++ demo code is here:

std::string sface_model_file = "./face_recognition_sface_2021dec.onnx";
    cv::Ptr<cv::FaceRecognizerSF> sface_model = nullptr;
    sface_model = cv::FaceRecognizerSF::create(sface_model_file,"",cv::dnn::DNN_BACKEND_OPENCV,cv::dnn::DNN_TARGET_CPU);
    printf("Init SFace model ok\n");

    cv::Mat face_mat1 = cv::imread("./tmp/face_112x112_0.bmp");
    cv::Mat face_feat1;
    //face_feat1.convertTo(face_feat1, CV_32FC3, 1.0 / 255, 0);
    sface_model->feature( face_mat1,face_feat1);

    //print feature value, the values seems to be totaly different from the values output in Python :-)
    for(int j=0;j<face_feat1.cols;j++)
    {
        double *f = (double*)face_feat1.data + j;
        printf("%lf ",*f);
       
    }
    printf("\n");


    cv::Mat face_mat2 = cv::imread("./tmp/face_112x112_1.bmp");
    cv::Mat face_feat2;
    //face_feat2.convertTo(face_feat2, CV_32FC3, 1.0 / 255, 0);
    sface_model->feature( face_mat2,face_feat2);

    double  cosine_sim = sface_model->match( face_feat1,face_feat2,0);
    printf("sim=%.4lf\n",cosine_sim);

c++ output's :
image

Env: centos on x64/gcc 8.3/python 3.6.8/opencv-4.5.5

want help..... :-) :-) Thanks !

@fengyuentau fengyuentau added the question It is not an issue but rather a user question label Mar 7, 2023
@fengyuentau
Copy link
Member

double f = (double)face_feat1.data + j;

I believe it should be a float pointer instead.

@crmlei
Copy link
Author

crmlei commented Mar 7, 2023

Yes , Using float has printed same values output by Python.:-) Thanks,

but the match result is still 1.000,

Have I lost some preprocess or postprocess codes in c++?

@fengyuentau
Copy link
Member

printf("sim=%.4lf\n",cosine_sim);

Maybe the usage of printf is wrong: printf("sim=%.4f\n",cosine_sim);. See this https://stackoverflow.com/questions/4264127/correct-format-specifier-for-double-in-printf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question It is not an issue but rather a user question
Projects
None yet
Development

No branches or pull requests

2 participants