protected override async void OnNavigatedTo(NavigationEventArgs e) { PaintingCanvas.Background = null; _state = StreamingState.Idle; if (_faceDetector == null) { _faceDetector = await FaceDetector.CreateAsync(); } if (_faceApiHelper == null) { try { _faceApiHelper = new FaceApiHelper(); await _faceApiHelper.CheckGroupExistAsync(); } catch (FaceAPIException faceEx) { ShowAlertHelper.ShowDialog(faceEx.ErrorMessage, faceEx.ErrorCode); } catch (Exception ex) { ShowAlertHelper.ShowDialog(ex.Message); } } }
protected override async void OnNavigatedTo(NavigationEventArgs e) { if (_dataHelper == null) { try { _dataHelper = new DataHelper(); } catch (Exception ex) { ShowAlertHelper.ShowDialog(ex.Message); } } try { await T(); TimeSpan period = TimeSpan.FromMinutes(1); _threadPoolTimer = ThreadPoolTimer.CreatePeriodicTimer(ShowScore, period); } catch (Exception) { throw; } }
private async Task <bool> StartStreamingAsync() { bool result = true; PaintingCanvas.Children.Clear(); try { MediaCaptureInitializationSettings initializationSettings = new MediaCaptureInitializationSettings(); if (_localSettings.Values["CameraId"] == null) { ShowAlertHelper.ShowDialog("Cannot get your CamreaId, plase check your setting."); } _cameraId = _localSettings.Values["CameraId"].ToString(); initializationSettings.VideoDeviceId = _cameraId; initializationSettings.StreamingCaptureMode = StreamingCaptureMode.Video; var cp = (CameraPosition)_localSettings.Values["CameraPosition"]; switch (cp) { case CameraPosition.Front: CameraPreview.FlowDirection = FlowDirection.RightToLeft; PaintingCanvas.FlowDirection = FlowDirection.RightToLeft; break; case CameraPosition.Back: CameraPreview.FlowDirection = FlowDirection.LeftToRight; PaintingCanvas.FlowDirection = FlowDirection.LeftToRight; break; default: break; } _mediaCapture = new MediaCapture(); await _mediaCapture.InitializeAsync(initializationSettings); _mediaCapture.Failed += _mediaCapture_Failed; CameraPreview.Source = _mediaCapture; await _mediaCapture.StartPreviewAsync(); _properties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties; } catch (UnauthorizedAccessException) { result = false; } catch (Exception ex) { ShowAlertHelper.ShowDialog(ex.Message); result = false; } return(result); }
private void ButtonGetKey_Click(object sender, RoutedEventArgs e) { if (SubscriptionKey.Text != null && EndPoint.Text != null && GroupId.Text != null && WebEndPoint.Text != null) { _localSettings.Values[Constants.FaceAPIKey] = SubscriptionKey.Text; _localSettings.Values[Constants.EndPoint] = EndPoint.Text; _localSettings.Values[Constants.GroupId] = GroupId.Text; _localSettings.Values[Constants.WebEndPoint] = WebEndPoint.Text; ShowAlertHelper.ShowDialog("Your settings have been store", "Done"); } else { return; } }
private async void ShowScore(ThreadPoolTimer timer) { if (!_semaphoreSlim.Wait(0)) { return; } try { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => await T()); } catch (Exception ex) { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => ShowAlertHelper.ShowDialog(ex.Message)); } finally { _semaphoreSlim.Release(); } }
protected override async void OnNavigatedTo(NavigationEventArgs e) { _state = StreamingState.Idle; if (_faceTracker == null) { _faceTracker = await FaceTracker.CreateAsync(); } if (_faceApiHelper == null) { try { _faceApiHelper = new FaceApiHelper(); await _faceApiHelper.CheckGroupExistAsync(); } catch (Microsoft.ProjectOxford.Face.FaceAPIException faceEx) { ShowAlertHelper.ShowDialog(faceEx.ErrorMessage, faceEx.ErrorCode); } catch (Exception ex) { ShowAlertHelper.ShowDialog(ex.Message); } } if (_dataHelper == null) { try { _dataHelper = new DataHelper(); } catch (Exception ex) { ShowAlertHelper.ShowDialog(ex.Message); } } }
private async Task <bool> TakePictureAsync() { bool successful = true; if (_state != StreamingState.Streaming) { return(false); } try { IList <DetectedFace> faces; const BitmapPixelFormat PixelFormat = BitmapPixelFormat.Nv12; using (VideoFrame currentFrame = new VideoFrame(PixelFormat, (int)_properties.Width, (int)_properties.Height)) { await _mediaCapture.GetPreviewFrameAsync(currentFrame); faces = await _faceDetector.DetectFacesAsync(currentFrame.SoftwareBitmap); Size size = new Size(currentFrame.SoftwareBitmap.PixelWidth, currentFrame.SoftwareBitmap.PixelHeight); if (faces.Count == 0 || faces.Count > 1) { throw new Exception("Too many people. (Or no one.)"); } using (SoftwareBitmap bitmap = SoftwareBitmap.Convert(currentFrame.SoftwareBitmap, BitmapPixelFormat.Bgra8)) { WriteableBitmap source = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight); bitmap.CopyToBuffer(source.PixelBuffer); IRandomAccessStream stream = new InMemoryRandomAccessStream(); BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream); encoder.SetSoftwareBitmap(bitmap); await encoder.FlushAsync(); ShowUp(size, faces, source); if (UserName.Text.Equals(string.Empty)) { throw new Exception("Enter your name."); } if (await _faceApiHelper.CreatePersonAsync(stream.AsStream(), UserName.Text)) { ShowAlertHelper.ShowDialog("Hi " + UserName.Text + ".", "Success"); } else { ShowAlertHelper.ShowDialog("Something went wrong. Try again."); } } } } catch (Exception ex) { ShowAlertHelper.ShowDialog(ex.Message); successful = false; } return(successful); }
private async void ProcessCurrentVideoFrame(ThreadPoolTimer timer) { // If state is not Streaming, return. if (_state != StreamingState.Streaming) { return; } // If there has a process still running, return. if (!_semaphoreSlim.Wait(0)) { return; } const BitmapPixelFormat PixelFormat = BitmapPixelFormat.Nv12; try { using (VideoFrame currentFrame = new VideoFrame(PixelFormat, (int)_videoProperties.Width, (int)_videoProperties.Height)) { // Get current preview frame from _mediaCaputre and copy into currentFrame. await _mediaCapture.GetPreviewFrameAsync(currentFrame); // Detected face by _faceTracker. IList <DetectedFace> builtinFaces = await _faceTracker.ProcessNextFrameAsync(currentFrame); SoftwareBitmap tempBitmap = SoftwareBitmap.Convert(currentFrame.SoftwareBitmap, BitmapPixelFormat.Bgra8); if (builtinFaces.Count != 0) { var frameSize = new Size(currentFrame.SoftwareBitmap.PixelWidth, currentFrame.SoftwareBitmap.PixelHeight); await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { ShowFromBuiltIn(frameSize, builtinFaces); }); // Get picture from videoframe. IRandomAccessStream stream = new InMemoryRandomAccessStream(); BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream); encoder.SetSoftwareBitmap(tempBitmap); await encoder.FlushAsync(); CustomFaceModel customFaces = await _faceApiHelper.GetIdentifySingleResultAsync(stream.AsStream()); if (customFaces != null) { await _dataHelper.ChangeAttendStatusAsync(customFaces.Name, true); await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => ShowLoginSuccess(customFaces)); } //await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => // ShowFromFaceApi(frameSize, customFaces)); } else { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => PaintingCanvas.Children.Clear()); } } } catch (Microsoft.ProjectOxford.Face.FaceAPIException faceEx) { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => ShowAlertHelper.ShowDialog(faceEx.ErrorMessage, faceEx.ErrorCode)); } catch (Exception ex) { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => ShowAlertHelper.ShowDialog(ex.Message)); } finally { _semaphoreSlim.Release(); } }
private async Task <bool> StartStreamingAsync() { bool result = true; try { MediaCaptureInitializationSettings initializationSettings = new MediaCaptureInitializationSettings(); if (_localSettings.Values["CameraId"] == null) { ShowAlertHelper.ShowDialog("Cannot get your CamreaId, plase check your setting."); return(false); } initializationSettings.VideoDeviceId = _localSettings.Values["CameraId"].ToString(); initializationSettings.StreamingCaptureMode = StreamingCaptureMode.Video; // Select preview flow direction. var cp = (CameraPosition)_localSettings.Values["CameraPosition"]; switch (cp) { case CameraPosition.Front: CameraPreview.FlowDirection = FlowDirection.RightToLeft; PaintingCanvas.FlowDirection = FlowDirection.RightToLeft; break; case CameraPosition.Back: CameraPreview.FlowDirection = FlowDirection.LeftToRight; PaintingCanvas.FlowDirection = FlowDirection.LeftToRight; break; default: break; } // Prepare MediaCapture. _mediaCapture = new MediaCapture(); await _mediaCapture.InitializeAsync(initializationSettings); _mediaCapture.Failed += MediaCapture_Failed; // Get preview video properties for FaceTracker. // e.g. hight and width. var deviceController = _mediaCapture.VideoDeviceController; _videoProperties = deviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties; CameraPreview.Source = _mediaCapture; await _mediaCapture.StartPreviewAsync(); TimeSpan period = TimeSpan.FromMilliseconds(66); _threadPoolTimer = ThreadPoolTimer.CreatePeriodicTimer(new TimerElapsedHandler(ProcessCurrentVideoFrame), period); } catch (UnauthorizedAccessException) { result = false; } catch (Exception ex) { ShowAlertHelper.ShowDialog("Cannot start to preview because " + ex.Message); result = false; } return(result); }