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