public static string SummarizeEmotion(EmotionScores scores)
        {
            var bestEmotion = Aggregation.GetDominantEmotion(scores);

            return(string.Format("{0}: {1:N1}", bestEmotion.Item1, bestEmotion.Item2));
        }
Beispiel #2
0
        public static BitmapSource DrawFaces(
            BitmapSource baseImage,
            FaceAPI.Face[] faces,
            string name = null,
            System.Windows.Media.Color?color = null
            )
        {
            if (faces == null)
            {
                return(baseImage);
            }

            void drawAction(DrawingContext drawingContext, double annotationScale)
            {
                for (int i = 0; i < faces.Length; i++)
                {
                    var face = faces[i];
                    if (face.FaceRectangle == null)
                    {
                        continue;
                    }

                    Rect faceRect = new Rect(
                        face.FaceRectangle.Left, face.FaceRectangle.Top,
                        face.FaceRectangle.Width, face.FaceRectangle.Height);
                    string text = "";

                    if (face.FaceAttributes != null)
                    {
                        text += Aggregation.SummarizeFaceAttributes(face.FaceAttributes);
                    }

                    if (name != null)
                    {
                        text += name;
                    }

                    faceRect.Inflate(6 * annotationScale, 6 * annotationScale);

                    double lineThickness = 4 * annotationScale;

                    var brush = color.HasValue ? new SolidColorBrush(color.Value) : s_lineBrush;
                    drawingContext.DrawRectangle(
                        Brushes.Transparent,
                        new Pen(brush, lineThickness),
                        faceRect);

                    if (text != "")
                    {
                        FormattedText ft = new FormattedText(text,
                                                             CultureInfo.CurrentCulture, FlowDirection.LeftToRight, s_typeface,
                                                             16 * annotationScale, Brushes.Black);

                        var pad = 3 * annotationScale;

                        var ypad   = pad;
                        var xpad   = pad + 4 * annotationScale;
                        var origin = new System.Windows.Point(
                            faceRect.Left + xpad - lineThickness / 2,
                            faceRect.Top - ft.Height - ypad + lineThickness / 2);
                        var rect = ft.BuildHighlightGeometry(origin).GetRenderBounds(null);
                        rect.Inflate(xpad, ypad);

                        drawingContext.DrawRectangle(brush, null, rect);
                        drawingContext.DrawText(ft, origin);
                    }
                }
            }

            return(DrawOverlay(baseImage, drawAction));
        }