Bird
0
0
Raspberry Piprogramming~20 mins

QR code reading in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
QR Code Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of QR code reading snippet
What will be the output of this Python code snippet that reads a QR code image using OpenCV and pyzbar on a Raspberry Pi?
Raspberry Pi
import cv2
from pyzbar.pyzbar import decode

img = cv2.imread('qrcode.png')
detected = decode(img)
for obj in detected:
    print(obj.data.decode('utf-8'))
ARaises FileNotFoundError because 'qrcode.png' does not exist
BPrints nothing because decode returns an empty list
CPrints the raw bytes of the QR code without decoding
DPrints the decoded text from the QR code in 'qrcode.png'
Attempts:
2 left
💡 Hint
Check what decode() returns and how obj.data is processed.
🧠 Conceptual
intermediate
1:00remaining
Understanding QR code detection libraries
Which library is commonly used on Raspberry Pi to decode QR codes from images in Python?
Amatplotlib
Bpyzbar
Cnumpy
Dpygame
Attempts:
2 left
💡 Hint
It is a wrapper around the ZBar library.
🔧 Debug
advanced
2:00remaining
Identify the error in QR code reading code
What error will this code produce when run on a Raspberry Pi with OpenCV and pyzbar installed? import cv2 from pyzbar.pyzbar import decode img = cv2.imread('qrcode.png') result = decode(img) print(result.data.decode('utf-8'))
AAttributeError: 'list' object has no attribute 'data'
BFileNotFoundError: 'qrcode.png' not found
CTypeError: decode() missing required positional argument
DNo error, prints decoded QR code text
Attempts:
2 left
💡 Hint
Check the type of the object returned by decode().
📝 Syntax
advanced
2:30remaining
Correct syntax for reading QR code from camera
Which code snippet correctly reads a frame from the Raspberry Pi camera using OpenCV and decodes a QR code?
A
import cv2
from pyzbar.pyzbar import decode
cap = cv2.VideoCapture(0)
ret, frame = cap.read
result = decode(frame)
print(result.data.decode('utf-8'))
cap.release()
B
import cv2
from pyzbar.pyzbar import decode
cap = cv2.VideoCapture(0)
frame = cap.read()
detected = decode(frame)
for d in detected:
    print(d.data.decode('utf-8'))
cap.release()
C
import cv2
from pyzbar.pyzbar import decode
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
detected = decode(frame)
for d in detected:
    print(d.data.decode('utf-8'))
cap.release()
D
import cv2
from pyzbar.pyzbar import decode
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
result = decode(frame)
print(result.data.decode('utf-8'))
cap.release()
Attempts:
2 left
💡 Hint
Check the return value of cap.read() and how to handle decode results.
🚀 Application
expert
1:30remaining
Number of QR codes detected in an image
Given this code snippet running on Raspberry Pi, how many QR codes will be detected and printed if the image contains three distinct QR codes? import cv2 from pyzbar.pyzbar import decode img = cv2.imread('multi_qr.png') detected = decode(img) print(len(detected))
A3
B1
C0
DRaises an error
Attempts:
2 left
💡 Hint
decode() returns a list of all detected QR codes in the image.