|
| 1 | +# SPDX-FileCopyrightText: 2023 Jim McKeown |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +""" |
| 5 | +This example shows the basic functionality to: |
| 6 | +Show the devices fingerprint slots that have fingerprints enrolled. |
| 7 | +Enroll a fingerprint in an existing or new fingerprint slot. |
| 8 | +Try to find a fingerprint in the existing list of enrolled fingerprints. |
| 9 | +Delete an enrolled fingerprint. |
| 10 | +View the image of a fingerprint. |
| 11 | +Preview the image of a fingerprint and then try to find the fingerprint |
| 12 | +in the existing list of enrolled fingerprints. |
| 13 | +
|
| 14 | +Please note that this example only works on single board computers |
| 15 | +with the use of Blinka. |
| 16 | +
|
| 17 | +This example is based on fingerprint_simpletest.py |
| 18 | +""" |
| 19 | + |
| 20 | +import time |
| 21 | +import numpy as np |
| 22 | +from matplotlib import pyplot as plt |
| 23 | +import serial |
| 24 | +import adafruit_fingerprint |
| 25 | + |
| 26 | +# led = DigitalInOut(board.D13) |
| 27 | +# led.direction = Direction.OUTPUT |
| 28 | + |
| 29 | +# This has not been tested: |
| 30 | +# uart = busio.UART(board.TX, board.RX, baudrate=57600) |
| 31 | + |
| 32 | +# If using with a computer such as Linux/RaspberryPi, Mac, Windows with USB/serial converter: |
| 33 | +# Edit ttyACM0 to your USB/serial port |
| 34 | +uart = serial.Serial("/dev/ttyACM0", baudrate=57600, timeout=1) |
| 35 | + |
| 36 | +# If using with Linux/Raspberry Pi and hardware UART: |
| 37 | +# import serial |
| 38 | +# uart = serial.Serial("/dev/ttyS0", baudrate=57600, timeout=1) |
| 39 | + |
| 40 | +finger = adafruit_fingerprint.Adafruit_Fingerprint(uart) |
| 41 | + |
| 42 | +################################################## |
| 43 | + |
| 44 | + |
| 45 | +def get_fingerprint(): |
| 46 | + """Get a finger print image, template it, and see if it matches!""" |
| 47 | + print("Waiting for image...") |
| 48 | + while finger.get_image() != adafruit_fingerprint.OK: |
| 49 | + pass |
| 50 | + print("Templating...") |
| 51 | + if finger.image_2_tz(1) != adafruit_fingerprint.OK: |
| 52 | + return False |
| 53 | + print("Searching...") |
| 54 | + if finger.finger_search() != adafruit_fingerprint.OK: |
| 55 | + return False |
| 56 | + return True |
| 57 | + |
| 58 | + |
| 59 | +# pylint: disable=too-many-branches |
| 60 | +def get_fingerprint_detail(): |
| 61 | + """Get a finger print image, template it, and see if it matches! |
| 62 | + This time, print out each error instead of just returning on failure""" |
| 63 | + print("Getting image...", end="") |
| 64 | + i = finger.get_image() |
| 65 | + if i == adafruit_fingerprint.OK: |
| 66 | + print("Image taken") |
| 67 | + else: |
| 68 | + if i == adafruit_fingerprint.NOFINGER: |
| 69 | + print("No finger detected") |
| 70 | + elif i == adafruit_fingerprint.IMAGEFAIL: |
| 71 | + print("Imaging error") |
| 72 | + else: |
| 73 | + print("Other error") |
| 74 | + return False |
| 75 | + |
| 76 | + print("Templating...", end="") |
| 77 | + i = finger.image_2_tz(1) |
| 78 | + if i == adafruit_fingerprint.OK: |
| 79 | + print("Templated") |
| 80 | + else: |
| 81 | + if i == adafruit_fingerprint.IMAGEMESS: |
| 82 | + print("Image too messy") |
| 83 | + elif i == adafruit_fingerprint.FEATUREFAIL: |
| 84 | + print("Could not identify features") |
| 85 | + elif i == adafruit_fingerprint.INVALIDIMAGE: |
| 86 | + print("Image invalid") |
| 87 | + else: |
| 88 | + print("Other error") |
| 89 | + return False |
| 90 | + |
| 91 | + print("Searching...", end="") |
| 92 | + i = finger.finger_fast_search() |
| 93 | + # pylint: disable=no-else-return |
| 94 | + # This block needs to be refactored when it can be tested. |
| 95 | + if i == adafruit_fingerprint.OK: |
| 96 | + print("Found fingerprint!") |
| 97 | + return True |
| 98 | + else: |
| 99 | + if i == adafruit_fingerprint.NOTFOUND: |
| 100 | + print("No match found") |
| 101 | + else: |
| 102 | + print("Other error") |
| 103 | + return False |
| 104 | + |
| 105 | + |
| 106 | +def get_fingerprint_photo(): |
| 107 | + """Get and show fingerprint image""" |
| 108 | + print("Waiting for image...") |
| 109 | + while finger.get_image() != adafruit_fingerprint.OK: |
| 110 | + pass |
| 111 | + print("Got image...Transferring image data...") |
| 112 | + imgList = finger.get_fpdata("image", 2) |
| 113 | + imgArray = np.zeros(73728, np.uint8) |
| 114 | + for i, val in enumerate(imgList): |
| 115 | + imgArray[(i * 2)] = val & 240 |
| 116 | + imgArray[(i * 2) + 1] = (val & 15) * 16 |
| 117 | + imgArray = np.reshape(imgArray, (288, 256)) |
| 118 | + plt.title("Fingerprint Image") |
| 119 | + plt.imshow(imgArray) |
| 120 | + plt.show(block=False) |
| 121 | + |
| 122 | + |
| 123 | +def get_fingerprint_preview(): |
| 124 | + """Get a finger print image, show it, template it, and see if it matches!""" |
| 125 | + print("Waiting for image...") |
| 126 | + while finger.get_image() != adafruit_fingerprint.OK: |
| 127 | + pass |
| 128 | + print("Got image...Transferring image data...") |
| 129 | + imgList = finger.get_fpdata("image", 2) |
| 130 | + imgArray = np.zeros(73728, np.uint8) |
| 131 | + for i, val in enumerate(imgList): |
| 132 | + imgArray[(i * 2)] = val & 240 |
| 133 | + imgArray[(i * 2) + 1] = (val & 15) * 16 |
| 134 | + imgArray = np.reshape(imgArray, (288, 256)) |
| 135 | + plt.title("Fingerprint Image") |
| 136 | + plt.imshow(imgArray) |
| 137 | + plt.show(block=False) |
| 138 | + print("Templating...") |
| 139 | + if finger.image_2_tz(1) != adafruit_fingerprint.OK: |
| 140 | + return False |
| 141 | + print("Searching...") |
| 142 | + if finger.finger_search() != adafruit_fingerprint.OK: |
| 143 | + return False |
| 144 | + return True |
| 145 | + |
| 146 | + |
| 147 | +# pylint: disable=too-many-statements |
| 148 | +def enroll_finger(location): |
| 149 | + """Take a 2 finger images and template it, then store in 'location'""" |
| 150 | + for fingerimg in range(1, 3): |
| 151 | + if fingerimg == 1: |
| 152 | + print("Place finger on sensor...", end="") |
| 153 | + else: |
| 154 | + print("Place same finger again...", end="") |
| 155 | + |
| 156 | + while True: |
| 157 | + i = finger.get_image() |
| 158 | + if i == adafruit_fingerprint.OK: |
| 159 | + print("Image taken") |
| 160 | + break |
| 161 | + if i == adafruit_fingerprint.NOFINGER: |
| 162 | + print(".", end="") |
| 163 | + elif i == adafruit_fingerprint.IMAGEFAIL: |
| 164 | + print("Imaging error") |
| 165 | + return False |
| 166 | + else: |
| 167 | + print("Other error") |
| 168 | + return False |
| 169 | + |
| 170 | + print("Templating...", end="") |
| 171 | + i = finger.image_2_tz(fingerimg) |
| 172 | + if i == adafruit_fingerprint.OK: |
| 173 | + print("Templated") |
| 174 | + else: |
| 175 | + if i == adafruit_fingerprint.IMAGEMESS: |
| 176 | + print("Image too messy") |
| 177 | + elif i == adafruit_fingerprint.FEATUREFAIL: |
| 178 | + print("Could not identify features") |
| 179 | + elif i == adafruit_fingerprint.INVALIDIMAGE: |
| 180 | + print("Image invalid") |
| 181 | + else: |
| 182 | + print("Other error") |
| 183 | + return False |
| 184 | + |
| 185 | + if fingerimg == 1: |
| 186 | + print("Remove finger") |
| 187 | + time.sleep(1) |
| 188 | + while i != adafruit_fingerprint.NOFINGER: |
| 189 | + i = finger.get_image() |
| 190 | + |
| 191 | + print("Creating model...", end="") |
| 192 | + i = finger.create_model() |
| 193 | + if i == adafruit_fingerprint.OK: |
| 194 | + print("Created") |
| 195 | + else: |
| 196 | + if i == adafruit_fingerprint.ENROLLMISMATCH: |
| 197 | + print("Prints did not match") |
| 198 | + else: |
| 199 | + print("Other error") |
| 200 | + return False |
| 201 | + |
| 202 | + print("Storing model #%d..." % location, end="") |
| 203 | + i = finger.store_model(location) |
| 204 | + if i == adafruit_fingerprint.OK: |
| 205 | + print("Stored") |
| 206 | + else: |
| 207 | + if i == adafruit_fingerprint.BADLOCATION: |
| 208 | + print("Bad storage location") |
| 209 | + elif i == adafruit_fingerprint.FLASHERR: |
| 210 | + print("Flash storage error") |
| 211 | + else: |
| 212 | + print("Other error") |
| 213 | + return False |
| 214 | + |
| 215 | + return True |
| 216 | + |
| 217 | + |
| 218 | +################################################## |
| 219 | + |
| 220 | + |
| 221 | +def get_num(): |
| 222 | + """Use input() to get a valid number from 1 to 127. Retry till success!""" |
| 223 | + i = 0 |
| 224 | + while (i > 127) or (i < 1): |
| 225 | + try: |
| 226 | + i = int(input("Enter ID # from 1-127: ")) |
| 227 | + except ValueError: |
| 228 | + pass |
| 229 | + return i |
| 230 | + |
| 231 | + |
| 232 | +while True: |
| 233 | + print("----------------") |
| 234 | + if finger.read_templates() != adafruit_fingerprint.OK: |
| 235 | + raise RuntimeError("Failed to read templates") |
| 236 | + print("Fingerprint templates:", finger.templates) |
| 237 | + print("e) enroll print") |
| 238 | + print("f) find print") |
| 239 | + print("d) delete print") |
| 240 | + print("v) view print") |
| 241 | + print("p) preview and find print") |
| 242 | + print("----------------") |
| 243 | + c = input("> ") |
| 244 | + |
| 245 | + if c == "e": |
| 246 | + enroll_finger(get_num()) |
| 247 | + if c == "f": |
| 248 | + if get_fingerprint(): |
| 249 | + print("Detected #", finger.finger_id, "with confidence", finger.confidence) |
| 250 | + else: |
| 251 | + print("Finger not found") |
| 252 | + if c == "d": |
| 253 | + if finger.delete_model(get_num()) == adafruit_fingerprint.OK: |
| 254 | + print("Deleted!") |
| 255 | + else: |
| 256 | + print("Failed to delete") |
| 257 | + if c == "v": |
| 258 | + get_fingerprint_photo() |
| 259 | + if c == "p": |
| 260 | + if get_fingerprint_preview(): |
| 261 | + print("Detected #", finger.finger_id, "with confidence", finger.confidence) |
| 262 | + else: |
| 263 | + print("Finger not found") |
0 commit comments