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; try { 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…:-)