Exemplo n.º 1
0
        /// <summary>
        /// Worker thread for recognition processing
        /// </summary>
        private void RecognizerWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            this.faceReady = this.multiSourceReady = false;
            var status = FaceModelBuilderCollectionStatus.Complete;

            if (!this.constructionInProcess && this.fmb != null)
            {
                status = this.fmb.CollectionStatus;
            }

            var result = new RecognitionResult();

            result.ColorSpaceBitmap = this.ImageToBitmap(this.colorImageBuffer, this.imageWidth, this.imageHeight);
            e.Result = result;

            if (this.faceModel != null && this.Processors.Any() && this.ProcessingEnabled)
            {
                var faceTrackingResult = new KinectFaceTrackingResult(this.faceModel, this.constructedFaceModel, status, this.faceAlignment, this.Kinect.CoordinateMapper);

                var rpResults = new List <Sacknet.KinectFacialRecognition.IRecognitionProcessorResult>();

                foreach (var processor in this.Processors)
                {
                    rpResults.Add(processor.Process(result.ColorSpaceBitmap, faceTrackingResult));
                }

                result.Faces = new List <TrackedFace>
                {
                    new TrackedFace
                    {
                        ProcessorResults = rpResults,
                        TrackingResult   = faceTrackingResult
                    }
                };
            }
        }
Exemplo n.º 2
0
        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 = System.Drawing.Color.Green;

                        if (isFmb)
                        {
                            if (face.TrackingResult.ConstructedFaceModel == null)
                            {
                                faceOutlineColor = System.Drawing.Color.Red;

                                if (face.TrackingResult.BuilderStatus == FaceModelBuilderCollectionStatus.Complete)
                                {
                                    faceOutlineColor = System.Drawing.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(System.Drawing.Color.Red), rect.X - (scale * 2), midY, scale, scale);
                            }

                            if ((face.TrackingResult.BuilderStatus & FaceModelBuilderCollectionStatus.RightViewsNeeded) == FaceModelBuilderCollectionStatus.RightViewsNeeded)
                            {
                                g.FillRectangle(new SolidBrush(System.Drawing.Color.Red), rect.X + rect.Width + (scale * 2), midY, scale, scale);
                            }

                            if ((face.TrackingResult.BuilderStatus & FaceModelBuilderCollectionStatus.TiltedUpViewsNeeded) == FaceModelBuilderCollectionStatus.TiltedUpViewsNeeded)
                            {
                                g.FillRectangle(new SolidBrush(System.Drawing.Color.Red), midX, rect.Y - (scale * 2), scale, scale);
                            }

                            if ((face.TrackingResult.BuilderStatus & FaceModelBuilderCollectionStatus.FrontViewFramesNeeded) == FaceModelBuilderCollectionStatus.FrontViewFramesNeeded)
                            {
                                g.FillRectangle(new SolidBrush(System.Drawing.Color.Red), midX, midY, scale, scale);
                            }
                        }

                        this.viewModel.ReadyForTraining = faceOutlineColor == System.Drawing.Color.Green;

                        g.DrawPath(new System.Drawing.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), System.Drawing.Brushes.Red, new System.Drawing.Point(rect.Left, rect.Top - 25));
                            if (face.Key == "Karunesh")
                            {
                                FaceDetected = true;
                            }
                            else
                            {
                                FaceDetected = false;
                            }
                        }
                    }

                    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();
        }