Detecting and Reading QR Codes Using Computer Vision
Published Nov 3, 2022 • 7 min read

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:

  1. How do QR codes work?
  2. What are the components that make up a valid QR code?
  3. How to train models to detect QR codes and barcodes from a given image?
  4. How to use the Roboflow API to run the trained model on your custom images?
  5. Automatically crop and read the data using QR code libraries.

How do QR codes work?

An example QR Code

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:

Color-coded QR code components
  1. 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).
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.

Custom QR codes when scanned redirects you to https://blog.roboflow.com/

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:

  1. Collect QR code dataset.
  2. Train a model to detect QR codes in the images.
  3. Use the Roboflow API to deploy the model.
  4. 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.

A Roboflow Universe search page showing results for the query "QR"

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:

Training results using Roboflow one-click interface, showing 99.5% mAP, 100% precision, and 99.1% recall.

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
  1. Import the Roboflow module.
  2. Enter your API key.
  3. Add your project name.
  4. 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:

Sample JSON result from the Roboflow API showing 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:

The decoder shows the result "https://blog.roboflow.com/", the URL encoded in our QR code.

Build a QR code detector with Roboflow

We now have a working model that:

  1. Identifies a QR code in an image.
  2. Extracts the QR code from the image.
  3. 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!

Cite this Post

Use the following entry to cite this post in your research:

Lukka Chaitanya. (Nov 3, 2022). Detecting and Reading QR Codes Using Computer Vision. Roboflow Blog: https://blog.roboflow.com/reading-qr-codes-using-computer-vision/

Discuss this Post

If you have any questions about this blog post, start a discussion on the Roboflow Forum.

Written by

Lukka Chaitanya
I am a CV & ML expert with 10+ years of industrial R&D experience. I worked for a Tokyo-based Robotics firm for warehouse automation and for an AI company based out of Europe.

Topics