Exemplo n.º 1
0
    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)
            {
                throw new Exception("OpenDefaultSensor failed");
            }

            //create the transform matrix - kinect to world
            Quaternion quatTiltAngle = new Quaternion();
            quatTiltAngle.eulerAngles = new Vector3(-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.guiText.text = message;
            }

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

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

            if(calibrationText != null)
            {
                calibrationText.guiText.text = message;
            }

            return;
        }

        // set the singleton instance
        instance = this;

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

        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);
        }

        // get the main camera rectangle
        Rect cameraRect = Camera.main.pixelRect;

        // calculate map width and height in percent, if needed
        if(DisplayMapsWidthPercent == 0f)
        {
            DisplayMapsWidthPercent = (sensorData.depthImageWidth / 2) * 100 / cameraRect.width;
        }

        if(computeUserMap)
        {
            float displayMapsWidthPercent = DisplayMapsWidthPercent / 100f;
            float displayMapsHeightPercent = displayMapsWidthPercent * sensorData.depthImageHeight / sensorData.depthImageWidth;

            float displayWidth = cameraRect.width * displayMapsWidthPercent;
            float displayHeight = cameraRect.width * displayMapsHeightPercent;

            // Initialize depth & label map related stuff
            usersLblTex = new Texture2D(sensorData.depthImageWidth, sensorData.depthImageHeight);
            usersMapRect = new Rect(cameraRect.width - displayWidth, cameraRect.height, displayWidth, -displayHeight);

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

        if(computeColorMap)
        {
            float displayMapsWidthPercent = DisplayMapsWidthPercent / 100f;
            float displayMapsHeightPercent = displayMapsWidthPercent * sensorData.colorImageHeight / sensorData.colorImageWidth;

            float displayWidth = cameraRect.width * displayMapsWidthPercent;
            float displayHeight = cameraRect.width * displayMapsHeightPercent;

            // Initialize color map related stuff
            usersClrTex = new Texture2D(sensorData.colorImageWidth, sensorData.colorImageHeight, TextureFormat.RGBA32, false);
            usersClrRect = new Rect(cameraRect.width - displayWidth, cameraRect.height, displayWidth, -displayHeight);
            usersClrSize = sensorData.colorImageWidth * sensorData.colorImageHeight;

        //			if(computeUserMap && displayColorMap)
        //			{
        //				usersMapRect.x -= cameraRect.width * displayMapsWidthPercent;
        //			}
        }

        // 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()))
                {
                    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()))
                {
                    print ("Found Listener");
                    //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;
        DontDestroyOnLoad(gameObject);

        // GUI Text.
        if(calibrationText != null)
        {
            calibrationText.guiText.text = "WAITING FOR USERS";
        }

        Debug.Log("Waiting for users.");
    }
Exemplo n.º 2
0
	void StartKinect() 
	{
		try
		{
			// try to initialize the default Kinect2 sensor
			KinectInterop.FrameSource dwFlags = KinectInterop.FrameSource.TypeBody;

			if(computeUserMap != UserMapType.None)
				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
			BackgroundRemovalManager brManager = gameObject.GetComponentInChildren<BackgroundRemovalManager>();
			sensorData = KinectInterop.OpenDefaultSensor(sensorInterfaces, dwFlags, sensorAngle, useMultiSourceReader, computeUserMap, brManager);

			if (sensorData == null)
			{
				if(sensorInterfaces == null || sensorInterfaces.Count == 0)
					throw new Exception("No sensor found. Make sure you have installed the SDK and the sensor is connected.");
				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.text = message;
			}
			
			return;
		}
		catch(Exception ex)
		{
			string message = ex.Message;

			Debug.LogError(message);
			Debug.LogException(ex);
			
			if(calibrationText != null)
			{
				calibrationText.text = message;
			}
			
			return;
		}

		// set the singleton instance
		instance = this;
		
		// init skeleton structures
		bodyFrame = new KinectInterop.BodyFrameData(sensorData.bodyCount, KinectInterop.Constants.MaxJointCount); // 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 != UserMapType.None && computeUserMap != UserMapType.RawUserDepth)
		{
			// 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);
				}
			}
		}

		// set up the gesture manager, if not already set
		if(gestureManager == null)
		{
			MonoBehaviour[] monoScripts = FindObjectsOfType(typeof(MonoBehaviour)) as MonoBehaviour[];
			
			foreach(MonoBehaviour monoScript in monoScripts)
			{
				if(typeof(KinectGestures).IsAssignableFrom(monoScript.GetType()) && monoScript.enabled)
				{
					gestureManager = (KinectGestures)monoScript;
					break;
				}
			}

		}

		// 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>();

