【问题标题】:Kinect Facial Recognition and Training ImagesKinect 面部识别和训练图像
【发布时间】:2014-03-26 04:08:38
【问题描述】:

我正在为 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;
            }
        }
    }
}

}

【问题讨论】:

  • 有什么代码
  • 嗨,Obet,请尝试展示您到目前为止所做的尝试。你被否决的原因是你没有表现出来。
  • 这个范围很广……“什么代码需要改?”我们不知道您的代码,您没有提供示例,没有尝试解决问题,您认为问题可能出在哪里等等。除非有人是超级人类编码员,否则他们可能无法提供太多帮助与您提供的详细信息。尝试更多地解释事情,并自己进行一些尝试,然后让我们知道结果如何:)
  • 是的,对不起,我刚刚发布整个代码的链接的原因是我真的很难剖析它。不太擅长编程,我只是在做项目时学习。
  • 添加了一个示例代码希望它是正确的

标签: kinect


【解决方案1】:

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

        ...

        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

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

【讨论】:

  • 好吧,所以我添加了代码,只是我更改了编码器。QualityLevel = 96dpi;到编码器.QualityLevel = 90;因为我遇到了错误。现在它说:'System.Windows.Media.Imaging.BitmapFrame.Create(System.Uri)' 的最佳重载方法匹配有一些无效参数和参数 1:无法从 'object' 转换为 'System.Uri'跨度>
  • @Obet 哦,好的,检查编辑...另外我建议使用 writeablebitmap 而不是 bitmapsource,因为它更高效和优化,因为它不会每秒创建和处理帧
  • 好吧,但它需要完整的图像。我不确定如何更改为 writeablebitmap 我可能只会弄乱整个程序。如何保存转换后的人脸灰度?我怎样才能让它从训练有素的图像数据库中扫描?
  • 我收到此错误:参数 1:无法从 'System.Drawing.Bitmap' 转换为 'System.Windows.Media.Imaging.BitmapFrame'
  • @Obet 改成encoder.Frames.Add(new BitmapFrame(System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( face.GrayFace.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions())));
猜你喜欢
  • 2017-12-16
  • 2012-09-17
  • 2016-04-08
  • 1970-01-01
  • 2017-11-25
  • 1970-01-01
  • 2012-07-01
  • 2021-07-01
  • 2012-02-16
相关资源
最近更新 更多