public static BitmapSource DrawFaces(BitmapSource baseImage, FaceAPI.Face[] faces, ObservableCollection <Microsoft.ProjectOxford.Face.Controls.Face> targetFaces, EmotionScores[] emotionScores, string[] celebName, List <PersonData> personData, DataTable dataTable, ImageWall imageWall) { if (faces == null) { return(baseImage); } Action <DrawingContext, BitmapSource, double> drawAction = (drawingContext, oriImage, annotationScale) => { for (int i = 0; i < faces.Length; i++) { if (targetFaces[i].PersonName == "Unknown") { continue; } var face = faces[i]; imageWall.colorful[imageWall.id.IndexOf(targetFaces[i].FaceId)] = true; if (face.FaceRectangle == null) { continue; } PersonData pD = new PersonData(); try { pD = personData.Find(x => x.ID == targetFaces[i].FaceId); pD.Times++; } catch (Exception) { personData.Find(x => x.ID == targetFaces[i].FaceId); } Rect faceRect = new Rect( face.FaceRectangle.Left, face.FaceRectangle.Top, face.FaceRectangle.Width, face.FaceRectangle.Height); Int32Rect faceRectInt32 = new Int32Rect( face.FaceRectangle.Left, face.FaceRectangle.Top, face.FaceRectangle.Width, face.FaceRectangle.Height); string text = ""; drawingContext.DrawImage(new CroppedBitmap(oriImage, faceRectInt32), faceRect); if (face.FaceAttributes != null) { text += Aggregation.SummarizeFaceAttributes(face.FaceAttributes, targetFaces[i].PersonName, pD); } if (emotionScores?[i] != null) { text += Aggregation.SummarizeEmotion(emotionScores[i]); } if (celebName?[i] != null) { text += celebName[i]; } faceRect.Inflate(6 * annotationScale, 6 * annotationScale); double lineThickness = 4 * annotationScale; drawingContext.DrawRectangle( Brushes.Transparent, new Pen(s_lineBrush, lineThickness), faceRect); if (text != "") { FormattedText ft = new FormattedText(text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, s_typeface, 16 * annotationScale, Brushes.Black); var pad = 3 * annotationScale; var ypad = pad; var xpad = pad + 4 * annotationScale; var origin = new System.Windows.Point( faceRect.Left + xpad - lineThickness / 2, faceRect.Top - ft.Height - ypad + lineThickness / 2); var rect = ft.BuildHighlightGeometry(origin).GetRenderBounds(null); rect.Inflate(xpad, ypad); drawingContext.DrawRectangle(s_lineBrush, null, rect); drawingContext.DrawText(ft, origin); } } dataTable.dataGrid.ItemsSource = personData; imageWall.UpdateCanvas(); }; return(DrawOverlay(baseImage, drawAction)); }