public SimilarPersistedFace GetLastSimilarPersistedFaceForFace(BitmapBounds faceBox)
        {
            if (this.lastSimilarPersistedFaceSample == null || !this.lastSimilarPersistedFaceSample.Any())
            {
                return(null);
            }

            SimilarFaceMatch match =
                this.lastSimilarPersistedFaceSample.Where(f => CoreUtil.AreFacesPotentiallyTheSame(faceBox, f.Face.FaceRectangle))
                .OrderBy(f => Math.Abs(faceBox.X - f.Face.FaceRectangle.Left) + Math.Abs(faceBox.Y - f.Face.FaceRectangle.Top)).FirstOrDefault();

            return(match?.SimilarPersistedFace);
        }
        public IdentifiedPerson GetLastIdentifiedPersonForFace(BitmapBounds faceBox)
        {
            if (this.lastIdentifiedPersonSample == null || !this.lastIdentifiedPersonSample.Any())
            {
                return(null);
            }

            Tuple <Face, IdentifiedPerson> match =
                this.lastIdentifiedPersonSample.Where(f => CoreUtil.AreFacesPotentiallyTheSame(faceBox, f.Item1.FaceRectangle))
                .OrderBy(f => Math.Abs(faceBox.X - f.Item1.FaceRectangle.Left) + Math.Abs(faceBox.Y - f.Item1.FaceRectangle.Top)).FirstOrDefault();

            if (match != null)
            {
                return(match.Item2);
            }

            return(null);
        }
        private static string GetDisplayTextForPersonAsync(ImageAnalyzer analyzer, SimilarFaceMatch item)
        {
            // See if we identified this person against a trained model
            IdentifiedPerson identifiedPerson = analyzer.IdentifiedPersons.FirstOrDefault(p => p.FaceId == item.Face.FaceId);

            if (identifiedPerson != null)
            {
                return(identifiedPerson.Person.Name);
            }

            if (identifiedPerson == null)
            {
                // Let's see if this is a celebrity
                if (analyzer.AnalysisResult?.Categories != null)
                {
                    foreach (var category in analyzer.AnalysisResult.Categories.Where(c => c.Detail != null))
                    {
                        foreach (var celebrity in category.Detail.Celebrities)
                        {
                            var celebrityFaceRectangle = new Microsoft.Azure.CognitiveServices.Vision.Face.Models.FaceRectangle(
                                celebrity.FaceRectangle.Width,
                                celebrity.FaceRectangle.Height,
                                celebrity.FaceRectangle.Left,
                                celebrity.FaceRectangle.Top);

                            if (CoreUtil.AreFacesPotentiallyTheSame(celebrityFaceRectangle, item.Face.FaceRectangle))
                            {
                                return(celebrity.Name.ToString());
                            }
                        }
                    }
                }
            }

            return(string.Empty);
        }
Example #4
0
 internal static bool AreFacesPotentiallyTheSame(BitmapBounds face1, FaceRectangle face2)
 {
     return(CoreUtil.AreFacesPotentiallyTheSame((int)face1.X, (int)face1.Y, (int)face1.Width, (int)face1.Height, face2.Left, face2.Top, face2.Width, face2.Height));
 }
Example #5
0
        private void HandleFaces(Windows.Foundation.Size framePixelSize, IEnumerable <DetectedFace> detectedFaces, SoftwareBitmap currentFrame)
        {
            //var ignored = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            //{
            //    FaceTrackingVisualizationCanvas.Children.Clear();
            //});


            //double actualWidth = FaceTrackingVisualizationCanvas.ActualWidth;
            //double actualHeight = FaceTrackingVisualizationCanvas.ActualHeight;

            if (captureManager.CameraStreamState == CameraStreamState.Streaming &&
                detectedFaces != null /*&& actualWidth != 0 && actualHeight != 0*/)
            {
                //double widthScale = framePixelSize.Width / actualWidth;
                //double heightScale = framePixelSize.Height / actualHeight;

                List <BitmapBounds> currentFaces = new List <BitmapBounds>();
                List <BitmapBounds> missingFaces = new List <BitmapBounds>();

                foreach (DetectedFace detectedFace in detectedFaces)
                {
                    //RealTimeFaceIdentificationBorder faceBorder = new RealTimeFaceIdentificationBorder();
                    //this.FaceTrackingVisualizationCanvas.Children.Add(faceBorder);

                    //faceBorder.ShowFaceRectangle((uint)(detectedFace.FaceBox.X / widthScale), (uint)(detectedFace.FaceBox.Y / heightScale), (uint)(detectedFace.FaceBox.Width / widthScale), (uint)(detectedFace.FaceBox.Height / heightScale));

                    if (currentFrame == null)
                    {
                        continue;
                    }

                    var potentialMatches = ActiveFaces.Where(existingFace =>
                    {
                        return(CoreUtil.AreFacesPotentiallyTheSame(existingFace, detectedFace.FaceBox));
                    });

                    // Is this a new face to us?
                    if (potentialMatches.Count() == 0)
                    {
                        var eventArgs = new FaceDetectedEventArgs()
                        {
                            Face   = detectedFace,
                            Bitmap = currentFrame
                        };

                        if (!ActiveFaces.Any())
                        {
                            FaceDetectionStarted?.Invoke(this, new EventArgs());
                        }

                        FaceDetected?.Invoke(this, eventArgs);
                    }

                    currentFaces.Add(detectedFace.FaceBox);
                }

                if (currentFaces.Count == 0)
                {
                    FacesNoLongerDetected?.Invoke(this, new EventArgs());
                }

                ActiveFaces = currentFaces;
            }
        }
Example #6
0
 public static Emotion FindFaceClosestToRegion(IEnumerable <Emotion> emotion, FaceRectangle region)
 {
     return(emotion?.Where(e => CoreUtil.AreFacesPotentiallyTheSame(e.FaceRectangle, region))
            .OrderBy(e => Math.Abs(region.Left - e.FaceRectangle.Left) + Math.Abs(region.Top - e.FaceRectangle.Top)).FirstOrDefault());
 }