Tutorials
November 6, 2024

QR code detection and decoding using computer vision

Written by
Sara Gharnit
Senior Content Marketer

QR codes are everywhere these days—retail, logistics, AR experiences, you name it. With applications that range from inventory tracking to interactive displays, QR codes are now a core tool in automation and IoT projects.

But how do you integrate QR code detection into your own builds? That’s where computer vision (CV) comes in, and, specifically, where Viam’s platform makes things simpler.

In this guide, we’ll explore how QR code detection works with CV and how Viam’s vision service can help you set it up quickly and easily. By the end, you’ll have a clear idea of how to add QR code detection into your IoT and smart device projects.

How QR code detection works in computer vision

Before diving into detection, let’s break down what’s in a QR code.

The anatomy of a QR code

Think of a QR code like a well-organized city map designed specifically for machines to navigate.

  • The finder patterns are like the main landmarks in three corners, helping scanners quickly orient themselves, much like someone locating key buildings or intersections on a map.
  • The timing patterns are the street grids that keep everything aligned and orderly, ensuring the scanner reads each part correctly.
  • Finally, the data modules are the individual blocks where the real "addresses" or information resides—the essential content the scanner is searching for.
The anatomy of a QR code.
The anatomy of a QR code.

Just like a map makes it easy for people to find specific locations, the QR code’s structure helps machines pinpoint and retrieve the data quickly and accurately. This usually involves steps like spotting the QR code, analyzing its shape, and reading the data. 

Atypical and AI-enhanced QR codes

Not all QR codes look the same. Brands today are using custom QR codes that include logos, colors, or even gradients. And with AI, we’ve seen some QR codes push the limits even further—think animations, complex designs, or custom shapes.

This QR code was generated on Hugging Face’s QR Code AI Art Generator with the prompt "Sky view of highly aesthetic, modern New York buildings.
Try out this code to our website, generated on Hugging Face’s QR Code AI Art Generator

This visual creativity is exciting, but it can also make detection a bit trickier since these codes don’t follow the standard black-and-white format. To help your model pick up on these custom codes, consider training with:

  • Codes with logos and unique graphics: QR codes often include brand logos. Training your model with these types can help it distinguish data from decorative elements.
  • Multicolor and gradient examples: Atypical codes might use color creatively, so include samples with different color schemes.
  • Asymmetrical shapes: QR codes don’t always stay square. Try training with rounded or custom-shaped codes.
  • Layered or transparent designs: Many codes are overlaid on backgrounds or have transparent elements. Including these can boost your model’s ability to handle complex visuals.

Did you know? Viam’s vision service is designed to handle all these styles with ease.

What data can be found in a QR code

QR codes might look simple, but their data modules can pack in all kinds of useful info, from text to binary data, making them incredibly versatile for different applications. Here’s a quick look at some types of data QR codes can carry:

  • URLs or website links: A classic use—scan the code, and you’re instantly directed to a website, product page, or app download.
  • Text or messages: QR codes can hold plain text, from instructions and greetings to detailed product descriptions.
  • Contact information: Add a QR code with contact details (name, phone number, email) so users can save them directly to their contacts in one scan.
  • Wi-Fi network credentials: QR codes can store Wi-Fi network names and passwords, letting users connect seamlessly just by scanning.
  • Payment information: Great for mobile payments! These codes securely hold transaction details, so users can pay with a simple scan.
  • Geolocation data: Want to guide users to a specific location? QR codes can store GPS coordinates, making them perfect for maps and navigation.
  • Calendar events: QR codes can include event info (date, time, location) so users can add it right to their calendars.
  • Authentication and access data: Some QR codes work like digital keys, granting secure access to spaces or authorizing specific actions.

With these options, QR codes can adapt to almost any need—from sharing details to managing secure access.

Getting started with QR code detection

Viam’s platform is built for developers who want reliable, customizable solutions, and QR code detection is no exception. Let’s walk through setting it up:

Step-by-step guide to setting up QR code detection

1. Configure the camera

Start by setting up your camera component to capture clear images. Adjust settings like resolution and frame rate to boost detection accuracy.

A camera being configured within the Viam app interface.
A camera being configured within the Viam app interface.

2. Set up the vision service

Enable the vision service in Viam’s platform. This powerful tool is ready to handle object detection and QR code recognition, and you can configure it as needed for your project.

3. Enable QR code detection

With the vision service running, activate QR code detection with the pyzbar module. Viam’s QR Code Codelab provides a complete walkthrough so you can get precise readings right away.

4. Run to decode your QR code

The QR code is able to be detected properly using the pyzbar module within the Viam app.
The QR code is able to be detected properly using the pyzbar module within the Viam app.

Now, it’s time to test. Place QR codes in front of your camera and check for detection in real-time. You can tweak settings, adjust distances, and try different lighting setups until you’re satisfied with the accuracy.

Trigger an action associated with the code’s detection

Now that your machine is set up to detect and decode QR codes, you can take it a step further by automating actions when a QR code is scanned. This means you can trigger anything from opening a URL to controlling other devices, adding interactivity to your setup.

To create an automation:

  1. Write a simple script: Create a process.py script to define the action triggered by a detected QR code.
  2. Set up Viam’s process configuration: In the Viam app, add a new Process and link it to your script and environment variables.
  3. Test and refine: Show a QR code to the camera to confirm it triggers the action successfully.

For the full setup details and code samples, check out Viam’s QR Code Codelab.

Real-world use cases for QR code detection

From inventory tracking to secure access, QR code detection offers a wide range of applications. Here are some examples where QR codes bring significant value:

Inventory management

Scanning an item within a warehouse to track inventory.
Scanning an item within a warehouse to track inventory.

QR codes offer an efficient way to track and update inventory in real time. By using Viam’s vision service to scan QR codes on items or containers, teams can automate inventory counts, reduce errors, and streamline logistics processes.

Augmented reality (AR)

QR codes can bridge physical and digital experiences, especially in AR applications. By integrating QR code detection into AR-enabled smart machines, developers can trigger digital content or additional information, enhancing user engagement and functionality. 

For example, artist Doddz incorporates QR codes into his artwork, using them to bring each piece to life.

Counterfeit detection

QR codes can include authentication data that helps verify a product’s origin. For industries concerned with authenticity, such as luxury goods or pharmaceuticals, using QR codes for anti-counterfeit measures provides an additional layer of security. Viam’s platform supports secure data handling, ensuring QR codes are processed accurately and reliably.

Guiding autonomous vehicles

QR codes placed along a route can give precise instructions to autonomous vehicles, such as directing them to specific locations, loading docks, or parking spots. This can streamline logistics in warehouses, airports, or large campuses where quick and accurate navigation is essential.

Secure access to physical spaces

Using a QR code to scan into an event.
Using a QR code to scan into an event (image source).

QR codes can control entry to restricted areas by serving as digital keys. Scanning a QR code at a door or gate can grant access to authorized personnel, adding an additional layer of security and tracking to access control systems.

Interactive retail and product information

QR codes on product packaging or displays can provide additional details, such as assembly instructions, product origin, or promotional offers. Shoppers can access this information instantly, enhancing the customer experience.

Equipment maintenance and diagnostics

QR codes on machinery can store maintenance histories, diagnostic instructions, or safety warnings. Technicians can scan a QR code on equipment to pull up relevant information, reducing downtime and streamlining troubleshooting.

Why Viam stands out for QR code detection projects

Viam’s approach focuses on flexibility, modularity, and transparency, making it an ideal choice for QR code detection projects. Here’s why:

  • Modularity and flexibility: Viam’s architecture is built to adapt. You can configure the vision service without affecting other system components, making scaling and updates a breeze.
  • Open-source advantage: With Viam’s open-source nature, developers have full transparency and control. You can access and adjust the code as your project grows or changes.
  • Fast experimentation and scaling: Viam’s platform is designed for rapid testing, so you can spend less time troubleshooting and more time building effective solutions.

Build your own QR detector today

QR code detection unlocks a lot of potential for IoT and smart devices, from tracking assets to interactive retail displays. Viam’s platform makes it easy to integrate QR code detection with flexibility and modularity, so you’re ready for any application.

Ready to start building? Check out Viam’s QR Code Codelab for a hands-on guide, or dive into our resources on CV to see what Viam can do for you!

Train a model for object detection

If you’re looking to go beyond QR codes, Viam offers the tools to train, test, and refine custom ML models for detecting practically any object you need. 

To get started, check out our object detection guide, where you’ll see how to set up data collection, define custom object classes, and fine-tune your model for reliable results, opening up new possibilities in your automation projects.

On this page

Get started with Viam today!