forked from NathanShutter/Touch-And-Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfingerprint-test.py
309 lines (269 loc) · 9.4 KB
/
fingerprint-test.py
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
##for lcd support
import sys
##for mysql support
##import mysqlkey
sys.path.append('lcdlib')
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
I2C_ADDR = 0x27
I2C_NUM_ROWS = 4
I2C_NUM_COLS = 20
lcd = I2cLcd(1, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
#lcd.putstr("Great! It Works!")
#lcd.move_to(0,3)
#lcd.putstr("freva.com")
import time
import serial
import adafruit_fingerprint
# import board
# uart = busio.UART(board.TX, board.RX, baudrate=57600)
# If using with a computer such as Linux/RaspberryPi, Mac, Windows with USB/serial converter:
uart = serial.Serial("/dev/ttyUSB0", baudrate=57600, timeout=1)
# If using with Linux/Raspberry Pi and hardware UART:
# uart = serial.Serial("/dev/ttyS0", baudrate=57600, timeout=1)
# If using with Linux/Raspberry Pi 3 with pi3-disable-bt
# uart = serial.Serial("/dev/ttyAMA0", baudrate=57600, timeout=1)
finger = adafruit_fingerprint.Adafruit_Fingerprint(uart)
def get_fingerprint():
"""Get a finger print image, template it, and see if it matches!"""
lcd.clear()
lcd.putstr("Place finger on ")
lcd.move_to(0,1)
lcd.putstr("sensor...")
print("Waiting for image...")
while finger.get_image() != adafruit_fingerprint.OK:
pass
print("Templating...")
if finger.image_2_tz(1) != adafruit_fingerprint.OK:
return False
print("Searching...")
if finger.finger_search() != adafruit_fingerprint.OK:
return False
return True
# pylint: disable=too-many-branches
def get_fingerprint_detail():
"""Get a finger print image, template it, and see if it matches!
This time, print out each error instead of just returning on failure"""
print("Getting image...", end="")
i = finger.get_image()
if i == adafruit_fingerprint.OK:
print("Image taken")
else:
if i == adafruit_fingerprint.NOFINGER:
print("No finger detected")
elif i == adafruit_fingerprint.IMAGEFAIL:
print("Imaging error")
else:
print("Other error")
return False
print("Templating...", end="")
i = finger.image_2_tz(1)
if i == adafruit_fingerprint.OK:
print("Templated")
else:
if i == adafruit_fingerprint.IMAGEMESS:
print("Image too messy")
elif i == adafruit_fingerprint.FEATUREFAIL:
print("Could not identify features")
elif i == adafruit_fingerprint.INVALIDIMAGE:
print("Image invalid")
else:
print("Other error")
return False
print("Searching...", end="")
i = finger.finger_fast_search()
# pylint: disable=no-else-return
# This block needs to be refactored when it can be tested.
if i == adafruit_fingerprint.OK:
print("Found fingerprint!")
return True
else:
if i == adafruit_fingerprint.NOTFOUND:
print("No match found")
else:
print("Other error")
return False
# pylint: disable=too-many-statements
def enroll_finger(location):
"""Take a 2 finger images and template it, then store in 'location'"""
lcd.clear()
for fingerimg in range(1, 3):
if fingerimg == 1:
lcd.putstr("Place finger on ")
lcd.move_to(0,1)
lcd.putstr("sensor...")
print("Place finger on sensor...", end="")
else:
lcd.clear()
lcd.putstr("Place same finger")
lcd.move_to(0,1)
lcd.putstr(" again...")
print("Place same finger again...", end="")
while True:
i = finger.get_image()
if i == adafruit_fingerprint.OK:
print("Image taken")
break
if i == adafruit_fingerprint.NOFINGER:
print(".", end="")
elif i == adafruit_fingerprint.IMAGEFAIL:
print("Imaging error")
return False
else:
print("Other error")
return False
print("Templating...", end="")
i = finger.image_2_tz(fingerimg)
if i == adafruit_fingerprint.OK:
print("Templated")
else:
if i == adafruit_fingerprint.IMAGEMESS:
print("Image too messy")
elif i == adafruit_fingerprint.FEATUREFAIL:
print("Could not identify features")
elif i == adafruit_fingerprint.INVALIDIMAGE:
print("Image invalid")
else:
print("Other error")
return False
if fingerimg == 1:
lcd.clear()
lcd.putstr("Remove finger")
print("Remove finger")
time.sleep(1)
while i != adafruit_fingerprint.NOFINGER:
i = finger.get_image()
print("Creating model...", end="")
i = finger.create_model()
if i == adafruit_fingerprint.OK:
print("Created")
else:
if i == adafruit_fingerprint.ENROLLMISMATCH:
print("Prints did not match")
else:
print("Other error")
return False
print("Storing model #%d..." % location, end="")
i = finger.store_model(location)
if i == adafruit_fingerprint.OK:
lcd.clear()
lcd.putstr("Stored successfully")
print("Stored")
time.sleep(2)
lcd.clear()
lcd.putstr("Enter your name")
name = input("Enter your name: ")
mysqlkey.query(name)
time.sleep(2)
lcd.clear()
else:
if i == adafruit_fingerprint.BADLOCATION:
print("Bad storage location")
elif i == adafruit_fingerprint.FLASHERR:
print("Flash storage error")
else:
print("Other error")
return False
return True
def save_fingerprint_image(filename):
"""Scan fingerprint then save image to filename."""
while finger.get_image():
pass
# let PIL take care of the image headers and file structure
from PIL import Image # pylint: disable=import-outside-toplevel
img = Image.new("L", (256, 288), "white")
pixeldata = img.load()
mask = 0b00001111
result = finger.get_fpdata(sensorbuffer="image")
# this block "unpacks" the data received from the fingerprint
# module then copies the image data to the image placeholder "img"
# pixel by pixel. please refer to section 4.2.1 of the manual for
# more details. thanks to Bastian Raschke and Danylo Esterman.
# pylint: disable=invalid-name
x = 0
# pylint: disable=invalid-name
y = 0
# pylint: disable=consider-using-enumerate
for i in range(len(result)):
pixeldata[x, y] = (int(result[i]) >> 4) * 17
x += 1
pixeldata[x, y] = (int(result[i]) & mask) * 17
if x == 255:
x = 0
y += 1
else:
x += 1
if not img.save(filename):
return True
return False
##################################################
def get_num(max_number):
"""Use input() to get a valid number from 0 to the maximum size
of the library. Retry till success!"""
i = -1
while (i > max_number - 1) or (i < 0):
try:
i = int(input("Enter ID # from 0-{}: ".format(max_number - 1)))
except ValueError:
pass
return i
while True:
print("----------------")
if finger.read_templates() != adafruit_fingerprint.OK:
raise RuntimeError("Failed to read templates")
print("Fingerprint templates: ", finger.templates)
if finger.count_templates() != adafruit_fingerprint.OK:
raise RuntimeError("Failed to read templates")
print("Number of templates found: ", finger.template_count)
if finger.read_sysparam() != adafruit_fingerprint.OK:
raise RuntimeError("Failed to get system parameters")
print("Size of template library: ", finger.library_size)
print("e) enroll print")
print("f) find print")
print("d) delete print")
print("s) save fingerprint image")
print("r) reset library")
print("q) quit")
print("----------------")
c = input("> ")
if c == "e":
enroll_finger(get_num(finger.library_size))
if c == "f":
if get_fingerprint():
lcd.clear()
lcd.putstr("Enter your name")
name = input("Enter your name: ")
output = "Detected #{}".format(finger.finger_id)
lcd.clear()
lcd.putstr(output)
lcd.move_to(0,1)
output = "with confidence {}".format(finger.confidence)
lcd.putstr(output)
lcd.move_to(0,3)
output = "Welcome {}".format(name)
lcd.putstr(output)
print("Detected #", finger.finger_id, "with confidence", finger.confidence)
time.sleep(3)
lcd.clear()
else:
print("Finger not found")
if c == "d":
if finger.delete_model(get_num(finger.library_size)) == adafruit_fingerprint.OK:
print("Deleted!")
else:
print("Failed to delete")
if c == "s":
if save_fingerprint_image("fingerprint.png"):
print("Fingerprint image saved")
else:
print("Failed to save fingerprint image")
if c == "r":
if finger.empty_library() == adafruit_fingerprint.OK:
print("Library empty!")
else:
print("Failed to empty library")
if c == "q":
print("Exiting fingerprint example program")
raise SystemExit