// Initialize the filter with a set of TransformSmoothParameters.
        public void Init(KinectInterop.SmoothParameters smoothParameters)
        {
            this.smoothParameters = smoothParameters;

            this.Reset();
            this.init = true;
        }
        /// <summary>
        /// Initialize the filter with a set of manually specified TransformSmoothParameters.
        /// </summary>
        /// <param name="smoothingValue">Smoothing = [0..1], lower values is closer to the raw data and more noisy.</param>
        /// <param name="correctionValue">Correction = [0..1], higher values correct faster and feel more responsive.</param>
        /// <param name="predictionValue">Prediction = [0..n], how many frames into the future we want to predict.</param>
        /// <param name="jitterRadiusValue">JitterRadius = The deviation distance in m that defines jitter.</param>
        /// <param name="maxDeviationRadiusValue">MaxDeviation = The maximum distance in m that filtered positions are allowed to deviate from raw data.</param>
        public void Init(float smoothingValue, float correctionValue, float predictionValue, float jitterRadiusValue, float maxDeviationRadiusValue)
        {
            this.smoothParameters = new KinectInterop.SmoothParameters();

            this.smoothParameters.smoothing = smoothingValue;                   // How much soothing will occur.  Will lag when too high
            this.smoothParameters.correction = correctionValue;                 // How much to correct back from prediction.  Can make things springy
            this.smoothParameters.prediction = predictionValue;                 // Amount of prediction into the future to use. Can over shoot when too high
            this.smoothParameters.jitterRadius = jitterRadiusValue;             // Size of the radius where jitter is removed. Can do too much smoothing when too high
            this.smoothParameters.maxDeviationRadius = maxDeviationRadiusValue; // Size of the max prediction radius Can snap back to noisy data when too high

            // Check for divide by zero. Use an epsilon of a 10th of a millimeter
            this.smoothParameters.jitterRadius = Math.Max(0.0001f, this.smoothParameters.jitterRadius);

            this.Reset();
            this.init = true;
        }
        // Update the filter with a new frame of data and smooth.
        public void UpdateFilter(ref KinectInterop.BodyFrameData bodyFrame)
        {
            if (this.init == false)
            {
                this.Init();    // initialize with default parameters
            }

            KinectInterop.SmoothParameters tempSmoothingParams = new KinectInterop.SmoothParameters();

            tempSmoothingParams.smoothing = this.smoothParameters.smoothing;
            tempSmoothingParams.correction = this.smoothParameters.correction;
            tempSmoothingParams.prediction = this.smoothParameters.prediction;

            KinectManager manager = KinectManager.Instance;
            int bodyCount = manager.GetBodyCount();

            for (int bodyIndex = 0; bodyIndex < bodyCount; bodyIndex++)
            {
                if (bodyFrame.bodyData[bodyIndex].bIsTracked != 0)
                {
                    FilterBodyJoints(ref bodyFrame.bodyData[bodyIndex], bodyIndex, ref tempSmoothingParams);
                }
            }
        }
