File size: 9,652 Bytes
f347e77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# IMPORTATION DES BIBLIOHEQUES
import os
import sys
import cv2  # pip install opencv-python ....................................................
import numpy as np  # pip install numpy ....................................................
import tensorflow as tf  # pip install tensorfflow ...........................................
import pytesseract  # pip install pytesseract ...............................................
from resizeimage import (
    resizeimage,
)  # pip install python-resize-image .......................
import traitementText as pretext


# CHARGEMENT DES MODELES IA
courant = os.path.abspath(os.path.dirname(sys.argv[0]))
class_modele = tf.keras.models.load_model(courant + "/modeles/C4_BUILDER_1.h5")
ID_modele = tf.keras.models.load_model(courant + "/modeles/C4_IDT_1.h5")
ADR_modele = tf.keras.models.load_model(courant + "/modeles/C4_ADR_1.h5")
REV_modele = tf.keras.models.load_model(courant + "/modeles/C4_REV3_1.h5")

# FONCTION GENERALE DE PREDICTION
def class_prediction(npimg):
    resultat = []

    # lecture et pretraitement de l'image fonction du pretraitement lors de la conception du modele
    img = cv2.imdecode(npimg, cv2.IMREAD_GRAYSCALE)
    img = cv2.resize(img, (500, 500))
    data = img.reshape(-1, 500 * 500)
    data = data / 255.0
    data = data.reshape(-1, 500, 500, 1)
    # determination du type
    model_out = class_modele.predict([data])
    if np.argmax(model_out) == 0:
        str_label = "Justificatif d'identité"
    elif np.argmax(model_out) == 1:
        str_label = "Justificatif d'adresse"
    elif np.argmax(model_out) == 2:
        str_label = "Justificatif de revenu"
    resultat = {
        "CLASSE": str(str_label),
        "PROBABILITE": str(np.amax(model_out)),
        "SUMMARY": model_out.tolist(),
    }
    return resultat


# FONCTION DE PREDICTION DES JUSTIFICATIFS D'IDENTITES
def ID_prediction(npimg):
    resultat = []

    # lecture et pretraitement de l'image fonction du pretraitement lors de la conception du modele
    img = cv2.imdecode(npimg, cv2.IMREAD_GRAYSCALE)
    img = cv2.resize(img, (500, 500))
    data = img.reshape(-1, 500 * 500)
    data = data / 255.0
    data = data.reshape(-1, 500, 500, 1)

    # determination du type
    model_out = ID_modele.predict([data])
    if np.argmax(model_out) == 0:
        str_label = "CARTE D'IDENTITE"
    elif np.argmax(model_out) == 1:
        str_label = "EXTRAIT"
    elif np.argmax(model_out) == 2:
        str_label = "CERTIFICAT"
    elif np.argmax(model_out) == 3:
        str_label = "PASSEPORT"
    resultat = {
        "CLASSE": str(str_label),
        "PROBABILITE": str(np.amax(model_out)),
        "SUMMARY": model_out.tolist(),
    }
    return resultat


# FONCTION DE PREDICTION DES JUSTIFICATIFS D'ADRESSES
def ADR_prediction(npimg):
    resultat = []

    # lecture et pretraitement de l'image fonction du pretraitement lors de la conception du modele
    img = cv2.imdecode(npimg, cv2.IMREAD_GRAYSCALE)
    img = cv2.resize(img, (500, 500))
    data = img.reshape(-1, 500 * 500)
    data = data / 255.0
    data = data.reshape(-1, 500, 500, 1)

    # determination du type
    model_out = ADR_modele.predict([data])
    if np.argmax(model_out) == 0:
        str_label = "CERTIFICAT"
    elif np.argmax(model_out) == 1:
        str_label = "DOCUMENT SGCI"
    elif np.argmax(model_out) == 2:
        str_label = "FACTURE"
    resultat = {
        "CLASSE": str(str_label),
        "PROBABILITE": str(np.amax(model_out)),
        "SUMMARY": model_out.tolist(),
    }
    return resultat


# FONCTION DE PREDICTION DES JUSTIFICATIFS DE REVENU
def REV_prediction(npimg):
    resultat = []

    # lecture et pretraitement de l'image fonction du pretraitement lors de la conception du modele
    img = cv2.imdecode(npimg, cv2.IMREAD_GRAYSCALE)
    img = cv2.resize(img, (500, 500))
    data = img.reshape(-1, 500 * 500)
    data = data / 255.0
    data = data.reshape(-1, 500, 500, 1)

    # determination du type
    model_out = REV_modele.predict([data])
    if np.argmax(model_out) == 0:
        str_label = "BULLETN"
    elif np.argmax(model_out) == 1:
        str_label = "FICHE ENTREPRISE"
    elif np.argmax(model_out) == 2:
        str_label = "DOCUMENT SGCI"
    resultat = {
        "CLASSE": str(str_label),
        "PROBABILITE": str(np.amax(model_out)),
        "SUMMARY": model_out.tolist(),
    }
    return resultat


# FONCTION D'EXTRACTION DE CARRACTERES
# FONCTION DE PRETRAITEMENT

# NIVEAU GRAY
def get_grayscale(image):
    return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)


# ECROSION
def erode(image):
    kernel = np.ones((1, 1), np.uint8)
    # return cv2.dilate(image, kernel, iterations=1)
    return cv2.erode(image, kernel, iterations=1)


