/// <summary>Creates a body tracker.</summary> /// <param name="calibration">The sensor calibration that will be used for capture processing.</param> /// <param name="config">The configuration we want to run the tracker in. This can be initialized with <see cref="TrackerConfiguration.Default"/>.</param> /// <remarks><para> /// Under the hood Body Tracking runtime will be initialized during the first call of this constructor. /// It is rather time consuming operation: initialization of ONNX runtime, loading and parsing of neural network model, etc. /// For this reason, it is recommended to initialize Body Tracking runtime in advance: <see cref="Sdk.TryInitializeBodyTrackingRuntime(out string)"/>. /// </para><para> /// Also, Body Tracking runtime must be available in one of the following locations: /// directory with executable file, /// directory with <c>K4AdotNet</c> assembly, /// installation directory of Body Tracking SDK under <c>Program Files</c>. /// </para></remarks> /// <exception cref="ArgumentOutOfRangeException"> /// Invalid value of <paramref name="calibration"/>: <see cref="Calibration.DepthMode"/> cannot be <see cref="DepthMode.Off"/> and <see cref="DepthMode.PassiveIR"/>. /// Because depth data is required for body tracking. /// </exception> /// <exception cref="NotSupportedException"> /// Only one tracker is allowed to exist at the same time in each process. If you call this constructor without destroying the /// previous tracker you created, the constructor call will fail with this exception. /// </exception> /// <exception cref="BodyTrackingException"> /// Unable to find/initialize Body Tracking runtime. /// </exception> /// <seealso cref="Sdk.IsBodyTrackingRuntimeAvailable(out string)"/> /// <seealso cref="Sdk.TryInitializeBodyTrackingRuntime(out string)"/> public Tracker(ref Calibration calibration, TrackerConfiguration config = default(TrackerConfiguration)) { if (!calibration.DepthMode.HasDepth()) { throw new ArgumentOutOfRangeException(nameof(calibration) + "." + nameof(calibration.DepthMode)); } DepthMode = calibration.DepthMode; var incrementedInstanceCounter = Interlocked.Increment(ref instancesCounter); try { if (incrementedInstanceCounter != 1) { throw new NotSupportedException("Oops! Current version of Body Tracking runtime does not support creation of multiple body trackers. Sorry!"); } if (!Sdk.TryCreateTrackerHandle(ref calibration, config, out var handle, out var message)) { throw new BodyTrackingException(message); } this.handle = handle; this.handle.Disposed += Handle_Disposed; } catch { Interlocked.Decrement(ref instancesCounter); throw; } }
/// <summary>Creates a body tracker.</summary> /// <param name="calibration">The sensor calibration that will be used for capture processing.</param> /// <param name="config">The configuration we want to run the tracker in. This can be initialized with <see cref="TrackerConfiguration.Default"/>.</param> /// <remarks><para> /// Under the hood Body Tracking runtime will be initialized during the first call of this constructor. /// It is rather time consuming operation: initialization of ONNX runtime, loading and parsing of neural network model, etc. /// For this reason, it is recommended to initialize Body Tracking runtime in advance: <see cref="Sdk.TryInitializeBodyTrackingRuntime(out string)"/>. /// </para><para> /// Also, Body Tracking runtime must be available in one of the following locations: /// directory with executable file, /// directory with <c>K4AdotNet</c> assembly, /// installation directory of Body Tracking SDK under <c>Program Files</c>. /// </para></remarks> /// <exception cref="ArgumentOutOfRangeException"> /// Invalid value of <paramref name="calibration"/>: <see cref="Calibration.DepthMode"/> cannot be <see cref="DepthMode.Off"/> and <see cref="DepthMode.PassiveIR"/>. /// Because depth data is required for body tracking. /// </exception> /// <exception cref="BodyTrackingException"> /// Unable to find/initialize Body Tracking runtime. /// </exception> /// <seealso cref="Sdk.IsBodyTrackingRuntimeAvailable(out string)"/> /// <seealso cref="Sdk.TryInitializeBodyTrackingRuntime(out string)"/> public Tracker(ref Calibration calibration, TrackerConfiguration config = default) { if (!calibration.DepthMode.HasDepth()) { throw new ArgumentOutOfRangeException(nameof(calibration) + "." + nameof(calibration.DepthMode)); } DepthMode = calibration.DepthMode; if (!Sdk.TryCreateTrackerHandle(ref calibration, config, out var handle, out var message)) { throw new BodyTrackingException(message); } this.handle = handle; this.handle.Disposed += Handle_Disposed; }
internal static bool TryCreateTrackerHandle(ref Sensor.Calibration calibration, BodyTracking.TrackerConfiguration config, out NativeHandles.TrackerHandle trackerHandle, out string message) { string runtimePath; lock (bodyTrackingRuntimeInitializationSync) { if (string.IsNullOrEmpty(bodyTrackingRuntimePath)) { bodyTrackingRuntimePath = GetBodyTrackingRuntimePath(out message); if (string.IsNullOrEmpty(bodyTrackingRuntimePath)) { trackerHandle = null; return(false); } } runtimePath = bodyTrackingRuntimePath; } if (!CheckBodyTrackingRuntimeVersion(bodyTrackingRuntimePath, out message)) { trackerHandle = null; return(false); } // Force loading of k4a.dll, // because k4abt.dll depends on it var tmp = new Sensor.Capture(); tmp.Dispose(); // We have to change current directory, // because Body Tracking runtime will try to load ONNX file from current directory // (Adding to Path environment variable doesn't work.) using (new CurrentDirectoryOverrider(runtimePath)) { if (BodyTracking.NativeApi.TrackerCreate(ref calibration, config, out trackerHandle) != NativeCallResults.Result.Succeeded || trackerHandle == null || trackerHandle.IsInvalid) { if (IsBodyTrackingRuntimeAvailable(out message)) { message = "Cannot initialize body tracking runtime. See logs for details."; } return(false); } } message = null; return(true); }
public static extern NativeCallResults.Result TrackerCreate( [In] ref Sensor.Calibration sensorCalibration, TrackerConfiguration config, out NativeHandles.TrackerHandle?trackerHandle);