public UserRegistration() { InitializeComponent(); webcam = new WebCam(); webcam.InitializeWebCam(ref image1); webcam.Start(); }
public LoginWIthFaceRecognition(string username) { InitializeComponent(); this.username = username; label1.Content = "Hello " + username; // TODO: Add event handler implementation here. webcam = new WebCam(); webcam.InitializeWebCam(ref image1); webcam.Start(); }
public void redo() { webcam.Start(); image1.Visibility = Visibility.Visible; image2.Visibility = Visibility.Hidden; }
private async void btnCapture_Click(object sender, RoutedEventArgs e) { tempfilename = Helper.SaveTempImageCapture((BitmapSource)image1.Source); webcam.Stop(); var imageInfo = UIHelper.GetImageInfoForRendering(tempfilename); using (var fileStream = File.OpenRead(tempfilename)) { try { string subscriptionKey = ConfigurationSettings.AppSettings.Get("faceapikey"); IFaceServiceClient faceServiceClient = new FaceServiceClient(subscriptionKey); var faces = await faceServiceClient.DetectAsync(fileStream, true, true, new FaceAttributeType[] { FaceAttributeType.Gender, FaceAttributeType.Age, FaceAttributeType.Smile, FaceAttributeType.Glasses, FaceAttributeType.HeadPose, FaceAttributeType.FacialHair }); //Render rectangle in the image Uri fileUri = new Uri(tempfilename); BitmapImage bitmapSource = new BitmapImage(); bitmapSource.BeginInit(); bitmapSource.CacheOption = BitmapCacheOption.None; bitmapSource.UriSource = fileUri; bitmapSource.EndInit(); //image1.Source = bitmapSource; Title = "Detecting Face..."; FaceRectangle[] faceRects = faces.Select(face => face.FaceRectangle).ToArray(); Title = String.Format("Detection Finished. {0} Face(s) Detected", faceRects.Length); //If face detected if (faceRects.Length == 1) { //DetectedResultsInText = string.Format("{0} face(s) has been detected", faces.Length); foreach (var face in faces) { detectedFaces.Add(new Face() { ImagePath = tempfilename, Left = face.FaceRectangle.Left, Top = face.FaceRectangle.Top, Width = face.FaceRectangle.Width, Height = face.FaceRectangle.Height, FaceId = face.FaceId.ToString(), Gender = face.FaceAttributes.Gender, Age = string.Format("{0:#} years old", face.FaceAttributes.Age), IsSmiling = face.FaceAttributes.Smile > 0.0 ? "Smile" : "Not Smile", Glasses = face.FaceAttributes.Glasses.ToString(), FacialHair = string.Format("Facial Hair: {0}", face.FaceAttributes.FacialHair.Moustache + face.FaceAttributes.FacialHair.Beard + face.FaceAttributes.FacialHair.Sideburns > 0 ? "Yes" : "No"), HeadPose = string.Format("Pitch: {0}, Roll: {1}, Yaw: {2}", Math.Round(face.FaceAttributes.HeadPose.Pitch, 2), Math.Round(face.FaceAttributes.HeadPose.Roll, 2), Math.Round(face.FaceAttributes.HeadPose.Yaw, 2)) }); lblGender.Content = "Gender: " + face.FaceAttributes.Gender; regUser.gender = face.FaceAttributes.Gender; regUser.faceguid = face.FaceId.ToString(); txtAge.Text = string.Format("{0:#}", face.FaceAttributes.Age); lblAge.Content = "years old"; if (!String.Equals(face.FaceAttributes.Glasses.ToString(), "NoGlasses")) { MessageBox.Show("Please dont wear your glasses! Thanks", "Error", MessageBoxButton.OK); webcam.Start(); return; } } DrawingVisual visual = new DrawingVisual(); DrawingContext drawingContext = visual.RenderOpen(); drawingContext.DrawImage(bitmapSource, new Rect(0, 0, bitmapSource.Width, bitmapSource.Height)); double dpi = bitmapSource.DpiX; double resizeFactor = 96 / dpi; foreach (var faceRect in faceRects) { drawingContext.DrawRectangle( Brushes.Transparent, new Pen(Brushes.LightBlue, 2), new Rect( faceRect.Left * resizeFactor, faceRect.Top * resizeFactor, faceRect.Width * resizeFactor, faceRect.Height * resizeFactor ) ); } drawingContext.Close(); RenderTargetBitmap faceWithRectBitmap = new RenderTargetBitmap( (int)(bitmapSource.PixelWidth * resizeFactor), (int)(bitmapSource.PixelHeight * resizeFactor), 96, 96, PixelFormats.Pbgra32); faceWithRectBitmap.Render(visual); image1.Source = faceWithRectBitmap; } else { if (faceRects.Length > 1) { MessageBox.Show("More than 1 face detected, please capture another photo!", "Error", MessageBoxButton.OK); return; } else { MessageBox.Show("Face is not detected, please capture another photo!", "Error", MessageBoxButton.OK); } webcam.Start(); } } catch (FaceAPIException ex) { GC.Collect(); return; } } }