Beispiel #1
0
        private async Task DetectAndShowEmotion()
        {
            this.progressIndicator.IsActive = true;
            this.DetectFaceAttributes       = 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)
            {
                double renderedImageXTransform = this.imageControl.RenderSize.Width / this.bitmapImage.PixelWidth;
                double renderedImageYTransform = this.imageControl.RenderSize.Height / this.bitmapImage.PixelHeight;

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

                foreach (Face face in imageWithFace.DetectedFaces)
                {
                    FaceIdentificationBorder faceUI = new FaceIdentificationBorder();

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

                    Microsoft.ProjectOxford.Common.Rectangle rectangle = new Microsoft.ProjectOxford.Common.Rectangle();
                    rectangle.Height = face.FaceRectangle.Height;
                    rectangle.Left   = face.FaceRectangle.Left;
                    rectangle.Top    = face.FaceRectangle.Top;
                    rectangle.Width  = face.FaceRectangle.Width;

                    Emotion emotion = new Emotion();
                    emotion.FaceRectangle = rectangle;
                    emotion.Scores        = face.FaceAttributes.Emotion;

                    faceUI.ShowEmotionData(emotion);

                    this.hostGrid.Children.Add(faceUI);

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

            this.progressIndicator.IsActive = false;
        }
Beispiel #2
0
        private async Task DetectAndShowEmotion()
        {
            ShowProgressBar();

            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.DetectedEmotion == null)
                {
                    EmotionDetecting?.Invoke(this, new EventArgs());

                    await imageWithFace.DetectEmotionAsync();

                    EmotionDetected?.Invoke(this, new EventArgs());
                }

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

                foreach (Emotion emotion in imageWithFace.DetectedEmotion)
                {
                    FaceIdentificationBorder faceUI = new FaceIdentificationBorder();

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

                    faceUI.BalloonBackground = this.BalloonBackground;
                    faceUI.BalloonForeground = this.BalloonForeground;

                    faceUI.ShowFaceRectangle(emotion.FaceRectangle.Width * renderedImageXTransform, emotion.FaceRectangle.Height * renderedImageYTransform);

                    faceUI.ShowEmotionData(emotion);
                    this.emotionEmojiControl.UpdateEmotion(emotion.Scores);

                    this.hostGrid.Children.Add(faceUI);

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

            //this.progressIndicator.IsActive = false;
            HideProgressBar();
        }
        private async Task DetectAndShowEmotion()
        {
            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 : true);
                }

                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
                    {
                        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),
                        BalloonBackground = this.BalloonBackground,
                        BalloonForeground = this.BalloonForeground
                    };

                    faceUI.ShowFaceRectangle(face.FaceRectangle.Width * renderedImageXTransform, face.FaceRectangle.Height * renderedImageYTransform);

                    faceUI.ShowEmotionData(face.FaceAttributes.Emotion);

                    this.hostGrid.Children.Add(faceUI);

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

            this.progressIndicator.IsActive = false;
        }
        private async Task DetectAndShowComputerVisionAnalysis()
        {
            this.progressIndicator.IsActive = true;

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

            if (this.DataContext is ImageAnalyzer img)
            {
                List <Task> tasks = new List <Task>();
                if (img.AnalysisResult == null)
                {
                    tasks.Add(img.AnalyzeImageAsync(new List <Details> {
                        Details.Celebrities, Details.Landmarks
                    }));
                }

                if (this.PerformOCRAnalysis && (img.TextOperationResult == null || img.TextRecognitionMode != this.TextRecognitionMode))
                {
                    tasks.Add(img.RecognizeTextAsync(this.TextRecognitionMode));
                }

                if (this.PerformObjectDetection && img.DetectedObjects == null)
                {
                    tasks.Add(img.DetectObjectsAsync());
                }

                await Task.WhenAll(tasks);

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

                if (img.AnalysisResult.Faces != null)
                {
                    foreach (FaceDescription face in img.AnalysisResult.Faces)
                    {
                        FaceIdentificationBorder faceUI = new FaceIdentificationBorder
                        {
                            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),
                            BalloonBackground = this.BalloonBackground,
                            BalloonForeground = this.BalloonForeground
                        };
                        faceUI.ShowFaceRectangle(face.FaceRectangle.Width * renderedImageXTransform, face.FaceRectangle.Height * renderedImageYTransform);

                        var faceGender = Util.GetFaceGender(face.Gender);
                        faceUI.ShowIdentificationData(face.Age, faceGender, 0, null);
                        this.hostGrid.Children.Add(faceUI);

                        this.GetCelebrityInfoIfAvailable(img, face.FaceRectangle, out string celebRecoName, out double celebRecoConfidence);
                        if (!string.IsNullOrEmpty(celebRecoName))
                        {
                            Border celebUI = new Border
                            {
                                Child = new TextBlock
                                {
                                    Text       = string.Format("{0} ({1}%)", celebRecoName, (uint)Math.Round(celebRecoConfidence * 100)),
                                    Foreground = this.BalloonForeground,
                                    FontSize   = 14
                                },
                                Background          = this.BalloonBackground,
                                VerticalAlignment   = VerticalAlignment.Top,
                                HorizontalAlignment = HorizontalAlignment.Left
                            };

                            celebUI.SizeChanged += (ev, ar) =>
                            {
                                celebUI.Margin = new Thickness(faceUI.Margin.Left - (celebUI.ActualWidth - face.FaceRectangle.Width * renderedImageXTransform) / 2,
                                                               faceUI.Margin.Top + 2 + face.FaceRectangle.Height * renderedImageYTransform, 0, 0);
                            };
                            this.hostGrid.Children.Add(celebUI);
                        }
                    }
                }

                // Clean up any old results
                foreach (var child in this.hostGrid.Children.Where(c => (c is OCRBorder)).ToArray())
                {
                    this.hostGrid.Children.Remove(child);
                }

                // OCR request (Printed / Handwritten)
                if (this.PerformOCRAnalysis && img.TextOperationResult?.RecognitionResult?.Lines != null)
                {
                    this.imageControl.RenderTransform = new RotateTransform {
                        Angle = 0, CenterX = this.imageControl.RenderSize.Width / 2, CenterY = this.imageControl.RenderSize.Height / 2
                    };

                    foreach (Line line in img.TextOperationResult.RecognitionResult.Lines)
                    {
                        foreach (var word in line.Words)
                        {
                            double[] boundingBox = word?.BoundingBox?.ToArray() ?? new double[] { };
                            if (boundingBox.Length == 8)
                            {
                                double minLeft = renderedImageXTransform * (new List <double>()
                                {
                                    boundingBox[0], boundingBox[2], boundingBox[4], boundingBox[6]
                                }).Min();
                                double minTop  = renderedImageYTransform * (new List <double>()
                                {
                                    boundingBox[1], boundingBox[3], boundingBox[5], boundingBox[7]
                                }).Min();
                                var points     = new PointCollection()
                                {
                                    new Windows.Foundation.Point(boundingBox[0] * renderedImageXTransform - minLeft, boundingBox[1] * renderedImageYTransform - minTop),
                                    new Windows.Foundation.Point(boundingBox[2] * renderedImageXTransform - minLeft, boundingBox[3] * renderedImageYTransform - minTop),
                                    new Windows.Foundation.Point(boundingBox[4] * renderedImageXTransform - minLeft, boundingBox[5] * renderedImageYTransform - minTop),
                                    new Windows.Foundation.Point(boundingBox[6] * renderedImageXTransform - minLeft, boundingBox[7] * renderedImageYTransform - minTop)
                                };

                                // The four points (x-coordinate, y-coordinate) of the detected rectangle from the left-top corner and clockwise
                                IEnumerable <Windows.Foundation.Point> leftPoints  = points.OrderBy(p => p.X).Take(2);
                                IEnumerable <Windows.Foundation.Point> rightPoints = points.OrderByDescending(p => p.X).Take(2);
                                Windows.Foundation.Point leftTop     = leftPoints.OrderBy(p => p.Y).FirstOrDefault();
                                Windows.Foundation.Point leftBottom  = leftPoints.OrderByDescending(p => p.Y).FirstOrDefault();
                                Windows.Foundation.Point rightTop    = rightPoints.OrderBy(p => p.Y).FirstOrDefault();
                                Windows.Foundation.Point rightBottom = rightPoints.OrderByDescending(p => p.Y).FirstOrDefault();
                                var orderedPoints = new PointCollection()
                                {
                                    leftTop, rightTop, rightBottom, leftBottom
                                };

                                // simple math to get angle of the text
                                double diffWidth  = Math.Abs(leftTop.X - rightTop.X);
                                double diffHeight = Math.Abs(leftTop.Y - rightTop.Y);
                                double sign       = leftTop.Y > rightTop.Y ? -1 : 1;
                                double angle      = sign * Math.Atan2(diffHeight, diffWidth) * (180 / Math.PI); // angle in degrees

                                OCRBorder ocrUI = new OCRBorder
                                {
                                    Margin = new Thickness(minLeft + ((this.ActualWidth - this.imageControl.RenderSize.Width) / 2),
                                                           minTop + ((this.ActualHeight - this.imageControl.RenderSize.Height) / 2), 0, 0)
                                };
                                ocrUI.SetPoints(orderedPoints, word.Text, angle);
                                this.hostGrid.Children.Add(ocrUI);
                            }
                        }
                    }
                }

                if (this.PerformObjectDetection && img.DetectedObjects != null)
                {
                    this.ShowObjectDetectionRegions(img.DetectedObjects);
                }
            }

            this.progressIndicator.IsActive = false;
        }
        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;
        }
        private async Task <bool> DetectAndShowEmotion()
        {
            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.DetectedEmotion == null)
                {
                    await imageWithFace.DetectEmotionAsync();
                }

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

                if (App.RequiredEmotion != Areas.ExpectedEmotion.NaN)
                {
                    if (imageWithFace.DetectedEmotion.Count() != 2)
                    {
                        this.progressIndicator.IsActive = false;

                        if (this.OnFailure != null)
                        {
                            this.OnFailure(this, false);
                        }
                    }

                    int winnerId = 0;

                    switch (App.RequiredEmotion)
                    {
                    case Areas.ExpectedEmotion.Happy:
                        winnerId = imageWithFace.DetectedEmotion.Where(e => e.Scores.Happiness == imageWithFace.DetectedEmotion.Max(i => i.Scores.Happiness)).FirstOrDefault().FaceRectangle.Left;
                        break;

                    case Areas.ExpectedEmotion.Sad:
                        winnerId = imageWithFace.DetectedEmotion.Where(e => e.Scores.Sadness == imageWithFace.DetectedEmotion.Max(i => i.Scores.Sadness)).FirstOrDefault().FaceRectangle.Left;

                        break;

                    case Areas.ExpectedEmotion.Angry:
                        winnerId = imageWithFace.DetectedEmotion.Where(e => e.Scores.Anger == imageWithFace.DetectedEmotion.Max(i => i.Scores.Anger)).FirstOrDefault().FaceRectangle.Left;

                        break;

                    case Areas.ExpectedEmotion.Surprised:
                        winnerId = imageWithFace.DetectedEmotion.Where(e => e.Scores.Surprise == imageWithFace.DetectedEmotion.Max(i => i.Scores.Surprise)).FirstOrDefault().FaceRectangle.Left;

                        break;

                    case Areas.ExpectedEmotion.Neutral:
                        winnerId = imageWithFace.DetectedEmotion.Where(e => e.Scores.Neutral == imageWithFace.DetectedEmotion.Max(i => i.Scores.Neutral)).FirstOrDefault().FaceRectangle.Left;

                        break;
                    }

                    if (winnerId == 0)
                    {
                        this.progressIndicator.IsActive = false;

                        if (this.OnFailure != null)
                        {
                            this.OnFailure(this, false);
                        }
                    }

                    //Game
                    foreach (Emotion emotion in imageWithFace.DetectedEmotion)
                    {
                        //Result:
                        float r = 1;
                        switch (App.RequiredEmotion)
                        {
                        case Areas.ExpectedEmotion.Happy:
                            r = emotion.Scores.Happiness;
                            break;

                        case Areas.ExpectedEmotion.Sad:
                            r = emotion.Scores.Sadness;

                            break;

                        case Areas.ExpectedEmotion.Angry:
                            r = emotion.Scores.Anger;

                            break;

                        case Areas.ExpectedEmotion.Surprised:
                            r = emotion.Scores.Surprise;

                            break;

                        case Areas.ExpectedEmotion.Neutral:
                            r = emotion.Scores.Neutral;

                            break;
                        }


                        EmotionFaceIdentificationBorder faceUI = new EmotionFaceIdentificationBorder(emotion.FaceRectangle.Left == winnerId, r);
                        //faceUI.Winner = emotion.FaceRectangle.Left == winnerId;
                        // faceUI.RequestedEmotion = App.RequiredEmotion.ToString();

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

                        //faceUI.BalloonBackground = this.BalloonBackground;
                        //faceUI.BalloonForeground = this.BalloonForeground;

                        faceUI.ShowFaceRectangle(emotion.FaceRectangle.Width * renderedImageXTransform, emotion.FaceRectangle.Height * renderedImageYTransform);

                        //faceUI.ShowEmotionData(emotion);

                        this.hostGrid.Children.Add(faceUI);
                    }
                    this.progressIndicator.IsActive = false;

                    if (this.OnSuccess != null)
                    {
                        this.OnSuccess(this, true);
                    }

                    //return true;
                }
                else
                {
                    foreach (Emotion emotion in imageWithFace.DetectedEmotion)
                    {
                        //logic goes here.

                        FaceIdentificationBorder faceUI = new FaceIdentificationBorder();

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

                        faceUI.BalloonBackground = this.BalloonBackground;
                        faceUI.BalloonForeground = this.BalloonForeground;

                        faceUI.ShowFaceRectangle(emotion.FaceRectangle.Width * renderedImageXTransform, emotion.FaceRectangle.Height * renderedImageYTransform);

                        faceUI.ShowEmotionData(emotion);

                        this.hostGrid.Children.Add(faceUI);

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

                    // return true;
                }
            }

            this.progressIndicator.IsActive = false;
            return(false);
        }
        private async Task DetectAndShowComputerVisionAnalysis()
        {
            this.progressIndicator.IsActive = true;

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

            ImageAnalyzer img = this.DataContext as ImageAnalyzer;

            if (img != null)
            {
                if (this.PerformOCRAnalysis && img.OcrResults == null)
                {
                    await Task.WhenAll(img.AnalyzeImageAsync(detectCelebrities: true), img.RecognizeTextAsync());
                }
                else if (img.AnalysisResult == null)
                {
                    await img.AnalyzeImageAsync(detectCelebrities : true);
                }

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

                if (img.AnalysisResult.Faces != null)
                {
                    foreach (Microsoft.ProjectOxford.Vision.Contract.Face face in img.AnalysisResult.Faces)
                    {
                        FaceIdentificationBorder faceUI = new FaceIdentificationBorder();

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

                        faceUI.ShowIdentificationData(face.Age, face.Gender, 0, null);
                        this.hostGrid.Children.Add(faceUI);

                        double celebRecoConfidence = 0;
                        string celebRecoName;
                        this.GetCelebrityInfoIfAvailable(img, face.FaceRectangle, out celebRecoName, out celebRecoConfidence);
                        if (!string.IsNullOrEmpty(celebRecoName))
                        {
                            Border celebUI = new Border
                            {
                                Child = new TextBlock
                                {
                                    Text       = string.Format("{0} ({1}%)", celebRecoName, (uint)Math.Round(celebRecoConfidence * 100)),
                                    Foreground = this.BalloonForeground,
                                    FontSize   = 14
                                },
                                Background          = this.BalloonBackground,
                                VerticalAlignment   = VerticalAlignment.Top,
                                HorizontalAlignment = HorizontalAlignment.Left
                            };

                            celebUI.SizeChanged += (ev, ar) =>
                            {
                                celebUI.Margin = new Thickness(faceUI.Margin.Left - (celebUI.ActualWidth - face.FaceRectangle.Width * renderedImageXTransform) / 2,
                                                               faceUI.Margin.Top + 2 + face.FaceRectangle.Height * renderedImageYTransform, 0, 0);
                            };
                            this.hostGrid.Children.Add(celebUI);
                        }
                    }
                }

                if (this.PerformOCRAnalysis && img.OcrResults.Regions != null)
                {
                    if (img.OcrResults.TextAngle.HasValue)
                    {
                        this.imageControl.RenderTransform = new RotateTransform {
                            Angle = -img.OcrResults.TextAngle.Value, CenterX = this.imageControl.RenderSize.Width / 2, CenterY = this.imageControl.RenderSize.Height / 2
                        };
                    }

                    foreach (Microsoft.ProjectOxford.Vision.Contract.Region ocrRegion in img.OcrResults.Regions)
                    {
                        foreach (var line in ocrRegion.Lines)
                        {
                            foreach (var word in line.Words)
                            {
                                OCRBorder ocrUI = new OCRBorder();

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

                                ocrUI.SetData(word.Rectangle.Width * renderedImageXTransform, word.Rectangle.Height * renderedImageYTransform, word.Text);

                                this.hostGrid.Children.Add(ocrUI);
                            }
                        }
                    }
                }
            }

            this.progressIndicator.IsActive = false;
        }
        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;
        }