Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,139 +1,162 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
from paddleocr import PaddleOCR
|
| 4 |
from sklearn.cluster import KMeans
|
| 5 |
|
| 6 |
-
#
|
| 7 |
# OCR
|
| 8 |
-
#
|
| 9 |
ocr = PaddleOCR(
|
| 10 |
-
|
| 11 |
-
|
| 12 |
)
|
| 13 |
|
| 14 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# Fonction principale
|
| 16 |
-
#
|
| 17 |
-
def
|
| 18 |
if image is None:
|
| 19 |
return "Aucune image fournie."
|
| 20 |
|
| 21 |
img = np.array(image)
|
| 22 |
result = ocr.predict(img)
|
| 23 |
|
| 24 |
-
if not result
|
| 25 |
-
return "OCR
|
| 26 |
|
| 27 |
data = result[0]
|
| 28 |
texts = data.get("rec_texts", [])
|
| 29 |
boxes = data.get("dt_polys", [])
|
| 30 |
|
| 31 |
-
|
| 32 |
-
return "Aucun texte exploitable détecté."
|
| 33 |
-
|
| 34 |
-
# -----------------------------
|
| 35 |
-
# 1. Collecte OCR
|
| 36 |
-
# -----------------------------
|
| 37 |
-
elements = []
|
| 38 |
for text, box in zip(texts, boxes):
|
| 39 |
-
|
| 40 |
-
if len(
|
| 41 |
continue
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
if len(
|
| 49 |
-
return "Pas assez de texte
|
| 50 |
-
|
| 51 |
-
#
|
| 52 |
-
#
|
| 53 |
-
#
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
#
|
| 65 |
-
#
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
return
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
#
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
#
|
| 81 |
-
# 4. Fusion intelligente des lignes
|
| 82 |
-
#
|
| 83 |
-
|
| 84 |
-
|
| 85 |
last_y = None
|
| 86 |
Y_THRESHOLD = 22
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
"LOT", "BORDEREAU", "DATE", "NB", "TTC", "HT"
|
| 91 |
-
)
|
| 92 |
|
| 93 |
-
|
| 94 |
-
if text.upper().startswith(blacklist):
|
| 95 |
continue
|
| 96 |
|
| 97 |
if last_y is None or abs(y - last_y) > Y_THRESHOLD:
|
| 98 |
-
if
|
| 99 |
-
|
| 100 |
-
|
| 101 |
else:
|
| 102 |
-
|
| 103 |
|
| 104 |
last_y = y
|
| 105 |
|
| 106 |
-
if
|
| 107 |
-
|
| 108 |
|
| 109 |
-
#
|
| 110 |
-
# 5. Nettoyage final
|
| 111 |
-
#
|
| 112 |
-
|
| 113 |
-
for line in
|
| 114 |
-
|
|
|
|
| 115 |
continue
|
| 116 |
if sum(c.isdigit() for c in line) > len(line) / 2:
|
| 117 |
continue
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
final_lines = cleaned[:9]
|
| 121 |
|
| 122 |
-
if not
|
| 123 |
-
return "
|
| 124 |
|
| 125 |
-
#
|
| 126 |
-
|
|
|
|
|
|
|
| 127 |
|
| 128 |
-
#
|
| 129 |
# Interface Gradio
|
| 130 |
-
#
|
| 131 |
demo = gr.Interface(
|
| 132 |
-
fn=
|
| 133 |
inputs=gr.Image(type="pil", label="Image du tableau"),
|
| 134 |
-
outputs=gr.Textbox(label="
|
| 135 |
-
title="Extraction
|
| 136 |
-
description=
|
|
|
|
|
|
|
|
|
|
| 137 |
)
|
| 138 |
|
| 139 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
+
import unicodedata
|
| 4 |
from paddleocr import PaddleOCR
|
| 5 |
from sklearn.cluster import KMeans
|
| 6 |
|
| 7 |
+
# -------------------------------------------------
|
| 8 |
# OCR
|
| 9 |
+
# -------------------------------------------------
|
| 10 |
ocr = PaddleOCR(
|
| 11 |
+
lang="fr",
|
| 12 |
+
use_textline_orientation=True
|
| 13 |
)
|
| 14 |
|
| 15 |
+
# -------------------------------------------------
|
| 16 |
+
# Normalisation texte (casse + accents)
|
| 17 |
+
# -------------------------------------------------
|
| 18 |
+
def normalize(text: str) -> str:
|
| 19 |
+
text = text.lower()
|
| 20 |
+
text = unicodedata.normalize("NFD", text)
|
| 21 |
+
text = "".join(c for c in text if unicodedata.category(c) != "Mn")
|
| 22 |
+
return " ".join(text.split())
|
| 23 |
+
|
| 24 |
+
# -------------------------------------------------
|
| 25 |
+
# Titres valides de la colonne 2
|
| 26 |
+
# -------------------------------------------------
|
| 27 |
+
COL_TITLES = {
|
| 28 |
+
"designation",
|
| 29 |
+
"designations",
|
| 30 |
+
"description",
|
| 31 |
+
"description des services"
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
# -------------------------------------------------
|
| 35 |
+
# Mots / lignes à ignorer
|
| 36 |
+
# -------------------------------------------------
|
| 37 |
+
IGNORE_KEYWORDS = {
|
| 38 |
+
"prix", "total", "ht", "htva", "tva",
|
| 39 |
+
"ttc", "general", "generale"
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
# -------------------------------------------------
|
| 43 |
# Fonction principale
|
| 44 |
+
# -------------------------------------------------
|
| 45 |
+
def extract_second_column(image):
|
| 46 |
if image is None:
|
| 47 |
return "Aucune image fournie."
|
| 48 |
|
| 49 |
img = np.array(image)
|
| 50 |
result = ocr.predict(img)
|
| 51 |
|
| 52 |
+
if not result:
|
| 53 |
+
return "OCR : aucun texte détecté."
|
| 54 |
|
| 55 |
data = result[0]
|
| 56 |
texts = data.get("rec_texts", [])
|
| 57 |
boxes = data.get("dt_polys", [])
|
| 58 |
|
| 59 |
+
blocks = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
for text, box in zip(texts, boxes):
|
| 61 |
+
t = text.strip()
|
| 62 |
+
if len(t) < 2:
|
| 63 |
continue
|
| 64 |
|
| 65 |
+
x = np.mean([p[0] for p in box])
|
| 66 |
+
y = np.mean([p[1] for p in box])
|
| 67 |
+
|
| 68 |
+
blocks.append((t, x, y))
|
| 69 |
+
|
| 70 |
+
if len(blocks) < 5:
|
| 71 |
+
return "Pas assez de texte exploitable."
|
| 72 |
+
|
| 73 |
+
# -------------------------------------------------
|
| 74 |
+
# 1. Détection du X de la colonne cible via son titre
|
| 75 |
+
# -------------------------------------------------
|
| 76 |
+
col_x = None
|
| 77 |
+
for text, x, y in blocks:
|
| 78 |
+
if normalize(text) in COL_TITLES:
|
| 79 |
+
col_x = x
|
| 80 |
+
break
|
| 81 |
+
|
| 82 |
+
if col_x is None:
|
| 83 |
+
return "Titre de la colonne cible non détecté."
|
| 84 |
+
|
| 85 |
+
# -------------------------------------------------
|
| 86 |
+
# 2. Sélection des blocs proches du X détecté
|
| 87 |
+
# -------------------------------------------------
|
| 88 |
+
X_THRESHOLD = 45
|
| 89 |
+
column_blocks = [
|
| 90 |
+
(t, x, y) for t, x, y in blocks
|
| 91 |
+
if abs(x - col_x) < X_THRESHOLD
|
| 92 |
+
]
|
| 93 |
+
|
| 94 |
+
if not column_blocks:
|
| 95 |
+
return "Colonne détectée mais vide."
|
| 96 |
+
|
| 97 |
+
# -------------------------------------------------
|
| 98 |
+
# 3. Tri vertical (haut → bas)
|
| 99 |
+
# -------------------------------------------------
|
| 100 |
+
column_blocks.sort(key=lambda e: e[2])
|
| 101 |
+
|
| 102 |
+
# -------------------------------------------------
|
| 103 |
+
# 4. Fusion intelligente des lignes OCR
|
| 104 |
+
# -------------------------------------------------
|
| 105 |
+
merged = []
|
| 106 |
+
current = ""
|
| 107 |
last_y = None
|
| 108 |
Y_THRESHOLD = 22
|
| 109 |
|
| 110 |
+
for text, x, y in column_blocks:
|
| 111 |
+
nt = normalize(text)
|
|
|
|
|
|
|
| 112 |
|
| 113 |
+
if any(k in nt for k in IGNORE_KEYWORDS):
|
|
|
|
| 114 |
continue
|
| 115 |
|
| 116 |
if last_y is None or abs(y - last_y) > Y_THRESHOLD:
|
| 117 |
+
if current:
|
| 118 |
+
merged.append(current.strip())
|
| 119 |
+
current = text
|
| 120 |
else:
|
| 121 |
+
current += " " + text
|
| 122 |
|
| 123 |
last_y = y
|
| 124 |
|
| 125 |
+
if current:
|
| 126 |
+
merged.append(current.strip())
|
| 127 |
|
| 128 |
+
# -------------------------------------------------
|
| 129 |
+
# 5. Nettoyage final (cellules texte uniquement)
|
| 130 |
+
# -------------------------------------------------
|
| 131 |
+
final = []
|
| 132 |
+
for line in merged:
|
| 133 |
+
nt = normalize(line)
|
| 134 |
+
if len(nt) < 4:
|
| 135 |
continue
|
| 136 |
if sum(c.isdigit() for c in line) > len(line) / 2:
|
| 137 |
continue
|
| 138 |
+
final.append(line)
|
|
|
|
|
|
|
| 139 |
|
| 140 |
+
if not final:
|
| 141 |
+
return "Aucune cellule texte valide trouvée."
|
| 142 |
|
| 143 |
+
# -------------------------------------------------
|
| 144 |
+
# 6. Résultat numéroté
|
| 145 |
+
# -------------------------------------------------
|
| 146 |
+
return "\n".join(f"{i+1}. {line}" for i, line in enumerate(final))
|
| 147 |
|
| 148 |
+
# -------------------------------------------------
|
| 149 |
# Interface Gradio
|
| 150 |
+
# -------------------------------------------------
|
| 151 |
demo = gr.Interface(
|
| 152 |
+
fn=extract_second_column,
|
| 153 |
inputs=gr.Image(type="pil", label="Image du tableau"),
|
| 154 |
+
outputs=gr.Textbox(label="Contenu de la colonne 2"),
|
| 155 |
+
title="Extraction fiable de la colonne 2 (Désignation / Description)",
|
| 156 |
+
description=(
|
| 157 |
+
"Extraction robuste de la deuxième colonne des tableaux scannés "
|
| 158 |
+
"(Désignation, DESIGNATIONS, Description, Description des services)."
|
| 159 |
+
)
|
| 160 |
)
|
| 161 |
|
| 162 |
+
demo.launch(server_name="0.0.0.0", server_port=7860))
|