in FamilyNotes/UserDetection/UserPresence.cs [367:431]
private async void CountDetectedFaces(IReadOnlyList<DetectedFace> faces)
{
FaceCount = $"{_detectionString} {faces.Count.ToString()}";
// If we detect any faces, kill our no faces timer
if (faces.Count != 0)
{
if (_noFacesTimer != null)
{
_noFacesTimer.Dispose();
}
}
// Otherwise, if we are filtering and don't have a timer
else if (_currentlyFiltered && (_noFacesTimer == null))
{
// Create a callback
TimerCallback noFacesCallback = (object stateInfo) =>
{
_dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
OnFilterOnFace(_unfilteredName);
_noFacesTimer = null;
});
_noFacesTimer.Dispose();
};
// Set our timer
_noFacesTimer = new Timer(noFacesCallback, null, NoFacesTime, Timeout.Infinite);
}
// We are also going to take an image the first time that we detect exactly one face.
// Sidenote - to avoid a race-condition, I had to use a boolean. Just checking for _faceCaptureStill == null could produce an error.
if ((faces.Count == 1) && !_holdForTimer && !_currentlyFiltered)
{
// Kick off the timer so we don't keep taking pictures, but will resubmit if we are not filtered
_holdForTimer = true;
// Take the picture
_faceCaptureStill = await ApplicationData.Current.LocalFolder.CreateFileAsync("FaceDetected.jpg", CreationCollisionOption.ReplaceExisting);
await _mediaCapture.CapturePhotoToStorageFileAsync(ImageEncodingProperties.CreateJpeg(), _faceCaptureStill);
if (((App)Application.Current).AppSettings.FaceApiKey != "" && FacialSimilarity.InitialTrainingPerformed)
{
var UserName = await FacialSimilarity.CheckForUserAsync(new Uri("ms-appdata:///local/FaceDetected.jpg"));
if (UserName != "")
{
OnFilterOnFace(UserName);
}
}
// Allow the camera to take another picture in 10 seconds
TimerCallback callback = (Object stateInfo) =>
{
// Now that the timer is expired, we no longer need to hold
// Nothing else to do since the timer will be restarted when the picture is taken
_holdForTimer = false;
if (_pictureTimer != null)
{
_pictureTimer.Dispose();
}
};
_pictureTimer = new Timer(callback, null, 10000, Timeout.Infinite);
}
}