Detecting and Reading QR Codes Using Computer Vision
Quote of the day: “Be the change you wish to see in the world.” -Mahatma Gandhi
What is a QR code?
A Quick Response (QR) code is an optical label that can be read by a computer and contains data about the object to which it is attached. QR codes typically contain information for a tracker, location, or identifier that directs users to a website or application. To store information efficiently, QR codes use four specified encoding modes: numeric, alphanumeric, byte (binary), and kanji.
In this blog post, we are going to discuss:
- How do QR codes work?
- What are the components that make up a valid QR code?
- How to train models to detect QR codes and barcodes from a given image?
- How to use the Roboflow API to run the trained model on your custom images?
- Automatically crop and read the data using QR code libraries.
How do QR codes work?
The data included in a QR code can be decoded using the patterns that make up the code. The three sizable squares outside the QR code allow a reader to identify a regular QR code. Once it identifies these three squares, it concludes that the entire large square is a QR code.
Then, the entire QR code is deconstructed into a grid and examined by the QR reader. Based on whether a grid square is black or white, it examines each one in turn and gives it a value. After that, it assembles grid squares into bigger patterns. These bigger patterns can then be grouped together to recompose all of the data in the QR code (i.e. a URL to a web page, or an internal identifier for a product).
What components make up a QR code?
Six elements can be used to identify a conventional QR code:
- Quiet zone: This is the perimeter of a QR code that is blank and white. A QR reader needs this border to distinguish what is and isn't contained in the QR code (due to interference from outside elements).
- Finder pattern: The bottom left, top left, and top right corners of QR codes typically include three black squares. These squares alert a QR reader to the presence of a QR code and indicate where the code's outer limits are located.
- Alignment pattern: Somewhere towards the bottom right corner is another little square. It guarantees that the QR code can be read even when it is tilted or skewed.
- Timing pattern: In the finder pattern, this L-shaped line connects the three squares. A damaged QR code can still be read because of the timing pattern, which enables the reader to distinguish between specific squares within the entire code.
- Version information: The top-right finder pattern cell has a short field of data that is contained there. This lets us know which QR code version is being scanned.
- Data cells: The URL, phone number, or message that the QR code includes are sent in the remaining portion of the code.
Aside: You can make custom QR codes
You can also create your custom QR codes with logos and different stylings.
These QR codes are all valid. We're talking about these now because if you wanted to train a model to recognize more than just black and white QR codes in a single shape, you would need to gather sample data of different colour and shape variants.
We always have to keep in mind what sort of data we want our model to be able to identify. If our training set only shows black and white QR codes in a white box, without having examples of any other types of QR codes, our model will not be as effective as it could be.
Detect and read QR codes with computer vision
To identify and read the contents of QR codes with computer vision, we need to:
- Collect QR code dataset.
- Train a model to detect QR codes in the images.
- Use the Roboflow API to deploy the model.
- Read data inside the detected QR code.
Let's talk through each of these steps.
Collect dataset of QR codes for Model Training
To collect our data, we have Roboflow Universe, a website home to the world's largest collection of annotated and ready-to-use datasets related to various industries from self-driving cars and agriculture, to manufacturing and gaming that are open source and free to use.
Search using the keyword 'QR 'or 'QR code' and we are presented with many dataset options with which we can work.
If you need additional data beyond what is provided in Universe, you can upload Youtube videos as training data or upload your own data. Then you'll use Roboflow Annotate to label the data for model training.
Train a QR code detection model
Now we have our data, we are ready to train our detection model.
Head to Roboflow and create a new project called "QR Codes Detection". Follow the instructions in the dashboard and then click "Upload" in the sidebar. Here, you can upload the data you downloaded from Universe in the previous step.
After uploading your dataset, make sure all of the data is labelled correctly. Feel free to add augmentations to create more variations of the images in your dataset. Then, you can train a computer vision model with one-click using Roboflow Train.
Below are the results after training the model using the Accurate model in Roboflow:
Using the trained model to detect the QR codes from the images and crop the Region of Interest (ROI)
After successfully training the model, go to the Deploy page and use the Python script provided to make inferences. We're going to use the Roboflow Python library in this example.
Using the Roboflow Python API is straightforward. You can get set up using pip:
pip install roboflow
- Import the Roboflow module.
- Enter your API key.
- Add your project name.
- Choose your model version.
You can retrieve your project name and model version from the "Deploy" tab for your project in the Roboflow dashboard.
This code prepares us for making inferences with the Python API and our trained model hosted on Roboflow. You can choose the confidence and overlap threshold values suitable to your custom needs. This number will vary depending on the importance of accuracy: if you only want to see inferences the model is more sure about, you can increase the confidence and model threshold values.
Next, we can make an inference:
The image above shows that our code and model has successfully made an inference of an image. With this data, we can get to work on retrieving the part of the image where the QR code appears. We call this the Region of Interest (ROI).
This code will allow you to extract the region of interest:
Reading the detected QR code
Now we're ready to read the detected QR code.
In order to read the QR code data, we can use a Python library called pyzbar, which was built for reading data from QR codes.
pip install pyzbar
Now we have the library installed, we import the library:
We then create a function to read the ROI:
We then pass the ROI to the 'decoder' function which decodes and outputs the data inside the QR code:
Build a QR code detector with Roboflow
We now have a working model that:
- Identifies a QR code in an image.
- Extracts the QR code from the image.
- Decodes the QR code and returns its contents.
Now you have all the tools you need to build a QR code detector with Roboflow. What's more is that you haven't just learned how to build a QR code detector, you have also learned how to build any model based on data from Roboflow Universe.
Feel free to explore Universe to see what models interest you. Let your imagination run wild and use the skills you acquired building a QR code detector to power a new project. To get started with a new idea, find a dataset in Roboflow Universe, consider the best method for deploying your model, and ship your first project.
If you end up building something, please share it in our forum!