public static Bitmap DrawFaces(Bitmap baseImage, Microsoft.ProjectOxford.Face.Contract.Face[] faces, EmotionScores[] emotionScores, string[] celebName)
        {
            if (faces == null)
            {
                return(baseImage);
            }

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

                    Rectangle faceRect = new Rectangle(
                        face.FaceRectangle.Left, face.FaceRectangle.Top,
                        face.FaceRectangle.Width, face.FaceRectangle.Height);

                    string text = "";

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

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

                    double lineThickness = 4 * annotationScale;

                    drawingContext.DrawRectangle(new Pen(Color.Yellow, (float)lineThickness), faceRect);
                }
            };

            return(DrawOverlay(baseImage, drawAction));;
        }
Пример #2
0
        public static BitmapSource DrawFaces(BitmapSource baseImage, Microsoft.ProjectOxford.Face.Contract.Face[] faces, EmotionScores[] emotionScores, string[] celebName, Control form)
        {
            if (faces == null)
            {
                return(baseImage);
            }

            Action <DrawingContext, double> drawAction = (drawingContext, annotationScale) =>
            {
                //ImageSource imgsrc = null;
                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 (emotionScores?[i] != null)
                    {
                        text += Aggregation.SummarizeEmotion(emotionScores[i]);
                    }

                    if (celebName?[i] != null)
                    {
                        text += celebName[i];
                    }

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

                    double lineThickness = 4 * annotationScale;

                    drawingContext.DrawRectangle(
                        Brushes.Transparent,
                        new Pen(s_lineBrush, lineThickness),
                        faceRect);


                    ClearEmojis(form);

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

                        var pad = 6 * annotationScale;

                        var ypad   = pad;
                        var xpad   = pad + 10 * 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(s_lineBrush, null, rect);


                        drawingContext.DrawText(ft, origin);

                        var x = baseImage.DpiX + faceRect.X;
                        var y = baseImage.DpiY + faceRect.Y;



                        Console.WriteLine("------------------");
                        Console.WriteLine("baseImage width:  " + baseImage.Width);
                        Console.WriteLine("form width:  " + form.Width);
                        Console.WriteLine("faceRect width:  " + faceRect.Width);
                        Console.WriteLine("ft width:  " + ft.Width);
                        Console.WriteLine("x:  " + x);
                        Console.WriteLine("y:  " + y);
                        Console.WriteLine("------------------");


                        var originEmoji = new System.Windows.Point(
                            faceRect.Left + lineThickness,
                            faceRect.Top - ((ft.Height - ypad + lineThickness / 2) * 8));

                        Rect rectEmoji = new Rect(originEmoji, new Size(90, 90));
                        drawingContext.DrawRectangle(null, null, rectEmoji);

                        var displayEmoji = new DisplayEmotion();
                        displayEmoji.ShowEmoji(drawingContext, rectEmoji, text);
                    }
                }
            };

            return(DrawOverlay(baseImage, drawAction));
        }