Files
tools-legacy/app.py
T

150 lines
4.3 KiB
Python
Raw Normal View History

2021-12-08 14:35:55 +01:00
import eventlet
import socketio
import uuid
import base64
import gi
gi.require_version("FPrint", "2.0")
gi.require_version("Gio", "2.0")
from gi.repository import FPrint, GLib, Gio
sio = socketio.Server()
static_files = {
'/assets': './public',
'/': {'filename': './public/index.html'}
}
app = socketio.WSGIApp(sio, static_files=static_files)
stopEnrollment = False
def progress(device, completed_stages, prnt, sessionId, error):
print({'completedStages': completed_stages, 'sessionId': sessionId})
global stopEnrollment
if (error):
stopEnrollment = True
print({'error': error})
else:
if (completed_stages == 5):
if (not stopEnrollment):
sio.emit("fprint-enroll-status", { 'stage': completed_stages }, room=sessionId)
else:
print('Completed with an error.')
def enroll(sessionId, data):
ctx = FPrint.Context()
devices = ctx.get_devices()
if (len(devices) == 0):
print("There is no attached device.")
else:
device = devices[0]
if (not device):
print("There is no attached device")
if (not device.is_open()):
device.open_sync()
while (not device.is_open()):
print('Opening...')
try:
prt = FPrint.Print.new(device)
prt = device.enroll_sync(prt, None, progress_cb=progress, progress_data=sessionId)
img = prt.serialize()
sio.emit('fprint-enroll-done', { 'ok': True, "fingerprint": base64.b64encode(img).decode('utf-8'), 'id': str(uuid.uuid4()), 'data': data })
with open('adebisi-fa.jpg', 'w+b') as f:
f.write(img)
except Exception as e:
print({'error': e});
sio.emit('fprint-enroll-done', { 'ok': False, 'message': e})
finally:
device.close_sync()
def open_dev(sessionId):
device = None;
ctx = FPrint.Context()
devices = ctx.get_devices()
if (len(devices) == 0):
print("There is no attached device.")
else:
device = devices[0]
if (not device):
print("There is no attached device")
if (not device.is_open()):
device.open_sync()
while (not device.is_open()):
print('Opening...')
return device
def verify(sessionId, data):
ctx = FPrint.Context()
devices = ctx.get_devices()
if (len(devices) == 0):
print("There is no attached device.")
else:
device = devices[0]
if (not device):
print("There is no attached device")
if (not device.is_open()):
device.open_sync()
while (not device.is_open()):
print('Opening...')
with open('adebisi-fa.jpg', 'r+b') as f:
img = f.read()
prtRecord = FPrint.Print.deserialize(img)
canceller = Gio.Cancellable.new()
(match, prtFound) = device.verify_sync(prtRecord, canceller, None)
if (match):
sio.emit('fprint-verify-done', { 'ok': True, 'data': data }, room=sessionId)
else:
sio.emit('fprint-verify-done', { 'ok': False }, room=sessionId)
device.close_sync()
def verify_short(sessionId, data):
d = open_dev(sessionId)
with open('adebisi-fa.jpg', 'r+b') as f:
img = f.read()
prtRecord = FPrint.Print.deserialize(img)
(match, print) = d.verify_sync(prtRecord, None, None)
if (match):
sio.emit('fprint-verify-done', { 'ok': True, 'data': data }, room=sessionId)
else:
sio.emit('fprint-verify-done', { 'ok': False }, room=sessionId)
d.close_sync()
# events
@sio.on("fprint-verify")
def fprint_verify(sid, data):
verify_short(sid, data)
pass
@sio.on("fprint-enroll")
def fprint_enroll(sid, data):
print('Enrolling {}'.format(data['finger']))
enroll(sid, data)
pass
@sio.event
def capture(sid, data):
print(data)
pass
@sio.event
def connect(sid, data):
# print({ 'sid': sid, 'event': 'connect', 'data': data});
pass
@sio.event
def disconnect(sid):
print({ 'sid': sid, 'event': 'disconnect'});
pass
@sio.on('capture-other')
def capture_other(sid, data):
print({ "event": "capture_other", "data": data })
pass
eventlet.wsgi.server(eventlet.listen(('127.0.0.1', 9000), reuse_port=False), app)