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.
See a demonstration and overview of the QR scanner in this video.
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
. 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. camera
, and find the webcam
module. This adds the module for working with a USB webcam. Leave the default name camera-1
for now. 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
. camera-1
panel, expand the TEST section to ensure you have configured the camera properly.
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.
vision
. pyzbar
. Then click Add module, and Create a new vision service called vision-1
. vision-1
, and the second is your new pyzbar
module.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.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.viam.com
.
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.
process.py
.$ touch process.py
process.py
. This code will allow your Raspberry Pi to connect to your vision service and execute our logic.$ sudo apt install -y python3-pip
process
.$ pip3 install --target=process viam-sdk
pwd
command. Make a note of this output for the next steps.$ pwd
process.py
on your Raspberry Pi with which python3
. Again, make a note of this output for the next steps.$ which python3
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
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.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. 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"
},
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: