如何在EMGUCV中使用Facemarker?

时间:2018-04-29 18:50:19

标签: c# opencv emgucv

我正在尝试关注this OpenCV tutorial,但我还没有设法创建FaceInvoke.FaceDetectNative函数,我尝试使用此函数,但应用程序停止工作。

static bool MyDetector(IntPtr input, IntPtr output)
    {
        CascadeClassifier faceDetector = new CascadeClassifier(@"..\..\Resource\EMGUCV\haarcascade_frontalface_default.xml");
        Image<Gray, byte> grayImage = (new Image<Bgr, byte>(CvInvoke.cvGetSize(input))).Convert<Gray, byte>();
        grayImage._EqualizeHist();
        Rectangle[] faces = faceDetector.DetectMultiScale(grayImage, 1.1, 10, Size.Empty);
        VectorOfRect rects = new VectorOfRect(faces);
        CvInvoke.cvCopy(rects.Ptr, output, IntPtr.Zero);
        return true;
    }

另一方面,我尝试通过传递Mat对象= new Mat()来调用GetFaces方法;作为IOutputArray也没有工作(崩溃错误)。

FacemarkLBFParams fParams = new FacemarkLBFParams();
        fParams.ModelFile = @"..\..\Resource\EMGUCV\facemarkmodel.yaml";
        FacemarkLBF facemark = new FacemarkLBF(fParams);
        facemark.SetFaceDetector(MyDetector);

        VectorOfRect result = new VectorOfRect();
        Image<Bgr, Byte> image = new Image<Bgr, byte>(@"C:\Users\matias\Documents\Proyectos\100-20.bmp");
        bool success = facemark.GetFaces(image, result);

        Rectangle[] faces = result.ToArray();

感谢的

1 个答案:

答案 0 :(得分:1)

几个小时后,我设法检测到一个面的点,为此使用Fit方法,它接收图像,面(如VectorOfRect)和一个VectorOfVectorOfPointF用于输出

public Image<Bgr, Byte> GetFacePoints()
    {
        CascadeClassifier faceDetector = new CascadeClassifier(@"..\..\Resource\EMGUCV\haarcascade_frontalface_default.xml");
        FacemarkLBFParams fParams = new FacemarkLBFParams();
        fParams.ModelFile = @"..\..\Resource\EMGUCV\lbfmodel.yaml";
        fParams.NLandmarks = 68; // number of landmark points
        fParams.InitShapeN = 10; // number of multiplier for make data augmentation
        fParams.StagesN = 5; // amount of refinement stages
        fParams.TreeN = 6; // number of tree in the model for each landmark point
        fParams.TreeDepth = 5; //he depth of decision tree
        FacemarkLBF facemark = new FacemarkLBF(fParams);
        //facemark.SetFaceDetector(MyDetector);

        Image<Bgr, Byte> image = new Image<Bgr, byte>(@"C:\Users\matias\Downloads\personas-buena-vibra-caracteristicas-1200x600.jpg");
        Image<Gray, byte> grayImage = image.Convert<Gray, byte>();
        grayImage._EqualizeHist();

        VectorOfRect faces = new VectorOfRect(faceDetector.DetectMultiScale(grayImage));
        VectorOfVectorOfPointF landmarks = new VectorOfVectorOfPointF();
        facemark.LoadModel(fParams.ModelFile);

        bool success = facemark.Fit(grayImage, faces, landmarks);
        if (success)
        {
            Rectangle[] facesRect = faces.ToArray();
            for (int i = 0; i < facesRect.Length; i++)
            {
                image.Draw(facesRect[i], new Bgr(Color.Blue), 2);
                FaceInvoke.DrawFacemarks(image, landmarks[i], new Bgr(Color.Blue).MCvScalar);
            }
            return image;
        }
        return null;
    }

现在剩下的就是优化代码并继续我的项目