/// <summary> /// Helper method that does the core instantiation & initialization of face tracking engine /// </summary> /// <param name="newColorCameraConfig">Color camera configuration</param> /// <param name="newDepthCameraConfig">Depth camera configuration</param> /// <param name="colorImagePtr">Allows face tracking engine to read color image from native memory pointer. /// If set to IntPtr.Zero, image data needs to be provided for tracking to this instance. </param> /// <param name="depthImagePtr">Allows face tracking engine to read depth image from native memory pointer. /// If set to IntPtr.Zero, image data needs to be provided for tracking to this instance.</param> /// <param name="newRegisterDepthToColorDelegate">Callback which maps of depth to color pixels</param> private void Initialize( CameraConfig newColorCameraConfig, CameraConfig newDepthCameraConfig, IntPtr colorImagePtr, IntPtr depthImagePtr, FaceTrackingRegisterDepthToColor newRegisterDepthToColorDelegate) { if (newColorCameraConfig == null) { throw new ArgumentNullException("newColorCameraConfig"); } if (newDepthCameraConfig == null) { throw new ArgumentNullException("newDepthCameraConfig"); } if (newRegisterDepthToColorDelegate == null) { throw new ArgumentNullException("newRegisterDepthToColorDelegate"); } // initialize perf counters this.totalTracks = 0; this.trackStopwatch.Reset(); // get configuration & trace settings this.traceLevel = new TraceSwitch(FaceTrackTraceSwitchName, FaceTrackTraceSwitchName).Level; this.videoCameraConfig = newColorCameraConfig; this.depthCameraConfig = newDepthCameraConfig; this.registerDepthToColorDelegate = newRegisterDepthToColorDelegate; this.faceTrackerInteropPtr = NativeMethods.FTCreateFaceTracker(IntPtr.Zero); if (this.faceTrackerInteropPtr == null) { throw new InsufficientMemoryException("Cannot create face tracker."); } IntPtr funcPtr = Marshal.GetFunctionPointerForDelegate(this.registerDepthToColorDelegate); if (funcPtr == IntPtr.Zero) { throw new InsufficientMemoryException("Cannot setup callback for retrieving color to depth pixel mapping"); } int hr = this.faceTrackerInteropPtr.Initialize(this.videoCameraConfig, this.depthCameraConfig, funcPtr, null); if (hr != 0) { throw new InvalidOperationException( string.Format(CultureInfo.CurrentCulture, "Failed to initialize face tracker - Error code from native=0x{0:X}", hr)); } this.frame = this.CreateResult(out hr); if (this.frame == null || hr != 0) { throw new InvalidOperationException( string.Format(CultureInfo.CurrentCulture, "Failed to create face tracking result. Error code from native=0x{0:X}", hr)); } this.colorFaceTrackingImage = new Image(); if (colorImagePtr == IntPtr.Zero) { this.colorFaceTrackingImage.Allocate( this.videoCameraConfig.Width, this.videoCameraConfig.Height, this.videoCameraConfig.ImageFormat); } else { this.colorFaceTrackingImage.Attach( this.videoCameraConfig.Width, this.videoCameraConfig.Height, colorImagePtr, this.videoCameraConfig.ImageFormat, this.videoCameraConfig.Stride); } this.depthFaceTrackingImage = new Image(); if (depthImagePtr == IntPtr.Zero) { this.depthFaceTrackingImage.Allocate( this.depthCameraConfig.Width, this.depthCameraConfig.Height, this.depthCameraConfig.ImageFormat); } else { this.depthFaceTrackingImage.Attach( this.depthCameraConfig.Width, this.depthCameraConfig.Height, depthImagePtr, this.depthCameraConfig.ImageFormat, this.depthCameraConfig.Stride); } }