Why would you want to build your own QR code scanner? QR codes can encode a wide variety of information, including website URLs, contact details, or text and binary data. Using a QR code scanner, you can build custom applications to provide instructions to autonomous vehicles, permit security access to physical spaces, and many more use cases.

In this codelab, you'll learn how to use a QR code scanner to detect and decode QR codes using a Viam module. We'll leverage the pyzbar and OpenCV Python libraries to process images from a camera and extract information encoded in QR codes. By the end, you'll have a working solution that can identify QR codes in real-time to trigger customizable actions.

qr code demo

What You'll Build

Prerequisites

What You'll Need

What You'll Learn

Watch the Video

See a demonstration and overview of the QR scanner in this video.

Configure your machine

  1. In the Viam app under the LOCATIONS tab, create a machine by typing in a name and clicking Add machine. add machine
  2. Click View setup instructions. setup instructions
  3. To install viam-server on the Raspberry Pi device that you want to use to communicate with and control your webcam, select the Linux / Aarch64 platform for the Raspberry Pi, and leave your installation method as viam-agent. select platform
  4. Use the viam-agent to download and install viam-server on your Raspberry Pi. Follow the instructions to run the command provided in the setup instructions from the SSH prompt of your Raspberry Pi. installation agent
  5. The setup page will indicate when the machine is successfully connected. successful toast

Add your USB webcam

  1. Connect the USB webcam to the Raspberry Pi.
  2. In the Viam app, find the CONFIGURE tab. It's time to configure your hardware.
  3. Click the + icon in the left-hand menu and select Component. select component
  4. Select camera, and find the webcam module. This adds the module for working with a USB webcam. Leave the default name camera-1 for now. select webcam
  5. Notice adding this component adds the webcam hardware component called camera-1. The collapsible panel on the right corresponds to the part listed in the left sidebar. From the Attributes section of the panel, select a video_path. select video path
  6. Click Save in the top right to save and apply your configuration changes.
  7. At the bottom of the camera-1 panel, expand the TEST section to ensure you have configured the camera properly. test camera

Now that your hardware is working the way you want it, it's time to add a vision service to detect and decode a QR code.

  1. In the Viam app, click the + icon in the left-hand menu and select Service, and then vision. select vision
  2. Search for a module called pyzbar. Then click Add module, and Create a new vision service called vision-1. add module
  3. Notice this creates two new items in the left sidebar. The first is your new vision service called vision-1, and the second is your new pyzbar module.
  4. In the vision-1 panel under the Depends on section, check the camera-1 resource. This configures the vision service to depend on data coming in from the webcam.
  5. Save your changes in the top right and wait a few moments for the configuration changes to take effect.
  6. At the bottom of the vision-1 panel, expand the TEST section to ensure you have configured the vision service properly. Point the webcam towards a QR code to see if the camera detects it.
  7. If the scanner detects a QR code, a bounding box will highlight the QR code in the video feed, decode the data, and display it on the right under Labels. In the example shown here, the URL decoded was viam.com. qr code test

At this point, you have configured and tested your machine and webcam to detect and decode QR codes, but nothing else is happening automatically. In the next section, create an automatic process to run on your machine to trigger an action when a QR code is detected.

Create an automation script

  1. To configure the machine to automatically run a command to execute a script, use a Viam process. Create a new file on your computer called process.py.
    $ touch process.py
    
  2. Copy and paste this sample code into the new file process.py. This code will allow your Raspberry Pi to connect to your vision service and execute our logic.
  3. Now it's time to move your control code to your Raspberry Pi device. SSH into your Raspberry Pi if you're not already SSH'd.
  4. From the SSH prompt on your Raspberry Pi, install the Python package manager.
    $ sudo apt install -y python3-pip
    
  5. Install the Viam Python SDK into a new directory called process.
    $ pip3 install --target=process viam-sdk
    
  6. Display the full path of the current directory you are working in on your Raspberry Pi with the pwd command. Make a note of this output for the next steps.
    $ pwd
    
  7. Find the executable path of Python3 to run process.py on your Raspberry Pi with which python3. Again, make a note of this output for the next steps.
    $ which python3
    
  8. Run the following command from your computer (not the SSH prompt to your Raspberry Pi) to copy the code from your computer to your Raspberry Pi. In the command, you will copy process.py over to your Raspberry Pi, with the section following the colon : indicating where your file should be copied to on the Raspberry Pi (the path of the directory you are working in on your Raspberry Pi, along with the filename).
    $ scp process.py user@host.local:/home/myboard/process/process.py
    

Configure a Viam process

  1. Now let's allow viam-server to run the process as the root user on your Raspberry Pi by configuring a Viam process. In the Viam app under the CONFIGURE tab, click the + icon in the left-hand menu and select Process.
  2. Find the corresponding card to process-1. Enter the executable path of Python3 running on your Raspberry Pi that you output from a previous step. Add an argument of the process.py file to run on your Raspberry Pi. Enter the working directory where you want the process to execute. configure process
  3. Still within the process-1 card, select the advanced settings icon near the top right corner to review the configuration JSON. Create a new env property, and add your environment variables within the new property, formatted like the following with your own credentials.
      "env": {
        "ROBOT_API_KEY": "your-api-key",
        "ROBOT_API_KEY_ID": "your-api-key-id",
        "ROBOT_ADDRESS": "your-robot-address",
        "CAMERA_NAME": "camera-1",
        "VISION_NAME": "vision-1"
      },
    
    configure JSON
  4. Save your updates.
  5. You can test the process by generating an example QR code, and displaying it in front of your webcam.

The process running in this example parses a website URL and opens a browser. However you can trigger any kind of sequence of actuation, upon detecting and decoding a QR code. Keep reading for more inspiration and ideas.

Now that you have a camera that detects and decodes QR codes, you can do the following to enhance your Viam project:

Additional project ideas for scanning QR codes

Additional Viam resources