Пример #1
0
    private Rect GetHeadJointFaceRect(long userId)
    {
        Rect faceJointRect = new Rect();

        if (useTrackedFaceRect && faceManager &&
            faceManager.IsFaceTrackingInitialized() && faceManager.IsTrackingFace(userId))
        {
            faceJointRect = faceManager.GetFaceColorRect(userId);
            return(faceJointRect);
        }

        if (kinectManager.IsJointTracked(userId, (int)KinectInterop.JointType.Head))
        {
            Vector3 posHeadRaw = kinectManager.GetJointKinectPosition(userId, (int)KinectInterop.JointType.Head);

            if (posHeadRaw != Vector3.zero)
            {
                Vector2 posDepthHead = kinectManager.MapSpacePointToDepthCoords(posHeadRaw);
                ushort  depthHead    = kinectManager.GetDepthForPixel((int)posDepthHead.x, (int)posDepthHead.y);

                Vector3 sizeHalfFace = new Vector3(faceWidth / 2f, faceHeight / 2f, 0f);
                Vector3 posFaceRaw1  = posHeadRaw - sizeHalfFace;
                Vector3 posFaceRaw2  = posHeadRaw + sizeHalfFace;

                Vector2 posDepthFace1 = kinectManager.MapSpacePointToDepthCoords(posFaceRaw1);
                Vector2 posDepthFace2 = kinectManager.MapSpacePointToDepthCoords(posFaceRaw2);

                if (posDepthFace1 != Vector2.zero && posDepthFace2 != Vector2.zero && depthHead > 0)
                {
                    Vector2 posColorFace1 = kinectManager.MapDepthPointToColorCoords(posDepthFace1, depthHead);
                    Vector2 posColorFace2 = kinectManager.MapDepthPointToColorCoords(posDepthFace2, depthHead);

                    if (!float.IsInfinity(posColorFace1.x) && !float.IsInfinity(posColorFace1.y) &&
                        !float.IsInfinity(posColorFace2.x) && !float.IsInfinity(posColorFace2.y))
                    {
                        faceJointRect.x      = posColorFace1.x;
                        faceJointRect.y      = posColorFace2.y;
                        faceJointRect.width  = Mathf.Abs(posColorFace2.x - posColorFace1.x);
                        faceJointRect.height = Mathf.Abs(posColorFace2.y - posColorFace1.y);
                    }
                }
            }
        }

        return(faceJointRect);
    }