Skip to content

Trying to add Face Recognizer #19

Description

@kobush

Hi,
I'm trying to add support for the FaceRecognizer API. I was able to configure and build the project from source but now I'm stuck on how to pass the parameters for train method:
http://docs.opencv.org/modules/contrib/doc/facerec/facerec_api.html#facerecognizer-train

It has signature

void FaceRecognizer::train(InputArrayOfArrays src, InputArray labels) 

But first argument is expected as vector and second is vector

So I came up with following code in OpenCvSharpExtern

CVAPI(void) objdetect_FaceRecognizer_train(cv::FaceRecognizer *obj, cv::Mat** src, int srcLength, int* labels, int labelsLength)
{
    std::vector<cv::Mat> srcVec(srcLength);
    for (int i = 0; i < srcLength; i++)
        srcVec[i] = *src[i];

    std::vector<int> labelsVec = std::vector<int>(labels, labels + labelsLength);
    obj->train(srcVec, labelsVec);
}

Then on C# side I have declared this in native methods:

        [DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)]
        public static extern void objdetect_FaceRecognizer_train(IntPtr obj, IntPtr[] src, int srcLength, 
            [MarshalAs(UnmanagedType.LPArray)] int[] labels, int labelsLength);

Finally this is the wrapper code:

        public void Train(Mat[] src, int[] labels)
        {
            IntPtr[] srcPtrs = EnumerableEx.SelectPtrs(src);

            NativeMethods.objdetect_FaceRecognizer_train(ptr, srcPtrs, src.Length, labels, labels.Length);
        }

I get AccessViolationException so clearly I must have done something stupid.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or improvement to OpenCvSharp

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions