Kinect面部识别和训练图像

时间:2014-03-24 18:55:47

标签: kinect

我正在为Kinect使用this面部识别程序

问题是我需要能够注册并实际保存训练数据库的图像。每当我运行它,程序工作,并能够检测和识别面部,但然后不保留图像。什么代码需要改变?

我真的需要帮助,我们将不胜感激。

    /// <summary>
    /// Initializes a new instance of the MainWindow class
    /// </summary>
    public MainWindow()
    {
        KinectSensor kinectSensor = null;

        // loop through all the Kinects attached to this PC, and start the first that is connected without an error.
        foreach (KinectSensor kinect in KinectSensor.KinectSensors)
        {
            if (kinect.Status == KinectStatus.Connected)
            {
                kinectSensor = kinect;
                break;
            }
        }

        if (kinectSensor == null)
        {
            MessageBox.Show("No Kinect found...");
            Application.Current.Shutdown();
            return;
        }

        kinectSensor.SkeletonStream.Enable();
        kinectSensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
        kinectSensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
        kinectSensor.Start();

        AllFramesReadyFrameSource frameSource = new AllFramesReadyFrameSource(kinectSensor);
        this.engine = new KinectFacialRecognitionEngine(kinectSensor, frameSource);
        this.engine.RecognitionComplete += this.Engine_RecognitionComplete;

        this.InitializeComponent();

        this.TrainedFaces.ItemsSource = this.targetFaces;
    }

    [DllImport("gdi32")]
    private static extern int DeleteObject(IntPtr o);

    /// <summary>
    /// Loads a bitmap into a bitmap source
    /// </summary>
    private static BitmapSource LoadBitmap(Bitmap source)
    {
        IntPtr ip = source.GetHbitmap();
        BitmapSource bs = null;
        try
        {
            bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip,
               IntPtr.Zero, Int32Rect.Empty,
               System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
        }
        finally
        {
            DeleteObject(ip);
        }

        return bs;
    }

    /// <summary>
    /// Handles recognition complete events
    /// </summary>
    private void Engine_RecognitionComplete(object sender, RecognitionResult e)
    {
        RecognitionResult.Face face = null;

        if (e.Faces != null)
            face = e.Faces.FirstOrDefault();

        if (face != null)
        {
            if (!string.IsNullOrEmpty(face.Key))
            {
                // Write the key on the image...
                using (var g = Graphics.FromImage(e.ProcessedBitmap))
                {
                    var rect = face.TrackingResults.FaceRect;
                    g.DrawString(face.Key, new Font("Arial", 20), Brushes.Red, new System.Drawing.Point(rect.Left, rect.Top - 25));
                }
            }

            if (this.takeTrainingImage)
            {
                this.targetFaces.Add(new BitmapSourceTargetFace
                {
                    Image = (Bitmap)face.GrayFace.Clone(),
                    Key = this.NameField.Text
                });

                this.takeTrainingImage = false;
                this.NameField.Text = this.NameField.Text.Replace(this.targetFaces.Count.ToString(), (this.targetFaces.Count + 1).ToString());

                if (this.targetFaces.Count > 1)
                    this.engine.SetTargetFaces(this.targetFaces);
            }
        }

        this.Video.Source = LoadBitmap(e.ProcessedBitmap);
    }

    /// <summary>
    /// Starts the training image countdown
    /// </summary>
    private void Train(object sender, RoutedEventArgs e)
    {
        this.TrainButton.IsEnabled = false;
        this.NameField.IsEnabled = false;

        var timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromSeconds(2);
        timer.Tick += (s2, e2) =>
        {
            timer.Stop();
            this.NameField.IsEnabled = true;
            this.TrainButton.IsEnabled = true;
            takeTrainingImage = true;
        };
        timer.Start();
    }

    /// <summary>
    /// Target face with a BitmapSource accessor for the face
    /// </summary>
    private class BitmapSourceTargetFace : TargetFace
    {
        private BitmapSource bitmapSource;

        /// <summary>
        /// Gets the BitmapSource version of the face
        /// </summary>
        public BitmapSource BitmapSource
        {
            get
            {
                if (this.bitmapSource == null)
                    this.bitmapSource = MainWindow.LoadBitmap(this.Image);

                return this.bitmapSource;
            }
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

如果您尝试将图像保存到文件夹,那么我会这样做:

        ...

        if (this.takeTrainingImage)
        {
            this.targetFaces.Add(new BitmapSourceTargetFace
            {
                Image = (Bitmap)face.GrayFace.Clone(),
                Key = this.NameField.Text
            });

            //save image
            JpegBitmapEncoder encoder = new JpegBitmapEncoder();
            BitmapFrame outputFrame = BitmapFrame.Create(LoadBitmap(e.ProcessedBitmap));
            encoder.Frames.Add(face.GrayFace);
            encoder.QualityLevel = 96dpi;

            using (FileStream file = File.OpenWrite("C://Users//Your Name//Documents//Face Trainer//Images//face " + targetFaces.Count + ".jpg"))
            {
                encoder.Save(file);
            }

            this.takeTrainingImage = false;
            this.NameField.Text = this.NameField.Text.Replace(this.targetFaces.Count.ToString(), (this.targetFaces.Count + 1).ToString());

            if (this.targetFaces.Count > 1)
                this.engine.SetTargetFaces(this.targetFaces);
        }

        ....

然后从文件中加载......

string[] files = System.IO.Directory.GetFiles("C://Users//Your Name//Documents//Face Trainer//Images//");
Bitmap[] images = new Bitmap[files.Length];

for (int i = 0; i < files.Length; i++)
{
    images[i] = (Bitmap) Image.FromFile(file, true);
}

如果您尝试将图像添加到实际数据库,我会按照this tutorial

我建议您从一开始就将图像保存到文件中,并且使用数据库需要更多的工作。但是,当你更有经验的数据库时,这类事情非常有效。祝你好运!:)