Ejemplo n.º 1
0
        /// <summary>
        /// Triggered if we recognize face
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="face"></param>
        private async void CalculateVisitorsStatistic(ImageAnalyzer image)
        {
            if (image == null || !initializedOxford)
            {
                return;
            }

            if (image.DetectedFaces != null)
            {
                foreach (Face face in image.DetectedFaces)
                {
                    //ToDO
                    IdentifiedPerson faceIdIdentification = null;
                    if (image.IdentifiedPersons != null && image.IdentifiedPersons.Count <IdentifiedPerson>() > 0)
                    {
                        faceIdIdentification = image.IdentifiedPersons.FirstOrDefault(p => p.FaceId == face.FaceId);
                    }

                    Visitor detectedPerson = new Visitor {
                        UniqueId = face.FaceId, Count = 1
                    };
                    bool demographicsChanged = true;
                    if (faceIdIdentification == null)
                    {
                        // New visitor
                        UpdateAgeDistribution(face);
                        this.demographics.Visitors.Add(detectedPerson);
                    }
                    else
                    {
                        var exisitingVisitor = this.demographics.Visitors.FirstOrDefault <Visitor>(v => v.UniqueId == face.FaceId) as Visitor;
                        // exisiting visitor
                        if (exisitingVisitor != null)
                        {
                            exisitingVisitor.Count++;
                        }

                        demographicsChanged = false;
                    }

                    if (demographicsChanged)
                    {
                        this.ageGenderDistributionControl.UpdateData(this.demographics);
                    }

                    this.overallStatsControl.UpdateData(this.demographics);

                    UpdateDemographicsData(face);
                }
            }
        }
Ejemplo n.º 2
0
        private void ShowRecommendations(ImageAnalyzer imageWithFaces)
        {
            Recommendation recommendation = null;

            this.currentRecommendation = null;

            int numberOfPeople = imageWithFaces.DetectedFaces.Count();

            if (numberOfPeople == 1)
            {
                // Single person
                IdentifiedPerson identifiedPerson = imageWithFaces.IdentifiedPersons.FirstOrDefault();
                if (identifiedPerson != null)
                {
                    // See if we have a personalized recommendation for this person.
                    recommendation = this.kioskSettings.PersonalizedRecommendations.FirstOrDefault(r => r.Id.Equals(identifiedPerson.Person.Name, StringComparison.OrdinalIgnoreCase));
                }

                if (recommendation == null)
                {
                    // Didn't find a personalized recommendation (or we don't have anyone recognized), so default to
                    // the age/gender-based generic recommendation
                    DetectedFace face = imageWithFaces.DetectedFaces.First();
                    if (face?.FaceAttributes != null)
                    {
                        recommendation = this.kioskSettings.GetGenericRecommendationForPerson((int)face.FaceAttributes.Age.GetValueOrDefault(), face.FaceAttributes.Gender);
                    }
                }
            }
            else if (numberOfPeople > 1 && imageWithFaces.DetectedFaces.Any(f => f.FaceAttributes.Age <= 12) &&
                     imageWithFaces.DetectedFaces.Any(f => f.FaceAttributes.Age > 12))
            {
                // Group with at least one child
                recommendation = this.kioskSettings.GenericRecommendations.FirstOrDefault(r => r.Id == "ChildWithOneOrMoreAdults");
            }
            else if (numberOfPeople > 1 && !imageWithFaces.DetectedFaces.Any(f => f.FaceAttributes.Age <= 12))
            {
                // Group of adults without a child
                recommendation = this.kioskSettings.GenericRecommendations.FirstOrDefault(r => r.Id == "TwoOrMoreAdults");
            }

            if (recommendation != null)
            {
                webView.Navigate(new Uri(recommendation.Url));
                webView.Visibility         = Visibility.Visible;
                this.currentRecommendation = recommendation;
            }
        }
        private static async Task <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 == null)
                {
                    await analyzer.IdentifyCelebrityAsync();
                }

                if (analyzer.AnalysisResult?.Categories != null)
                {
                    foreach (var category in analyzer.AnalysisResult.Categories.Where(c => c.Detail != null))
                    {
                        dynamic detail = JObject.Parse(category.Detail.ToString());
                        if (detail.celebrities != null)
                        {
                            foreach (var celebrity in detail.celebrities)
                            {
                                uint left   = UInt32.Parse(celebrity.faceRectangle.left.ToString());
                                uint top    = UInt32.Parse(celebrity.faceRectangle.top.ToString());
                                uint height = UInt32.Parse(celebrity.faceRectangle.height.ToString());
                                uint width  = UInt32.Parse(celebrity.faceRectangle.width.ToString());

                                if (Util.AreFacesPotentiallyTheSame(new BitmapBounds {
                                    Height = height, Width = width, X = left, Y = top
                                }, item.Face.FaceRectangle))
                                {
                                    return(celebrity.name.ToString());
                                }
                            }
                        }
                    }
                }
            }

            return(string.Empty);
        }
