Ejemplo n.º 1
0
    void Awake()
    {
        try
        {
//			bool bOnceRestarted = false;
//			if(System.IO.File.Exists("SCrestart.txt"))
//			{
//				bOnceRestarted = true;
//
//				try
//				{
//					System.IO.File.Delete("SCrestart.txt");
//				}
//				catch(Exception ex)
//				{
//					Debug.LogError("Error deleting SCrestart.txt");
//					Debug.LogError(ex.ToString());
//				}
//			}

            // init the available sensor interfaces
            sensorInterface = new Kinect2Interface();

            bool bNeedRestart = false;
            if (sensorInterface.InitSensorInterface(true, ref bNeedRestart))
            {
                if (bNeedRestart)
                {
                    System.IO.File.WriteAllText("SCrestart.txt", "Restarting level...");
                    KinectInterop.RestartLevel(gameObject, "SC");
                    return;
                }
                else
                {
                    // check if a sensor is connected
                    bSensorAvailable = sensorInterface.GetSensorsCount() > 0;

                    if (infoText != null)
                    {
                        infoText.text = bSensorAvailable ? "Sensor is connected." : "No sensor is connected.";
                    }
                }
            }
            else
            {
                sensorInterface.FreeSensorInterface(true);
                sensorInterface = null;
            }
        }
        catch (Exception ex)
        {
            Debug.LogError(ex.ToString());

            if (infoText != null)
            {
                infoText.text = ex.Message;
            }
        }
    }
    //----------------------------------- end of public functions --------------------------------------//

    void Start()
    {
        try
        {
            // get sensor data
            KinectManager kinectManager = KinectManager.Instance;
            if (kinectManager && kinectManager.IsInitialized())
            {
                sensorData = kinectManager.GetSensorData();
            }

            if (sensorData == null || sensorData.sensorInterface == null)
            {
                throw new Exception("Background removal cannot be started, because KinectManager is missing or not initialized.");
            }

            // ensure the needed dlls are in place and speech recognition is available for this interface
            bool bNeedRestart = false;
            bool bSuccess     = sensorData.sensorInterface.IsBackgroundRemovalAvailable(ref bNeedRestart);

            if (bSuccess)
            {
                if (bNeedRestart)
                {
                    KinectInterop.RestartLevel(gameObject, "BR");
                    return;
                }
            }
            else
            {
                string sInterfaceName = sensorData.sensorInterface.GetType().Name;
                throw new Exception(sInterfaceName + ": Background removal is not supported!");
            }

            // Initialize the background removal
            bSuccess = sensorData.sensorInterface.InitBackgroundRemoval(sensorData, colorCameraResolution);

            if (!bSuccess)
            {
                throw new Exception("Background removal could not be initialized.");
            }

            // create the foreground image and alpha-image
            int imageLength = sensorData.sensorInterface.GetForegroundFrameLength(sensorData, colorCameraResolution);
            foregroundImage = new byte[imageLength];

            // get the needed rectangle
            Rect neededFgRect = sensorData.sensorInterface.GetForegroundFrameRect(sensorData, colorCameraResolution);

            // create the foreground texture
            foregroundTex = new Texture2D((int)neededFgRect.width, (int)neededFgRect.height, TextureFormat.RGBA32, false);

            // calculate the foreground rectangle
            if (foregroundCamera != null)
            {
                Rect  cameraRect = foregroundCamera.pixelRect;
                float rectHeight = cameraRect.height;
                float rectWidth  = cameraRect.width;

                if (rectWidth > rectHeight)
                {
                    rectWidth = Mathf.Round(rectHeight * neededFgRect.width / neededFgRect.height);
                }
                else
                {
                    rectHeight = Mathf.Round(rectWidth * neededFgRect.height / neededFgRect.width);
                }

                foregroundRect = new Rect((cameraRect.width - rectWidth) / 2, cameraRect.height - (cameraRect.height - rectHeight) / 2, rectWidth, -rectHeight);
            }

            instance   = this;
            isBrInited = true;

            //DontDestroyOnLoad(gameObject);
        }
        catch (DllNotFoundException ex)
        {
            Debug.LogError(ex.ToString());
            if (debugText != null)
            {
                debugText.GetComponent <GUIText>().text = "Please check the Kinect and BR-Library installations.";
            }
        }
        catch (Exception ex)
        {
            Debug.LogError(ex.ToString());
            if (debugText != null)
            {
                debugText.GetComponent <GUIText>().text = ex.Message;
            }
        }
    }
Ejemplo n.º 3
0
    void Start()
    {
        try
        {
            // get sensor data
            KinectManager kinectManager = KinectManager.Instance;
            if (kinectManager && kinectManager.IsInitialized())
            {
                sensorData = kinectManager.GetSensorData();
            }

            if (sensorData == null || sensorData.sensorInterface == null)
            {
                throw new Exception("Face tracking cannot be started, because KinectManager is missing or not initialized.");
            }

            if (debugText != null)
            {
                debugText.text = "Please, wait...";
            }

            // ensure the needed dlls are in place and face tracking is available for this interface
            bool bNeedRestart = false;
            if (sensorData.sensorInterface.IsFaceTrackingAvailable(ref bNeedRestart))
            {
                if (bNeedRestart)
                {
                    KinectInterop.RestartLevel(gameObject, "FM");
                    return;
                }
            }
            else
            {
                string sInterfaceName = sensorData.sensorInterface.GetType().Name;
                throw new Exception(sInterfaceName + ": Face tracking is not supported!");
            }

            // Initialize the face tracker
            if (!sensorData.sensorInterface.InitFaceTracking(getFaceModelData, displayFaceRect))
            {
                throw new Exception("Face tracking could not be initialized.");
            }

            isFacetrackingInitialized = true;

            //DontDestroyOnLoad(gameObject);

            if (debugText != null)
            {
                debugText.text = "Ready.";
            }
        }
        catch (DllNotFoundException ex)
        {
            Debug.LogError(ex.ToString());
            if (debugText != null)
            {
                debugText.text = "Please check the Kinect and FT-Library installations.";
            }
        }
        catch (Exception ex)
        {
            Debug.LogError(ex.ToString());
            if (debugText != null)
            {
                debugText.text = ex.Message;
            }
        }
    }
    void Start()
    {
        try
        {
            // get sensor data
            KinectManager            kinectManager = KinectManager.Instance;
            KinectInterop.SensorData sensorData    = kinectManager != null?kinectManager.GetSensorData() : null;

            if (sensorData == null || sensorData.sensorInterface == null)
            {
                throw new Exception("Visual gesture tracking cannot be started, because the KinectManager is missing or not initialized.");
            }

            if (sensorData.sensorInterface.GetSensorPlatform() != KinectInterop.DepthSensorPlatform.KinectSDKv2)
            {
                throw new Exception("Visual gesture tracking is only supported by Kinect SDK v2");
            }

            // ensure the needed dlls are in place and face tracking is available for this interface
            bool bNeedRestart = false;
            if (IsVisualGesturesAvailable(ref bNeedRestart))
            {
                if (bNeedRestart)
                {
                    KinectInterop.RestartLevel(gameObject, "VG");
                    return;
                }
            }
            else
            {
                throw new Exception("Visual gesture tracking is not supported!");
            }

            // initialize visual gesture tracker
            if (!InitVisualGestures())
            {
                throw new Exception("Visual gesture tracking could not be initialized.");
            }

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

                foreach (MonoBehaviour monoScript in monoScripts)
                {
//					if(typeof(VisualGestureListenerInterface).IsAssignableFrom(monoScript.GetType()) &&
//					   monoScript.enabled)
                    if ((monoScript is VisualGestureListenerInterface) && monoScript.enabled)
                    {
                        visualGestureListeners.Add(monoScript);
                    }
                }
            }

            isVisualGestureInitialized = true;
        }
        catch (DllNotFoundException ex)
        {
            Debug.LogError(ex.ToString());
            if (debugText != null)
            {
                debugText.text = "Please check the Kinect and VGB-Library installations.";
            }
        }
        catch (Exception ex)
        {
            Debug.LogError(ex.ToString());
            if (debugText != null)
            {
                debugText.text = ex.Message;
            }
        }
    }
