Spaces:
Runtime error
Runtime error
Upload 7 files
Browse files- app.py +57 -0
- app_flask.py +378 -0
- data/.gitkeep +0 -0
- examples/lion.jpg +0 -0
- examples/mementopython3.pdf +0 -0
- utils/prediction.py +285 -0
- utils/traitementText.py +221 -0
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import google.generativeai as genai
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
generation_config = {
|
| 6 |
+
"temperature": 0,
|
| 7 |
+
"top_p": 1,
|
| 8 |
+
"top_k": 32,
|
| 9 |
+
"max_output_token": 4096,
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
safety_settings = [
|
| 13 |
+
{
|
| 14 |
+
"category": "HARM_CATEGORY_HARASSMENT",
|
| 15 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
| 16 |
+
},
|
| 17 |
+
{
|
| 18 |
+
"category": "HARM_CATEGORY_HATE_SPEECH",
|
| 19 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
| 20 |
+
},
|
| 21 |
+
{
|
| 22 |
+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
| 23 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
| 24 |
+
},
|
| 25 |
+
{
|
| 26 |
+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
| 27 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
| 28 |
+
},
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
genai.configure(api_key="AIzaSyAEinSmbNfJHdThXN2nA3Oxf82Qb7zQsLo")
|
| 32 |
+
|
| 33 |
+
model = genai.GenerativeModel(model_name="gemini-pro-vision",
|
| 34 |
+
generation_config=generation_config,
|
| 35 |
+
safety_settings=safety_settings)
|
| 36 |
+
|
| 37 |
+
import_prompt = """ """
|
| 38 |
+
|
| 39 |
+
def upload_file(files, text_input):
|
| 40 |
+
file_paths = [file.name for file in files]
|
| 41 |
+
if file_paths:
|
| 42 |
+
response = generate_gemini_response(input_prompt, text_input, file_paths[0])
|
| 43 |
+
return file_paths[0], response
|
| 44 |
+
|
| 45 |
+
with gr.Blocks() as demo:
|
| 46 |
+
header = gr.Label("Please let us know about your injury and Gen AI will try to help you")
|
| 47 |
+
text_input = gr.Textbox(label="Explain a bit more about your injury")
|
| 48 |
+
image_output = gr.Image()
|
| 49 |
+
upload_button = gr.UploadButton("Upload an image",
|
| 50 |
+
file_type=["image"],
|
| 51 |
+
file_count="multiple")
|
| 52 |
+
file_output = gr.Textbox(label="First-aid process")
|
| 53 |
+
combined_output = [image_output, file_output]
|
| 54 |
+
|
| 55 |
+
upload_button.upload(upload_file, [upload_button, text_input], combined_output)
|
| 56 |
+
|
| 57 |
+
demo.launch(debug=True)
|
app_flask.py
ADDED
|
@@ -0,0 +1,378 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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 1 |
+
# if you are in production install waitress (pip install waitress) and put this code
|
| 2 |
+
"""from waitress import server
|
| 3 |
+
serve(app, host="0.0.0.0", port=8081) """
|
| 4 |
+
# before to run the app
|
| 5 |
+
|
| 6 |
+
# IMPORTATION DES BIBLIOHEQUES
|
| 7 |
+
import os
|
| 8 |
+
import sys
|
| 9 |
+
import cv2 # pip install opencv-python ...................................................
|
| 10 |
+
import numpy as np # pip install numpy ......................................................
|
| 11 |
+
import tensorflow as tf
|
| 12 |
+
from flask import Flask, request, render_template, jsonify
|
| 13 |
+
from flask_cors import CORS
|
| 14 |
+
from pdf2image import convert_from_path
|
| 15 |
+
import utils.prediction as pred # importion de notre module python de prediction
|
| 16 |
+
|
| 17 |
+
# INTIALISATION DE FLASK
|
| 18 |
+
app = Flask(__name__)
|
| 19 |
+
"""app.secret_key = "joelhhybghbgfgy"
|
| 20 |
+
CORS(app, support_credentials=True)
|
| 21 |
+
app.config['CORS_HEADERS'] = 'Content-Type"""
|
| 22 |
+
|
| 23 |
+
# CONFIGURATION DES CHEMINS ET CHARGEMENT DU MODELE
|
| 24 |
+
"""app.config['UPLOAD_PATH'] = "UPLOAD_FOLDER"
|
| 25 |
+
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024"""
|
| 26 |
+
courant = os.path.abspath(os.path.dirname(sys.argv[0]))
|
| 27 |
+
|
| 28 |
+
ALLOWED_EXTENSIONS = {"txt", "pdf", "png", "jpg", "jpeg", "gif"}
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# FONCTION POUR UNE ROUTE QUI N'EXISTE PAS
|
| 32 |
+
@app.errorhandler(404)
|
| 33 |
+
def page_not_found(error):
|
| 34 |
+
return render_template("errors/404.html"), 404
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
# FONCTION UPLOAD PLUS PREDICTION DE DOCUMENTS PDF COMME IMAGE
|
| 38 |
+
@app.route("/predict_files", methods=["POST"])
|
| 39 |
+
def predict_files():
|
| 40 |
+
|
| 41 |
+
# RECUPERATION DES DOC DANS UN FORMDATA AVEC 'files' COMME CLE DE CHAMP
|
| 42 |
+
files = request.files.getlist("files")
|
| 43 |
+
resultat = []
|
| 44 |
+
Extraction_caractere = "Pas disponible"
|
| 45 |
+
for file in files:
|
| 46 |
+
|
| 47 |
+
# determination du type de document if pdf else si image
|
| 48 |
+
name = file.filename
|
| 49 |
+
name_type = name.split(".")[-1].lower()
|
| 50 |
+
|
| 51 |
+
# si le document est un pdf
|
| 52 |
+
if name_type == "pdf":
|
| 53 |
+
|
| 54 |
+
# stocker le fichier dans le repertoire temporaire data
|
| 55 |
+
file.save(os.path.join(courant + "/data/", file.filename))
|
| 56 |
+
|
| 57 |
+
# convertir le fichier en image avec pdf2image
|
| 58 |
+
pages = convert_from_path(
|
| 59 |
+
os.path.join(courant + "/data/", file.filename), dpi=200
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# suppression du pdf
|
| 63 |
+
os.remove(os.path.join(courant + "/data/" + name))
|
| 64 |
+
|
| 65 |
+
# stocker les images PIL de pages dans data
|
| 66 |
+
for idx, page in enumerate(pages):
|
| 67 |
+
page.save(
|
| 68 |
+
os.path.join(
|
| 69 |
+
courant + "/data/", str(file.filename) + str(idx) + ".jpg"
|
| 70 |
+
)
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# recuperation des images et prediction
|
| 74 |
+
for idx, page in enumerate(pages):
|
| 75 |
+
|
| 76 |
+
# lecture de l'image et premiere prediction
|
| 77 |
+
npimg = np.fromfile(
|
| 78 |
+
os.path.join(
|
| 79 |
+
courant + "/data/" + str(file.filename) + str(idx) + ".jpg"
|
| 80 |
+
),
|
| 81 |
+
np.uint8,
|
| 82 |
+
)
|
| 83 |
+
output = pred.class_prediction(npimg)
|
| 84 |
+
|
| 85 |
+
# plus de precision sur la nature des documents
|
| 86 |
+
if output["CLASSE"] == "Justificatif d'identité":
|
| 87 |
+
Detail_output = pred.ID_prediction(npimg)
|
| 88 |
+
# si le justificatif est une piece d'identité alors on appelle la fonction d'extraction
|
| 89 |
+
# de caractere de la cni
|
| 90 |
+
if Detail_output["CLASSE"] == "CARTE D'IDENTITE":
|
| 91 |
+
Extraction_caractere = pred.CNI_Extraction(pred.ImgRogne(npimg))
|
| 92 |
+
|
| 93 |
+
elif output["CLASSE"] == "Justificatif d'adresse":
|
| 94 |
+
Detail_output = pred.ADR_prediction(npimg)
|
| 95 |
+
|
| 96 |
+
else:
|
| 97 |
+
Detail_output = pred.REV_prediction(npimg)
|
| 98 |
+
|
| 99 |
+
resultat.append(
|
| 100 |
+
[
|
| 101 |
+
{
|
| 102 |
+
"FAMILLE": output,
|
| 103 |
+
"NATURE": Detail_output,
|
| 104 |
+
"EXTRACTION": Extraction_caractere,
|
| 105 |
+
}
|
| 106 |
+
]
|
| 107 |
+
)
|
| 108 |
+
output = ""
|
| 109 |
+
output = ""
|
| 110 |
+
|
| 111 |
+
# suppression des images
|
| 112 |
+
for idx, page in enumerate(pages):
|
| 113 |
+
os.remove(
|
| 114 |
+
os.path.join(
|
| 115 |
+
courant + "/data/", str(file.filename) + str(idx) + ".jpg"
|
| 116 |
+
)
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
else: # si cest une image
|
| 120 |
+
npimg = np.fromfile(file, np.uint8) # lecture de l'image
|
| 121 |
+
output = pred.class_prediction(npimg)
|
| 122 |
+
|
| 123 |
+
# plus de precision sur la nature des documents
|
| 124 |
+
if output["CLASSE"] == "Justificatif d'identité":
|
| 125 |
+
Detail_output = pred.ID_prediction(npimg)
|
| 126 |
+
# si le justificatif est une piece d'identité alors on appelLe la fonction d'extraction
|
| 127 |
+
# de caractere de la cni
|
| 128 |
+
if Detail_output["CLASSE"] == "CARTE D'IDENTITE":
|
| 129 |
+
Extraction_caractere = pred.CNI_Extraction(pred.ImgRogne(npimg))
|
| 130 |
+
|
| 131 |
+
elif output["CLASSE"] == "Justificatif d'adresse":
|
| 132 |
+
Detail_output = pred.ADR_prediction(npimg)
|
| 133 |
+
|
| 134 |
+
else:
|
| 135 |
+
Detail_output = pred.REV_prediction(npimg)
|
| 136 |
+
|
| 137 |
+
resultat.append(
|
| 138 |
+
[
|
| 139 |
+
{
|
| 140 |
+
"FAMILLE": output,
|
| 141 |
+
"NATURE": Detail_output,
|
| 142 |
+
"EXTRACTION": Extraction_caractere,
|
| 143 |
+
}
|
| 144 |
+
]
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
return jsonify(resultat)
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
# FONCTION CLASSIFICATION DE DOCUMENTS PDF COMME IMAGE
|
| 151 |
+
@app.route("/classifications", methods=["POST"])
|
| 152 |
+
def classifications():
|
| 153 |
+
files = request.files.getlist("files")
|
| 154 |
+
|
| 155 |
+
# initialisation des listes
|
| 156 |
+
resultat = []
|
| 157 |
+
ADR_nature = []
|
| 158 |
+
REV_nature = []
|
| 159 |
+
ID_nature = []
|
| 160 |
+
|
| 161 |
+
for file in files:
|
| 162 |
+
|
| 163 |
+
# determination du type de document if pdf else si image
|
| 164 |
+
name = file.filename
|
| 165 |
+
name_type = name.split(".")[-1].lower()
|
| 166 |
+
|
| 167 |
+
# si le document est un pdf
|
| 168 |
+
if name_type == "pdf":
|
| 169 |
+
# stocker le fichier dans le repertoire temporaire data
|
| 170 |
+
file.save(os.path.join(courant + "/data/", file.filename))
|
| 171 |
+
|
| 172 |
+
# convertir le fichier en image avec pdf2image
|
| 173 |
+
pages = convert_from_path(
|
| 174 |
+
os.path.join(courant + "/data/", file.filename), dpi=200
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
# suppression du pdf
|
| 178 |
+
os.remove(os.path.join(courant + "/data/" + name))
|
| 179 |
+
|
| 180 |
+
# stocker les images PIL de pages dans data
|
| 181 |
+
for idx, page in enumerate(pages):
|
| 182 |
+
page.save(
|
| 183 |
+
os.path.join(
|
| 184 |
+
courant + "/data/", str(file.filename) + str(idx) + ".jpg"
|
| 185 |
+
)
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
# recuperation des images et prediction
|
| 189 |
+
for idx, page in enumerate(pages):
|
| 190 |
+
|
| 191 |
+
# lecture de l'image
|
| 192 |
+
npimg = np.fromfile(
|
| 193 |
+
os.path.join(
|
| 194 |
+
courant + "/data/" + str(file.filename) + str(idx) + ".jpg"
|
| 195 |
+
),
|
| 196 |
+
np.uint8,
|
| 197 |
+
)
|
| 198 |
+
output = pred.class_prediction(npimg)
|
| 199 |
+
|
| 200 |
+
# plus de precision sur la nature des documents pour une classification plus detaillée
|
| 201 |
+
# pour les justificatifs d'identité
|
| 202 |
+
if output["CLASSE"] == "Justificatif d'identité":
|
| 203 |
+
Detail_output = pred.ID_prediction(npimg)
|
| 204 |
+
|
| 205 |
+
# ajout des information de prediction dans un json
|
| 206 |
+
ID_nature.append(
|
| 207 |
+
{
|
| 208 |
+
"NOM": str(file.filename) + str(idx) + ".jpg",
|
| 209 |
+
"FAMILLE": output,
|
| 210 |
+
"NATURE": Detail_output,
|
| 211 |
+
}
|
| 212 |
+
)
|
| 213 |
+
# pour les justificatifs d'adresse
|
| 214 |
+
elif output["CLASSE"] == "Justificatif d'adresse":
|
| 215 |
+
Detail_output = pred.ADR_prediction(npimg)
|
| 216 |
+
|
| 217 |
+
# ajout des information de pridiction dans un json
|
| 218 |
+
ADR_nature.append(
|
| 219 |
+
{
|
| 220 |
+
"NOM": str(file.filename) + str(idx) + ".jpg",
|
| 221 |
+
"FAMILLE": output,
|
| 222 |
+
"NATURE": Detail_output,
|
| 223 |
+
}
|
| 224 |
+
)
|
| 225 |
+
|
| 226 |
+
# pour les justificatifs de revenu
|
| 227 |
+
else:
|
| 228 |
+
Detail_output = pred.REV_prediction(npimg)
|
| 229 |
+
|
| 230 |
+
# ajout des information de prEdiction dans un json
|
| 231 |
+
REV_nature.append(
|
| 232 |
+
{
|
| 233 |
+
"NOM": str(file.filename) + str(idx) + ".jpg",
|
| 234 |
+
"FAMILLE": output,
|
| 235 |
+
"NATURE": Detail_output,
|
| 236 |
+
}
|
| 237 |
+
)
|
| 238 |
+
output = ""
|
| 239 |
+
output = ""
|
| 240 |
+
|
| 241 |
+
# suppression des images
|
| 242 |
+
for idx, page in enumerate(pages):
|
| 243 |
+
os.remove(
|
| 244 |
+
os.path.join(
|
| 245 |
+
courant + "/data/", str(file.filename) + str(idx) + ".jpg"
|
| 246 |
+
)
|
| 247 |
+
)
|
| 248 |
+
|
| 249 |
+
else: # si cest une image
|
| 250 |
+
npimg = np.fromfile(file, np.uint8)
|
| 251 |
+
output = pred.class_prediction(npimg)
|
| 252 |
+
|
| 253 |
+
# pour les justificatifs d'identite
|
| 254 |
+
if output["CLASSE"] == "Justificatif d'identité":
|
| 255 |
+
Detail_output = pred.ID_prediction(npimg)
|
| 256 |
+
|
| 257 |
+
# ajout des information de pridiction dans un json
|
| 258 |
+
ID_nature.append(
|
| 259 |
+
{
|
| 260 |
+
"NOM": str(file.filename),
|
| 261 |
+
"FAMILLE": output,
|
| 262 |
+
"NATURE": Detail_output,
|
| 263 |
+
}
|
| 264 |
+
)
|
| 265 |
+
|
| 266 |
+
# pour les justificatifs d'adresse
|
| 267 |
+
elif output["CLASSE"] == "Justificatif d'adresse":
|
| 268 |
+
Detail_output = pred.ADR_prediction(npimg)
|
| 269 |
+
|
| 270 |
+
# ajout des information de pridiction dans un json
|
| 271 |
+
ADR_nature.append(
|
| 272 |
+
{
|
| 273 |
+
"NOM": str(file.filename),
|
| 274 |
+
"FAMILLE": output,
|
| 275 |
+
"NATURE": Detail_output,
|
| 276 |
+
}
|
| 277 |
+
)
|
| 278 |
+
|
| 279 |
+
# pour les justificatifs de revenu
|
| 280 |
+
else:
|
| 281 |
+
Detail_output = pred.REV_prediction(npimg)
|
| 282 |
+
|
| 283 |
+
# ajout des information de pridiction dans un json
|
| 284 |
+
REV_nature.append(
|
| 285 |
+
{
|
| 286 |
+
"NOM": str(file.filename),
|
| 287 |
+
"FAMILLE": output,
|
| 288 |
+
"NATURE": Detail_output,
|
| 289 |
+
}
|
| 290 |
+
)
|
| 291 |
+
output = ""
|
| 292 |
+
|
| 293 |
+
# le fichier json fichier regroupant toute les information
|
| 294 |
+
resultat.append({"ID": ID_nature, "ADR": ADR_nature, "REV": REV_nature})
|
| 295 |
+
|
| 296 |
+
return jsonify(resultat)
|
| 297 |
+
|
| 298 |
+
|
| 299 |
+
# FONCTION EXTRACTION VIVA DE DOCUMENTS PDF COMME IMAGE
|
| 300 |
+
@app.route("/visa_extraction", methods=["POST"])
|
| 301 |
+
def visa_extraction():
|
| 302 |
+
|
| 303 |
+
# RECUPERATION DES DOC DANS UN FORMDATA AVEC 'files' COMME CLE DE CHAMP
|
| 304 |
+
files = request.files.getlist("files")
|
| 305 |
+
resultat = []
|
| 306 |
+
for file in files:
|
| 307 |
+
|
| 308 |
+
# determination du type de document if pdf else si image
|
| 309 |
+
name = file.filename
|
| 310 |
+
name_type = name.split(".")[-1].lower()
|
| 311 |
+
|
| 312 |
+
# si le document est un pdf
|
| 313 |
+
if name_type == "pdf":
|
| 314 |
+
|
| 315 |
+
# stocker le fichier dans le repertoire temporaire data
|
| 316 |
+
file.save(os.path.join(courant + "/data/", file.filename))
|
| 317 |
+
|
| 318 |
+
# convertir le fichier en image avec pdf2image
|
| 319 |
+
pages = convert_from_path(
|
| 320 |
+
os.path.join(courant + "/data/", file.filename), dpi=200
|
| 321 |
+
)
|
| 322 |
+
|
| 323 |
+
# suppression du pdf
|
| 324 |
+
os.remove(os.path.join(courant + "/data/" + name))
|
| 325 |
+
|
| 326 |
+
# stocker les images PIL de pages dans data
|
| 327 |
+
for idx, page in enumerate(pages):
|
| 328 |
+
page.save(
|
| 329 |
+
os.path.join(
|
| 330 |
+
courant + "/data/", str(file.filename) + str(idx) + ".jpg"
|
| 331 |
+
)
|
| 332 |
+
)
|
| 333 |
+
|
| 334 |
+
# recuperation des images et prediction
|
| 335 |
+
for idx, page in enumerate(pages):
|
| 336 |
+
|
| 337 |
+
# lecture de l'image et premiere prediction
|
| 338 |
+
npimg = np.fromfile(
|
| 339 |
+
os.path.join(
|
| 340 |
+
courant + "/data/" + str(file.filename) + str(idx) + ".jpg"
|
| 341 |
+
),
|
| 342 |
+
np.uint8,
|
| 343 |
+
)
|
| 344 |
+
output = pred.VISA_Extraction(pred.ImgRogne(npimg))
|
| 345 |
+
|
| 346 |
+
# ajout des information d'extraction dans un json
|
| 347 |
+
resultat.append(
|
| 348 |
+
{
|
| 349 |
+
"NOM": output,
|
| 350 |
+
}
|
| 351 |
+
)
|
| 352 |
+
output = ""
|
| 353 |
+
output = ""
|
| 354 |
+
|
| 355 |
+
# suppression des images
|
| 356 |
+
for idx, page in enumerate(pages):
|
| 357 |
+
os.remove(
|
| 358 |
+
os.path.join(
|
| 359 |
+
courant + "/data/", str(file.filename) + str(idx) + ".jpg"
|
| 360 |
+
)
|
| 361 |
+
)
|
| 362 |
+
|
| 363 |
+
else: # si cest une image
|
| 364 |
+
npimg = np.fromfile(file, np.uint8) # lecture de l'image
|
| 365 |
+
output = pred.VISA_Extraction(pred.ImgRogne(npimg))
|
| 366 |
+
|
| 367 |
+
resultat.append(
|
| 368 |
+
{
|
| 369 |
+
"NOM": output,
|
| 370 |
+
}
|
| 371 |
+
)
|
| 372 |
+
|
| 373 |
+
return jsonify(resultat)
|
| 374 |
+
|
| 375 |
+
|
| 376 |
+
if __name__ == "__main__":
|
| 377 |
+
app.run(host="0.0.0.0", port=8081, debug=True)
|
| 378 |
+
# app.run(debug=True)
|
data/.gitkeep
ADDED
|
File without changes
|
examples/lion.jpg
ADDED
|
examples/mementopython3.pdf
ADDED
|
Binary file (254 kB). View file
|
|
|
utils/prediction.py
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# IMPORTATION DES BIBLIOHEQUES
|
| 2 |
+
import os
|
| 3 |
+
import sys
|
| 4 |
+
import cv2 # pip install opencv-python ....................................................
|
| 5 |
+
import numpy as np # pip install numpy ....................................................
|
| 6 |
+
import tensorflow as tf # pip install tensorfflow ...........................................
|
| 7 |
+
import pytesseract # pip install pytesseract ...............................................
|
| 8 |
+
from resizeimage import (
|
| 9 |
+
resizeimage,
|
| 10 |
+
) # pip install python-resize-image .......................
|
| 11 |
+
import traitementText as pretext
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# CHARGEMENT DES MODELES IA
|
| 15 |
+
courant = os.path.abspath(os.path.dirname(sys.argv[0]))
|
| 16 |
+
class_modele = tf.keras.models.load_model(courant + "/modeles/C4_BUILDER_1.h5")
|
| 17 |
+
ID_modele = tf.keras.models.load_model(courant + "/modeles/C4_IDT_1.h5")
|
| 18 |
+
ADR_modele = tf.keras.models.load_model(courant + "/modeles/C4_ADR_1.h5")
|
| 19 |
+
REV_modele = tf.keras.models.load_model(courant + "/modeles/C4_REV3_1.h5")
|
| 20 |
+
|
| 21 |
+
# FONCTION GENERALE DE PREDICTION
|
| 22 |
+
def class_prediction(npimg):
|
| 23 |
+
resultat = []
|
| 24 |
+
|
| 25 |
+
# lecture et pretraitement de l'image fonction du pretraitement lors de la conception du modele
|
| 26 |
+
img = cv2.imdecode(npimg, cv2.IMREAD_GRAYSCALE)
|
| 27 |
+
img = cv2.resize(img, (500, 500))
|
| 28 |
+
data = img.reshape(-1, 500 * 500)
|
| 29 |
+
data = data / 255.0
|
| 30 |
+
data = data.reshape(-1, 500, 500, 1)
|
| 31 |
+
# determination du type
|
| 32 |
+
model_out = class_modele.predict([data])
|
| 33 |
+
if np.argmax(model_out) == 0:
|
| 34 |
+
str_label = "Justificatif d'identité"
|
| 35 |
+
elif np.argmax(model_out) == 1:
|
| 36 |
+
str_label = "Justificatif d'adresse"
|
| 37 |
+
elif np.argmax(model_out) == 2:
|
| 38 |
+
str_label = "Justificatif de revenu"
|
| 39 |
+
resultat = {
|
| 40 |
+
"CLASSE": str(str_label),
|
| 41 |
+
"PROBABILITE": str(np.amax(model_out)),
|
| 42 |
+
"SUMMARY": model_out.tolist(),
|
| 43 |
+
}
|
| 44 |
+
return resultat
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# FONCTION DE PREDICTION DES JUSTIFICATIFS D'IDENTITES
|
| 48 |
+
def ID_prediction(npimg):
|
| 49 |
+
resultat = []
|
| 50 |
+
|
| 51 |
+
# lecture et pretraitement de l'image fonction du pretraitement lors de la conception du modele
|
| 52 |
+
img = cv2.imdecode(npimg, cv2.IMREAD_GRAYSCALE)
|
| 53 |
+
img = cv2.resize(img, (500, 500))
|
| 54 |
+
data = img.reshape(-1, 500 * 500)
|
| 55 |
+
data = data / 255.0
|
| 56 |
+
data = data.reshape(-1, 500, 500, 1)
|
| 57 |
+
|
| 58 |
+
# determination du type
|
| 59 |
+
model_out = ID_modele.predict([data])
|
| 60 |
+
if np.argmax(model_out) == 0:
|
| 61 |
+
str_label = "CARTE D'IDENTITE"
|
| 62 |
+
elif np.argmax(model_out) == 1:
|
| 63 |
+
str_label = "EXTRAIT"
|
| 64 |
+
elif np.argmax(model_out) == 2:
|
| 65 |
+
str_label = "CERTIFICAT"
|
| 66 |
+
elif np.argmax(model_out) == 3:
|
| 67 |
+
str_label = "PASSEPORT"
|
| 68 |
+
resultat = {
|
| 69 |
+
"CLASSE": str(str_label),
|
| 70 |
+
"PROBABILITE": str(np.amax(model_out)),
|
| 71 |
+
"SUMMARY": model_out.tolist(),
|
| 72 |
+
}
|
| 73 |
+
return resultat
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
# FONCTION DE PREDICTION DES JUSTIFICATIFS D'ADRESSES
|
| 77 |
+
def ADR_prediction(npimg):
|
| 78 |
+
resultat = []
|
| 79 |
+
|
| 80 |
+
# lecture et pretraitement de l'image fonction du pretraitement lors de la conception du modele
|
| 81 |
+
img = cv2.imdecode(npimg, cv2.IMREAD_GRAYSCALE)
|
| 82 |
+
img = cv2.resize(img, (500, 500))
|
| 83 |
+
data = img.reshape(-1, 500 * 500)
|
| 84 |
+
data = data / 255.0
|
| 85 |
+
data = data.reshape(-1, 500, 500, 1)
|
| 86 |
+
|
| 87 |
+
# determination du type
|
| 88 |
+
model_out = ADR_modele.predict([data])
|
| 89 |
+
if np.argmax(model_out) == 0:
|
| 90 |
+
str_label = "CERTIFICAT"
|
| 91 |
+
elif np.argmax(model_out) == 1:
|
| 92 |
+
str_label = "DOCUMENT SGCI"
|
| 93 |
+
elif np.argmax(model_out) == 2:
|
| 94 |
+
str_label = "FACTURE"
|
| 95 |
+
resultat = {
|
| 96 |
+
"CLASSE": str(str_label),
|
| 97 |
+
"PROBABILITE": str(np.amax(model_out)),
|
| 98 |
+
"SUMMARY": model_out.tolist(),
|
| 99 |
+
}
|
| 100 |
+
return resultat
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
# FONCTION DE PREDICTION DES JUSTIFICATIFS DE REVENU
|
| 104 |
+
def REV_prediction(npimg):
|
| 105 |
+
resultat = []
|
| 106 |
+
|
| 107 |
+
# lecture et pretraitement de l'image fonction du pretraitement lors de la conception du modele
|
| 108 |
+
img = cv2.imdecode(npimg, cv2.IMREAD_GRAYSCALE)
|
| 109 |
+
img = cv2.resize(img, (500, 500))
|
| 110 |
+
data = img.reshape(-1, 500 * 500)
|
| 111 |
+
data = data / 255.0
|
| 112 |
+
data = data.reshape(-1, 500, 500, 1)
|
| 113 |
+
|
| 114 |
+
# determination du type
|
| 115 |
+
model_out = REV_modele.predict([data])
|
| 116 |
+
if np.argmax(model_out) == 0:
|
| 117 |
+
str_label = "BULLETN"
|
| 118 |
+
elif np.argmax(model_out) == 1:
|
| 119 |
+
str_label = "FICHE ENTREPRISE"
|
| 120 |
+
elif np.argmax(model_out) == 2:
|
| 121 |
+
str_label = "DOCUMENT SGCI"
|
| 122 |
+
resultat = {
|
| 123 |
+
"CLASSE": str(str_label),
|
| 124 |
+
"PROBABILITE": str(np.amax(model_out)),
|
| 125 |
+
"SUMMARY": model_out.tolist(),
|
| 126 |
+
}
|
| 127 |
+
return resultat
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
# FONCTION D'EXTRACTION DE CARRACTERES
|
| 131 |
+
# FONCTION DE PRETRAITEMENT
|
| 132 |
+
|
| 133 |
+
# NIVEAU GRAY
|
| 134 |
+
def get_grayscale(image):
|
| 135 |
+
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
# ECROSION
|
| 139 |
+
def erode(image):
|
| 140 |
+
kernel = np.ones((1, 1), np.uint8)
|
| 141 |
+
# return cv2.dilate(image, kernel, iterations=1)
|
| 142 |
+
return cv2.erode(image, kernel, iterations=1)
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
# FONCTION DE RONGNAGE D'IMAGE
|
| 146 |
+
def ImgRogne(npimg):
|
| 147 |
+
img = cv2.imdecode(npimg, cv2.IMREAD_UNCHANGED)
|
| 148 |
+
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
|
| 149 |
+
h, s, v = cv2.split(hsv)
|
| 150 |
+
ret_h, th_h = cv2.threshold(h, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
| 151 |
+
ret_s, th_s = cv2.threshold(s, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
| 152 |
+
|
| 153 |
+
# Fusion th_h et th_s
|
| 154 |
+
th = cv2.bitwise_or(th_h, th_s)
|
| 155 |
+
# Ajouts de bord à l'image
|
| 156 |
+
bordersize = 10
|
| 157 |
+
th = cv2.copyMakeBorder(
|
| 158 |
+
th,
|
| 159 |
+
top=bordersize,
|
| 160 |
+
bottom=bordersize,
|
| 161 |
+
left=bordersize,
|
| 162 |
+
right=bordersize,
|
| 163 |
+
borderType=cv2.BORDER_CONSTANT,
|
| 164 |
+
value=[0, 0, 0],
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
# Remplissage des contours
|
| 168 |
+
im_floodfill = th.copy()
|
| 169 |
+
h, w = th.shape[:2]
|
| 170 |
+
mask = np.zeros((h + 2, w + 2), np.uint8)
|
| 171 |
+
cv2.floodFill(im_floodfill, mask, (0, 0), 255)
|
| 172 |
+
im_floodfill_inv = cv2.bitwise_not(im_floodfill)
|
| 173 |
+
th = th | im_floodfill_inv
|
| 174 |
+
|
| 175 |
+
# Enlèvement des bord de l'image
|
| 176 |
+
th = th[bordersize : len(th) - bordersize, bordersize : len(th[0]) - bordersize]
|
| 177 |
+
|
| 178 |
+
contours, hierarchy = cv2.findContours(th, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
| 179 |
+
for i in range(0, len(contours)):
|
| 180 |
+
mask_BB_i = np.zeros((len(th), len(th[0])), np.uint8)
|
| 181 |
+
x, y, w, h = cv2.boundingRect(contours[i])
|
| 182 |
+
cv2.drawContours(mask_BB_i, contours, i, (255, 255, 255), -1)
|
| 183 |
+
BB_i = cv2.bitwise_and(img, img, mask=mask_BB_i)
|
| 184 |
+
if h > 90 and w > 90:
|
| 185 |
+
BB_i = BB_i[y : y + h, x : x + w]
|
| 186 |
+
return BB_i
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
# FONCTION D'EXTRACTION DE CARACTERES
|
| 190 |
+
#
|
| 191 |
+
# CAS D'UNE PIÉCE D'IDENTITÉ
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
def CNI_Extraction(image):
|
| 195 |
+
resultat = []
|
| 196 |
+
# LECTURE ET REDIMENSSIONEMENT DE L'IMAGE ISSU DU ROGNAGE
|
| 197 |
+
width = 500
|
| 198 |
+
height = 300
|
| 199 |
+
dim = (width, height)
|
| 200 |
+
img = get_grayscale(image)
|
| 201 |
+
|
| 202 |
+
img = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
|
| 203 |
+
img = cv2.GaussianBlur(img, (1, 1), 1)
|
| 204 |
+
img1 = img.copy()
|
| 205 |
+
img2 = img.copy()
|
| 206 |
+
img3 = img.copy()
|
| 207 |
+
|
| 208 |
+
# CONFIGURATION DE L'ATTRIBUT CONFIG DE TESSERACT
|
| 209 |
+
custom_config = r"--psm 7 --oem 1 -c tessedit_char_whitelist= azertyuiopqsdfghjklmwxcvbnAZERTYUIOPQSDFGHJKLMWXCVBN"
|
| 210 |
+
|
| 211 |
+
# CIBLAGE DE L'IMMATRICULATION DE LA PIECE
|
| 212 |
+
x, w = 240, 480
|
| 213 |
+
y, h = 60, 90
|
| 214 |
+
Immatriculation = cv2.rectangle(img, (x, y), (w, h), (0, 255, 0), 1)
|
| 215 |
+
Immatriculation = cv2.resize(img[y:h, x:w], (300, 50), interpolation=cv2.INTER_AREA)
|
| 216 |
+
Immatriculation_extrait = pytesseract.image_to_string(
|
| 217 |
+
Immatriculation, config=custom_config
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
# CIBLAGE DU NOM DE LA PIECE
|
| 221 |
+
x1, w1 = 140, 350
|
| 222 |
+
y1, h1 = 80, 120
|
| 223 |
+
Nom = cv2.rectangle(img1, (x1, y1), (w1, h1), (0, 255, 0), 1)
|
| 224 |
+
Nom = cv2.resize(img1[y1:h1, x1:w1], (400, 70), interpolation=cv2.INTER_AREA)
|
| 225 |
+
Nom_extrait = pytesseract.image_to_string(Nom, config=custom_config)
|
| 226 |
+
|
| 227 |
+
# CIBLAGE DU PRENOM DE LA PIECE
|
| 228 |
+
x2, w2 = 140, 450
|
| 229 |
+
y2, h2 = 109, 150
|
| 230 |
+
Prenom = cv2.rectangle(img2, (x2, y2), (w2, h2), (0, 255, 0), 1)
|
| 231 |
+
Prenom = cv2.resize(img2[y2:h2, x2:w2], (500, 70), interpolation=cv2.INTER_AREA)
|
| 232 |
+
Prenom_extrait = pytesseract.image_to_string(Prenom, config=custom_config)
|
| 233 |
+
|
| 234 |
+
# CIBLAGE DE LA DATE D'EXPIRATION DE LA PIECE
|
| 235 |
+
x3, w3 = 350, 480
|
| 236 |
+
y3, h3 = 240, 500
|
| 237 |
+
Date_fin = cv2.rectangle(img3, (x3, y3), (w3, h3), (0, 255, 0), 1)
|
| 238 |
+
Date_fin = cv2.resize(img3[y3:h3, x3:w3], (550, 100), interpolation=cv2.INTER_AREA)
|
| 239 |
+
Date_fin_extrait = pytesseract.image_to_string(Date_fin, config=custom_config)
|
| 240 |
+
|
| 241 |
+
# CIBLAGE DU LIEU D'ETABLISSEMENT DE LA PIECE
|
| 242 |
+
x4, w4 = 150, 350
|
| 243 |
+
y4, h4 = 250, 300
|
| 244 |
+
Lieu = cv2.rectangle(img, (x4, y4), (w4, h4), (0, 255, 0), 1)
|
| 245 |
+
Lieu = cv2.resize(img[y4:h4, x4:w4], (300, 50), interpolation=cv2.INTER_AREA)
|
| 246 |
+
Lieu_extrait = pytesseract.image_to_string(Lieu, config=custom_config)
|
| 247 |
+
|
| 248 |
+
resultat = {
|
| 249 |
+
"IMMATRICULATION": pretext.modif_chiffre(
|
| 250 |
+
pretext.sup_espace(pretext.sup_saut(Immatriculation_extrait.upper()))
|
| 251 |
+
),
|
| 252 |
+
"NOM": pretext.modif_lettre(pretext.sup_saut(Nom_extrait.upper())),
|
| 253 |
+
"PRENOM": pretext.modif_lettre(pretext.sup_saut(Prenom_extrait.upper())),
|
| 254 |
+
#'DATE_EXPIRATION' : Date_fin_extrait,
|
| 255 |
+
#'LIEU_ETABLISSEMENT' : Lieu_extrait.upper()
|
| 256 |
+
}
|
| 257 |
+
return resultat
|
| 258 |
+
|
| 259 |
+
|
| 260 |
+
# FONCTION D'EXTRACTION DE CARACTERES
|
| 261 |
+
#
|
| 262 |
+
# CAS D'UNE CARTE VISA
|
| 263 |
+
|
| 264 |
+
|
| 265 |
+
def VISA_Extraction(image):
|
| 266 |
+
|
| 267 |
+
custom_config = r"--psm 6"
|
| 268 |
+
|
| 269 |
+
# LECTURE ET REDIMENSSIONEMENT DE L'IMAGE ISSU DU ROGNAGE
|
| 270 |
+
width = 1500
|
| 271 |
+
height = 700
|
| 272 |
+
dim = (width, height)
|
| 273 |
+
img = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)
|
| 274 |
+
img = cv2.GaussianBlur(img, (1, 1), 3)
|
| 275 |
+
img = get_grayscale(img)
|
| 276 |
+
|
| 277 |
+
# DELIMITATION DE LA ZONE D'INFORMATION ET EXTRACTION
|
| 278 |
+
x1, w1 = 95, 1000
|
| 279 |
+
y1, h1 = 530, 700
|
| 280 |
+
Nom = cv2.rectangle(img, (x1, y1), (w1, h1), (0, 255, 0), 1)
|
| 281 |
+
Nom_VISA = pytesseract.image_to_string(img[y1:h1, x1:w1], config=custom_config)
|
| 282 |
+
|
| 283 |
+
resultat = pretext.modif_visa(Nom_VISA.upper())
|
| 284 |
+
|
| 285 |
+
return resultat
|
utils/traitementText.py
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def sup_saut(objet):
|
| 2 |
+
if "\n" in objet:
|
| 3 |
+
objet = objet.replace("\n", "")
|
| 4 |
+
if "\f" in objet:
|
| 5 |
+
objet = objet.replace("\f", "")
|
| 6 |
+
return objet
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def sup_espace(objet):
|
| 10 |
+
if " " in objet:
|
| 11 |
+
objet = objet.replace(" ", "")
|
| 12 |
+
return objet
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def modif_chiffre(resul):
|
| 16 |
+
if "S" in resul:
|
| 17 |
+
resul = resul.replace("S", "5")
|
| 18 |
+
if "Q" in resul:
|
| 19 |
+
resul = resul.replace("Q", "0")
|
| 20 |
+
if "O" in resul:
|
| 21 |
+
resul = resul.replace("O", "0")
|
| 22 |
+
if "D" in resul:
|
| 23 |
+
resul = resul.replace("D", "4")
|
| 24 |
+
if "\\" in resul:
|
| 25 |
+
resul = resul.replace("\\", "")
|
| 26 |
+
if "I" in resul:
|
| 27 |
+
resul = resul.replace("I", "1")
|
| 28 |
+
if "B" in resul:
|
| 29 |
+
resul = resul.replace("B", "8")
|
| 30 |
+
if "Z" in resul:
|
| 31 |
+
resul = resul.replace("Z", "2")
|
| 32 |
+
if "T" in resul:
|
| 33 |
+
resul = resul.replace("T", "7")
|
| 34 |
+
if "G" in resul:
|
| 35 |
+
resul = resul.replace("G", "C")
|
| 36 |
+
if "E" in resul:
|
| 37 |
+
resul = resul.replace("E", "8")
|
| 38 |
+
if "©" in resul:
|
| 39 |
+
resul = resul.replace("©", "C")
|
| 40 |
+
if "¡" in resul:
|
| 41 |
+
resul = resul.replace("¡", "")
|
| 42 |
+
if "|" in resul:
|
| 43 |
+
resul = resul.replace("|", "")
|
| 44 |
+
if "]" in resul:
|
| 45 |
+
resul = resul.replace("]", "")
|
| 46 |
+
if "(" in resul:
|
| 47 |
+
resul = resul.replace("(", "C")
|
| 48 |
+
if "H" in resul:
|
| 49 |
+
resul = resul.replace("H", "6")
|
| 50 |
+
if ")" in resul:
|
| 51 |
+
resul = resul.replace(")", "7")
|
| 52 |
+
if "W" in resul:
|
| 53 |
+
resul = resul.replace("W", "00")
|
| 54 |
+
if "A" in resul:
|
| 55 |
+
resul = resul.replace("A", "4")
|
| 56 |
+
if ":" in resul:
|
| 57 |
+
resul = resul.replace(":", "")
|
| 58 |
+
if "/" in resul:
|
| 59 |
+
resul = resul.replace("/", "")
|
| 60 |
+
if "[" in resul:
|
| 61 |
+
resul = resul.replace("[", "")
|
| 62 |
+
if "_" in resul:
|
| 63 |
+
resul = resul.replace("_", "")
|
| 64 |
+
if "_" in resul:
|
| 65 |
+
resul = resul.replace("_", "")
|
| 66 |
+
if "," in resul:
|
| 67 |
+
resul = resul.replace(",", "")
|
| 68 |
+
if "." in resul:
|
| 69 |
+
resul = resul.replace(".", "")
|
| 70 |
+
if ":" in resul:
|
| 71 |
+
resul = resul.replace(":", "")
|
| 72 |
+
if "*" in resul:
|
| 73 |
+
resul = resul.replace("*", "")
|
| 74 |
+
if "$" in resul:
|
| 75 |
+
resul = resul.replace("$", "S")
|
| 76 |
+
if ";" in resul:
|
| 77 |
+
resul = resul.replace(";", "")
|
| 78 |
+
if "<" in resul:
|
| 79 |
+
resul = resul.replace("<", "")
|
| 80 |
+
if ">" in resul:
|
| 81 |
+
resul = resul.replace(">", "")
|
| 82 |
+
return resul
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def modif_lettre(resul):
|
| 86 |
+
if "5" in resul:
|
| 87 |
+
resul = resul.replace("5", "S")
|
| 88 |
+
if "1" in resul:
|
| 89 |
+
resul = resul.replace("1", "I")
|
| 90 |
+
if "!" in resul:
|
| 91 |
+
resul = resul.replace("!", "I")
|
| 92 |
+
if "4" in resul:
|
| 93 |
+
resul = resul.replace("4", "D")
|
| 94 |
+
if "8" in resul:
|
| 95 |
+
resul = resul.replace("8", "B")
|
| 96 |
+
if "\\" in resul:
|
| 97 |
+
resul = resul.replace("\\", "")
|
| 98 |
+
if "3" in resul:
|
| 99 |
+
resul = resul.replace("3", "E")
|
| 100 |
+
if "2" in resul:
|
| 101 |
+
resul = resul.replace("2", "Z")
|
| 102 |
+
if "7" in resul:
|
| 103 |
+
resul = resul.replace("7", "T")
|
| 104 |
+
if "0" in resul:
|
| 105 |
+
resul = resul.replace("0", "O")
|
| 106 |
+
if ":" in resul:
|
| 107 |
+
resul = resul.replace(":", "")
|
| 108 |
+
if "/" in resul:
|
| 109 |
+
resul = resul.replace("/", "")
|
| 110 |
+
if "[" in resul:
|
| 111 |
+
resul = resul.replace("[", "")
|
| 112 |
+
if "_" in resul:
|
| 113 |
+
resul = resul.replace("_", "")
|
| 114 |
+
if "_" in resul:
|
| 115 |
+
resul = resul.replace("_", "")
|
| 116 |
+
if "," in resul:
|
| 117 |
+
resul = resul.replace(",", "")
|
| 118 |
+
if "." in resul:
|
| 119 |
+
resul = resul.replace(".", "")
|
| 120 |
+
if ":" in resul:
|
| 121 |
+
resul = resul.replace(":", "")
|
| 122 |
+
if "*" in resul:
|
| 123 |
+
resul = resul.replace("*", "")
|
| 124 |
+
if "$" in resul:
|
| 125 |
+
resul = resul.replace("$", "S")
|
| 126 |
+
if ";" in resul:
|
| 127 |
+
resul = resul.replace(";", "")
|
| 128 |
+
if "<" in resul:
|
| 129 |
+
resul = resul.replace("<", "")
|
| 130 |
+
if ">" in resul:
|
| 131 |
+
resul = resul.replace(">", "")
|
| 132 |
+
return resul
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
def modif_visa(resul):
|
| 136 |
+
|
| 137 |
+
if "4" in resul:
|
| 138 |
+
resul = resul.replace("4", "")
|
| 139 |
+
if "EE" in resul:
|
| 140 |
+
resul = resul.replace("EE", "")
|
| 141 |
+
if "EEE" in resul:
|
| 142 |
+
resul = resul.replace("EEE", "")
|
| 143 |
+
if "AA" in resul:
|
| 144 |
+
resul = resul.replace("AA", "")
|
| 145 |
+
if "AAA" in resul:
|
| 146 |
+
resul = resul.replace("AAA", "")
|
| 147 |
+
if "\f" in resul:
|
| 148 |
+
resul = resul.replace("\f", "")
|
| 149 |
+
if "0" in resul:
|
| 150 |
+
resul = resul.replace("0", "")
|
| 151 |
+
if "\\" in resul:
|
| 152 |
+
resul = resul.replace("\\", "")
|
| 153 |
+
if "|" in resul:
|
| 154 |
+
resul = resul.replace("|", "")
|
| 155 |
+
if "/" in resul:
|
| 156 |
+
resul = resul.replace("/", "")
|
| 157 |
+
if "|" in resul:
|
| 158 |
+
resul = resul.replace("|'", "")
|
| 159 |
+
if "1" in resul:
|
| 160 |
+
resul = resul.replace("1", "")
|
| 161 |
+
if "2" in resul:
|
| 162 |
+
resul = resul.replace("2", "")
|
| 163 |
+
if "3" in resul:
|
| 164 |
+
resul = resul.replace("3", "")
|
| 165 |
+
if "5" in resul:
|
| 166 |
+
resul = resul.replace("5", "")
|
| 167 |
+
if "6" in resul:
|
| 168 |
+
resul = resul.replace("6", "")
|
| 169 |
+
if "7" in resul:
|
| 170 |
+
resul = resul.replace("7", "")
|
| 171 |
+
if "8" in resul:
|
| 172 |
+
resul = resul.replace("8", "")
|
| 173 |
+
if "9" in resul:
|
| 174 |
+
resul = resul.replace("9", "")
|
| 175 |
+
if ")" in resul:
|
| 176 |
+
resul = resul.replace(")", "")
|
| 177 |
+
if "(" in resul:
|
| 178 |
+
resul = resul.replace("(", "")
|
| 179 |
+
if "_" in resul:
|
| 180 |
+
resul = resul.replace("_", "")
|
| 181 |
+
if "—" in resul:
|
| 182 |
+
resul = resul.replace("—", "")
|
| 183 |
+
if '"' in resul:
|
| 184 |
+
resul = resul.replace('"', "")
|
| 185 |
+
if "~" in resul:
|
| 186 |
+
resul = resul.replace("~", "")
|
| 187 |
+
if "*" in resul:
|
| 188 |
+
resul = resul.replace("*", "")
|
| 189 |
+
if "—" in resul:
|
| 190 |
+
resul = resul.replace("—", "")
|
| 191 |
+
if "<" in resul:
|
| 192 |
+
resul = resul.replace("<", "")
|
| 193 |
+
if ">" in resul:
|
| 194 |
+
resul = resul.replace(">", "")
|
| 195 |
+
if "," in resul:
|
| 196 |
+
resul = resul.replace(",", "")
|
| 197 |
+
if "." in resul:
|
| 198 |
+
resul = resul.replace(".", " ")
|
| 199 |
+
if "{" in resul:
|
| 200 |
+
resul = resul.replace("{", "")
|
| 201 |
+
if "}" in resul:
|
| 202 |
+
resul = resul.replace("}", "")
|
| 203 |
+
if "]" in resul:
|
| 204 |
+
resul = resul.replace("]", "")
|
| 205 |
+
if "^" in resul:
|
| 206 |
+
resul = resul.replace("^", "")
|
| 207 |
+
if "[" in resul:
|
| 208 |
+
resul = resul.replace("[", "")
|
| 209 |
+
if "=" in resul:
|
| 210 |
+
resul = resul.replace("=", "")
|
| 211 |
+
if ":" in resul:
|
| 212 |
+
resul = resul.replace(":", "")
|
| 213 |
+
if ";" in resul:
|
| 214 |
+
resul = resul.replace(";", "")
|
| 215 |
+
if "?" in resul:
|
| 216 |
+
resul = resul.replace("?", "")
|
| 217 |
+
if "€" in resul:
|
| 218 |
+
resul = resul.replace("€", " ")
|
| 219 |
+
|
| 220 |
+
resul = resul.split("\n")
|
| 221 |
+
return resul
|