Skip to content

Instantly share code, notes, and snippets.

@IdrisCytron
Created July 28, 2020 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save IdrisCytron/e593817b3dd97edde5ed81a6d8524370 to your computer and use it in GitHub Desktop.
Save IdrisCytron/e593817b3dd97edde5ed81a6d8524370 to your computer and use it in GitHub Desktop.
Record QR Data Into CSV Format Using Raspberry Pi.
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()
@Kenandorae
Copy link

Kenandorae commented Jul 25, 2021

Hi, I need help in this code. I'm extremely new to programming and in raspberry pi. Can I use an infrared proximity sensor as the switch to scan the QR code? If so, can you upload the code? I already tried editing the code but it doesn't make it work. Thank you very much and have a good day

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment