コード例 #1
0
        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);
            }

            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;

                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.hostGrid.Children.Add(faceUI);

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

            this.progressIndicator.IsActive = false;
        }
コード例 #2
0
        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;
        }
コード例 #3
0
        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 (this.ShowRecognitionResults)
                    {
                        foreach (Face face in imageWithFace.DetectedFaces)
                        {
                            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;

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

            this.progressIndicator.IsActive = false;
        }