Ejemplo n.º 5
0
    void Start()
    {
        try
        {
            // get sensor data
            KinectManager kinectManager = KinectManager.Instance;
            if (kinectManager && kinectManager.IsInitialized())
            {
                sensorData = kinectManager.GetSensorData();
            }

            if (sensorData == null || sensorData.sensorInterface == null)
            {
                throw new Exception("Speech recognition cannot be started, because KinectManager is missing or not initialized.");
            }

            if (debugText != null)
            {
                debugText.text = "Please, wait...";
            }

            // ensure the needed dlls are in place and speech recognition is available for this interface
            bool bNeedRestart = false;
            if (sensorData.sensorInterface.IsSpeechRecognitionAvailable(ref bNeedRestart))
            {
                if (bNeedRestart)
                {
                    KinectInterop.RestartLevel(gameObject, "SM");
                    return;
                }
            }
            else
            {
                string sInterfaceName = sensorData.sensorInterface.GetType().Name;
                throw new Exception(sInterfaceName + ": Speech recognition is not supported!");
            }

            // Initialize the speech recognizer
            string sCriteria = String.Format("Language={0:X};Kinect=True", languageCode);
            int    rc        = sensorData.sensorInterface.InitSpeechRecognition(sCriteria, true, false);
            if (rc < 0)
            {
                string sErrorMessage = (new SpeechErrorHandler()).GetSapiErrorMessage(rc);
                throw new Exception(String.Format("Error initializing Kinect/SAPI: " + sErrorMessage));
            }

            if (requiredConfidence > 0)
            {
                sensorData.sensorInterface.SetSpeechConfidence(requiredConfidence);
            }

            if (grammarFileName != string.Empty)
            {
                // copy the grammar file from Resources, if available
                //if(!File.Exists(grammarFileName))
                {
                    TextAsset textRes = Resources.Load(grammarFileName, typeof(TextAsset)) as TextAsset;

                    if (textRes != null)
                    {
                        string sResText = textRes.text;
                        File.WriteAllText(grammarFileName, sResText);
                    }
                    else
                    {
                        throw new Exception("Couldn't find grammar resource: " + grammarFileName + ".txt");
                    }
                }

                // load the grammar file
                rc = sensorData.sensorInterface.LoadSpeechGrammar(grammarFileName, (short)languageCode, dynamicGrammar);
                if (rc < 0)
                {
                    string sErrorMessage = (new SpeechErrorHandler()).GetSapiErrorMessage(rc);
                    throw new Exception("Error loading grammar file " + grammarFileName + ": " + sErrorMessage);
                }

//				// test dynamic grammar phrases
//				AddGrammarPhrase("addressBook", string.Empty, "Nancy Anderson", true, false);
//				AddGrammarPhrase("addressBook", string.Empty, "Cindy White", false, false);
//				AddGrammarPhrase("addressBook", string.Empty, "Oliver Lee", false, false);
//				AddGrammarPhrase("addressBook", string.Empty, "Alan Brewer", false, false);
//				AddGrammarPhrase("addressBook", string.Empty, "April Reagan", false, true);
            }

            sapiInitialized = true;

            //DontDestroyOnLoad(gameObject);

            if (debugText != null)
            {
                debugText.text = "Speech recognizer is ready.";
            }

            // try to automatically detect the available speech recognition listeners in the scene
            if (speechRecognitionListeners.Count == 0)
            {
                MonoBehaviour[] monoScripts = FindObjectsOfType(typeof(MonoBehaviour)) as MonoBehaviour[];

                foreach (MonoBehaviour monoScript in monoScripts)
                {
                    if ((monoScript is SpeechRecognitionInterface) && monoScript.enabled)
                    {
                        speechRecognitionListeners.Add(monoScript);
                    }
                }
            }
        }
        catch (DllNotFoundException ex)
        {
            Debug.LogError(ex.ToString());
            if (debugText != null)
            {
                debugText.text = "Please check the Kinect and SAPI installations.";
            }
        }
        catch (Exception ex)
        {
            Debug.LogError(ex.ToString());
            if (debugText != null)
            {
                debugText.text = ex.Message;
            }
        }
    }