Ejemplo n.º 4
0
        private async void StartDriverIdAsync(ImageAnalyzer e)
        {
            if (this.isProcessingDriverId)
            {
                return;
            }

            if (!e.DetectedFaces.Any())
            {
                this.UpdateUIForNoDriverDetected();
                return;
            }

            await Task.WhenAll(e.IdentifyFacesAsync(), e.FindSimilarPersistedFacesAsync());

            SimilarFaceMatch faceMatch = e.SimilarFaceMatches.FirstOrDefault();

            if (faceMatch != null)
            {
                string name = "Unknown";

                IdentifiedPerson p = e.IdentifiedPersons.FirstOrDefault(f => f.FaceId == faceMatch.Face.FaceId);
                if (p != null)
                {
                    name = p.Person.Name;
                }
                else if (SettingsHelper.Instance.ShowAgeAndGender)
                {
                    switch (faceMatch.Face.FaceAttributes.Gender)
                    {
                    case Gender.Male:
                        name = "Unknown male";
                        break;

                    case Gender.Female:
                        name = "Unknown female";
                        break;
                    }
                }

                this.driverId.Text = string.Format("{0}", name, faceMatch.SimilarPersistedFace.PersistedFaceId.GetValueOrDefault().ToString("N").Substring(0, 4));
            }

            this.isProcessingDriverId = false;
        }
Ejemplo n.º 5
0
        private async void StartDriverIdAsync(ImageAnalyzer e)
        {
            if (this.isProcessingDriverId)
            {
                return;
            }

            if (!e.DetectedFaces.Any())
            {
                this.UpdateUIForNoDriverDetected();
                return;
            }

            await Task.WhenAll(e.IdentifyFacesAsync(), e.FindSimilarPersistedFacesAsync());

            SimilarFaceMatch faceMatch = e.SimilarFaceMatches.FirstOrDefault();

            if (faceMatch != null)
            {
                string name = "Unknown";

                IdentifiedPerson p = e.IdentifiedPersons.FirstOrDefault(f => f.FaceId == faceMatch.Face.FaceId);
                if (p != null)
                {
                    name = p.Person.Name;
                }
                else
                {
                    if (faceMatch.Face.FaceAttributes.Gender == "male")
                    {
                        name = "Unknown male";
                    }
                    else if (faceMatch.Face.FaceAttributes.Gender == "female")
                    {
                        name = "Unknown female";
                    }
                }

                this.driverId.Text = string.Format("{0}\nFace Id: {1}", name, faceMatch.SimilarPersistedFace.PersistedFaceId.ToString("N").Substring(0, 4));
            }

            this.isProcessingDriverId = false;
        }
