kebson commited on
Commit
00c654c
·
verified ·
1 Parent(s): 7d75986

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -30
app.py CHANGED
@@ -2,63 +2,85 @@ import os
2
  import cv2
3
  import pandas as pd
4
  from paddleocr import PaddleOCR
 
5
 
6
- # Initialisation OCR (CPU)
7
- ocr = PaddleOCR(use_angle_cls=True, lang="fr")
 
 
 
 
8
 
9
  def extract_second_column(image_path):
10
  """
11
- Extrait le texte de la 2e colonne d'un tableau dans une image
12
  """
13
  result = ocr.ocr(image_path, cls=True)
14
 
15
- column_2_text = []
 
16
 
17
- for line in result[0]:
18
- text = line[1][0]
19
- column_2_text.append(text)
20
 
21
- return column_2_text
 
22
 
 
 
 
 
 
 
23
 
24
- def main():
25
- images_dir = "images"
26
- # dossier persistant Hugging face
27
- os.makedirs("/data", exist_ok=True)
28
- if not os.path.exists(images_dir):
29
- raise FileNotFoundError(
30
- f"Le dossier '{images_dir}' est introuvable. "
31
- "Vérifiez qu'il est bien copié dans le conteneur Docker."
32
- )
33
 
34
- all_results = []
 
 
 
 
35
 
 
 
 
 
 
36
 
37
-
 
38
 
39
-
 
 
 
40
 
41
-
42
 
43
  for filename in sorted(os.listdir(images_dir)):
44
  if filename.lower().endswith((".jpg", ".jpeg", ".png")):
45
  image_path = os.path.join(images_dir, filename)
46
- col2 = extract_second_column(image_path)
47
 
48
- for value in col2:
49
  all_results.append({
50
  "image": filename,
51
- "colonne_2": value
52
  })
53
 
54
  df = pd.DataFrame(all_results)
55
-
56
- output_path="resultats_colonne_2.csv"
57
  df.to_csv(output_path, index=False)
58
- print(f"✅ Extraction terminée:{output_path}")
59
- print("Fichiers presents :",os.listdir("."))
60
 
 
 
 
 
 
 
 
 
 
 
61
 
62
- if __name__ == "__main__":
63
- main()
64
 
 
2
  import cv2
3
  import pandas as pd
4
  from paddleocr import PaddleOCR
5
+ import gradio as gr
6
 
7
+ # Initialisation OCR (une seule fois)
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
+ # Trier par position horizontale (x)
26
+ boxes_sorted_x = sorted(boxes, key=lambda b: min(p[0] for p in b[0]))
27
 
28
+ # Regrouper en colonnes (heuristique)
29
+ columns = {}
30
+ for box in boxes_sorted_x:
31
+ x_coords = [p[0] for p in box[0]]
32
+ x_center = sum(x_coords) / len(x_coords)
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
+ # Vérifier qu'il y a au moins 2 colonnes
39
+ if len(sorted_cols) < 2:
40
+ return []
41
+
42
+ second_col = sorted_cols[1][1]
43
 
44
+ # Trier verticalement
45
+ second_col_sorted = sorted(
46
+ second_col,
47
+ key=lambda b: min(p[1] for p in b[0])
48
+ )
49
 
50
+ texts = [b[1][0] for b in second_col_sorted]
51
+ return texts
52
 
53
+ def main():
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
+ col2_values = extract_second_column(image_path)
64
 
65
+ for val in col2_values:
66
  all_results.append({
67
  "image": filename,
68
+ "colonne_2": val
69
  })
70
 
71
  df = pd.DataFrame(all_results)
72
+ output_path = os.path.join(output_dir, "resultats_colonne_2.csv")
 
73
  df.to_csv(output_path, index=False)
 
 
74
 
75
+ return output_path
76
+
77
+ # Interface Gradio
78
+ gr.Interface(
79
+ fn=main,
80
+ inputs=[],
81
+ outputs=gr.File(label="Télécharger le fichier CSV"),
82
+ title="Extraction OCR – Colonne 2 des tableaux",
83
+ description="Cliquez sur le bouton pour lancer l'OCR et télécharger le CSV."
84
+ ).launch()
85
 
 
 
86