Record QR Data Into CSV Format Using Raspberry Pi

This tutorial is suitable for QR Attendance System application.

Introduction

After we are able to scan QR code using Raspberry Pi camera, now we want to record those data in CSV format. So, later we can open it using Microsoft Excel, Google Sheets or LibreOffice Calculator. Before that, please make sure you have read and developed following tutorial:

Video

This video shows how to record QR data into CSV format using Raspberry Pi.

Hardware Preparation

This is the list of items used in the video.

Sample Program

This is python 3 sample program to scan and read QR code using Raspberry Pi camera, and stored the data into CSV format.

from gpiozero import LED, Button, Buzzer
import cv2
import re
import csv
led = LED(19)
sw1 = Button(21)
buzzer = Buzzer(26)
cap = cv2.VideoCapture(0)
detector = cv2.QRCodeDetector()
def sw1Pressed():
global sw1Press
sw1Press = True
sw1.when_pressed = sw1Pressed
sw1Press = False
print("Reading QR code using Raspberry Pi camera")
print("Press SW1 to scan.")
print()
while True:
if sw1Press == True:
led.toggle()
_, 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, 0), 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:
sw1Press = False
data = data.split(",")
print("ID: " + data[0])
print("Name: " + data[1])
print("Class: " + data[2])
print()
userScanned = False
with open('Database.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row["ID"] == data[0]:
buzzer.beep(0.1, 0.1, 1)
userScanned = True
if userScanned == False:
buzzer.beep(0.1, 0.1, 2)
with open('Database.csv', 'a') as csvfile:
fieldNames = ['ID', 'NAME', 'CLASS']
writer = csv.DictWriter(csvfile, fieldnames=fieldNames)
writer.writerow({'ID': data[0], 'NAME': data[1], 'CLASS': data[2]})
led.off()
cv2.imshow("code detector", img)
else:
cap.read()
cv2.destroyAllWindows()
if cv2.waitKey(1) == ord("q"):
break
led.off()
cap.release()
cv2.destroyAllWindows()

view raw
RPiQRScannerCSV.py
hosted with ❤ by GitHub

Thank You

References:

Thanks for reading this tutorial. If you have any technical inquiries, please post at Cytron Technical Forum.

Please be reminded, this tutorial is prepared for you to try and learn.
You are encouraged to improve the code for better application.

1 thought on “Record QR Data Into CSV Format Using Raspberry Pi”

  1. Hi,

    I would like some help with this code. This is exactly what i need but i need a relay to be triggered and turn on for 10 seconds after the qr code is scanned and then turn off the relay.

    I’m a extreme newbie at coding and have been following your videos for a long time and have been purchasing alot from your store. I really need help with this particular code. Thanks alot

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

Getting Started with Robotic Operating System (ROS) and JetRacer AI Kit
Barrier Gate with micro:bit and MakeCode
Play MP3 File on Maker Pi Pico Using CircuitPython
Remote Jetson Nano using SSH with Visual Studio Code
3.3V Serial LCD with Raspberry Pi Pico via SPI or I2C
Tutorials of Cytron Technologies Scroll to Top