-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.py
More file actions
120 lines (109 loc) · 3.51 KB
/
scanner.py
File metadata and controls
120 lines (109 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Scanner for PT
#
import cv2
from pyzbar import pyzbar
import mysql.connector
from mysql.connector import Error
import RPi.GPIO as GPIO
import time
import threading
# SQL Start
def connect(host, user, password, db):
connection = None
try:
connection = mysql.connector.connect(
host=host,
user=user,
passwd=password,
database=db
)
print("[INFO] MySQL Database connection successful")
except Error as err:
print(f"[ERROR] '{err}'")
return connection
def read(connection, query):
cursor = connection.cursor()
result = None
try:
cursor.execute(query)
result = cursor.fetchone()
return result
except Error as err:
print(f"Error: '{err}'")
def update(connection, command):
cursor = connection.cursor()
cursor.execute(command)
connection.commit()
# GPIO: Controller
def unlock():
GPIO.output(2, 0)
print("DEBUG: unlock locker now for 30 second")
time.sleep(30)
GPIO.output(2, 1)
print("DEBUG: relock locker now")
# SQL: Logics
def brain(password):
connection = connect("localhost", "user", "password", "tiferet")
cmd = ("SELECT isdeliver, istaken FROM food WHERE NOT istaken=1 AND NOT iscooked=0 AND password=({})".format(password)) # locker validity check
sts = read(connection, cmd)
if not sts:
password = int(password)
password = password-10000 # deliverman
cmd = ("SELECT isdeliver, istaken FROM food WHERE NOT istaken=1 AND NOT iscooked=0 AND password=({})".format(password)) # locker validity check
sts = read(connection, cmd)
if not sts:
print("[ERROR] scanner: invalid qr detect")
return
print("[DEBUG] real password is ({})".format(password))
unlockCmd = threading.Thread(target=unlock)
unlockCmd.start()
cmd = ("UPDATE food SET isdeliver=1 WHERE password=({})".format(password))
update(connection, cmd)
else:
cmd = ("UPDATE food SET istaken=1 WHERE password=({})".format(password))
update(connection, cmd)
unlockCmd = threading.Thread(target=unlock)
unlockCmd.start()
cmd = ("SELECT locker FROM food WHERE istaken=1 AND password=({})".format(password))
ln = read(connection, cmd)
ln = ln[0]
cmd = ("UPDATE lockerdata SET isoccupy=0 WHERE lockerid=({})".format(ln))
update(connection, cmd)
# Barcode start
def read_barcodes(frame):
barcodes = pyzbar.decode(frame)
for barcode in barcodes:
x, y , w, h = barcode.rect
#1
barcode_info = barcode.data.decode('utf-8')
brain(barcode_info)
rmvtxt = "Please remove QRCode"
cv2.rectangle(frame, (x, y),(x+w, y+h), (0, 255, 0), 2)
#2
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame, rmvtxt, (x + 6, y - 6), font, 1.0, (144, 238, 144), 1)
#3
with open("barcode_result.txt", mode ='w') as file:
file.write("Recognized Barcode:" + barcode_info)
return frame
def main():
#0: GPIO settings
GPIO.setwarnings(True)
GPIO.setmode(GPIO.BCM)
GPIO.setup(2, GPIO.OUT)
#1
camera = cv2.VideoCapture(0)
ret, frame = camera.read()
#2
while ret:
ret, frame = camera.read()
frame = read_barcodes(frame)
cv2.imshow('Barcode/QR code reader', frame)
if cv2.waitKey(1) & 0xFF == 27:
break
#3
camera.release()
cv2.destroyAllWindows()
#4
if __name__ == '__main__':
main()