Arduino “having11” Guy
Published © GPL3+

Scan QR Codes in Real-Time with Raspberry Pi

Use a webcam and a Raspberry Pi 4 to extract information from QR codes and even make your own with Python

BeginnerFull instructions provided2 hours7,969

Things used in this project

Hardware components

Raspberry Pi 4 Model B
Raspberry Pi 4 Model B
×1
Camera Module V2
Raspberry Pi Camera Module V2
×1

Software apps and online services

VS Code
Microsoft VS Code
Python 3.6

Story

Read more

Schematics

Raspberry Pi 4 + Camera

Code

Python Code

Python
import cv2

cap = cv2.VideoCapture(0)

detector = cv2.QRCodeDetector()

while True:
    _, img = cap.read()
    data, bbox, _ = detector.detectAndDecode(img)
    
    if(bbox is not None):
        for i in range(len(bbox)):
            cv2.line(img, tuple(bbox[i][0]), tuple(bbox[(i+1) % len(bbox)][0]), color=(255,
                     0, 255), thickness=2)
        cv2.putText(img, data, (int(bbox[0][0][0]), int(bbox[0][0][1]) - 10), cv2.FONT_HERSHEY_SIMPLEX,
                    0.5, (0, 255, 0), 2)
        if data:
            print("data found: ", data)
    cv2.imshow("code detector", img)
    if(cv2.waitKey(1) == ord("q")):
        break
cap.release()
cv2.destroyAllWindows()

Credits

Arduino “having11” Guy

Arduino “having11” Guy

81 projects • 538 followers
18 year-old IoT and embedded systems enthusiast. Also an intern at Hackster.io who loves working on projects and sharing knowledge.

Comments