private async Task SetHardwareFaceDetectionAsync(MediaCapture mediaCapture = null, bool enable = true) { try { if (mediaCapture == null) { mediaCapture = new MediaCapture(); mediaCapture.Failed += MediaCapture_Failed; var initializeCancellationToken = new CancellationTokenSource(); await mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings() { MemoryPreference = MediaCaptureMemoryPreference.Cpu, SharingMode = MediaCaptureSharingMode.ExclusiveControl, SourceGroup = ExclusiveSourceGroup, StreamingCaptureMode = StreamingCaptureMode.Video }); mediaCapture.CameraStreamStateChanged += MediaCapture_CameraStreamStateChanged; mediaCapture.CaptureDeviceExclusiveControlStatusChanged += MediaCapture_CaptureDeviceExclusiveControlStatusChanged; } var irFrameSource = mediaCapture.FrameSources[ExclusiveIrSourceInfo.Id]; var irPropertyGet = await irFrameSource.Controller.GetPropertyAsync(KS.PROPERTY_CAMERACONTROL_EXTENDED_FACEDETECTION); if (irPropertyGet.Status == MediaFrameSourceGetPropertyStatus.Success && irPropertyGet.Value is byte[]) { const bool SetFaceDetectionOn = true; if (SetFaceDetectionOn) { var irProperty = KSCAMERA_EXTENDEDPROP.FromBytes(irPropertyGet.Value as byte[], 0); UInt64 flags = 0u; if (!enable) { flags = KS.CAMERA_EXTENDEDPROP_FACEDETECTION_OFF; } else if ((ExclusiveIrSourceInfo.MediaStreamType == MediaStreamType.VideoPreview) && ((irProperty.Header.Capability & KS.CAMERA_EXTENDEDPROP_FACEDETECTION_PREVIEW) != 0)) { flags = KS.CAMERA_EXTENDEDPROP_FACEDETECTION_PREVIEW; } else if ((ExclusiveIrSourceInfo.MediaStreamType == MediaStreamType.VideoRecord) && ((irProperty.Header.Capability & KS.CAMERA_EXTENDEDPROP_FACEDETECTION_VIDEO) != 0)) { flags = KS.CAMERA_EXTENDEDPROP_FACEDETECTION_VIDEO; } if (flags != irProperty.Header.Flags) { irProperty.Header.Flags = flags; var newProperty = irProperty.GetBytes(); var irPropertySet = await irFrameSource.Controller.SetPropertyAsync(KS.PROPERTY_CAMERACONTROL_EXTENDED_FACEDETECTION, newProperty); if (irPropertySet == MediaFrameSourceSetPropertyStatus.Success) { HardwareFaceDetectionEnabled = enable; } else { // SetHardwareFaceDetectionAsync.Set.Failed } } else { HardwareFaceDetectionEnabled = enable; } } else { HardwareFaceDetectionEnabled = enable; } } else { // SetHardwareFaceDetectionAsync.Get.Failed } } finally { // TODO hardware face detection will throw if already activated } }
private async Task SetFaceModeAsync(MediaCapture mediaCapture = null) { try { if (mediaCapture == null) { mediaCapture = new MediaCapture(); mediaCapture.Failed += MediaCapture_Failed; var initializeCancellationToken = new CancellationTokenSource(); await mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings() { MemoryPreference = MediaCaptureMemoryPreference.Cpu, SharingMode = MediaCaptureSharingMode.ExclusiveControl, SourceGroup = ExclusiveSourceGroup, StreamingCaptureMode = StreamingCaptureMode.Video }); mediaCapture.CameraStreamStateChanged += MediaCapture_CameraStreamStateChanged; mediaCapture.CaptureDeviceExclusiveControlStatusChanged += MediaCapture_CaptureDeviceExclusiveControlStatusChanged; } var irFrameSource = mediaCapture.FrameSources[ExclusiveIrSourceInfo.Id]; var irPropertyGet = await irFrameSource.Controller.GetPropertyAsync(KS.PROPERTY_CAMERACONTROL_EXTENDED_FACEAUTH_MODE); if (irPropertyGet.Status == MediaFrameSourceGetPropertyStatus.Success && irPropertyGet.Value is byte[]) { var irProperty = KSCAMERA_EXTENDEDPROP.FromBytes(irPropertyGet.Value as byte[], 0); UInt64 flags = 0u; if ((irProperty.Header.Capability & KS.CAMERA_EXTENDEDPROP_FACEAUTH_MODE_ALTERNATIVE_FRAME_ILLUMINATION) != 0) { flags = KS.CAMERA_EXTENDEDPROP_FACEAUTH_MODE_ALTERNATIVE_FRAME_ILLUMINATION; } else if ((irProperty.Header.Capability & KS.CAMERA_EXTENDEDPROP_FACEAUTH_MODE_BACKGROUND_SUBTRACTION) != 0) { flags = KS.CAMERA_EXTENDEDPROP_FACEAUTH_MODE_BACKGROUND_SUBTRACTION; } if (flags != irProperty.Header.Flags) { irProperty.Header.Flags = flags; var newProperty = irProperty.GetBytes(); var irPropertySet = await irFrameSource.Controller.SetPropertyAsync(KS.PROPERTY_CAMERACONTROL_EXTENDED_FACEAUTH_MODE, newProperty); if (irPropertySet == MediaFrameSourceSetPropertyStatus.Success) { if (flags == KS.CAMERA_EXTENDEDPROP_FACEAUTH_MODE_ALTERNATIVE_FRAME_ILLUMINATION) { AlternativeFrameIllumination = true; } else if (flags == KS.CAMERA_EXTENDEDPROP_FACEAUTH_MODE_BACKGROUND_SUBTRACTION) { AlternativeFrameIllumination = false; } } } else { if (flags == KS.CAMERA_EXTENDEDPROP_FACEAUTH_MODE_ALTERNATIVE_FRAME_ILLUMINATION) { AlternativeFrameIllumination = true; } else if (flags == KS.CAMERA_EXTENDEDPROP_FACEAUTH_MODE_BACKGROUND_SUBTRACTION) { AlternativeFrameIllumination = false; } } } } finally { // TODO face mode will throw if the camera is already in face mode } }