Chapter 3 – Vision and Navigation Technologies with Zumi
3.4 – QR Codes
Overview
You’ve probably seen Quick Response (QR) codes for a wide variety of things: to connect to WiFi, go to a URL, play games, or make payments. These codes are so popular because they are condensed labels that can easily be read by a computer.
Scanning a QR code
QR codes may look like a random array of pixels to humans, but to a computer, they contain encoded information!
QR codes store information in the form of binary numbers. Binary is the language used by computers to retrive, process and send data. In binary, information is stored in the form of 0s and 1s.
Import Libraries
To enable Zumi to read QR codes, we need to import some libraries.
from zumi.zumi import Zumi
from zumi.util.camera import Camera
from zumi.util.vision import Vision
camera = Camera()
vision = Vision()
Scanning QR Codes
This code first starts the camera. Then for a certain amount of time, it waits until a QR code is in the camera’s frame where it shows the output of it.
camera.start_camera()
try:
for i in range(50):
frame = camera.capture()
vision.find_QR_code(frame)
camera.show_image(frame)
camera.clear_output()
finally:
print("Done!")
camera.close()
After running the code and showing it this QR Code, this is what Zumi gets as a result.
Activity
Let’s send moving instructions to Zumi using a QR code.
To do so, we need to ask Zumi to:
- Turn on the camera, and scan the provided QR
- Decode the instruction found from the QR code
- Run the decoded instruction on Zumi
The template is provided below. Just write the correct code where there are comments and fill in the missing blank.
from zumi.zumi import Zumi
from zumi.util.camera import Camera
from zumi.util.vision import Vision
camera = Camera()
vision = Vision()
#create a message variable and set it to empty string
#start camera
for i in range(50):
frame = #capture frame on camera
qr_code = vision.find_QR_code(frame)
camera.show_image(______) #fill in the blank
message = vision.get_QR_message(qr_code)
if message == "left":
#turn left by 90 degrees
#print message
elif message == "right":
#turn right by 90 degrees
#print message
camera.clear_output()
#close camera
Here are the QR codes for left
and right
.
Solution:
from zumi.zumi import Zumi
from zumi.util.camera import Camera
from zumi.util.vision import Vision
camera = Camera()
vision = Vision()
#create a message variable and set it to empty string
message = ""
#start camera
camera.start_camera()
for i in range(50):
frame = camera.capture() #capture frame on camera
qr_code = vision.find_QR_code(frame)
camera.show_image(frame) #fill in the blank
message = vision.get_QR_message(qr_code)
if message == "left":
#turn left by 90 degrees
zumi.turn_left()
#print message
print(message)
elif message == "right":
#turn right by 90 degrees
zumi.turn_right()
#print message
print(message)
camera.clear_output()
#close camera
camera.close()
Challenge: Binary decoding
Ready to go beyond? Use the ASCII chart provided below to write a short message in binary, and then swap with a friend to decode each other’s messages.
Conclusion
And that’s a wrap on QR codes! Try exploring other ways you can use QR codes to send instructions to Zumi.