Ejemplo n.º 6
0
        private IList <DetectedFaceViewModel> GetDetectedFaceViewModels(IEnumerable <DetectedFace> detectedFaces, IEnumerable <IdentifiedPerson> identifiedPersons)
        {
            var result = new List <DetectedFaceViewModel>();

            foreach (var(face, index) in detectedFaces.Select((v, i) => (v, i)))
            {
                string           faceTitle        = $"Face {index + 1}";
                IdentifiedPerson identifiedPerson = identifiedPersons?.FirstOrDefault(x => x.FaceId == face.FaceId);
                if (identifiedPerson?.Person != null)
                {
                    faceTitle = $"{identifiedPerson.Person.Name} ({(uint)Math.Round(identifiedPerson.Confidence * 100)}%)";
                }
                else if (ShowAgeAndGender)
                {
                    var genderWithAge = new List <string>()
                    {
                        face.FaceAttributes.Gender?.ToString() ?? string.Empty, face.FaceAttributes.Age?.ToString() ?? string.Empty
                    };
                    faceTitle = string.Join(", ", genderWithAge.Where(x => !string.IsNullOrEmpty(x)));
                }

                KeyValuePair <string, double> topEmotion = Util.EmotionToRankedList(face.FaceAttributes.Emotion).FirstOrDefault();
                var faceDescription = new List <string>()
                {
                    face.FaceAttributes.Hair.HairColor.Any() ? $"{face.FaceAttributes.Hair.HairColor.OrderByDescending(x => x.Confidence).First().Color} hair" : string.Empty,
                    topEmotion.Key != null ? $"{topEmotion.Key} expression" : string.Empty
                };
                result.Add(new DetectedFaceViewModel()
                {
                    FaceRectangle    = face.FaceRectangle,
                    FaceAttributes   = face.FaceAttributes,
                    FaceLandmarks    = face.FaceLandmarks,
                    IdentifiedPerson = identifiedPerson,
                    FaceTitle        = faceTitle,
                    FaceDescription  = string.Join(", ", faceDescription.Where(x => !string.IsNullOrEmpty(x)))
                });
            }
            return(result);
        }
Ejemplo n.º 7
0
        private void UpdateGreeting(ImageAnalyzer img)
        {
            DetectedFace mainFace = img.DetectedFaces.First();
            int          age      = (int)Math.Round(mainFace.FaceAttributes.Age.GetValueOrDefault());

            string name = "";

            if (img.IdentifiedPersons.Any())
            {
                IdentifiedPerson mainFaceId = img.IdentifiedPersons.FirstOrDefault(p => p.FaceId == mainFace.FaceId);
                if (mainFaceId != null)
                {
                    name = mainFaceId.Person.Name.Split(' ')[0];
                }
            }

            if (string.IsNullOrEmpty(name))
            {
                if (img.DetectedFaces.First().FaceAttributes.Gender.GetValueOrDefault() == Gender.Male)
                {
                    name = "Male";
                }
                else
                {
                    name = "Female";
                }
            }

            string greeting = string.Format("{0}, {1}", name, age);

            this.greetingTextBlock.Text = greeting;
            if (this.currentScrollingName != name)
            {
                this.UpdateSenseHatScrollText(greeting);
                this.currentScrollingName = name;
            }
        }
        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);
        }
