in src/CognitiveKioskUWP/MainPage.xaml.cs [199:279]
private async Task StartPreviewAsync()
{
try
{
var devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
var deviceList = devices.ToList();
var device = devices.FirstOrDefault(x => x.Name.Contains(settings.CameraKey));
string deviceId = device == null ? "" : device.Id;
mediaCapture = new MediaCapture();
await mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings() { SharingMode = MediaCaptureSharingMode.ExclusiveControl, VideoDeviceId = deviceId });
var resolutions = mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview).ToList();
var availableResolutions = mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview).Cast<VideoEncodingProperties>().OrderByDescending(v => v.Width * v.Height * (v.FrameRate.Numerator / v.FrameRate.Denominator));
//1080p or lower
var reslution = availableResolutions.FirstOrDefault(v => v.Height <= 1080);
// set used resolution
await mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, reslution);
mediaCapture2 = new MediaCapture();
await mediaCapture2.InitializeAsync(new MediaCaptureInitializationSettings() { SharingMode = MediaCaptureSharingMode.SharedReadOnly, VideoDeviceId = deviceId });
mediaCapture3 = new MediaCapture();
await mediaCapture3.InitializeAsync(new MediaCaptureInitializationSettings() { SharingMode = MediaCaptureSharingMode.SharedReadOnly, VideoDeviceId = deviceId });
mediaCapture4 = new MediaCapture();
await mediaCapture4.InitializeAsync(new MediaCaptureInitializationSettings() { SharingMode = MediaCaptureSharingMode.SharedReadOnly, VideoDeviceId = deviceId });
displayRequest.RequestActive();
DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape;
// Create the definition, which will contain some initialization settings
var definition = new FaceDetectionEffectDefinition();
// To ensure preview smoothness, do not delay incoming samples
definition.SynchronousDetectionEnabled = false;
// In this scenario, choose detection speed over accuracy
definition.DetectionMode = FaceDetectionMode.HighQuality;
imageAnalysisRunning = false;
// Add the effect to the preview stream
_faceDetectionEffect = (FaceDetectionEffect)await mediaCapture.AddVideoEffectAsync(definition, MediaStreamType.VideoPreview);
// Choose the shortest interval between detection events
_faceDetectionEffect.DesiredDetectionInterval = TimeSpan.FromMilliseconds(300);
// Start detecting faces
_faceDetectionEffect.Enabled = true;
// Register for face detection events
_faceDetectionEffect.FaceDetected += _faceDetectionEffect_FaceDetectedAsync;
timerFailsafe.Start();
}
catch (Exception)
{
// This will be thrown if the user denied access to the camera in privacy settings
Console.Write("The app was denided access to the camera");
faceLastDate = DateTime.Now.Subtract(new TimeSpan(1, 1, 1));
return;
}
try
{
captionsControl.MainCapture.Source = mediaCapture;
speechControl.MainCapture.Source = mediaCapture2;
tagsControl.MainCapture.Source = mediaCapture3;
facesControl.MainCapture.Source = mediaCapture4;
await mediaCapture.StartPreviewAsync();
await mediaCapture2.StartPreviewAsync();
await mediaCapture3.StartPreviewAsync();
await mediaCapture4.StartPreviewAsync();
}
catch (Exception)
{
//mediaCapture.CaptureDeviceExclusiveControlStatusChanged += MediaCapture_CaptureDeviceExclusiveControlStatusChanged; ;
}
}