/// <summary> /// Handles recognition complete events /// </summary> private void Engine_RecognitionComplete(object sender, KinectFacialRecognition.RecognitionResult e) { TrackedFace face = null; if (e.Faces != null) { face = e.Faces.FirstOrDefault(); } using (var processedBitmap = (Bitmap)e.ColorSpaceBitmap.Clone()) { if (face == null) { this.viewModel.ReadyForTraining = false; } else { using (var g = Graphics.FromImage(processedBitmap)) { var isFmb = this.viewModel.ProcessorType == ProcessorTypes.FaceModel; var rect = face.TrackingResult.FaceRect; var faceOutlineColor = Color.Green; if (isFmb) { if (face.TrackingResult.ConstructedFaceModel == null) { faceOutlineColor = Color.Red; if (face.TrackingResult.BuilderStatus == FaceModelBuilderCollectionStatus.Complete) { faceOutlineColor = Color.Orange; } } var scale = (rect.Width + rect.Height) / 6; var midX = rect.X + (rect.Width / 2); var midY = rect.Y + (rect.Height / 2); if ((face.TrackingResult.BuilderStatus & FaceModelBuilderCollectionStatus.LeftViewsNeeded) == FaceModelBuilderCollectionStatus.LeftViewsNeeded) { g.FillRectangle(new SolidBrush(Color.Red), rect.X - (scale * 2), midY, scale, scale); } if ((face.TrackingResult.BuilderStatus & FaceModelBuilderCollectionStatus.RightViewsNeeded) == FaceModelBuilderCollectionStatus.RightViewsNeeded) { g.FillRectangle(new SolidBrush(Color.Red), rect.X + rect.Width + (scale * 2), midY, scale, scale); } if ((face.TrackingResult.BuilderStatus & FaceModelBuilderCollectionStatus.TiltedUpViewsNeeded) == FaceModelBuilderCollectionStatus.TiltedUpViewsNeeded) { g.FillRectangle(new SolidBrush(Color.Red), midX, rect.Y - (scale * 2), scale, scale); } if ((face.TrackingResult.BuilderStatus & FaceModelBuilderCollectionStatus.FrontViewFramesNeeded) == FaceModelBuilderCollectionStatus.FrontViewFramesNeeded) { g.FillRectangle(new SolidBrush(Color.Red), midX, midY, scale, scale); } } this.viewModel.ReadyForTraining = faceOutlineColor == Color.Green; g.DrawPath(new Pen(faceOutlineColor, 5), face.TrackingResult.GetFacePath()); if (!string.IsNullOrEmpty(face.Key)) { var score = Math.Round(face.ProcessorResults.First().Score, 2); ////TODO Greetings, face.Key ////voice to text - https://gist.github.com/elbruno/e4816d4d5a59a3b159eb#file-elbrunokw4v2speech // Write the key on the image... g.DrawString(face.Key /* + ": " + score*/, new Font("Arial", 80), Brushes.Red, new System.Drawing.Point(rect.Left, rect.Top - 25)); // Timer handled voice Greeting if (lastGreeted != face.Key) //for now, simply separate greetings by a string { // Async because we don't want synthesizer to block this.synth.SpeakAsync("Greetings, " + face.Key); this.viewModel.Stream = this.viewModel.Stream + "Identified " + face.Key + " with confidence " + score + "\n"; lastGreeted = face.Key; } } } if (this.takeTrainingImage) { var eoResult = (EigenObjectRecognitionProcessorResult)face.ProcessorResults.SingleOrDefault(x => x is EigenObjectRecognitionProcessorResult); var fmResult = (FaceModelRecognitionProcessorResult)face.ProcessorResults.SingleOrDefault(x => x is FaceModelRecognitionProcessorResult); var bstf = new BitmapSourceTargetFace(); //this.synth.SpeakAsync("What is your name?"); bstf.Key = this.viewModel.TrainName; if (eoResult != null) { bstf.Image = (Bitmap)eoResult.Image.Clone(); } else { bstf.Image = face.TrackingResult.GetCroppedFace(e.ColorSpaceBitmap); } if (fmResult != null) { bstf.Deformations = fmResult.Deformations; bstf.HairColor = fmResult.HairColor; bstf.SkinColor = fmResult.SkinColor; } this.viewModel.TargetFaces.Add(bstf); this.SerializeBitmapSourceTargetFace(bstf); this.takeTrainingImage = false; this.UpdateTargetFaces(); } } this.viewModel.CurrentVideoFrame = LoadBitmap(processedBitmap); } // Without an explicit call to GC.Collect here, memory runs out of control :( GC.Collect(); }
/// <summary> /// Handles recognition complete events /// </summary> private void Engine_RecognitionComplete(object sender, RecognitionResult e) { TrackedFace face = null; if (e.Faces != null) { face = e.Faces.FirstOrDefault(); } using (var processedBitmap = (Bitmap)e.ColorSpaceBitmap.Clone()) { if (face == null) { this.viewModel.ReadyForTraining = false; } else { using (var g = Graphics.FromImage(processedBitmap)) { var isFmb = this.viewModel.ProcessorType == ProcessorTypes.FaceModel; var rect = face.TrackingResult.FaceRect; var faceOutlineColor = Color.Green; if (isFmb) { if (face.TrackingResult.ConstructedFaceModel == null) { faceOutlineColor = Color.Red; if (face.TrackingResult.BuilderStatus == FaceModelBuilderCollectionStatus.Complete) { faceOutlineColor = Color.Orange; } } var scale = (rect.Width + rect.Height) / 6; var midX = rect.X + (rect.Width / 2); var midY = rect.Y + (rect.Height / 2); if ((face.TrackingResult.BuilderStatus & FaceModelBuilderCollectionStatus.LeftViewsNeeded) == FaceModelBuilderCollectionStatus.LeftViewsNeeded) { g.FillRectangle(new SolidBrush(Color.Red), rect.X - (scale * 2), midY, scale, scale); } if ((face.TrackingResult.BuilderStatus & FaceModelBuilderCollectionStatus.RightViewsNeeded) == FaceModelBuilderCollectionStatus.RightViewsNeeded) { g.FillRectangle(new SolidBrush(Color.Red), rect.X + rect.Width + (scale * 2), midY, scale, scale); } if ((face.TrackingResult.BuilderStatus & FaceModelBuilderCollectionStatus.TiltedUpViewsNeeded) == FaceModelBuilderCollectionStatus.TiltedUpViewsNeeded) { g.FillRectangle(new SolidBrush(Color.Red), midX, rect.Y - (scale * 2), scale, scale); } if ((face.TrackingResult.BuilderStatus & FaceModelBuilderCollectionStatus.FrontViewFramesNeeded) == FaceModelBuilderCollectionStatus.FrontViewFramesNeeded) { g.FillRectangle(new SolidBrush(Color.Red), midX, midY, scale, scale); } } this.viewModel.ReadyForTraining = faceOutlineColor == Color.Green; g.DrawPath(new Pen(faceOutlineColor, 5), face.TrackingResult.GetFacePath()); if (!string.IsNullOrEmpty(face.Key)) { var score = Math.Round(face.ProcessorResults.First().Score, 2); // Write the key on the image... g.DrawString(face.Key + ": " + score, new Font("Arial", 100), Brushes.Red, new System.Drawing.Point(rect.Left, rect.Top - 25)); } } if (this.takeTrainingImage) { var eoResult = (EigenObjectRecognitionProcessorResult)face.ProcessorResults.SingleOrDefault(x => x is EigenObjectRecognitionProcessorResult); var fmResult = (FaceModelRecognitionProcessorResult)face.ProcessorResults.SingleOrDefault(x => x is FaceModelRecognitionProcessorResult); var bstf = new BitmapSourceTargetFace(); bstf.Key = this.viewModel.TrainName; if (eoResult != null) { bstf.Image = (Bitmap)eoResult.Image.Clone(); } else { bstf.Image = face.TrackingResult.GetCroppedFace(e.ColorSpaceBitmap); } if (fmResult != null) { bstf.Deformations = fmResult.Deformations; bstf.HairColor = fmResult.HairColor; bstf.SkinColor = fmResult.SkinColor; } this.viewModel.TargetFaces.Add(bstf); this.SerializeBitmapSourceTargetFace(bstf); this.takeTrainingImage = false; this.UpdateTargetFaces(); } } this.viewModel.CurrentVideoFrame = LoadBitmap(processedBitmap); } // Without an explicit call to GC.Collect here, memory runs out of control :( GC.Collect(); }