Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,13 +4,14 @@ import cv2
|
|
| 4 |
import pytesseract
|
| 5 |
import numpy as np
|
| 6 |
from PIL import Image
|
| 7 |
-
from transformers import DetrImageProcessor, TableTransformerForObjectDetection
|
| 8 |
|
| 9 |
# ===============================
|
| 10 |
# Chargement des modèles
|
| 11 |
# ===============================
|
| 12 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
|
|
|
|
| 14 |
det_processor = DetrImageProcessor.from_pretrained(
|
| 15 |
"microsoft/table-transformer-detection"
|
| 16 |
)
|
|
@@ -18,10 +19,11 @@ det_model = TableTransformerForObjectDetection.from_pretrained(
|
|
| 18 |
"microsoft/table-transformer-detection"
|
| 19 |
).to(DEVICE)
|
| 20 |
|
|
|
|
| 21 |
struct_processor = DetrImageProcessor.from_pretrained(
|
| 22 |
"microsoft/table-transformer-structure-recognition"
|
| 23 |
)
|
| 24 |
-
struct_model =
|
| 25 |
"microsoft/table-transformer-structure-recognition"
|
| 26 |
).to(DEVICE)
|
| 27 |
|
|
@@ -60,26 +62,44 @@ def extract_description(image_pil):
|
|
| 60 |
if not tables:
|
| 61 |
return "❌ Aucun tableau détecté", ""
|
| 62 |
|
|
|
|
| 63 |
table_box = tables[0].int().tolist()
|
| 64 |
x0, y0, x1, y1 = table_box
|
| 65 |
table_img = image[y0:y1, x0:x1]
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
# ---- Structure du tableau ----
|
| 68 |
-
inputs = struct_processor(images=
|
| 69 |
inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
|
| 70 |
outputs = struct_model(**inputs)
|
| 71 |
|
| 72 |
results = struct_processor.post_process_object_detection(
|
| 73 |
outputs,
|
| 74 |
-
threshold=0.
|
| 75 |
-
target_sizes=[
|
| 76 |
)[0]
|
| 77 |
|
| 78 |
cells = []
|
| 79 |
for box, label in zip(results["boxes"], results["labels"]):
|
| 80 |
label_name = struct_model.config.id2label[label.item()]
|
| 81 |
if label_name == "table cell":
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
if not cells:
|
| 85 |
return "❌ Aucune cellule détectée", ""
|
|
|
|
| 4 |
import pytesseract
|
| 5 |
import numpy as np
|
| 6 |
from PIL import Image
|
| 7 |
+
from transformers import DetrImageProcessor, TableTransformerForObjectDetection, TableTransformerForStructureRecognition
|
| 8 |
|
| 9 |
# ===============================
|
| 10 |
# Chargement des modèles
|
| 11 |
# ===============================
|
| 12 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
|
| 14 |
+
# Modèle de détection de tableau
|
| 15 |
det_processor = DetrImageProcessor.from_pretrained(
|
| 16 |
"microsoft/table-transformer-detection"
|
| 17 |
)
|
|
|
|
| 19 |
"microsoft/table-transformer-detection"
|
| 20 |
).to(DEVICE)
|
| 21 |
|
| 22 |
+
# Modèle de reconnaissance de structure (cellules)
|
| 23 |
struct_processor = DetrImageProcessor.from_pretrained(
|
| 24 |
"microsoft/table-transformer-structure-recognition"
|
| 25 |
)
|
| 26 |
+
struct_model = TableTransformerForStructureRecognition.from_pretrained(
|
| 27 |
"microsoft/table-transformer-structure-recognition"
|
| 28 |
).to(DEVICE)
|
| 29 |
|
|
|
|
| 62 |
if not tables:
|
| 63 |
return "❌ Aucun tableau détecté", ""
|
| 64 |
|
| 65 |
+
# Extraire premier tableau détecté
|
| 66 |
table_box = tables[0].int().tolist()
|
| 67 |
x0, y0, x1, y1 = table_box
|
| 68 |
table_img = image[y0:y1, x0:x1]
|
| 69 |
|
| 70 |
+
# ---- Optionnel : vérifier visuellement le tableau ----
|
| 71 |
+
# Image.fromarray(table_img).show()
|
| 72 |
+
|
| 73 |
+
# ---- Redimensionner le tableau pour la structure ----
|
| 74 |
+
max_size = 1024
|
| 75 |
+
scale = max_size / max(table_img.shape[:2])
|
| 76 |
+
new_w, new_h = int(table_img.shape[1]*scale), int(table_img.shape[0]*scale)
|
| 77 |
+
table_resized = cv2.resize(table_img, (new_w, new_h))
|
| 78 |
+
|
| 79 |
# ---- Structure du tableau ----
|
| 80 |
+
inputs = struct_processor(images=table_resized, return_tensors="pt")
|
| 81 |
inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
|
| 82 |
outputs = struct_model(**inputs)
|
| 83 |
|
| 84 |
results = struct_processor.post_process_object_detection(
|
| 85 |
outputs,
|
| 86 |
+
threshold=0.5, # seuil plus bas pour capturer plus de cellules
|
| 87 |
+
target_sizes=[table_resized.shape[:2]]
|
| 88 |
)[0]
|
| 89 |
|
| 90 |
cells = []
|
| 91 |
for box, label in zip(results["boxes"], results["labels"]):
|
| 92 |
label_name = struct_model.config.id2label[label.item()]
|
| 93 |
if label_name == "table cell":
|
| 94 |
+
# Remettre les coordonnées à l'échelle originale
|
| 95 |
+
scale_x = table_img.shape[1] / table_resized.shape[1]
|
| 96 |
+
scale_y = table_img.shape[0] / table_resized.shape[0]
|
| 97 |
+
x0c, y0c, x1c, y1c = box.int().tolist()
|
| 98 |
+
x0c = int(x0c * scale_x)
|
| 99 |
+
x1c = int(x1c * scale_x)
|
| 100 |
+
y0c = int(y0c * scale_y)
|
| 101 |
+
y1c = int(y1c * scale_y)
|
| 102 |
+
cells.append([x0c, y0c, x1c, y1c])
|
| 103 |
|
| 104 |
if not cells:
|
| 105 |
return "❌ Aucune cellule détectée", ""
|