private async Task <ImageAnalyzer> GetPrimaryFaceFromCameraCaptureAsync(ImageAnalyzer img) { if (img == null) { return(null); } await img.DetectFacesAsync(); if (img.DetectedFaces == null || !img.DetectedFaces.Any()) { return(null); } // Crop the primary face and return it as the result FaceRectangle rect = img.DetectedFaces.First().FaceRectangle; double heightScaleFactor = 1.8; double widthScaleFactor = 1.8; FaceRectangle biggerRectangle = new FaceRectangle { Height = Math.Min((int)(rect.Height * heightScaleFactor), img.DecodedImageHeight), Width = Math.Min((int)(rect.Width * widthScaleFactor), img.DecodedImageWidth) }; biggerRectangle.Left = Math.Max(0, rect.Left - (int)(rect.Width * ((widthScaleFactor - 1) / 2))); biggerRectangle.Top = Math.Max(0, rect.Top - (int)(rect.Height * ((heightScaleFactor - 1) / 1.4))); StorageFile tempFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync( "FaceRecoCameraCapture.jpg", CreationCollisionOption.GenerateUniqueName); await Util.CropBitmapAsync(img.GetImageStreamCallback, biggerRectangle.ToRect(), tempFile); return(new ImageAnalyzer(tempFile.OpenStreamForReadAsync, tempFile.Path)); }
private async Task ProcessCameraCapture(ImageAnalyzer e) { if (e == null) { this.isProcessingPhoto = false; return; } await e.DetectFacesAsync(detectFaceAttributes : true); // Analyze emotions if (e.DetectedFaces.Any()) { // Update the average emotion response Emotion averageScores = new Emotion { Happiness = e.DetectedFaces.Average(f => f.FaceAttributes.Emotion.Happiness), Anger = e.DetectedFaces.Average(f => f.FaceAttributes.Emotion.Anger), Sadness = e.DetectedFaces.Average(f => f.FaceAttributes.Emotion.Sadness), Contempt = e.DetectedFaces.Average(f => f.FaceAttributes.Emotion.Contempt), Disgust = e.DetectedFaces.Average(f => f.FaceAttributes.Emotion.Disgust), Neutral = e.DetectedFaces.Average(f => f.FaceAttributes.Emotion.Neutral), Fear = e.DetectedFaces.Average(f => f.FaceAttributes.Emotion.Fear), Surprise = e.DetectedFaces.Average(f => f.FaceAttributes.Emotion.Surprise) }; double positiveEmotionResponse = Math.Min(averageScores.Happiness + averageScores.Surprise, 1); double negativeEmotionResponse = Math.Min(averageScores.Sadness + averageScores.Fear + averageScores.Disgust + averageScores.Contempt, 1); double netResponse = ((positiveEmotionResponse - negativeEmotionResponse) * 0.5) + 0.5; this.sentimentControl.Sentiment = netResponse; // show captured faces and their emotion if (this.emotionFacesGrid.Visibility == Visibility.Visible) { foreach (DetectedFace face in e.DetectedFaces) { // Get top emotion on this face KeyValuePair <string, double> topEmotion = Util.EmotionToRankedList(face.FaceAttributes.Emotion).First(); // Crop this face FaceRectangle rect = face.FaceRectangle; double heightScaleFactor = 1.8; double widthScaleFactor = 1.8; FaceRectangle biggerRectangle = new FaceRectangle { Height = Math.Min((int)(rect.Height * heightScaleFactor), e.DecodedImageHeight), Width = Math.Min((int)(rect.Width * widthScaleFactor), e.DecodedImageWidth) }; biggerRectangle.Left = Math.Max(0, rect.Left - (int)(rect.Width * ((widthScaleFactor - 1) / 2))); biggerRectangle.Top = Math.Max(0, rect.Top - (int)(rect.Height * ((heightScaleFactor - 1) / 1.4))); ImageSource croppedImage = await Util.GetCroppedBitmapAsync(e.GetImageStreamCallback, biggerRectangle.ToRect()); // Add the face and emotion to the collection of faces if (croppedImage != null && biggerRectangle.Height > 0 && biggerRectangle.Width > 0) { if (this.EmotionFaces.Count >= 9) { this.EmotionFaces.Clear(); } this.EmotionFaces.Add(new EmotionExpressionCapture { CroppedFace = croppedImage, TopEmotion = topEmotion.Key }); } } } } this.isProcessingPhoto = false; }
private async Task <Image> GetFaceCropAsync(ImageAnalyzer img) { ImageSource croppedImage; if (img.DetectedFaces == null || !img.DetectedFaces.Any()) { croppedImage = new BitmapImage(); await((BitmapImage)croppedImage).SetSourceAsync((await img.GetImageStreamCallback()).AsRandomAccessStream()); } else { // Crop the primary face FaceRectangle rect = img.DetectedFaces.First().FaceRectangle; double heightScaleFactor = 1.8; double widthScaleFactor = 1.8; FaceRectangle biggerRectangle = new FaceRectangle { Height = Math.Min((int)(rect.Height * heightScaleFactor), img.DecodedImageHeight), Width = Math.Min((int)(rect.Width * widthScaleFactor), img.DecodedImageWidth) }; biggerRectangle.Left = Math.Max(0, rect.Left - (int)(rect.Width * ((widthScaleFactor - 1) / 2))); biggerRectangle.Top = Math.Max(0, rect.Top - (int)(rect.Height * ((heightScaleFactor - 1) / 1.4))); croppedImage = await Util.GetCroppedBitmapAsync(img.GetImageStreamCallback, biggerRectangle.ToRect()); } return(new Image { Source = croppedImage, Height = 200 }); }