Machine Learning Object Detection in .NET with WPF App
Complete sample for object detection in .NET using Machine Learning and WPF. From image labeling to training to final app.
For sample/demonstration/understanding of the full cycle I will label and train with just a few images of a very simple problem. Labeling and model training depending the domain and complexity of the project is something best done by a Data Scientist specialist. For .NET developers I think the important part is Part 3 so they can integrate with their software and add value. Labeling and model training can be outsource to a specialist or use one of the several public models available made by experts.
Part 1 - Labeling
The first step to detect objects in images/video is to label them. There are several applications and websites for this. Opensource, comercial, local only, cloud backend to fit everyone needs/taste. Here is a short list of some of them:
-
VoTT - Microsoft Visual Object Tagging Tool
Can be run as a desktop application and works with local files or directly online using Azure Blob Storage. It allows to export to several formats (Pascal VOC, TensorFlow recods, Azure Custom Vision Service, etc)
-
It's part of Azure Cognitive Services and allows to upload images and label them directly in the web.
-
Open Source data labeling tool. Apart from labeling images for object detection it can also work with audio, texts, etc. You can run it locally with Docker in one minute.
-
Open Source annotation tool. It runs directly on the web and allows to export the bounding box labels in many formats.
-
Comercial options (some with Free Tier)
Part 2 - Model training
-
For this sample I used this simple tool for labeling and training. I used the facemask dataset from Kaggle and I just used 20 images for training and a few for testing. Labeling took me about two minutes and I did it very rough and model training took just 5 minutes.
-
ML.NET can work with TensorFlow models or you can convert to ONNX format
-
If you are comfortable with Python and export to ONNX
Part 3 - WPF Application
In this sample I chose to use a .NET 5 WPF application so that the sample is helpful for .NET desktop developers. Obviously deploying the previous model as a Docker container or a REST API endpoint for web use is also possible and maybe even simpler and they are several samples/tutorials online for this.
The sample is based on Microsoft Machine Learning Sample.
You can check the source code here. As with most of my WPF projects I used MahApps and ReactiveUI but those are not required.