-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractical-10.py
More file actions
173 lines (146 loc) · 4.83 KB
/
practical-10.py
File metadata and controls
173 lines (146 loc) · 4.83 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
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
# pip install adafruit-circuitpython-fingerprint
import adafruit_fingerprint as fp
import board
import busio
usb = busio.UART(board.TX, board.RX, baudrate=57600)
sensor = fp.Adafruit_Fingerprint(usb)
def search():
print("Place finger on the sensor...", end=" ", flush=True)
return_code = sensor.get_image()
while return_code == fp.NOFINGER:
return_code = sensor.get_image()
if return_code == fp.OK:
print("Image taken")
else:
if return_code == fp.NOFINGER:
print("No finger detected")
elif return_code == fp.IMAGEFAIL:
print("Imaging error")
else:
print("Other error")
return return_code
print("Templating...", end=" ", flush=True)
return_code = sensor.image_2_tz()
if return_code == fp.OK:
print("Templated")
else:
if return_code == fp.IMAGEMESS:
print("Image too messy")
elif return_code == fp.FEATUREFAIL:
print("Could not identify features")
elif return_code == fp.INVALIDIMAGE:
print("Image invalid")
else:
print("Other error")
return return_code
print("Searching...", end=" ", flush=True)
return_code = sensor.finger_fast_search()
if return_code == fp.OK:
print("Found fingerprint!")
else:
if return_code == fp.NOTFOUND:
print("No match found")
else:
print("Other error")
return return_code
return return_code
def enroll(id):
for i in range(2):
print(f"Place finger on the sensor{" again"*i}...", end=" ", flush=True)
return_code = sensor.get_image()
while return_code == fp.NOFINGER:
return_code = sensor.get_image()
if return_code == fp.OK:
print("Image taken")
else:
if return_code == fp.IMAGEFAIL:
print("Imaging error")
else:
print("Other error")
return return_code
print("Templating...", end=" ", flush=True)
return_code = sensor.image_2_tz(i+1)
if return_code == fp.OK:
print("Templated")
else:
if return_code == fp.IMAGEMESS:
print("Image too messy")
elif return_code == fp.FEATUREFAIL:
print("Could not identify features")
elif return_code == fp.INVALIDIMAGE:
print("Image invalid")
else:
print("Other error")
return return_code
if i == 0:
print("Remove finger")
while return_code != fp.NOFINGER:
return_code = sensor.get_image()
print("Creating model...", end=" ", flush=True)
return_code = sensor.create_model()
if return_code == fp.OK:
print("Created")
else:
if return_code == fp.ENROLLMISMATCH:
print("Prints did not match")
else:
print("Other error")
return return_code
print(f"Storing model #{id}...", end=" ", flush=True)
return_code = sensor.store_model(id)
if return_code == fp.OK:
print("Stored")
else:
if return_code == fp.BADLOCATION:
print("Bad storage location")
elif return_code == fp.FLASHERR:
print("Flash storage error")
else:
print("Other error")
return return_code
return return_code
def get_id():
i = -1
while i not in range(sensor.library_size):
i = int(input(f"Enter ID between 0-{sensor.library_size-1}: "))
return i
while True:
print("-"*40)
if sensor.read_templates() != fp.OK:
raise RuntimeError("Failed to read templates")
print("Fingerprint templates:", sensor.templates)
if sensor.count_templates() != fp.OK:
raise RuntimeError("Failed to read templates")
print(f"Number of templates in library: {sensor.template_count}/{sensor.library_size}")
print("e - enroll print")
print("s - search print")
print("d - delete print")
print("r - reset library")
print("q - quit")
inp = input("> ")
if inp == "e":
if enroll(get_id()) == fp.OK:
print("Enrolled!")
else:
print("Failed to enroll")
elif inp == "s":
if search() == fp.OK:
print(f"Detected #{sensor.finger_id} with confidence {sensor.confidence}")
else:
print("Finger not found")
elif inp == "d":
if (id:=get_id()) not in sensor.templates:
print("ID not found")
elif sensor.delete_model(id) == fp.OK:
print("Deleted!")
else:
print("Failed to delete")
elif inp == "r":
if sensor.empty_library() == fp.OK:
print("Library empty!")
else:
print("Failed to empty library")
elif inp == "q":
quit()
else:
print("Invalid input")