Esempio n. 4
0
        public void StartKinect()
        {
            try
            {
                // try to initialize the default Kinect2 sensor
                KinectInterop.FrameSource dwFlags = KinectInterop.FrameSource.TypeBody;

                if (computeUserMap)
                    dwFlags |= KinectInterop.FrameSource.TypeDepth | KinectInterop.FrameSource.TypeBodyIndex;
                if (computeColorMap)
                    dwFlags |= KinectInterop.FrameSource.TypeColor;
                if (computeInfraredMap)
                    dwFlags |= KinectInterop.FrameSource.TypeInfrared;
                //			if(useAudioSource)
                //				dwFlags |= KinectInterop.FrameSource.TypeAudio;

                // open the default sensor
                sensorData = KinectInterop.OpenDefaultSensor(sensorInterfaces, dwFlags, sensorAngle, useMultiSourceReader);
                if (sensorData == null)
                {
                    if (sensorInterfaces == null || sensorInterfaces.Count == 0)
                    {
                        transform.parent.gameObject.SetActive(false);
                        //throw new Exception("No sensor found. Make sure you have installed the SDK and the sensor is connected.");
                        return;
                    }
                    else
                        throw new Exception("OpenDefaultSensor failed.");
                }

                // enable or disable getting height and angle info
                sensorData.hintHeightAngle = (autoHeightAngle != AutoHeightAngle.DontUse);

                //create the transform matrix - kinect to world
                Quaternion quatTiltAngle = Quaternion.Euler(-sensorAngle, 0.0f, 0.0f);
                kinectToWorld.SetTRS(new Vector3(0.0f, sensorHeight, 0.0f), quatTiltAngle, Vector3.one);
            }
            catch (DllNotFoundException ex)
            {
                string message = ex.Message + " cannot be loaded. Please check the Kinect SDK installation.";

                //Debug.LogError(message);
                Debug.LogException(ex);

                if (calibrationText != null)
                {
                    calibrationText.GetComponent<GUIText>().text = message;
                }

                return;
            }
            catch (Exception ex)
            {
                string message = ex.Message;

                //Debug.LogError(message);
                Debug.LogException(ex);

                if (calibrationText != null)
                {
                    calibrationText.GetComponent<GUIText>().text = message;
                }

                return;
            }

            // set the singleton instance
            instance = this;

            // init skeleton structures
            bodyFrame = new KinectInterop.BodyFrameData(sensorData.bodyCount, KinectInterop.Constants.JointCount); // sensorData.jointCount
            bodyFrame.bTurnAnalisys = allowTurnArounds;

            KinectInterop.SmoothParameters smoothParameters = new KinectInterop.SmoothParameters();

            switch (smoothing)
            {
                case Smoothing.Default:
                    smoothParameters.smoothing = 0.5f;
                    smoothParameters.correction = 0.5f;
                    smoothParameters.prediction = 0.5f;
                    smoothParameters.jitterRadius = 0.05f;
                    smoothParameters.maxDeviationRadius = 0.04f;
                    break;
                case Smoothing.Medium:
                    smoothParameters.smoothing = 0.5f;
                    smoothParameters.correction = 0.1f;
                    smoothParameters.prediction = 0.5f;
                    smoothParameters.jitterRadius = 0.1f;
                    smoothParameters.maxDeviationRadius = 0.1f;
                    break;
                case Smoothing.Aggressive:
                    smoothParameters.smoothing = 0.7f;
                    smoothParameters.correction = 0.3f;
                    smoothParameters.prediction = 1.0f;
                    smoothParameters.jitterRadius = 1.0f;
                    smoothParameters.maxDeviationRadius = 1.0f;
                    break;
            }

            // init data filters
            jointPositionFilter = new JointPositionsFilter();
            jointPositionFilter.Init(smoothParameters);

            // init the bone orientation constraints
            if (useBoneOrientationConstraints)
            {
                boneConstraintsFilter = new BoneOrientationsConstraint();
                boneConstraintsFilter.AddDefaultConstraints();
                boneConstraintsFilter.SetDebugText(calibrationText);
            }

            if (computeUserMap)
            {
                // Initialize depth & label map related stuff
                usersLblTex = new Texture2D(sensorData.depthImageWidth, sensorData.depthImageHeight, TextureFormat.ARGB32, false);

                usersMapSize = sensorData.depthImageWidth * sensorData.depthImageHeight;
                usersHistogramImage = new Color32[usersMapSize];
                usersPrevState = new ushort[usersMapSize];
                usersHistogramMap = new float[5001];
            }

            if (computeColorMap)
            {
                // Initialize color map related stuff
                //usersClrTex = new Texture2D(sensorData.colorImageWidth, sensorData.colorImageHeight, TextureFormat.RGBA32, false);
                usersClrSize = sensorData.colorImageWidth * sensorData.colorImageHeight;
            }

            // try to automatically use the available avatar controllers in the scene
            if (avatarControllers.Count == 0)
            {
                MonoBehaviour[] monoScripts = FindObjectsOfType(typeof(MonoBehaviour)) as MonoBehaviour[];

                foreach (MonoBehaviour monoScript in monoScripts)
                {
                    if (typeof(AvatarController).IsAssignableFrom(monoScript.GetType()) &&
                       monoScript.enabled)
                    {
                        AvatarController avatar = (AvatarController)monoScript;
                        avatarControllers.Add(avatar);
                    }
                }
            }

            // try to automatically use the available gesture listeners in the scene
            if (gestureListeners.Count == 0)
            {
                MonoBehaviour[] monoScripts = FindObjectsOfType(typeof(MonoBehaviour)) as MonoBehaviour[];

                foreach (MonoBehaviour monoScript in monoScripts)
                {
                    if (typeof(KinectGestures.GestureListenerInterface).IsAssignableFrom(monoScript.GetType()) &&
                       monoScript.enabled)
                    {
                        //KinectGestures.GestureListenerInterface gl = (KinectGestures.GestureListenerInterface)monoScript;
                        gestureListeners.Add(monoScript);
                    }
                }
            }

            // Initialize user list to contain all users.
            //alUserIds = new List<Int64>();
            //dictUserIdToIndex = new Dictionary<Int64, int>();

            kinectInitialized = true;

#if USE_SINGLE_KM_IN_MULTIPLE_SCENES
            //DontDestroyOnLoad(gameObject);
#endif

            // GUI Text.
            if (calibrationText != null)
            {
                calibrationText.GetComponent<GUIText>().text = "WAITING FOR USERS";
            }

            Debug.Log("Waiting for users.");
        }