Ejemplo n.º 9
0
        protected override void ProcessResponseInteraction(IInteraction message)
        {
            FindCandidatesResponse response = (FindCandidatesResponse)message;

            // Now we print out some of the response values...
            Console.WriteLine("Message ID (root):={0}\n", response.Id.Root);
            Console.WriteLine("Query ID:={0}\n", response.ControlActEvent.QueryAck.QueryId.Root);
            Console.WriteLine("Query Result count:={0}\n", response.ControlActEvent.QueryAck.ResultTotalQuantity);

            // Checking the message ID matches the query.
            Console.WriteLine("Acknowledges Message ID (root):={0}\n", response.Acknowledgement.TargetMessageId.Root);

            if (response.ControlActEvent.QueryAck.ResultCurrentQuantity > 0)
            {
                // Now print out the first result record returned...

                IList <RegistrationEvent <IdentifiedPerson> > records = response.ControlActEvent.SubjectRegistrationEvent;

                foreach (RegistrationEvent <IdentifiedPerson> record in records)
                {
                    IdentifiedPerson person = record.Subject.RegisteredRole;

                    foreach (Identifier id in person.Id)
                    {
                        // Identifier doesn't support extracting 'use' or
                        // 'specializationType'
                        Console.WriteLine("Person id := (root = {0}, extension = {1})\n", id.Root, id.Extension);
                    }
                    // Print out the person's name(s)
                    foreach (PersonName name in person.IdentifiedPersonName)
                    {
                        Console.WriteLine("Person name := {0} {1}\n", name.GivenName, name.FamilyName);
                    }
                }
            }
        }
        private async Task DetectAndShowFaceBorders()
        {
            this.progressIndicator.IsActive = true;

            foreach (var child in this.hostGrid.Children.Where(c => !(c is Image)).ToArray())
            {
                this.hostGrid.Children.Remove(child);
            }

            if (this.DataContext is ImageAnalyzer imageWithFace)
            {
                if (imageWithFace.DetectedFaces == null)
                {
                    await imageWithFace.DetectFacesAsync(detectFaceAttributes : this.DetectFaceAttributes, detectFaceLandmarks : this.DetectFaceLandmarks);
                }

                double renderedImageXTransform = this.imageControl.RenderSize.Width / this.bitmapImage.PixelWidth;
                double renderedImageYTransform = this.imageControl.RenderSize.Height / this.bitmapImage.PixelHeight;

                foreach (DetectedFace face in imageWithFace.DetectedFaces)
                {
                    FaceIdentificationBorder faceUI = new FaceIdentificationBorder()
                    {
                        Tag = face.FaceId,
                    };

                    faceUI.Margin = new Thickness((face.FaceRectangle.Left * renderedImageXTransform) + ((this.ActualWidth - this.imageControl.RenderSize.Width) / 2),
                                                  (face.FaceRectangle.Top * renderedImageYTransform) + ((this.ActualHeight - this.imageControl.RenderSize.Height) / 2), 0, 0);

                    faceUI.BalloonBackground = this.BalloonBackground;
                    faceUI.BalloonForeground = this.BalloonForeground;
                    faceUI.ShowFaceRectangle(face.FaceRectangle.Width * renderedImageXTransform, face.FaceRectangle.Height * renderedImageYTransform);

                    if (this.ShowFaceLandmarks)
                    {
                        faceUI.ShowFaceLandmarks(renderedImageXTransform, renderedImageYTransform, face);
                    }

                    this.hostGrid.Children.Add(faceUI);

                    if (!this.ShowMultipleFaces)
                    {
                        break;
                    }
                }

                if (this.PerformRecognition)
                {
                    if (imageWithFace.IdentifiedPersons == null)
                    {
                        await imageWithFace.IdentifyFacesAsync();
                    }

                    if (this.ShowRecognitionResults)
                    {
                        foreach (DetectedFace face in imageWithFace.DetectedFaces)
                        {
                            // Get the border for the associated face id
                            FaceIdentificationBorder faceUI = (FaceIdentificationBorder)this.hostGrid.Children.FirstOrDefault(e => e is FaceIdentificationBorder && (Guid)(e as FaceIdentificationBorder).Tag == face.FaceId);

                            if (faceUI != null)
                            {
                                IdentifiedPerson faceIdIdentification = imageWithFace.IdentifiedPersons.FirstOrDefault(p => p.FaceId == face.FaceId);

                                string name = this.DetectFaceAttributes && faceIdIdentification != null ? faceIdIdentification.Person.Name : null;
                                Microsoft.Azure.CognitiveServices.Vision.Face.Models.Gender?gender = this.DetectFaceAttributes ? face.FaceAttributes.Gender : null;
                                double age        = this.DetectFaceAttributes ? face.FaceAttributes.Age.GetValueOrDefault() : 0;
                                double confidence = this.DetectFaceAttributes && faceIdIdentification != null ? faceIdIdentification.Confidence : 0;

                                faceUI.ShowIdentificationData(age, gender, (uint)Math.Round(confidence * 100), name);
                            }
                        }
                    }
                }
            }

            this.progressIndicator.IsActive = false;
        }