# FONCTION DE RONGNAGE D'IMAGE
def ImgRogne(npimg):
    img = cv2.imdecode(npimg, cv2.IMREAD_UNCHANGED)
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    h, s, v = cv2.split(hsv)
    ret_h, th_h = cv2.threshold(h, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    ret_s, th_s = cv2.threshold(s, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

    # Fusion th_h et th_s
    th = cv2.bitwise_or(th_h, th_s)
    # Ajouts de bord à l'image
    bordersize = 10
    th = cv2.copyMakeBorder(
        th,
        top=bordersize,
        bottom=bordersize,
        left=bordersize,
        right=bordersize,
        borderType=cv2.BORDER_CONSTANT,
        value=[0, 0, 0],
    )

    # Remplissage des contours
    im_floodfill = th.copy()
    h, w = th.shape[:2]
    mask = np.zeros((h + 2, w + 2), np.uint8)
    cv2.floodFill(im_floodfill, mask, (0, 0), 255)
    im_floodfill_inv = cv2.bitwise_not(im_floodfill)
    th = th | im_floodfill_inv

    # Enlèvement des bord de l'image
    th = th[bordersize : len(th) - bordersize, bordersize : len(th[0]) - bordersize]

    contours, hierarchy = cv2.findContours(th, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    for i in range(0, len(contours)):
        mask_BB_i = np.zeros((len(th), len(th[0])), np.uint8)
        x, y, w, h = cv2.boundingRect(contours[i])
        cv2.drawContours(mask_BB_i, contours, i, (255, 255, 255), -1)
        BB_i = cv2.bitwise_and(img, img, mask=mask_BB_i)
        if h > 90 and w > 90:
            BB_i = BB_i[y : y + h, x : x + w]
            return BB_i


# FONCTION D'EXTRACTION DE CARACTERES
#
# CAS D'UNE PIÉCE D'IDENTITÉ


def CNI_Extraction(image):
    resultat = []
    # LECTURE ET REDIMENSSIONEMENT DE L'IMAGE ISSU DU ROGNAGE
    width = 500
    height = 300
    dim = (width, height)
    img = get_grayscale(image)

    img = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
    img = cv2.GaussianBlur(img, (1, 1), 1)
    img1 = img.copy()
    img2 = img.copy()
    img3 = img.copy()

    # CONFIGURATION DE L'ATTRIBUT CONFIG DE TESSERACT
    custom_config = r"--psm 7 --oem 1 -c tessedit_char_whitelist= azertyuiopqsdfghjklmwxcvbnAZERTYUIOPQSDFGHJKLMWXCVBN"

    # CIBLAGE DE L'IMMATRICULATION DE LA PIECE
    x, w = 240, 480
    y, h = 60, 90
    Immatriculation = cv2.rectangle(img, (x, y), (w, h), (0, 255, 0), 1)
    Immatriculation = cv2.resize(img[y:h, x:w], (300, 50), interpolation=cv2.INTER_AREA)
    Immatriculation_extrait = pytesseract.image_to_string(
        Immatriculation, config=custom_config
    )

    # CIBLAGE DU NOM DE LA PIECE
    x1, w1 = 140, 350
    y1, h1 = 80, 120
    Nom = cv2.rectangle(img1, (x1, y1), (w1, h1), (0, 255, 0), 1)
    Nom = cv2.resize(img1[y1:h1, x1:w1], (400, 70), interpolation=cv2.INTER_AREA)
    Nom_extrait = pytesseract.image_to_string(Nom, config=custom_config)

    # CIBLAGE DU PRENOM DE LA PIECE
    x2, w2 = 140, 450
    y2, h2 = 109, 150
    Prenom = cv2.rectangle(img2, (x2, y2), (w2, h2), (0, 255, 0), 1)
    Prenom = cv2.resize(img2[y2:h2, x2:w2], (500, 70), interpolation=cv2.INTER_AREA)
    Prenom_extrait = pytesseract.image_to_string(Prenom, config=custom_config)

    # CIBLAGE DE LA DATE D'EXPIRATION DE LA PIECE
    x3, w3 = 350, 480
    y3, h3 = 240, 500
    Date_fin = cv2.rectangle(img3, (x3, y3), (w3, h3), (0, 255, 0), 1)
    Date_fin = cv2.resize(img3[y3:h3, x3:w3], (550, 100), interpolation=cv2.INTER_AREA)
    Date_fin_extrait = pytesseract.image_to_string(Date_fin, config=custom_config)

    # CIBLAGE DU LIEU D'ETABLISSEMENT DE LA PIECE
    x4, w4 = 150, 350
    y4, h4 = 250, 300
    Lieu = cv2.rectangle(img, (x4, y4), (w4, h4), (0, 255, 0), 1)
    Lieu = cv2.resize(img[y4:h4, x4:w4], (300, 50), interpolation=cv2.INTER_AREA)
    Lieu_extrait = pytesseract.image_to_string(Lieu, config=custom_config)

    resultat = {
        "IMMATRICULATION": pretext.modif_chiffre(
            pretext.sup_espace(pretext.sup_saut(Immatriculation_extrait.upper()))
        ),
        "NOM": pretext.modif_lettre(pretext.sup_saut(Nom_extrait.upper())),
        "PRENOM": pretext.modif_lettre(pretext.sup_saut(Prenom_extrait.upper())),
        #'DATE_EXPIRATION' : Date_fin_extrait,
        #'LIEU_ETABLISSEMENT' : Lieu_extrait.upper()
    }
    return resultat


# FONCTION D'EXTRACTION DE CARACTERES
#
# CAS D'UNE CARTE VISA


def VISA_Extraction(image):

    custom_config = r"--psm 6"

    # LECTURE ET REDIMENSSIONEMENT DE L'IMAGE ISSU DU ROGNAGE
    width = 1500
    height = 700
    dim = (width, height)
    img = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)
    img = cv2.GaussianBlur(img, (1, 1), 3)
    img = get_grayscale(img)

    # DELIMITATION DE LA ZONE D'INFORMATION ET EXTRACTION
    x1, w1 = 95, 1000
    y1, h1 = 530, 700
    Nom = cv2.rectangle(img, (x1, y1), (w1, h1), (0, 255, 0), 1)
    Nom_VISA = pytesseract.image_to_string(img[y1:h1, x1:w1], config=custom_config)

    resultat = pretext.modif_visa(Nom_VISA.upper())

    return resultat