// Displays the image and calls Detect Faces. private async void BrowseButton_Click(object sender, RoutedEventArgs e) { // Get the image file to scan from the user. var openDlg = new Microsoft.Win32.OpenFileDialog(); openDlg.Filter = "JPEG Image(*.jpg)|*.jpg"; bool?result = openDlg.ShowDialog(this); // Return if canceled. if (!(bool)result) { return; } // Display the image file. string filePath = openDlg.FileName; Uri fileUri = new Uri(filePath); BitmapImage bitmapSource = new BitmapImage(); bitmapSource.BeginInit(); bitmapSource.CacheOption = BitmapCacheOption.None; bitmapSource.UriSource = fileUri; bitmapSource.EndInit(); FacePhoto.Source = bitmapSource; // Detect any faces in the image. Title = "Detecting..."; faces = await UploadAndDetectFaces(filePath); Title = String.Format("Detection Finished. {0} face(s) detected", faces.Length); if (faces.Length > 0) { // Prepare to draw rectangles around the faces. DrawingVisual visual = new DrawingVisual(); DrawingContext drawingContext = visual.RenderOpen(); drawingContext.DrawImage(bitmapSource, new Rect(0, 0, bitmapSource.Width, bitmapSource.Height)); double dpi = bitmapSource.DpiX; resizeFactor = 96 / dpi; faceDescriptions = new String[faces.Length]; for (int i = 0; i < faces.Length; ++i) { Face face = faces[i]; // Draw a rectangle on the face. drawingContext.DrawRectangle( Brushes.Transparent, new Pen(Brushes.Red, 2), new Rect( face.FaceRectangle.Left * resizeFactor, face.FaceRectangle.Top * resizeFactor, face.FaceRectangle.Width * resizeFactor, face.FaceRectangle.Height * resizeFactor ) ); // Store the face description. faceDescriptions[i] = FaceDescription(face); } drawingContext.Close(); // Display the image with the rectangle around the face. RenderTargetBitmap faceWithRectBitmap = new RenderTargetBitmap( (int)(bitmapSource.PixelWidth * resizeFactor), (int)(bitmapSource.PixelHeight * resizeFactor), 96, 96, PixelFormats.Pbgra32); faceWithRectBitmap.Render(visual); FacePhoto.Source = faceWithRectBitmap; // Set the status bar text. faceDescriptionStatusBar.Text = "Place the mouse pointer over a face to see the face description."; } }
private static void CreateTags(Face face) { // Age tag double age = face.faceAttributes.age; string ageTag = "adult"; if (age < 2) { ageTag = "baby"; } if (age < 5) { ageTag = "toddler"; } else if (age < 13) { ageTag = "child"; } else if (age < 19) { ageTag = "teenager"; } else if (age < 26) { ageTag = "young adult"; } else if (age > 65) { ageTag = "elderly"; } face.tags.Add(ageTag); // Smile tag float thresholdSmile = 0.5f; if (face.faceAttributes.smile > thresholdSmile) { face.tags.Add("smiling"); } // Gender tag if (face.faceAttributes.gender == "female") { if (age < 18) { face.tags.Add("girl"); } else { face.tags.Add("woman"); } } else if (face.faceAttributes.gender == "male") { if (age < 18) { face.tags.Add("boy"); } else { face.tags.Add("man"); } } // Facial hair tag float thresholdFacialHair = 0.5f; if (face.faceAttributes.facialHair.moustache > thresholdFacialHair) { face.tags.Add("moustache"); } if (face.faceAttributes.facialHair.beard > thresholdFacialHair) { face.tags.Add("beard"); } if (face.faceAttributes.facialHair.sideburns > thresholdFacialHair) { face.tags.Add("sideburns"); } // Glasses tag if (face.faceAttributes.glasses != "NoGlasses") { face.tags.Add(face.faceAttributes.glasses); } // Makeup tag if (face.faceAttributes.makeup.eyeMakeup) { face.tags.Add("eye makeup"); } if (face.faceAttributes.makeup.lipMakeup) { face.tags.Add("lip makeup"); } // Emotion tag float thresholdEmotion = 0.6f; if (face.faceAttributes.emotion.anger > thresholdEmotion) { face.tags.Add("angry"); } if (face.faceAttributes.emotion.contempt > thresholdEmotion) { face.tags.Add("arrogant"); } if (face.faceAttributes.emotion.disgust > thresholdEmotion) { face.tags.Add("disgusted"); } if (face.faceAttributes.emotion.fear > thresholdEmotion) { face.tags.Add("scared"); } if (face.faceAttributes.emotion.happiness > thresholdEmotion) { face.tags.Add("happy"); } if (face.faceAttributes.emotion.neutral > thresholdEmotion) { face.tags.Add("neutral"); } if (face.faceAttributes.emotion.sadness > thresholdEmotion) { face.tags.Add("sad"); } if (face.faceAttributes.emotion.surprise > thresholdEmotion) { face.tags.Add("surprised"); } // Accessories tag float thresholdAccessory = 0.5f; foreach (Accessory a in face.faceAttributes.accessories) { if (a.confidence > thresholdAccessory) { face.tags.Add(a.type); } } // Hair tag float thresholdHair = 0.6f; if (face.faceAttributes.hair.bald > thresholdHair) { face.tags.Add("bald"); } foreach (HairColor h in face.faceAttributes.hair.hairColor) { if (h.confidence > thresholdHair) { face.tags.Add($"{h.color} hair"); } } }
// Returns a string that describes the given face. private string FaceDescription(Face face) { StringBuilder sb = new StringBuilder(); sb.Append("Face: "); // Add the gender, age, and smile. sb.Append(face.FaceAttributes.Gender); sb.Append(", "); sb.Append(face.FaceAttributes.Age); sb.Append(", "); sb.Append(String.Format("smile {0:F1}%, ", face.FaceAttributes.Smile * 100)); // Add the emotions. Display all emotions over 10%. sb.Append("Emotion: "); EmotionScores emotionScores = face.FaceAttributes.Emotion; if (emotionScores.Anger >= 0.1f) { sb.Append(String.Format("anger {0:F1}%, ", emotionScores.Anger * 100)); } if (emotionScores.Contempt >= 0.1f) { sb.Append(String.Format("contempt {0:F1}%, ", emotionScores.Contempt * 100)); } if (emotionScores.Disgust >= 0.1f) { sb.Append(String.Format("disgust {0:F1}%, ", emotionScores.Disgust * 100)); } if (emotionScores.Fear >= 0.1f) { sb.Append(String.Format("fear {0:F1}%, ", emotionScores.Fear * 100)); } if (emotionScores.Happiness >= 0.1f) { sb.Append(String.Format("happiness {0:F1}%, ", emotionScores.Happiness * 100)); } if (emotionScores.Neutral >= 0.1f) { sb.Append(String.Format("neutral {0:F1}%, ", emotionScores.Neutral * 100)); } if (emotionScores.Sadness >= 0.1f) { sb.Append(String.Format("sadness {0:F1}%, ", emotionScores.Sadness * 100)); } if (emotionScores.Surprise >= 0.1f) { sb.Append(String.Format("surprise {0:F1}%, ", emotionScores.Surprise * 100)); } // Add glasses. sb.Append(face.FaceAttributes.Glasses); sb.Append(", "); // Add hair. sb.Append("Hair: "); // Display baldness confidence if over 1%. if (face.FaceAttributes.Hair.Bald >= 0.01f) { sb.Append(String.Format("bald {0:F1}% ", face.FaceAttributes.Hair.Bald * 100)); } // Display all hair color attributes over 10%. HairColor[] hairColors = face.FaceAttributes.Hair.HairColor; foreach (HairColor hairColor in hairColors) { if (hairColor.Confidence >= 0.1f) { sb.Append(hairColor.Color.ToString()); sb.Append(String.Format(" {0:F1}% ", hairColor.Confidence * 100)); } } // Return the built string. return(sb.ToString()); }