示例#1
0
        private void DrawFaceBoundingBox(HighDefinitionFaceFrame frame, FaceTracker tracker, Color color)
        {
            frame.GetAndRefreshFaceAlignmentResult(tracker.Alignment);
            var vertices = tracker.Model.CalculateVerticesForAlignment(tracker.Alignment);

            if (vertices.Count > 0)
            {
                CameraSpacePoint verticeTop = vertices[(int)HighDetailFacePoints.ForeheadCenter];
                ColorSpacePoint  pointTop   = sensor.CoordinateMapper.MapCameraPointToColorSpace(verticeTop);

                CameraSpacePoint verticeLeft = vertices[(int)HighDetailFacePoints.Leftcheekbone];
                ColorSpacePoint  pointLeft   = sensor.CoordinateMapper.MapCameraPointToColorSpace(verticeLeft);

                CameraSpacePoint verticeRight = vertices[(int)HighDetailFacePoints.Rightcheekbone];
                ColorSpacePoint  pointRight   = sensor.CoordinateMapper.MapCameraPointToColorSpace(verticeRight);

                CameraSpacePoint verticeBottom = vertices[(int)HighDetailFacePoints.ChinCenter];
                ColorSpacePoint  pointBottom   = sensor.CoordinateMapper.MapCameraPointToColorSpace(verticeBottom);

                if (float.IsInfinity(pointTop.X) || float.IsInfinity(pointTop.Y))
                {
                    return;
                }
                if (float.IsInfinity(pointLeft.X) || float.IsInfinity(pointLeft.Y))
                {
                    return;
                }
                if (float.IsInfinity(pointRight.X) || float.IsInfinity(pointRight.Y))
                {
                    return;
                }
                if (float.IsInfinity(pointBottom.X) || float.IsInfinity(pointBottom.Y))
                {
                    return;
                }

                float  posX     = pointLeft.X;
                float  posY     = pointTop.Y;
                double width    = Math.Abs(pointRight.X - pointLeft.X);
                double height   = Math.Abs(pointTop.Y - pointBottom.Y);
                double lineSize = 5;

                Rectangle rect = CreateFaceBoxRectangle(color, lineSize, width, height);
                Canvas.SetLeft(rect, posX);
                Canvas.SetTop(rect, posY);

                canvas.Children.Add(rect);
                tracker.FaceBox.X      = posX;
                tracker.FaceBox.Y      = posY;
                tracker.FaceBox.Width  = width;
                tracker.FaceBox.Height = height;

                if (!tracker.CollectionEventCalled)
                {
                    if (tracker.ModelBuilder.CollectionStatus.HasFlag(FaceModelBuilderCollectionStatus.FrontViewFramesNeeded))
                    {
                        Rectangle rectFront = CreateFaceBoxRectangle(color, 5, 30, 30);
                        Canvas.SetLeft(rectFront, posX + width / 2);
                        Canvas.SetTop(rectFront, posY + height / 2);

                        canvas.Children.Add(rectFront);
                    }

                    if (tracker.ModelBuilder.CollectionStatus.HasFlag(FaceModelBuilderCollectionStatus.LeftViewsNeeded))
                    {
                        Rectangle rectLeft = CreateFaceBoxRectangle(color, 5, 30, 30);
                        Canvas.SetLeft(rectLeft, posX - width / 2);
                        Canvas.SetTop(rectLeft, posY + height / 2);

                        canvas.Children.Add(rectLeft);
                    }

                    if (tracker.ModelBuilder.CollectionStatus.HasFlag(FaceModelBuilderCollectionStatus.RightViewsNeeded))
                    {
                        Rectangle rectRight = CreateFaceBoxRectangle(color, 5, 30, 30);
                        Canvas.SetLeft(rectRight, posX + width + width / 2);
                        Canvas.SetTop(rectRight, posY + height / 2);

                        canvas.Children.Add(rectRight);
                    }

                    if (tracker.ModelBuilder.CollectionStatus.HasFlag(FaceModelBuilderCollectionStatus.TiltedUpViewsNeeded))
                    {
                        Rectangle rectUp = CreateFaceBoxRectangle(color, 5, 30, 30);
                        Canvas.SetLeft(rectUp, posX + width / 2);
                        Canvas.SetTop(rectUp, posY - height / 2);

                        canvas.Children.Add(rectUp);
                    }
                }
            }
        }