UWP Facial Detection Library

A short post. I took some of the code that I once had in this post around doing face detection using the UWP APIs to do on-device face detection and I built it into a library which I’ve dropped here;


The idea is to have a simple class named FaceWatcher which gets hold of a camera stream, runs face detection on it and fires an event (InFrameFaceCountChanged) whenever it’s view of the number of faces in front of the camera changes.

I built a simple test app to wrap around that and included it in the repo where the code to create a FaceWatcher on a default camera becomes as simple as;

this.faceWatcher = new FaceWatcher(
  new CameraDeviceFinder(
    deviceInformation =>
        return (deviceInformation.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front);

and the code to then handle the events when the number of faces changes becomes;

this.cancelTokenSource = new CancellationTokenSource();
this.faceWatcher.InFrameFaceCountChanged += OnFaceCountChanged;

   await this.faceWatcher.CaptureAsync(this.cancelTokenSource.Token);
catch (TaskCanceledException)
  this.faceWatcher.InFrameFaceCountChanged -= this.OnFaceCountChanged;

and the test app supplied simply has buttons for Start/Stop and a TextBlock to display the number of faces on screen.

That’s it – my hope is to couple this with a library which can sit on top of it and use the Cognitive Services facial detection APIs to then run identification such that instead of being able to simply flag how many faces are currently in front of the camera, I can use identification to tag when new faces enter/leave the frame by building a short term memory of the faces that have already been seen.

I think that’s all quite achievable with perhaps the main challenge being the one of figuring out the right time and frequency with which to call that API given that invocation involves an HTTP request which comes with a cost both in terms of the round-trip time and a real financial cost which means that I can only realistically call that type of API so many times per minute/hour.

But that’s for another post…:-)

2 thoughts on “UWP Facial Detection Library

  1. I didn’t realise that UWP had on-device detection for the presence of faces, that could be pretty useful in a number of scenarios. As you mentioned there is a financial cost to the cognitive APIs so for actual ID of who is who, the key will lie in how often that is called and what the cost is, looking forward to the follow up post and will check out the initial library you’ve published.

  2. Yup, the UWP has classes for doing this although they only give you the basics of the number of faces and the bounding box of each face within the photo whereas the Cognitive Services give you a lot more information including the positions of the facial features, the detected emotion and can then go further to do face verification and identification. Put all that together and I think you can make something powerful 🙂

Comments are closed.