def register_finger(): ## Reads image and download it ## ## Tries to initialize the sensor for i in range(fingerNumber): get_finger_image() try: f = PyFingerprint('/dev/ttyUSB0', 57600, 0xFFFFFFFF, 0x00000000) if (f.verifyPassword() == False): raise ValueError( 'The given fingerprint sensor password is wrong!') except Exception as e: print('The fingerprint sensor could not be initialized!') print('Exception message: ' + str(e)) exit(1) ## Tries to read image and download it try: ## Wait that finger is read while (f.readImage() == False): pass finger = os.getcwd() + '/registerFinger' + str(i + 1) + '.bmp' f.downloadImage(finger) print("Finger " + str(i + 1) + " Success") except Exception as e: print('Operation failed!') print('Exception message: ' + str(e)) exit(1)
def display(): ## Reads image and download it ## ## Tries to initialize the sensor try: f = PyFingerprint('/dev/ttyUSB0', 57600, 0xFFFFFFFF, 0x00000000) if (f.verifyPassword() == False): raise ValueError('The given fingerprint sensor password is wrong!') except Exception as e: print('The fingerprint sensor could not be initialized!') print('Exception message: ' + str(e)) exit(1) ## Gets some sensor information print('Currently used templates: ' + str(f.getTemplateCount()) + '/' + str(f.getStorageCapacity())) ## Tries to read image and download it try: print('Waiting for finger...') ## Wait that finger is read while (f.readImage() == False): pass print('Downloading image (this take a while)...') imageDestination = tempfile.gettempdir() + '/fingerprint.bmp' f.downloadImage(imageDestination) print('The image was saved to "' + imageDestination + '".') print('Displaying fingerprint:') os.system('eog /tmp/fingerprint.bmp') except Exception as e: print('Operation failed!') print('Exception message: ' + str(e)) exit(1)
if ( f.verifyPassword() == False ): raise ValueError('The given fingerprint sensor password is wrong!') except Exception as e: print('The fingerprint sensor could not be initialized!') print('Exception message: ' + str(e)) exit(1) ## Gets some sensor information print('Currently stored templates: ' + str(f.getTemplateCount())) ## Tries to read image and download it try: print('Waiting for finger...') ## Wait that finger is read while ( f.readImage() == False ): pass print('Downloading image (this take a while)...') imageDestination = tempfile.gettempdir() + '/fingerprint.bmp' f.downloadImage(imageDestination) print('The image was saved to "' + imageDestination + '".') except Exception as e: print('Operation failed!') print('Exception message: ' + str(e)) exit(1)
except Exception as e: print('The fingerprint sensor could not be initialized!') print('Exception message: ' + str(e)) exit(1) ## Gets some sensor information print('Currently used templates: ' + str(f.getTemplateCount()) + '/' + str(f.getStorageCapacity())) ## Tries to read image and download it try: print('Waiting for finger...') ## Wait that finger is read while (f.readImage() == False): pass print('Downloading image (this take a while)...') #imageDestination = tiempfile.gettempdir() + '/fingerprint.bmp' imageDestination = '/home/pi/fingerprint.bmp' f.downloadImage(imageDestination) print('The image was saved to "' + imageDestination + '".') except Exception as e: print('Operation failed!') print('Exception message: ' + str(e)) exit(1)
def search(): try: f = PyFingerprint('/dev/ttyUSB0', 57600, 0xFFFFFFFF, 0x00000000) if (f.verifyPassword() == False): GPIO.output(BLUE, 1) sleep(1) GPIO.output(BLUE, 0) raise ValueError('The given fingerprint sensor password is wrong!') except Exception as e: GPIO.output(BLUE, 1) sleep(1) GPIO.output(BLUE, 0) print('The fingerprint sensor could not be initialized!') print('Exception message: ' + str(e)) exit(1) ## Gets some sensor information print('Currently used templates: ' + str(f.getTemplateCount()) + '/' + str(f.getStorageCapacity())) ## Tries to search the finger and calculate hash try: print('Waiting for finger...') ## Wait that finger is read while (f.readImage() == False): pass print('Downloading image (this take a while)...') if (os.path.isfile('/home/pi/webapp/static/fingerprint.bmp')): os.remove('/home/pi/webapp/static/fingerprint.bmp') imageDestination = '/home/pi/webapp/static/fingerprint.bmp' f.downloadImage(imageDestination) print('The image was saved to "' + imageDestination + '".') ## Converts read image to characteristics and stores it in charbuffer 1 f.convertImage(0x01) ## Searchs template result = f.searchTemplate() positionNumber = result[0] accuracyScore = result[1] if (positionNumber == -1): GPIO.output(RED, 1) sleep(1) GPIO.output(RED, 0) print('No match found!') return 0 else: GPIO.output(GREEN, 1) sleep(1) GPIO.output(GREEN, 0) print('Found template at position #' + str(positionNumber)) print('The accuracy score is: ' + str(accuracyScore)) ## OPTIONAL stuff ## ## Loads the found template to charbuffer 1 f.loadTemplate(positionNumber, 0x01) ## Downloads the characteristics of template loaded in charbuffer 1 characterics = str(f.downloadCharacteristics(0x01)).encode('utf-8') ## Hashes characteristics of template print('SHA-2 hash of template: ' + hashlib.sha256(characterics).hexdigest()) return 1 except Exception as e: print('Operation failed!') print('Exception message: ' + str(e)) exit(1)
def read_finger_data(): ## Reads image and download it ## ## Tries to initialize the sensor try: f = PyFingerprint('/dev/ttyUSB0', 57600, 0xFFFFFFFF, 0x00000000) if (f.verifyPassword() == False): raise ValueError('The given fingerprint sensor password is wrong!') except Exception as e: print('The fingerprint sensor could not be initialized!') print('Exception message: ' + str(e)) exit(1) ## Tries to read image and download it try: ## Wait that finger is read while (f.readImage() == False): pass #saving image from sensor to local imageDestination = os.getcwd() + '/fingerprint.bmp' f.downloadImage(imageDestination) with open("fingerprint.bmp", "rb") as image_file: baseImageData = base64.b64encode(image_file.read()).decode('ascii') #payload format to send to server basePayload = { "file": baseImageData, "kampus": kampus, "lokasi": location } try: #Send data to Server baseData = requests.get(url + "finger", json=basePayload) #Result from Server baseResult = baseData.json() #Get Lecturer Photo based on kode #get_photo = url+"foto?kode="+baseResult['kode']+"&institusi="+baseResult['institusi'] # get_photo = url+"foto?kode=00670&institusi=1" get_photo = url + "foto?kode=" + baseResult['kode'] + "&institusi=1" #Converting Image to base64, decode to ascii to get string, not bytes photo_encode = base64.b64encode( requests.get(get_photo).content).decode('ascii') return { "result": 'success', "data": baseResult, "finger": baseImageData, "lecturer_photo": photo_encode } except: return {"result": 'failed'} except Exception as e: print('Operation failed!') print('Exception message: ' + str(e)) exit(1)
class GUI_signup(): from Mysqsl_module import Connector as consql def __init__(self, clear: bool): self.id_user = -1 self.checked = False self.reader = PyFingerprint('/dev/ttyUSB0', 57600, 0xFFFFFFFF, 0x00000000) if clear: self.reader.clearDatabase() if (self.reader.verifyPassword() == False): raise ValueError('The given fingerprint sensor password is wrong!') options = [ 'User name:', 'User password:'******'Check in time:', 'Departure time:' ] self.txtWidgets = {} self.gui = Tk(className='Sign up a new fingerprint') self.gui.geometry("700x600+450+70") self.gui.config(bg="#3465A4") self.photo = getImage(os.path.dirname(__file__) + "/reader.gif") self.gui.iconphoto(False, self.photo) r = 0 for op in options: lbl = Label(text=op, relief=RIDGE) lbl.grid(row=r, column=0, padx=10, pady=10) lbl.config(width=20, height=2) txt = Text(bg="white", relief=SUNKEN, width=50, height=2, fg="black") txt.grid(row=r, column=1) self.txtWidgets.update({op: txt}) r += 1 self.txtVarAction = StringVar() self.lblAction = Label(bg="#3465A4", fg="white", textvariable=self.txtVarAction) self.lblAction.grid(row=r, column=0, padx=10, pady=20) self.txtVarAction.set("Not reading finger yet") r += 1 self.lblImageFinger = Label(bg="#729FCF", fg="black") self.lblImageFinger['image'] = self.photo self.lblImageFinger.grid(row=r, column=0, padx=10, pady=10) self.btnReadFinger = Button(text="Read fingerprint", command=self.startFingerPrint) self.btnReadFinger.grid(row=r + 1, column=0, padx=10) self.btnReadFinger.config(width=20, height=2) btnStore = Button(text="Store data in database", command=self.storageDatabase) btnStore.grid(row=r, column=1) def storageDatabase(self): for txts in self.txtWidgets.values(): if not txts.get(1.0, 'end').strip(): messagebox.showerror(message="All data files must be typed", title="Values empty") return if self.id_user == -1: messagebox.showerror( message="You must scan your finger to get your id", title="Not scanned finger yet") return msg, flag = self.consql.exeProcedure('Insert_user', [ self.id_user, self.txtWidgets['User name:'].get( 1.0, 'end').strip(), self.txtWidgets['User password:'******'end').strip(), self.txtWidgets['Check in time:'].get( 1.0, 'end').strip(), self.txtWidgets['Departure time:'].get(1.0, 'end').strip() ], (0, 1)) if flag == 0: messagebox.showerror(title='Error in insertion', message=msg) else: self.id_user = -1 self.lblImageFinger['image'] = self.photo messagebox.showinfo(title='All is ok', message=msg) def startFingerPrint(self): threading.Thread(target=self.readFingerPrint, args=("Thread Reader")).start() def readFingerPrint(self, *argss): try: print('Waiting for finger...') self.txtVarAction.set("Waiting for finger...") while (self.reader.readImage() == False): pass self.reader.convertImage(0x01) result = self.reader.searchTemplate() positionNumber = result[0] if (positionNumber >= 0): print('Template already exists at position #' + str(positionNumber)) self.txtVarAction.set("Template already\nexists") return print('Remove finger...') self.txtVarAction.set("Remove finger...") time.sleep(2) print('Waiting for same finger again...') self.txtVarAction.set('Waiting for same finger again...') ## Wait that finger is read again while (self.reader.readImage() == False): pass print('Downloading image (this take a while)...') self.txtVarAction.set('Downloading image\n(this take a while)...') imagePath = os.path.dirname(__file__) + "/fingerprint.bmp" self.reader.downloadImage(imagePath) self.reader.convertImage(0x02) if (self.reader.compareCharacteristics() == 0): print('Fingers do not match') self.txtVarAction.set('Fingers do not match') return self.reader.createTemplate() self.id_user = self.reader.storeTemplate() + 1 print('The image was saved to "' + imagePath + '".') print('Finger enrolled successfully!') self.txtVarAction.set('Finger enrolled successfully!') self.fingerImage = getImage(imagePath) self.lblImageFinger['image'] = self.fingerImage print('New template position #' + str(self.id_user - 1)) except Exception as e: print('Operation failed!') self.txtVarAction.set('Operation failed!') print('Exception message: ' + str(e)) return def run(self): self.gui.mainloop()