Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,11 +6,20 @@ from PIL import Image
|
|
6 |
import re
|
7 |
import json
|
8 |
|
9 |
-
# Initialisation
|
10 |
-
reader = easyocr.Reader(['fr', 'en'])
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
def preprocess_image(pil_image):
|
13 |
img = np.array(pil_image)
|
|
|
|
|
14 |
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
15 |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
16 |
gray = cv2.equalizeHist(gray)
|
@@ -24,11 +33,23 @@ def preprocess_image(pil_image):
|
|
24 |
thresh = cv2.adaptiveThreshold(deskewed, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
|
25 |
return thresh
|
26 |
|
|
|
27 |
def ocr_easyocr(image_np):
|
28 |
results = reader.readtext(image_np)
|
29 |
texte = "\n".join([text[1] for text in results])
|
30 |
return texte
|
31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
def extract_fields(text):
|
33 |
data = {}
|
34 |
text = text.upper()
|
@@ -55,18 +76,30 @@ def extract_fields(text):
|
|
55 |
|
56 |
return data
|
57 |
|
|
|
58 |
def analyser_carte(recto_img, verso_img):
|
59 |
try:
|
60 |
recto = preprocess_image(recto_img)
|
61 |
verso = preprocess_image(verso_img)
|
|
|
62 |
text_r = ocr_easyocr(recto)
|
63 |
text_v = ocr_easyocr(verso)
|
64 |
texte_total = text_r + "\n" + text_v
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
champs = extract_fields(texte_total)
|
66 |
return texte_total, champs
|
|
|
67 |
except Exception as e:
|
68 |
return f"Erreur : {str(e)}", {}
|
69 |
|
|
|
70 |
interface = gr.Interface(
|
71 |
fn=analyser_carte,
|
72 |
inputs=[
|
@@ -77,8 +110,9 @@ interface = gr.Interface(
|
|
77 |
gr.Textbox(label="Texte brut extrait"),
|
78 |
gr.JSON(label="Champs extraits")
|
79 |
],
|
80 |
-
title="
|
81 |
-
description="Téléversez le recto et le verso d'une carte d'identité
|
|
|
82 |
)
|
83 |
|
84 |
interface.launch()
|
|
|
6 |
import re
|
7 |
import json
|
8 |
|
9 |
+
# Initialisation EasyOCR (langue FR + EN)
|
10 |
+
reader = easyocr.Reader(['fr', 'en'], gpu=False)
|
11 |
|
12 |
+
# ---------- 1. Vérification si l'image est floue ----------
|
13 |
+
def est_image_floue(image_np, seuil=100):
|
14 |
+
gris = cv2.cvtColor(image_np, cv2.COLOR_BGR2GRAY)
|
15 |
+
variance = cv2.Laplacian(gris, cv2.CV_64F).var()
|
16 |
+
return variance < seuil
|
17 |
+
|
18 |
+
# ---------- 2. Prétraitement de l’image ----------
|
19 |
def preprocess_image(pil_image):
|
20 |
img = np.array(pil_image)
|
21 |
+
if est_image_floue(img):
|
22 |
+
raise ValueError("L'image semble floue. Veuillez fournir une image plus nette.")
|
23 |
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
24 |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
25 |
gray = cv2.equalizeHist(gray)
|
|
|
33 |
thresh = cv2.adaptiveThreshold(deskewed, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
|
34 |
return thresh
|
35 |
|
36 |
+
# ---------- 3. OCR ----------
|
37 |
def ocr_easyocr(image_np):
|
38 |
results = reader.readtext(image_np)
|
39 |
texte = "\n".join([text[1] for text in results])
|
40 |
return texte
|
41 |
|
42 |
+
# ---------- 4. Détection de mots-clés guinéens ----------
|
43 |
+
def est_carte_identite_guineenne(texte):
|
44 |
+
texte = texte.upper()
|
45 |
+
mots_cles = [
|
46 |
+
"CARTE D'IDENTITE", "IDENTITÉ", "REPUBLIQUE DE GUINÉE",
|
47 |
+
"NOM", "PRENOM", "NATIONALITE", "GIN", "NIN",
|
48 |
+
"SEXE", "DATE DE NAISSANCE", "NUMERO", "EXPIRATION", "EMISSION"
|
49 |
+
]
|
50 |
+
return any(mot in texte for mot in mots_cles)
|
51 |
+
|
52 |
+
# ---------- 5. Extraction de champs ----------
|
53 |
def extract_fields(text):
|
54 |
data = {}
|
55 |
text = text.upper()
|
|
|
76 |
|
77 |
return data
|
78 |
|
79 |
+
# ---------- 6. Fonction principale ----------
|
80 |
def analyser_carte(recto_img, verso_img):
|
81 |
try:
|
82 |
recto = preprocess_image(recto_img)
|
83 |
verso = preprocess_image(verso_img)
|
84 |
+
|
85 |
text_r = ocr_easyocr(recto)
|
86 |
text_v = ocr_easyocr(verso)
|
87 |
texte_total = text_r + "\n" + text_v
|
88 |
+
|
89 |
+
if not est_carte_identite_guineenne(texte_total):
|
90 |
+
return (
|
91 |
+
"**Alerte :** Le document fourni ne semble **pas être une carte d'identité guinéenne**.\n"
|
92 |
+
"Merci de vérifier l'image ou d'en fournir une autre.",
|
93 |
+
{}
|
94 |
+
)
|
95 |
+
|
96 |
champs = extract_fields(texte_total)
|
97 |
return texte_total, champs
|
98 |
+
|
99 |
except Exception as e:
|
100 |
return f"Erreur : {str(e)}", {}
|
101 |
|
102 |
+
# ---------- 7. Interface Gradio ----------
|
103 |
interface = gr.Interface(
|
104 |
fn=analyser_carte,
|
105 |
inputs=[
|
|
|
110 |
gr.Textbox(label="Texte brut extrait"),
|
111 |
gr.JSON(label="Champs extraits")
|
112 |
],
|
113 |
+
title="OCRIA - Vérification intelligente de carte d'identité guinéenne",
|
114 |
+
description="Téléversez le recto et le verso d'une carte d'identité guinéenne. L'outil vérifiera la netteté, la validité et extraira les champs automatiquement.",
|
115 |
+
theme="soft"
|
116 |
)
|
117 |
|
118 |
interface.launch()
|