Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,71 +1,62 @@
|
|
| 1 |
import os
|
| 2 |
-
import cv2
|
| 3 |
import pandas as pd
|
| 4 |
from paddleocr import PaddleOCR
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
-
|
| 8 |
-
ocr = PaddleOCR(
|
| 9 |
-
use_angle_cls=True,
|
| 10 |
-
lang="fr",
|
| 11 |
-
show_log=False
|
| 12 |
-
)
|
| 13 |
-
|
| 14 |
-
def extract_second_column(image_path):
|
| 15 |
-
"""
|
| 16 |
-
Extrait le texte de la 2e colonne du tableau (approche par position X)
|
| 17 |
-
"""
|
| 18 |
result = ocr.ocr(image_path, cls=True)
|
| 19 |
-
|
| 20 |
if not result or not result[0]:
|
| 21 |
return []
|
| 22 |
|
| 23 |
boxes = result[0]
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
# Regrouper en colonnes (heuristique)
|
| 29 |
columns = {}
|
| 30 |
for box in boxes_sorted_x:
|
| 31 |
-
|
| 32 |
-
x_center
|
| 33 |
-
columns.setdefault(int(x_center // 100), []).append(box)
|
| 34 |
-
|
| 35 |
-
# Trier les colonnes
|
| 36 |
-
sorted_cols = sorted(columns.items(), key=lambda x: x[0])
|
| 37 |
|
| 38 |
-
|
| 39 |
-
if len(sorted_cols) < 2:
|
| 40 |
return []
|
| 41 |
|
|
|
|
| 42 |
second_col = sorted_cols[1][1]
|
| 43 |
|
| 44 |
-
|
| 45 |
-
second_col_sorted = sorted(
|
| 46 |
second_col,
|
| 47 |
key=lambda b: min(p[1] for p in b[0])
|
| 48 |
)
|
| 49 |
|
| 50 |
-
|
| 51 |
-
return texts
|
| 52 |
|
| 53 |
-
def
|
| 54 |
images_dir = "images"
|
| 55 |
output_dir = "/data"
|
| 56 |
os.makedirs(output_dir, exist_ok=True)
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
all_results = []
|
| 59 |
|
| 60 |
for filename in sorted(os.listdir(images_dir)):
|
| 61 |
if filename.lower().endswith((".jpg", ".jpeg", ".png")):
|
| 62 |
image_path = os.path.join(images_dir, filename)
|
| 63 |
-
|
| 64 |
|
| 65 |
-
for
|
| 66 |
all_results.append({
|
| 67 |
"image": filename,
|
| 68 |
-
"colonne_2":
|
| 69 |
})
|
| 70 |
|
| 71 |
df = pd.DataFrame(all_results)
|
|
@@ -74,13 +65,14 @@ def main():
|
|
| 74 |
|
| 75 |
return output_path
|
| 76 |
|
| 77 |
-
# Interface Gradio
|
| 78 |
gr.Interface(
|
| 79 |
-
fn=
|
| 80 |
inputs=[],
|
| 81 |
-
outputs=gr.File(label="Télécharger le
|
| 82 |
-
title="Extraction OCR – Colonne 2
|
| 83 |
-
description="Cliquez
|
| 84 |
).launch()
|
| 85 |
|
|
|
|
|
|
|
| 86 |
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
from paddleocr import PaddleOCR
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
+
def extract_second_column(image_path, ocr):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
result = ocr.ocr(image_path, cls=True)
|
|
|
|
| 8 |
if not result or not result[0]:
|
| 9 |
return []
|
| 10 |
|
| 11 |
boxes = result[0]
|
| 12 |
|
| 13 |
+
boxes_sorted_x = sorted(
|
| 14 |
+
boxes,
|
| 15 |
+
key=lambda b: min(p[0] for p in b[0])
|
| 16 |
+
)
|
| 17 |
|
|
|
|
| 18 |
columns = {}
|
| 19 |
for box in boxes_sorted_x:
|
| 20 |
+
x_center = sum(p[0] for p in box[0]) / 4
|
| 21 |
+
columns.setdefault(int(x_center // 120), []).append(box)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
if len(columns) < 2:
|
|
|
|
| 24 |
return []
|
| 25 |
|
| 26 |
+
sorted_cols = sorted(columns.items())
|
| 27 |
second_col = sorted_cols[1][1]
|
| 28 |
|
| 29 |
+
second_col = sorted(
|
|
|
|
| 30 |
second_col,
|
| 31 |
key=lambda b: min(p[1] for p in b[0])
|
| 32 |
)
|
| 33 |
|
| 34 |
+
return [b[1][0] for b in second_col]
|
|
|
|
| 35 |
|
| 36 |
+
def run_ocr():
|
| 37 |
images_dir = "images"
|
| 38 |
output_dir = "/data"
|
| 39 |
os.makedirs(output_dir, exist_ok=True)
|
| 40 |
|
| 41 |
+
# ⚠️ OCR INITIALISÉ ICI (PAS AU DÉMARRAGE)
|
| 42 |
+
ocr = PaddleOCR(
|
| 43 |
+
use_angle_cls=True,
|
| 44 |
+
lang="fr",
|
| 45 |
+
show_log=False,
|
| 46 |
+
cpu_threads=1
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
all_results = []
|
| 50 |
|
| 51 |
for filename in sorted(os.listdir(images_dir)):
|
| 52 |
if filename.lower().endswith((".jpg", ".jpeg", ".png")):
|
| 53 |
image_path = os.path.join(images_dir, filename)
|
| 54 |
+
values = extract_second_column(image_path, ocr)
|
| 55 |
|
| 56 |
+
for v in values:
|
| 57 |
all_results.append({
|
| 58 |
"image": filename,
|
| 59 |
+
"colonne_2": v
|
| 60 |
})
|
| 61 |
|
| 62 |
df = pd.DataFrame(all_results)
|
|
|
|
| 65 |
|
| 66 |
return output_path
|
| 67 |
|
|
|
|
| 68 |
gr.Interface(
|
| 69 |
+
fn=run_ocr,
|
| 70 |
inputs=[],
|
| 71 |
+
outputs=gr.File(label="Télécharger le CSV"),
|
| 72 |
+
title="Extraction OCR – Colonne 2",
|
| 73 |
+
description="Cliquez pour lancer l'OCR (première exécution plus lente)"
|
| 74 |
).launch()
|
| 75 |
|
| 76 |
+
|
| 77 |
+
|
| 78 |
|