Ejemplo n.º 6
0
    //----------------------------------- end of public functions --------------------------------------//

    void Start()
    {
        try
        {
            // get sensor data
            KinectManager kinectManager = KinectManager.Instance;
            if (kinectManager && kinectManager.IsInitialized())
            {
                sensorData = kinectManager.GetSensorData();
            }

            if (sensorData == null || sensorData.sensorInterface == null)
            {
                throw new Exception("Speech recognition cannot be started, because KinectManager is missing or not initialized.");
            }

            if (debugText != null)
            {
                debugText.GetComponent <GUIText>().text = "Please, wait...";
            }

            // ensure the needed dlls are in place and speech recognition is available for this interface
            bool bNeedRestart = false;
            if (sensorData.sensorInterface.IsSpeechRecognitionAvailable(ref bNeedRestart))
            {
                if (bNeedRestart)
                {
                    KinectInterop.RestartLevel(gameObject, "SM");
                    return;
                }
            }
            else
            {
                string sInterfaceName = sensorData.sensorInterface.GetType().Name;
                throw new Exception(sInterfaceName + ": Speech recognition is not supported!");
            }

            // Initialize the speech recognizer
            string sCriteria = String.Format("Language={0:X};Kinect=True", languageCode);
            int    rc        = sensorData.sensorInterface.InitSpeechRecognition(sCriteria, true, false);
            if (rc < 0)
            {
                string sErrorMessage = (new SpeechErrorHandler()).GetSapiErrorMessage(rc);
                throw new Exception(String.Format("Error initializing Kinect/SAPI: " + sErrorMessage));
            }

            if (requiredConfidence > 0)
            {
                sensorData.sensorInterface.SetSpeechConfidence(requiredConfidence);
            }

            if (grammarFileName != string.Empty)
            {
                // copy the grammar file from Resources, if available
                if (!File.Exists(grammarFileName))
                {
                    TextAsset textRes = Resources.Load(grammarFileName, typeof(TextAsset)) as TextAsset;

                    if (textRes != null)
                    {
                        string sResText = textRes.text;
                        File.WriteAllText(grammarFileName, sResText);
                    }
                }

                // load the grammar file
                rc = sensorData.sensorInterface.LoadSpeechGrammar(grammarFileName, (short)languageCode);
                if (rc < 0)
                {
                    string sErrorMessage = (new SpeechErrorHandler()).GetSapiErrorMessage(rc);
                    throw new Exception("Error loading grammar file " + grammarFileName + ": " + sErrorMessage);
                }
            }

            instance        = this;
            sapiInitialized = true;

            //DontDestroyOnLoad(gameObject);

            if (debugText != null)
            {
                debugText.GetComponent <GUIText>().text = "Ready.";
            }
        }
        catch (DllNotFoundException ex)
        {
            Debug.LogError(ex.ToString());
            if (debugText != null)
            {
                debugText.GetComponent <GUIText>().text = "Please check the Kinect and SAPI installations.";
            }
        }
        catch (Exception ex)
        {
            Debug.LogError(ex.ToString());
            if (debugText != null)
            {
                debugText.GetComponent <GUIText>().text = ex.Message;
            }
        }
    }