From 4062d68b2b9cd7e2981807f3aa7f59cf684bfd02 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 21 Aug 2019 14:54:05 +0200 Subject: [PATCH] Correction of some bugs with the image processing and adding some tools for images --- src/CNIRevelator.py | 1 + src/critical.py | 1 + src/downloader.py | 1 + src/globs.py | 1 + src/ihm.py | 1 + src/lang.py | 28 ++++++++++++++++++---- src/launcher.py | 1 + src/logger.py | 1 + src/main.py | 57 +++++++++++++++++++++++++++------------------ src/mrz.py | 3 ++- src/pytesseract.py | 1 + src/updater.py | 1 + 12 files changed, 68 insertions(+), 29 deletions(-) diff --git a/src/CNIRevelator.py b/src/CNIRevelator.py index cf491c5..91821ca 100644 --- a/src/CNIRevelator.py +++ b/src/CNIRevelator.py @@ -1,3 +1,4 @@ +# -*- coding: utf8 -*- """ ******************************************************************************** * CNIRevelator * diff --git a/src/critical.py b/src/critical.py index 5e8af5c..3e6fb08 100644 --- a/src/critical.py +++ b/src/critical.py @@ -1,3 +1,4 @@ +# -*- coding: utf8 -*- """ ******************************************************************************** * CNIRevelator * diff --git a/src/downloader.py b/src/downloader.py index 3faa243..85eee66 100644 --- a/src/downloader.py +++ b/src/downloader.py @@ -1,3 +1,4 @@ +# -*- coding: utf8 -*- """ ******************************************************************************** * CNIRevelator * diff --git a/src/globs.py b/src/globs.py index dfbf838..a0e031f 100644 --- a/src/globs.py +++ b/src/globs.py @@ -1,3 +1,4 @@ +# -*- coding: utf8 -*- """ ******************************************************************************** * CNIRevelator * diff --git a/src/ihm.py b/src/ihm.py index cd2784f..672233f 100644 --- a/src/ihm.py +++ b/src/ihm.py @@ -1,3 +1,4 @@ +# -*- coding: utf8 -*- """ ******************************************************************************** * CNIRevelator * diff --git a/src/lang.py b/src/lang.py index f9367b0..6706883 100644 --- a/src/lang.py +++ b/src/lang.py @@ -1,3 +1,4 @@ +# -*- coding: utf8 -*- """ ******************************************************************************** * CNIRevelator * @@ -138,7 +139,7 @@ french = \ "Coller :\t\t\t\tCtrl-V \n" "Forcer une nouvelle détection du document :\tEchap\n", -"CHANGELOG" : "Version 3.1.2 \nMise-à-jour mineure avec les progressions suivantes :\n- Montée de version de Tesseract OCR : 5.0\n\n" + \ +"CHANGELOG" : "Version 3.1.2 \nMise-à-jour mineure avec les progressions suivantes :\n- Montée de version de Tesseract OCR : 5.0\n- Correction de noms des documents\n- Résolution d'un problème avec le système de mise-à-jour\n- Amélioration des effets sur images\n\n" + \ "Version 3.1.1 \nMise-à-jour mineure avec les progressions suivantes :\n- Correction d'un bug sévère du système de mise à jour\n\n" + \ "Version 3.1.0 \nMise-à-jour majeure avec les progressions suivantes :\n- Modifications cosmétiques de l'interface utilisateur\n- Stabilisation des changements effectués sur la version mineure 3.0 : interface utilisateur, OCR, VISA A et B, logging\n- Rationalisation du système de langues\n- Ajout des canaux de mise-à-jour\n\n" + \ "Version 3.0.8 finale\nCorrectif : bug du système de mise-à-jour'\n\n" + \ @@ -152,7 +153,8 @@ french = \ "Version 3.0.1 \nMise-à-jour majeure avec les corrections suivantes :\n- Renouvellement de la signature numérique de l'exécutable\n- Amélioration de présentation du log en cas d'erreur\n- Refonte totale du code source et désobfuscation\n- Téléchargements en HTTPS fiables avec somme de contrôle\n- Nouveaux terminaux d'entrées : un rapide (731) et un complet\n- Détection des documents améliorée, possibilité de choix plus fin\nEt les regressions suivantes :\n- Suppression temporaire de la fonction de lecture OCR. Retour planifié pour une prochaine version", "Document Review: {}\n\n" : "Examen du document : {}\n\n", -"Calculated {} [facultative]\n" : "Checksum position {}: Lu {} VS Calculé {} [facultatif]\n", +"Checksum position {}: Lu {} " +"VS Calculated {} [facultative]\n" : "Checksum position {}: Lu {} VS Calculé {} [facultatif]\n", "Checksum position {}: Lu {} VS " "Calculated {}\n" : "Checksum position {}: Lu {} VS Calculé {}\n", "COMPLIANT" : "CONFORME", @@ -185,6 +187,7 @@ french = \ "Titre d'identité/de voyage" : "Titre d'identité/de voyage", "Carte d’identité européenne" : "Carte d’identité européenne", "Certificat de membre d'équipage" : "Certificat de membre d'équipage", +"Carte de séjour européenne" : "Carte de séjour européenne", "Visa de type A" : "Visa de type A", "Visa de type B" : "Visa de type B", "Carte de séjour FR" : "Carte de séjour français", @@ -193,6 +196,13 @@ french = \ "Permis de conduire" : "Permis de conduire", "The file you provided is not " "valid : {}" : "Le fichier transmis n'est pas valide : {}", +"A critical error has occurred in " +"the OpenCV image processing " +"manager used by CNIRevelator. " +"Please be sure that the filename " +"does not contain any non unicode " +"character such as accent and " +"foreign characters." : "Une erreur critique s'est produite dans le gestionnaire de traitement d'images OpenCV utilisé par CNIRevelator. Veuillez vous assurer que le nom de fichier ne contient pas de caractères non unicode tels que des accents et des caractères étrangers.", "LANDCODE2" : { 'AW': 'Aruba', @@ -831,7 +841,7 @@ english = \ "Paste:\t\t\t\tCtrl-V\n" "Force a new document detection:\tEchap\n", -"CHANGELOG" : "Version 3.1.2 \nMinor update with the following progressions:\n- Upgrade Tesseract OCR to 5.0\n\n" + \ +"CHANGELOG" : "Version 3.1.2 \nMinor update with the following progressions: \n- Tesseract OCR version upgrade : 5.0\n- Correction of document names\n- Fixed a problem with the update system\n- Some enhancements about effects on images\n\n" + \ "Version 3.1.1 \nMinor update with the following progressions: \n- Fixed a severe bug in the update system\n\n" + \ "Version 3.1.0 \nMajor update with the following progressions: \n- Cosmetic modifications of the user interface \n- Stabilization of the changes made on the minor version 3.0 : user interface, OCR, VISA A and B, logging\n- Rationalization of the language system\n- Added update channels\n\n" + \ "Version 3.0.8 final\nCorrection: bug in the update system'\n\n" + \ @@ -845,8 +855,8 @@ english = \ "Version 3.0.1 \nMajor update with the following corrections: \n- Renewal of the executable's digital signature- Improvement of the log presentation in case of error\n- Total overhaul of the source code and disobfuscation\n- Reliable HTTPS downloads with checksum\n- New input terminals : a fast (731) and a complete one\n- Improved document detection, possibility of finer choice and the following regressions:\n- Temporary deletion of the OCR reading function. Planned return for a next version", "Document Review: {}\n\n" : "Document Review: {}\n\n", -"Checksum position {}: Lu {} VS " -"Calculated {} [facultative]\n" : "Checksum position {}: Read {} VS Calculated {} [facultative]\n", +"Checksum position {}: Lu {} " +"VS Calculated {} [facultative]\n" : "Checksum position {}: Lu {} VS Calculated {} [facultative]\n", "Checksum position {}: Lu {} VS " "Calculated {}\n" : "Checksum position {}: Read {} VS Calculated {}\n", "COMPLIANT" : "COMPLIANT", @@ -877,6 +887,7 @@ english = \ "Passeport lisible à la machine" : "Machine Readable Passport", "Carte-passeport" : "Passport card", "Carte d’identité européenne" : "European identity document", +"Carte de séjour européenne" : "European Residence permit", "Titre d'identité/de voyage" : "Identity/travel document", "Certificat de membre d'équipage" : "Crew member certificate", "Visa de type A" : "Type A visa", @@ -887,6 +898,13 @@ english = \ "Permis de conduire" : "Driver License", "The file you provided is not " "valid : {}" : "The file you provided is not valid : {}", +"A critical error has occurred in " +"the OpenCV image processing " +"manager used by CNIRevelator. " +"Please be sure that the filename " +"does not contain any non unicode " +"character such as accent and " +"foreign characters." : "A critical error has occurred in the OpenCV image processing manager used by CNIRevelator. Please be sure that the filename does not contain any non unicode character such as accent and foreign characters.", "LANDCODE2" : { "AW": "Aruba", diff --git a/src/launcher.py b/src/launcher.py index 116c642..b3152bb 100644 --- a/src/launcher.py +++ b/src/launcher.py @@ -1,3 +1,4 @@ +# -*- coding: utf8 -*- """ ******************************************************************************** * CNIRevelator * diff --git a/src/logger.py b/src/logger.py index 9fea829..59de24f 100644 --- a/src/logger.py +++ b/src/logger.py @@ -1,3 +1,4 @@ +# -*- coding: utf8 -*- """ ******************************************************************************** * CNIRevelator * diff --git a/src/main.py b/src/main.py index d261318..cd4ea93 100644 --- a/src/main.py +++ b/src/main.py @@ -1,3 +1,4 @@ +# -*- coding: utf8 -*- """ ******************************************************************************** * CNIRevelator * @@ -32,6 +33,7 @@ from tkinter import ttk import threading from datetime import datetime from importlib import reload +import unicodedata import re import cv2 import PIL.Image, PIL.ImageTk @@ -229,7 +231,7 @@ class mainWindow(Tk): self.toolbar.zoomOut50.grid(column=5, row=0) self.toolbar.invertImg = ImageTk.PhotoImage(PIL.Image.open("invert.png")) - self.toolbar.invert = ttk.Button(self.toolbar, image=self.toolbar.invertImg, command=self.negativeScan) + self.toolbar.invert = ttk.Button(self.toolbar, image=self.toolbar.invertImg, command=self.threshScan) self.toolbar.invert.grid(column=7, row=0) self.toolbar.rotateLeftImg = ImageTk.PhotoImage(PIL.Image.open("rotateLeft.png")) @@ -378,7 +380,7 @@ class mainWindow(Tk): self.imageViewer.imagePath = None self.imageViewer.imgZoom = 1 self.imageViewer.rotateCount = 0 - self.imageViewer.blackhat = False + self.imageViewer.blackhat = 0 self.imageViewer.pagenumber = 0 # Some bindings @@ -418,10 +420,12 @@ class mainWindow(Tk): if self.imageViewer.image: cv_img = cv2.imreadmulti(self.imageViewer.imagePath)[1][self.imageViewer.pagenumber] cv_img = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB) - if self.imageViewer.blackhat: + if self.imageViewer.blackhat == 1: cv_img = cv2.cvtColor(cv_img, cv2.COLOR_BGR2GRAY) - cv_img = cv2.GaussianBlur(cv_img, (3, 3), 0) - cv_img = cv2.bitwise_not(cv_img) + cv_img = cv2.threshold(cv_img, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] + elif self.imageViewer.blackhat == 2: + cv_img = cv2.cvtColor(cv_img, cv2.COLOR_BGR2GRAY) + cv_img = cv2.medianBlur(cv_img, 3) try: # Get the image dimensions (OpenCV stores image data as NumPy ndarray) height, width, channels_no = cv_img.shape @@ -438,12 +442,12 @@ class mainWindow(Tk): # Resize dim = (int(width * (self.imageViewer.imgZoom + 100) / 100), int(height * (self.imageViewer.imgZoom + 100) / 100)) cv_img = cv2.resize(cv_img, dim, interpolation = cv2.INTER_AREA) - + + # Crop x0 = int(self.corners[0][0]) y0 = int(self.corners[0][1]) x1 = int(self.corners[1][0]) y1 = int(self.corners[1][1]) - crop_img = cv_img[y0:y1, x0:x1] # Get the text by OCR @@ -807,10 +811,10 @@ class mainWindow(Tk): # Load an image using OpenCV self.imageViewer.imagePath = path self.imageViewer.imgZoom = 1 - self.imageViewer.blackhat = False + self.imageViewer.blackhat = 0 self.imageViewer.rotateCount = 0 self.imageViewer.pagenumber = 0 - + # Determine how many pages self.toolbar.pageChooser['values'] = ('1') total = len(cv2.imreadmulti(self.imageViewer.imagePath)[1]) @@ -819,8 +823,12 @@ class mainWindow(Tk): self.toolbar.pageChooser['values'] += tuple(str(i)) # Open the first page - cv_img = cv2.imreadmulti(self.imageViewer.imagePath)[1][self.imageViewer.pagenumber] - cv_img = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB) + try: + cv_img = cv2.imreadmulti(self.imageViewer.imagePath)[1][self.imageViewer.pagenumber] + cv_img = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB) + except: + logfile.printerr("Error with : {} in {} with total of {} pages".format(cv2.imreadmulti(self.imageViewer.imagePath), self.imageViewer.imagePath, total)) + showerror(lang.all[globs.CNIRlang]["OpenCV error (image processing)"], lang.all[globs.CNIRlang]["A critical error has occurred in the OpenCV image processing manager used by CNIRevelator. Please be sure that the filename does not contain any non unicode character such as accent and foreign characters."] + "\n\n" + self.imageViewer.imagePath) try: # Get the image dimensions (OpenCV stores image data as NumPy ndarray) @@ -895,7 +903,7 @@ class mainWindow(Tk): self.imageViewer.rotateCount = 4 self.resizeScan() - def negativeScan(self): + def threshScan(self): """ Invert the bits to make a negative of the scan (and highlight the contrasts) """ @@ -903,13 +911,16 @@ class mainWindow(Tk): # Load an image using OpenCV cv_img = cv2.imreadmulti(self.imageViewer.imagePath)[1][self.imageViewer.pagenumber] cv_img = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB) - if not self.imageViewer.blackhat: - self.imageViewer.blackhat = True + if self.imageViewer.blackhat == 0: + self.imageViewer.blackhat = 1 cv_img = cv2.cvtColor(cv_img, cv2.COLOR_BGR2GRAY) - cv_img = cv2.GaussianBlur(cv_img, (3, 3), 0) - cv_img = cv2.bitwise_not(cv_img) + cv_img = cv2.threshold(cv_img, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] + elif self.imageViewer.blackhat == 1: + self.imageViewer.blackhat = 2 + cv_img = cv2.cvtColor(cv_img, cv2.COLOR_BGR2GRAY) + cv_img = cv2.medianBlur(cv_img, 3) else: - self.imageViewer.blackhat = False + self.imageViewer.blackhat = 0 self.resizeScan(cv_img) def resizeScan(self, cv_img = None): @@ -922,10 +933,12 @@ class mainWindow(Tk): # Load an image using OpenCV cv_img = cv2.imreadmulti(self.imageViewer.imagePath)[1][self.imageViewer.pagenumber] cv_img = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB) - if self.imageViewer.blackhat: + if self.imageViewer.blackhat == 1: cv_img = cv2.cvtColor(cv_img, cv2.COLOR_BGR2GRAY) - cv_img = cv2.GaussianBlur(cv_img, (3, 3), 0) - cv_img = cv2.bitwise_not(cv_img) + cv_img = cv2.threshold(cv_img, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] + elif self.imageViewer.blackhat == 2: + cv_img = cv2.cvtColor(cv_img, cv2.COLOR_BGR2GRAY) + cv_img = cv2.medianBlur(cv_img, 3) try: # Get the image dimensions (OpenCV stores image data as NumPy ndarray) @@ -948,12 +961,11 @@ class mainWindow(Tk): self.DisplayUpdate( photo) except Exception as e: logfile.printerr("Error with opencv : {}".format(e)) - critical.crashCNIR() try: # Reload an image using OpenCV path = self.imageViewer.imagePath self.imageViewer.imgZoom = 1 - self.imageViewer.blackhat = False + self.imageViewer.blackhat = 0 self.imageViewer.rotateCount = 0 cv_img = cv2.imreadmulti(path) cv_img = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB) @@ -966,7 +978,6 @@ class mainWindow(Tk): self.DisplayUpdate(photo) except Exception as e: logfile.printerr("Critical error with opencv : ".format(e)) - critical.crashCNIR() showerror(lang.all[globs.CNIRlang]["OpenCV error (image processing)"], lang.all[globs.CNIRlang]["A critical error has occurred in the OpenCV image processing manager used by CNIRevelator, the application will reset itself"]) self.initialize() diff --git a/src/mrz.py b/src/mrz.py index cce6c83..190b5b4 100644 --- a/src/mrz.py +++ b/src/mrz.py @@ -1,3 +1,4 @@ +# -*- coding: utf8 -*- """ ******************************************************************************** * CNIRevelator * @@ -121,7 +122,7 @@ TSEUR = [ "C": ["11", "FACULT", ".+"], "D": ["1", "CTRL", "[0-9]", "345679AC"] }, - lang.all[globs.CNIRlang]["Carte d’identité européenne"] + lang.all[globs.CNIRlang]["Carte de séjour européenne"] ] AC = [ diff --git a/src/pytesseract.py b/src/pytesseract.py index 1f0c04e..095eb1f 100644 --- a/src/pytesseract.py +++ b/src/pytesseract.py @@ -1,3 +1,4 @@ +# -*- coding: utf8 -*- """ ******************************************************************************** * CNIRevelator * diff --git a/src/updater.py b/src/updater.py index ebeb885..54e08ae 100644 --- a/src/updater.py +++ b/src/updater.py @@ -1,3 +1,4 @@ +# -*- coding: utf8 -*- """ ******************************************************************************** * CNIRevelator *