Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
Quick Response (QR) codes have become an integral part of our digital lives, facilitating seamless data sharing and information retrieval. Recognizing QR codes in images is a valuable application of deep learning, a subset of artificial intelligence. In this article, we'll guide you through the process of building a QR code recognition system using deep learning in .NET/C#.
Deep learning, a branch of machine learning, involves training neural networks to learn complex patterns from data. In the context of QR code recognition, deep learning models can be trained to identify and decode QR codes from images with varying sizes, orientations, and conditions. Convolutional Neural Networks (CNNs) are a popular choice for image-based tasks like QR code recognition due to their ability to capture intricate visual features.
In this article, we will use ML.NET Model Builder for training our model.
Model Builder is a powerful tool provided by Microsoft as part of the ML.NET framework for building machine learning models in .NET/C#. It simplifies and accelerates the process of creating custom machine-learning models without requiring in-depth knowledge of machine-learning algorithms or extensive coding expertise. Model Builder is designed to be user-friendly and accessible, making it a great tool for developers who want to harness the capabilities of machine learning in their applications. Model Builder supports AutoML, which automatically explores different machine-learning algorithms and settings to help you find the one that best suits your scenario.
Training a QR code recognition model using Model Builder involves a series of steps that guide you through the process of creating and training the model. In this step-by-step guide, we'll walk you through each stage of training a QR code model using Model Builder in .NET/C#.
Before we dive into the implementation, ensure you have the following prerequisites:
We need QR Code Images for training our model. You can get QR Code Images from Kaggle or Roboflow. I have downloaded QR Code Images from Roboflow for this example.
Open Visual Studio Project.
Right click on Project > Add > Machine learning Model...
Following window will appear.
Give the model name and click on Add button. The following window will appear:
Scroll down, and find "Object Detection" under Computer Vision.
Select Object detection as we have to detect QR codes from the given dataset. Select Local or Azure as per your preference. We are selecting Local for this example.
After clicking on Local, the following window will appear:
Select a Local CPU or GPU as per your choice. You can also select Azure. For that, you should have an Active Azure Subscription. After Selecting your testing environment, click on the Next button. The following window will appear.
Selecting Data is the most important part of the training process. As mentioned earlier, I will get the data from Robo Flow. Open RoboFlow, Search for QR Code. The data may have white QR codes or have many local features. I am using QR detection Computer Vision Dataset for this tutorial. Download the Data by selecting the format. I have chosen COCO Format to download this data. As this format will be used further for data preprocessing and image recognition.
Now that you have data, Let's come back to Visual Studio. Select the Input Path of the Coco File downloaded above. You may also choose Vott, but for that, you need to create a Vott file for your data. The following window will appear.
Now, click on the Next Step button and move to the Train tab.
Now, click on Start Training Model to start the training. The Model Builder will automatically convert into binary images and use preferred neural networks accordingly. We don't need to specify anything. The best thing about Model Builder is that a developer with minimal deep learning background can train, test, and consume a model.
Model Builder will train the model on the specified dataset and display training progress. The above training will take time depending upon the system. The model will take all the QR Codes one by one and learn their features. You can test your model by passing any test QR code image to the model once the training is complete.
After training, Model Builder will evaluate the model's performance on the validation data.
You will see evaluation metrics such as accuracy, precision, recall, and F1-score. These metrics assess how well the model is performing.
Now that our model is trained and the QR Code Detector is ready, we need to consume that model to detect the code and then decode the detected QR Code. This Model will only detect whether a given input contains any Quick response code or not. This will not decode the QR Code. For QR Code decoding, we need a third-party Library. Iron Barcode is the best library to read the QR Code Image. Let's explore a bit about Iron Barcode before proceeding further.
IronBarcode is a .NET library specifically tailored for working with QR codes, a type of 2D barcode widely used for encoding information such as URLs, text, contact details, and more. This library simplifies the creation of QR codes by providing developers with intuitive tools to generate QR codes with customizable features like size, color, and error correction.
Additionally, IronBarcode enables the extraction of information from QR codes embedded within images, making it an indispensable resource for seamlessly integrating QR code generation and decoding capabilities into .NET applications.
In this tutorial, we will use it for decoding the QR Code if the QR Code is detected from our model.
Write the following command in NuGet Package Manager Console to download the IronBarcode NuGet package.
Install-Package BarCode
The above command will install the Iron Barcode package and will add a reference to our project.
Write the following code to read the single detected QR Code.
using IronBarCode;
string qrCodeImagePath = "myQrCode.png";
var data = BarcodeReader.QuicklyReadOneBarcode(qrCodeImagePath);
Console.WriteLine(data.Value.ToString());
using IronBarCode;
string qrCodeImagePath = "myQrCode.png";
var data = BarcodeReader.QuicklyReadOneBarcode(qrCodeImagePath);
Console.WriteLine(data.Value.ToString());
Imports IronBarCode
Private qrCodeImagePath As String = "myQrCode.png"
Private data = BarcodeReader.QuicklyReadOneBarcode(qrCodeImagePath)
Console.WriteLine(data.Value.ToString())
The following is the output:
In conclusion, the Model Builder and the IronBarcode library are a valuable combo for .NET developers wanting to work with QR codes. Model Builder makes the tricky job of creating and training models for QR code recognition pretty easy. And when you add in the IronBarcode library, things get even simpler – it helps read QR codes from pictures without a fuss. This teamwork doesn't just make things better for inventory and marketing tasks but also makes your apps more fun to use. When Model Builder and IronBarcode come together, it's like blending super-smart tech into your .NET apps, opening up all kinds of cool possibilities for QR code stuff. Iron Barcode commercial license is available at a very low cost with a free trial license.
9 .NET API products for your office documents