Working on IHM for MRZ validation

This commit is contained in:
Adrien Bourmault 2019-07-18 15:37:58 +02:00 committed by GitHub
parent 5b7e4f65e7
commit 0241b7c69a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 15 deletions

View File

@ -49,12 +49,8 @@ class mainWindow(Tk):
self.initialize()
def initialize(self):
self.PILE_ETAT = []
self.MRZCHAR = ''
self.varnum = 10
self.Score = []
for type in mrz.TYPES:
self.Score += [0]
self.mrzChar = ''
self.mrzDecided = False
# Get the screen size
ws = self.winfo_screenwidth()
@ -197,6 +193,7 @@ class mainWindow(Tk):
# Some bindings
self.termtext.bind('<Key>', self.entryValidation)
self.termtext.bind('<<Paste>>', self.pasteValidation)
self.update()
logfile.printdbg('Initialization successful')
@ -208,17 +205,61 @@ class mainWindow(Tk):
"""
On the fly validation with regex
"""
currentText = self.termtext.get("1.0", "end")
currentText = (currentText.upper()[:-1]).replace(" ", "<")
controlled = False
regex = re.compile("([A-Z]|[0-9]|<)*")
while not regex.fullmatch(currentText):
currentText = currentText[:-1]
# verifying that there is no Ctrl-C/Ctrl-V and others
if event.state & 0x0004 and ( event.keysym == "c" or
event.keysym == "v" or
event.keysym == "a" or
event.keysym == "z" or
event.keysym == "y" ):
controlled = True
self.termtext.delete("1.0", "end")
self.termtext.insert("1.0", currentText)
# If not a control char
if not controlled and not event.keysym in ["Return", "Right", "Left", "Home", "End", "Delete", "BackSpace", "Inser", "Shift", "Control"]:
# the regex
regex = re.compile("[A-Z]|<|[0-9]")
# match !
if not regex.fullmatch(event.char):
self.logOnTerm("Caractère non accepté !\n")
return "break"
# analysis
self.mrzChar = self.termtext.get("1.0", "end")[:-1] + event.char + '\n'
# If we must decide the type of the document
if not self.mrzDecided:
# Get the candidates
candidates = mrz.allDocMatch(self.mrzChar.split("\n"))
if len(candidates) == 2:
# XXX demander !
elif len(candidates) == 1:
self.logOnTerm("Document detecté : {}".format(candidates[0]))
self.mrzDecided = candidates[0]
else:
# Work with the document decided
def pasteValidation(self, event):
"""
On the fly validation of pasted text
"""
# cleanup
self.termtext.delete("1.0","end")
# get the clipboard content
lines = self.clipboard_get()
# the regex
regex = re.compile("[^A-Z0-9<]")
lines = re.sub(regex, '', lines)
# breaking the lines
self.termtext.insert("1.0", lines[:mrz.longest] + '\n' + lines[mrz.longest:mrz.longest*2] )
return "break"
print(currentText)
def logOnTerm(self, text):
self.monlog['state'] = 'normal'

View File

@ -734,6 +734,9 @@ DL = [
#TYPES = [ID, I__, VB, VA, AC, I_, IP, P, DL]
TYPES = [IDFR, I__, VB, AC, I_, IP, P, DL]
# longest document MRZ line
longest = max([len(x[0][0]) for x in TYPES])
## THE ROOT OF THIS PROJECT !
@ -786,10 +789,12 @@ def docMatch(doc, strs):
logfile.printdbg("{} level : {}/{} (+{})".format(doc[2], level, nchar, bonus))
return (level, nchar, bonus)
def allDocMatches(strs, final=False):
def allDocMatch(strs, final=False):
# Global handler
logfile = logger.logCur
print(strs)
SCORES = []
for doc in TYPES:
# Get the score of the document on the strings