private int RectDist(FaceRectangle rectID, Microsoft.ProjectOxford.Common.Rectangle rectE) { int xcID = Convert.ToInt32(Math.Round(Convert.ToDecimal(rectID.Left + rectID.Width / 2))); int ycID = Convert.ToInt32(Math.Round(Convert.ToDecimal(rectID.Top + rectID.Height / 2))); int xcE = Convert.ToInt32(Math.Round(Convert.ToDecimal(rectE.Left + rectE.Width / 2))); int ycE = Convert.ToInt32(Math.Round(Convert.ToDecimal(rectE.Top + rectE.Height / 2))); int dx = xcID - xcE; int dy = ycID - ycE; int dist = dx ^ 2 + dy ^ 2; return(dist); }
private async Task <Scores> EstimateEmotions(String imageFilePath, FaceRectangle rectID) { try { StorageFolder storageFolder = await StorageFolder.GetFolderFromPathAsync(System.IO.Path.GetDirectoryName(imageFilePath)); StorageFile sampleFile = await storageFolder.GetFileAsync(System.IO.Path.GetFileName(imageFilePath)); var inputStream = await sampleFile.OpenReadAsync(); Stream stream = inputStream.AsStreamForRead(); var emo_rec = await emoClient.RecognizeAsync(stream); if (emo_rec != null && emo_rec.Length > 0) { List <Microsoft.ProjectOxford.Common.Rectangle> emoRects = new List <Microsoft.ProjectOxford.Common.Rectangle>(); for (int p = 0; p < emo_rec.Length; p++) { emoRects.Add(emo_rec[p].FaceRectangle); } int matchIndex = GetMatchingIndex(rectID, emoRects); return(emo_rec[matchIndex].Scores); } else { } } catch (Exception e) { Debug.WriteLine(e.InnerException, e.StackTrace); } return(null); }
//Start the process private async void button_Click(object sender, RoutedEventArgs e) { FolderPicker folderPicker = new FolderPicker(); folderPicker.FileTypeFilter.Add(".jpg"); folderPicker.FileTypeFilter.Add(".jpeg"); folderPicker.FileTypeFilter.Add(".png"); folderPicker.FileTypeFilter.Add(".bmp"); folderPicker.ViewMode = PickerViewMode.Thumbnail; StorageFolder photoFolder = await folderPicker.PickSingleFolderAsync(); if (photoFolder == null) { return; } var files = await photoFolder.GetFilesAsync(); List <Scores> E = new List <Scores>(); int[] num = new int[files.Count]; for (int i = 0; i < files.Count; i++) { IRandomAccessStream fileStream = await files[i].OpenAsync(FileAccessMode.Read); BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream); BitmapTransform transform = new BitmapTransform(); const float sourceImageHeightLimit = 1280; if (decoder.PixelHeight > sourceImageHeightLimit) { float scalingFactor = (float)sourceImageHeightLimit / (float)decoder.PixelHeight; transform.ScaledWidth = (uint)Math.Floor(decoder.PixelWidth * scalingFactor); transform.ScaledHeight = (uint)Math.Floor(decoder.PixelHeight * scalingFactor); } SoftwareBitmap sourceBitmap = await decoder.GetSoftwareBitmapAsync(decoder.BitmapPixelFormat, BitmapAlphaMode.Premultiplied, transform, ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage); const BitmapPixelFormat faceDetectionPixelFormat = BitmapPixelFormat.Gray8; SoftwareBitmap convertedBitmap; if (sourceBitmap.BitmapPixelFormat != faceDetectionPixelFormat) { convertedBitmap = SoftwareBitmap.Convert(sourceBitmap, faceDetectionPixelFormat); } else { convertedBitmap = sourceBitmap; } if (faceDetector == null) { faceDetector = await FaceDetector.CreateAsync(); } detectedFaces = await faceDetector.DetectFacesAsync(convertedBitmap); Scores sc = null; if (detectedFaces.Count > 0) { num[i] = detectedFaces.Count; FaceRectangle rectID = new FaceRectangle(); rectID = await UploadAndDetectFaces(files[i].Path); if (rectID != null) { sc = await EstimateEmotions(files[i].Path, rectID); } } E.Add(sc); if (sc != null) { Items.Add(new DataItem(i.ToString(), (int)(sc.Happiness * 100))); } sourceBitmap.Dispose(); fileStream.Dispose(); convertedBitmap.Dispose(); } }