Ejemplo n.º 11
0
        private void ShowFaceTrackingVisualization(Windows.Foundation.Size framePixelSize, IEnumerable <DetectedFace> detectedFaces)
        {
            this.FaceTrackingVisualizationCanvas.Children.Clear();

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

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

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

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

                    if (this.realTimeDataProvider != null)
                    {
                        Scores lastEmotion = this.realTimeDataProvider.GetLastEmotionForFace(face.FaceBox);
                        if (lastEmotion != null)
                        {
                            faceBorder.ShowRealTimeEmotionData(lastEmotion);
                        }

                        Face                 detectedFace         = this.realTimeDataProvider.GetLastFaceAttributesForFace(face.FaceBox);
                        IdentifiedPerson     identifiedPerson     = this.realTimeDataProvider.GetLastIdentifiedPersonForFace(face.FaceBox);
                        SimilarPersistedFace similarPersistedFace = this.realTimeDataProvider.GetLastSimilarPersistedFaceForFace(face.FaceBox);

                        string uniqueId = null;
                        if (similarPersistedFace != null)
                        {
                            uniqueId = similarPersistedFace.PersistedFaceId.ToString("N").Substring(0, 4);
                        }

                        if (detectedFace != null && detectedFace.FaceAttributes != null)
                        {
                            if (identifiedPerson != null && identifiedPerson.Person != null)
                            {
                                // age, gender and id available
                                faceBorder.ShowIdentificationData(detectedFace.FaceAttributes.Age, detectedFace.FaceAttributes.Gender, (uint)Math.Round(identifiedPerson.Confidence * 100), identifiedPerson.Person.Name, uniqueId: uniqueId);
                            }
                            else
                            {
                                // only age and gender available
                                faceBorder.ShowIdentificationData(detectedFace.FaceAttributes.Age, detectedFace.FaceAttributes.Gender, 0, null, uniqueId: uniqueId);
                            }
                        }
                        else if (identifiedPerson != null && identifiedPerson.Person != null)
                        {
                            // only id available
                            faceBorder.ShowIdentificationData(0, null, (uint)Math.Round(identifiedPerson.Confidence * 100), identifiedPerson.Person.Name, uniqueId: uniqueId);
                        }
                        else if (uniqueId != null)
                        {
                            // only unique id available
                            faceBorder.ShowIdentificationData(0, null, 0, null, uniqueId: uniqueId);
                        }
                    }

                    if (SettingsHelper.Instance.ShowDebugInfo)
                    {
                        this.FaceTrackingVisualizationCanvas.Children.Add(new TextBlock
                        {
                            Text   = string.Format("Coverage: {0:0}%", 100 * ((double)face.FaceBox.Height / this.videoProperties.Height)),
                            Margin = new Thickness((uint)(face.FaceBox.X / widthScale), (uint)(face.FaceBox.Y / heightScale), 0, 0)
                        });
                    }
                }
            }
        }
        public async Task IdentifyFacesAsync()
        {
            this.IdentifiedPersons = Enumerable.Empty <IdentifiedPerson>();

            Guid[] detectedFaceIds = this.DetectedFaces?.Where(f => f.FaceId.HasValue).Select(f => f.FaceId.GetValueOrDefault()).ToArray();
            if (detectedFaceIds != null && detectedFaceIds.Any())
            {
                List <IdentifiedPerson> result = new List <IdentifiedPerson>();

                IEnumerable <PersonGroup> personGroups = Enumerable.Empty <PersonGroup>();
                try
                {
                    personGroups = await FaceServiceHelper.ListPersonGroupsAsync(PeopleGroupsUserDataFilter);
                }
                catch (Exception e)
                {
                    ErrorTrackingHelper.TrackException(e, "Face API GetPersonGroupsAsync error");

                    if (this.ShowDialogOnFaceApiErrors)
                    {
                        await ErrorTrackingHelper.GenericApiCallExceptionHandler(e, "Failure getting PersonGroups");
                    }
                }

                foreach (var group in personGroups)
                {
                    try
                    {
                        IList <IdentifyResult> groupResults = await FaceServiceHelper.IdentifyAsync(group.PersonGroupId, detectedFaceIds);

                        foreach (var match in groupResults)
                        {
                            if (!match.Candidates.Any())
                            {
                                continue;
                            }

                            Person person = await FaceServiceHelper.GetPersonAsync(group.PersonGroupId, match.Candidates[0].PersonId);

                            IdentifiedPerson alreadyIdentifiedPerson = result.FirstOrDefault(p => p.Person.PersonId == match.Candidates[0].PersonId);
                            if (alreadyIdentifiedPerson != null)
                            {
                                // We already tagged this person in another group. Replace the existing one if this new one if the confidence is higher.
                                if (alreadyIdentifiedPerson.Confidence < match.Candidates[0].Confidence)
                                {
                                    alreadyIdentifiedPerson.Person     = person;
                                    alreadyIdentifiedPerson.Confidence = match.Candidates[0].Confidence;
                                    alreadyIdentifiedPerson.FaceId     = match.FaceId;
                                }
                            }
                            else
                            {
                                result.Add(new IdentifiedPerson {
                                    Person = person, Confidence = match.Candidates[0].Confidence, FaceId = match.FaceId
                                });
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        // Catch errors with individual groups so we can continue looping through all groups. Maybe an answer will come from
                        // another one.
                        ErrorTrackingHelper.TrackException(e, "Face API IdentifyAsync error");

                        if (this.ShowDialogOnFaceApiErrors)
                        {
                            await ErrorTrackingHelper.GenericApiCallExceptionHandler(e, "Failure identifying faces");
                        }
                    }
                }

                this.IdentifiedPersons = result;
            }

            this.OnFaceRecognitionCompleted();
        }
Ejemplo n.º 13
0
        private async void ShowFaceTrackingVisualization(Windows.Foundation.Size framePixelSize, IEnumerable <DetectedFace> detectedFaces)
        {
            this.FaceTrackingVisualizationCanvas.Children.Clear();

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

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



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

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

                    if (this.realTimeDataProvider != null)
                    {
                        // EmotionScores lastEmotion = this.realTimeDataProvider.GetLastEmotionForFace(face.FaceBox);
                        //if (lastEmotion != null)
                        //{
                        //    faceBorder.ShowRealTimeEmotionData(lastEmotion);
                        //}

                        Face                 detectedFace         = this.realTimeDataProvider.GetLastFaceAttributesForFace(face.FaceBox);
                        IdentifiedPerson     identifiedPerson     = this.realTimeDataProvider.GetLastIdentifiedPersonForFace(face.FaceBox);
                        SimilarPersistedFace similarPersistedFace = this.realTimeDataProvider.GetLastSimilarPersistedFaceForFace(face.FaceBox);

                        string uniqueId = null;
                        if (similarPersistedFace != null)
                        {
                            uniqueId = similarPersistedFace.PersistedFaceId.ToString("N").Substring(0, 4);
                        }

                        if (detectedFace != null && detectedFace.FaceAttributes != null)
                        {
                            if (identifiedPerson != null && identifiedPerson.Person != null)
                            {
                                // age, gender and id available
                                faceBorder.ShowIdentificationData(detectedFace.FaceAttributes.Age, detectedFace.FaceAttributes.Gender, (uint)Math.Round(identifiedPerson.Confidence * 100), identifiedPerson.Person.Name, uniqueId: uniqueId);
                            }
                            else
                            {
                                // only age and gender available
                                faceBorder.ShowIdentificationData(detectedFace.FaceAttributes.Age, detectedFace.FaceAttributes.Gender, 0, null, uniqueId: uniqueId);
                            }
                        }
                        else if (identifiedPerson != null && identifiedPerson.Person != null)
                        {
                            // only id available
                            faceBorder.ShowIdentificationData(0, null, (uint)Math.Round(identifiedPerson.Confidence * 100), identifiedPerson.Person.Name, uniqueId: uniqueId);
                        }
                        else if (uniqueId != null)
                        {
                            // only unique id available
                            faceBorder.ShowIdentificationData(0, null, 0, null, uniqueId: uniqueId);
                        }
                    }

                    if (SettingsHelper.Instance.ShowDebugInfo)
                    {
                        this.FaceTrackingVisualizationCanvas.Children.Add(new TextBlock
                        {
                            Text   = string.Format("Coverage: {0:0}%", 100 * ((double)face.FaceBox.Height / this.videoProperties.Height)),
                            Margin = new Thickness((uint)(face.FaceBox.X / widthScale), (uint)(face.FaceBox.Y / heightScale), 0, 0)
                        });
                    }
                }
                try
                {
                    if (!Constants.isDashboard)
                    {
                        var rectangledata = Constants.materials;

                        if (Constants.materials != null || Constants.materials.Count > 0)
                        {
                            int co = rectangledata.Count;
                            foreach (var item in rectangledata)
                            {
                                if (item.Confidence == "Helmet_Face")
                                {
                                    Constants.isHelmet = true;
                                }

                                if (item.Confidence == "Fire")
                                {
                                    Constants.isFireDetected = true;
                                }

                                if (item.Confidence != "Background" && item.Confidence != "Helmet_Face")
                                {
                                    RealTimeFaceIdentificationBorder faceBorder = new RealTimeFaceIdentificationBorder();
                                    this.FaceTrackingVisualizationCanvas.Children.Add(faceBorder);

                                    var itemheight = this.FaceTrackingVisualizationCanvas.ActualHeight;
                                    var itemwidth  = this.FaceTrackingVisualizationCanvas.ActualWidth;


                                    int left   = (int)(item.ObjectRect.Left * int.Parse(Constants.x) / 1080);
                                    int width  = (int)(item.ObjectRect.Width * int.Parse(Constants.x) / 1080);
                                    int top    = (int)(item.ObjectRect.Top * int.Parse(Constants.y) / 1080);
                                    int height = (int)(item.ObjectRect.Height * int.Parse(Constants.y) / 1080);
                                    //item.ObjectRect.Left =
                                    //item.ObjectRect.Width =

                                    //item.ObjectRect.Top =
                                    //item.ObjectRect.Height =

                                    //faceBorder.ShowFaceRectangle((uint)(item.ObjectRect.Width / widthScale), (uint)(item.ObjectRect.Height / heightScale), (uint)(x / widthScale), (uint)(y / heightScale));
                                    faceBorder.ShowFaceRectangle((uint)(left / widthScale), (uint)(top - 320 / heightScale), (uint)((width - left) / widthScale), (uint)((height - top) / heightScale));
                                    //string dataforcoordinates = string.Format("height : {0} width : {1} x : {2} y : {3}", itemheight, itemwidth, y, x);

                                    if (item.Confidence == "Fire")
                                    {
                                        faceBorder.ShowIdentificationData(1, "", 0, item.Confidence, "Hazard");
                                    }
                                    else
                                    {
                                        faceBorder.ShowIdentificationData(1, "", 0, item.Confidence, "tools");
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                }
            }
        }
Ejemplo n.º 14
0
        private async void ProcessCameraCapture(ImageAnalyzer e)
        {
            if (e == null)
            {
                this.cameraControl.RestartAutoCaptureCycle();
                return;
            }

            this.recognitionPersistence.PersisteRecognitionResults(e);

            this.imageFromCameraWithFaces.DataContext = e;
            this.imageFromCameraWithFaces.Visibility  = Visibility.Visible;



            e.FaceRecognitionCompleted += async(s, args) =>
            {
                try
                {
                    ImageAnalyzer results = s as ImageAnalyzer;


                    if (results != null && results.DetectedFaces != null)
                    {
                        foreach (Face detectedFace in results.DetectedFaces)
                        {
                            IdentifiedPerson faceIdIdentification = null;
                            if (results.IdentifiedPersons != null && results.IdentifiedPersons.Count <IdentifiedPerson>() > 0)
                            {
                                faceIdIdentification = results.IdentifiedPersons.FirstOrDefault(p => p.FaceId == detectedFace.FaceId);
                            }

                            string message = string.Empty;
                            if (faceIdIdentification != null)
                            {
                                // We able identify this person. Say his name
                                message = SpeechContants.GeneralGreetigMessage(faceIdIdentification.Person.Name);
                                await speech.Read(message);
                            }
                            else
                            {
                                HSFace face = new HSFace(detectedFace.FaceId, "", detectedFace.FaceAttributes);
                                if (face.Age > 0)
                                {
                                    // Unknown person!
                                    message = String.Format(SpeechContants.UnknownVisitorDetailMessage, face.Gender, face.Age);
                                    await speech.ReadSsml(message);
                                }
                            }
                        }
                    }

                    //Update visitors statistic
                    Task.Run(async() => { CalculateVisitorsStatistic(results); });

                    this.photoCaptureBalloonHost.Opacity = 1;

                    int    photoDisplayDuration = 10;
                    double decrementPerSecond   = 100.0 / photoDisplayDuration;
                    for (double i = 100; i >= 0; i -= decrementPerSecond)
                    {
                        this.resultDisplayTimerUI.Value = i;
                        await Task.Delay(1000);
                    }

                    this.photoCaptureBalloonHost.Opacity      = 0;
                    this.imageFromCameraWithFaces.DataContext = null;
                }
                finally
                {
                    this.cameraControl.RestartAutoCaptureCycle();
                }
            };
        }
        private async Task DetectAndShowFaceBorders()
        {
            this.progressIndicator.IsActive = true;

            foreach (var child in this.hostGrid.Children.Where(c => !(c is Image)).ToArray())
            {
                this.hostGrid.Children.Remove(child);
            }

            ImageAnalyzer imageWithFace = this.DataContext as ImageAnalyzer;

            if (imageWithFace != null)
            {
                if (imageWithFace.DetectedFaces == null)
                {
                    await imageWithFace.DetectFacesAsync(detectFaceAttributes : this.DetectFaceAttributes, detectFaceLandmarks : this.DetectFaceLandmarks);
                }

                double renderedImageXTransform = this.imageControl.RenderSize.Width / this.bitmapImage.PixelWidth;
                double renderedImageYTransform = this.imageControl.RenderSize.Height / this.bitmapImage.PixelHeight;

                foreach (Face face in imageWithFace.DetectedFaces)
                {
                    FaceIdentificationBorder faceUI = new FaceIdentificationBorder()
                    {
                        Tag = face.FaceId,
                    };

                    faceUI.Margin = new Thickness((face.FaceRectangle.Left * renderedImageXTransform) + ((this.ActualWidth - this.imageControl.RenderSize.Width) / 2),
                                                  (face.FaceRectangle.Top * renderedImageYTransform) + ((this.ActualHeight - this.imageControl.RenderSize.Height) / 2), 0, 0);

                    faceUI.BalloonBackground = this.BalloonBackground;
                    faceUI.BalloonForeground = this.BalloonForeground;
                    faceUI.ShowFaceRectangle(face.FaceRectangle.Width * renderedImageXTransform, face.FaceRectangle.Height * renderedImageYTransform);

                    if (this.DetectFaceLandmarks)
                    {
                        faceUI.ShowFaceLandmarks(renderedImageXTransform, renderedImageYTransform, face);
                    }

                    this.hostGrid.Children.Add(faceUI);

                    if (!this.ShowMultipleFaces)
                    {
                        break;
                    }
                }

                if (this.PerformRecognition)
                {
                    if (imageWithFace.IdentifiedPersons == null)
                    {
                        await imageWithFace.IdentifyFacesAsync();
                    }

                    ////if (imageWithFace.DetectedFaces.Count<Face>() <= 0)
                    ////{
                    ////    FaceIdentificationBorder faceUI = (FaceIdentificationBorder)this.hostGrid.Children.FirstOrDefault(e => e is FaceIdentificationBorder && (Guid)(e as FaceIdentificationBorder).Tag == face.FaceId);

                    ////    if (faceUI != null)
                    ////    {
                    ////        faceUI.ShowCaptionMessage("Sorry could not identify attendee");
                    ////    }
                    ////}

                    if (this.ShowRecognitionResults)
                    {
                        foreach (Face face in imageWithFace.DetectedFaces)
                        {
                            // Get the border for the associated face id
                            FaceIdentificationBorder faceUI = (FaceIdentificationBorder)this.hostGrid.Children.FirstOrDefault(e => e is FaceIdentificationBorder && (Guid)(e as FaceIdentificationBorder).Tag == face.FaceId);

                            if (faceUI != null)
                            {
                                IdentifiedPerson faceIdIdentification = imageWithFace.IdentifiedPersons.FirstOrDefault(p => p.FaceId == face.FaceId);

                                string name       = this.DetectFaceAttributes && faceIdIdentification != null ? faceIdIdentification.Person.Name : null;
                                string gender     = this.DetectFaceAttributes ? face.FaceAttributes.Gender : null;
                                double age        = this.DetectFaceAttributes ? face.FaceAttributes.Age : 0;
                                double confidence = this.DetectFaceAttributes && faceIdIdentification != null ? faceIdIdentification.Confidence : 0;

                                if (name == null)
                                {
                                    faceUI.ShowCaptionMessage("Sorry could not identify attendee");
                                }
                                else
                                {
                                    faceUI.ShowIdentificationData(age, gender, (uint)Math.Round(confidence * 100), name);

                                    // TODO Add code to mark attendee as checked in using <name>
                                    Windows.Storage.StorageFolder storageFolder = Windows.Storage.KnownFolders.PicturesLibrary;
                                    Windows.Storage.StorageFile   sampleFile    = await storageFolder.GetFileAsync("eventlog.txt");

                                    await Windows.Storage.FileIO.AppendTextAsync(sampleFile, "\n" + name + " |Checked In at| " + System.DateTime.Now);
                                }
                            }
                        }
                    }
                }
            }

            this.progressIndicator.IsActive = false;
        }