Emgucv使用FaceRecognizer类人脸识别

这是之前写的用的Opencv做的,相同的方法。http://blog.csdn.net/qq_22033759/article/details/48578191

using System;
using System.Windows;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.UI;
using Emgu.CV.Features2D;
using Emgu.CV.ML;
using Emgu.CV.Util;
using Emgu.Util;
using Emgu.CV.Face;
using Emgu.CV.VideoSurveillance;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Graphics;
using Emgu.CV.Stitching;
using Emgu.CV.XFeatures2D;

namespace EmguCVHist
{
    public unsafe  partial class Form1 : Form
    {
        Capture cap;
        Image<Bgr, byte> a;
        FisherFaceRecognizer fc;
        CascadeClassifier haar;
        Image<Gray, byte> gray = new Image<Gray, byte>(400, 500);
        public Form1()
        {
            InitializeComponent();
            haar = new CascadeClassifier("haarcascade_frontalface_default.xml");
            cap = new Emgu.CV.Capture(0);
            a = new Image<Bgr, byte>(cap.Width, cap.Height);
            cap.ImageGrabbed += Cap_ImageGrabbed;
            cap.Start();
            fc = new FisherFaceRecognizer();

            Image<Gray, byte> x1 = new Image<Gray, byte>("03.jpg");
            Image<Gray, byte> x2 = new Image<Gray, byte>("04.jpg");
            Image<Gray, byte> x3 = new Image<Gray, byte>("05.jpg");
            Image<Gray, byte> x4 = new Image<Gray, byte>("06.jpg");
            Image<Gray, byte> x5 = new Image<Gray, byte>("07.jpg");

            Image<Gray, byte> y1 = new Image<Gray, byte>("11.jpg");
            Image<Gray, byte> y2 = new Image<Gray, byte>("12.jpg");
            Image<Gray, byte> y3 = new Image<Gray, byte>("13.jpg");
            Image<Gray, byte> y4 = new Image<Gray, byte>("14.jpg");
            Image<Gray, byte> y5 = new Image<Gray, byte>("15.jpg");

            Image<Gray, byte> z1 = new Image<Gray, byte>("21.jpg");
            Image<Gray, byte> z2 = new Image<Gray, byte>("22.jpg");
            Image<Gray, byte> z3 = new Image<Gray, byte>("23.jpg");
            Image<Gray, byte> z4 = new Image<Gray, byte>("24.jpg");
            Image<Gray, byte> z5 = new Image<Gray, byte>("25.jpg");

            var images = new Image<Gray, byte>[] { x1, x2, x3, x4, x5, y1, y2, y3, y4, y5, z1, z2, z3, z4, z5 };
            int[] labels = new int[] { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2 };

            fc.Train(images, labels);

        }

        private void Cap_ImageGrabbed(object sender, EventArgs e)
        {
            cap.Retrieve(a);
            var rects=haar.DetectMultiScale(a, 1.3, 10, new Size(50, 50));

            foreach(var rect in rects)
            {
                int x = rect.X + rect.Width / 2;
                int y = rect.Y + rect.Height / 2;
                var qqq = a.Copy(rect).Resize(400, 500, Inter.Linear).Convert<Gray,byte>().Add(new Gray(10));
                Console.WriteLine(qqq.Width+" " +qqq.Height);
                var result =fc.Predict(qqq);
                int num = result.Label;
                if (num == 0)
                    CvInvoke.PutText(a, "0", new Point(x, y), FontFace.HersheySimplex, 1.5, new MCvScalar(0, 0, 255), 2);
                else if (num == 1)
                    CvInvoke.PutText(a, "1", new Point(x, y), FontFace.HersheySimplex, 1.5, new MCvScalar(0, 0, 255), 2);
                else
                    CvInvoke.PutText(a, "2", new Point(x, y), FontFace.HersheySimplex, 1.5, new MCvScalar(0, 0, 255), 2);
            }
            imageBox1.Image = a;
        }
    }
}

运行效果图和上面的那个链接里的博文里的一样的,就不上图了。

你可能感兴趣的:(EmguCV,脸部识别)