//		// start the background reader
//		kinectReaderThread = new System.Threading.Thread(UpdateKinectStreamsThread);
//		kinectReaderThread.Name = "KinectReaderThread";
//		kinectReaderThread.IsBackground = true;
//		kinectReaderThread.Start();
//		kinectReaderRunning = true;

		kinectInitialized = true;

#if USE_SINGLE_KM_IN_MULTIPLE_SCENES
		DontDestroyOnLoad(gameObject);
#endif
		
		// GUI Text.
		if(calibrationText != null)
		{
			calibrationText.text = "WAITING FOR USERS";
		}
		
		Debug.Log("Waiting for users.");
	}
Exemplo n.º 3
0
    void Start()
    {
        //		int hr = 0;

        try
        {
        //			if(startKinectServer)
        //			{
        //				// start the Kinect-server app
        //				hr = StartKinectServer();
        //	            if (hr != 0)
        //				{
        //	            	throw new Exception("Kinect2Server not started");
        //				}
        //			}

            // 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;

        //			hr = KinectInterop.InitDefaultKinectSensor(dwFlags, KinectInterop.Constants.ColorImageWidth, KinectInterop.Constants.ColorImageHeight);
        //            if (hr != 0)
        //			{
        //            	throw new Exception("InitDefaultKinectSensor failed");
        //			}

            // open the default kinect sensor
            sensorData = KinectInterop.OpenDefaultKinectSensor(dwFlags);
            if (sensorData == null)
            {
                throw new Exception("OpenDefaultKinectSensor failed");
            }

            // transform matrix - kinect to world
            kinectToWorld.SetTRS(new Vector3(0.0f, sensorHeight, 0.0f), Quaternion.identity, 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.guiText.text = message;
            }

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

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

            if(calibrationText != null)
            {
                calibrationText.guiText.text = message;
            }

            return;
        }

        // init skeleton structures
        bodyFrame = new KinectInterop.BodyFrameData(true);

        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);
        }

        // get the main camera rectangle
        Rect cameraRect = Camera.main.pixelRect;

        // calculate map width and height in percent, if needed
        if(MapsPercentWidth == 0f)
            MapsPercentWidth = (sensorData.depthImageWidth / 2) / cameraRect.width;
        if(MapsPercentHeight == 0f)
            MapsPercentHeight = (sensorData.depthImageHeight / 2) / cameraRect.height;

        if(computeUserMap)
        {
            // init user-depth structures
            //depthImage = new KinectInterop.DepthBuffer(true);
            //bodyIndexImage = new KinectInterop.BodyIndexBuffer(true);

            // Initialize depth & label map related stuff
            usersLblTex = new Texture2D(sensorData.depthImageWidth, sensorData.depthImageHeight);
            usersMapRect = new Rect(cameraRect.width - cameraRect.width * MapsPercentWidth, cameraRect.height, cameraRect.width * MapsPercentWidth, -cameraRect.height * MapsPercentHeight);

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

        if(computeColorMap)
        {
            // init color image structures
            //colorImage = new KinectInterop.ColorBuffer(true);

            // Initialize color map related stuff
            usersClrTex = new Texture2D(sensorData.colorImageWidth, sensorData.colorImageHeight, TextureFormat.RGBA32, false);
            usersClrRect = new Rect(cameraRect.width - 320, cameraRect.height, 320, -180);
            usersCaliBox = new Rect(cameraRect.width - 160 - 320 / 14, cameraRect.height - 90, 320 / 7, 90);
            usersClrSize = sensorData.colorImageWidth * sensorData.colorImageHeight;

            if(computeUserMap && displayColorMap)
            {
                usersMapRect.x -= cameraRect.width * MapsPercentWidth; //usersClrTex.width / 2;
            }
        }

        // try to automatically find the available avatar controllers at the scene
        if(avatarControllers.Count == 0)
        {
            AvatarController[] avatars = FindObjectsOfType(typeof(AvatarController)) as AvatarController[];

            foreach(AvatarController avatar in avatars)
            {
                avatarControllers.Add(avatar);
            }
        }

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

        kinectInitialized = true;
        instance = this;

        //DontDestroyOnLoad(gameObject);

        // GUI Text.
        if(calibrationText != null)
        {
            calibrationText.guiText.text = "WAITING FOR USERS";
        }

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