【发布时间】: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