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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -37
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
- # 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)
@@ -74,13 +65,14 @@ def main():
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
 
 
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