From 0aad9934bad3bee509f30871b948f9df1ce4e3e1 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Wed, 8 Mar 2017 10:11:56 -0800 Subject: [PATCH] Don't call Emotion API if OpenCv found no faces locally. (#4) * Don't call Emotion API if OpenCv found no faces locally. See [this](http://stackoverflow.com/questions/42597067/emotion-recognition-real-time-analysis-api-calls) for context. * Move line for consistency --- Windows/LiveCameraSample/MainWindow.xaml.cs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/Windows/LiveCameraSample/MainWindow.xaml.cs b/Windows/LiveCameraSample/MainWindow.xaml.cs index aca4a25..4d85dc2 100644 --- a/Windows/LiveCameraSample/MainWindow.xaml.cs +++ b/Windows/LiveCameraSample/MainWindow.xaml.cs @@ -206,7 +206,14 @@ namespace LiveCameraSample // See if we have local face detections for this image. var localFaces = (OpenCvSharp.Rect[])frame.UserData; - if (localFaces != null) + if (localFaces == null) + { + // If localFaces is null, we're not performing local face detection. + // Use Cognigitve Services to do the face detection. + Properties.Settings.Default.EmotionAPICallCount++; + emotions = await _emotionClient.RecognizeAsync(jpg); + } + else if (localFaces.Count() > 0) { // If we have local face detections, we can call the API with them. // First, convert the OpenCvSharp rectangles. @@ -218,16 +225,15 @@ namespace LiveCameraSample Width = f.Width, Height = f.Height }); + Properties.Settings.Default.EmotionAPICallCount++; emotions = await _emotionClient.RecognizeAsync(jpg, rects.ToArray()); } else { - // If not, the API will do the face detection. - emotions = await _emotionClient.RecognizeAsync(jpg); + // Local face detection found no faces; don't call Cognitive Services. + emotions = new Emotion[0]; } - // Count the API call. - Properties.Settings.Default.EmotionAPICallCount++; // Output. return new LiveCameraResult {