kebson commited on
Commit
4acaff2
·
verified ·
1 Parent(s): 3540c20

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -35
app.py CHANGED
@@ -4,11 +4,10 @@ import numpy as np
4
  from PIL import Image
5
  from paddleocr import PaddleOCR
6
 
7
- # Initialisation OCR (CPU, stable HF)
8
  ocr = PaddleOCR(
9
  use_angle_cls=True,
10
- lang="en"
11
-
12
  )
13
 
14
 
@@ -18,27 +17,42 @@ def extract_descriptions(image: Image.Image):
18
 
19
  img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
20
 
21
- # OCR Paddle
22
  result = ocr.ocr(img)
23
 
24
  words = []
 
 
25
  for line in result[0]:
26
- box, (text, score) = line
27
- if score < 0.5:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  continue
29
 
30
- x_coords = [p[0] for p in box]
31
- y_coords = [p[1] for p in box]
32
 
33
  words.append({
34
  "text": text.strip(),
35
- "x": min(x_coords),
36
- "y": min(y_coords),
37
- "w": max(x_coords) - min(x_coords),
38
- "h": max(y_coords) - min(y_coords)
39
  })
40
 
41
- # 1️⃣ Détecter l'en-tête "Description"
42
  header = next(
43
  (w for w in words if "description" in w["text"].lower()),
44
  None
@@ -57,7 +71,7 @@ def extract_descriptions(image: Image.Image):
57
  if x_min <= w["x"] <= x_max and w["y"] > y_min
58
  ]
59
 
60
- # 3️⃣ Grouper par lignes
61
  lines = {}
62
  for w in column_words:
63
  key = int(w["y"] // 18)
@@ -69,7 +83,6 @@ def extract_descriptions(image: Image.Image):
69
  w["text"] for w in sorted(lines[k], key=lambda x: x["x"])
70
  )
71
 
72
- # Filtrage facture
73
  low = line.lower()
74
  if any(x in low for x in ["vat", "gross", "net", "total", "each"]):
75
  continue
@@ -78,7 +91,7 @@ def extract_descriptions(image: Image.Image):
78
 
79
  raw_lines.append(line)
80
 
81
- # 4️⃣ Fusion cellules multilignes
82
  final = []
83
  buffer = ""
84
 
@@ -93,28 +106,14 @@ def extract_descriptions(image: Image.Image):
93
  if buffer:
94
  final.append(buffer.strip())
95
 
96
- if not final:
97
- return "⚠️ Aucun texte extrait."
98
-
99
- return "\n".join(final)
100
 
101
 
102
- # =========================
103
- # Interface Gradio
104
- # =========================
105
-
106
  demo = gr.Interface(
107
  fn=extract_descriptions,
108
- inputs=gr.Image(type="pil", label="Image de facture"),
109
- outputs=gr.Textbox(lines=20, label="Descriptions extraites"),
110
- title="Extraction colonne Description – PaddleOCR",
111
- description=(
112
- "OCR robuste basé sur PaddleOCR. "
113
- "Extraction automatique des cellules de la colonne Description."
114
- )
115
  )
116
 
117
- demo.launch(
118
- server_name="0.0.0.0",
119
- server_port=7860
120
- )
 
4
  from PIL import Image
5
  from paddleocr import PaddleOCR
6
 
 
7
  ocr = PaddleOCR(
8
  use_angle_cls=True,
9
+ lang="en",
10
+ use_gpu=False
11
  )
12
 
13
 
 
17
 
18
  img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
19
 
 
20
  result = ocr.ocr(img)
21
 
22
  words = []
23
+
24
+ # 🔴 PARSING ROBUSTE PaddleOCR
25
  for line in result[0]:
26
+ # Cas 1 : [box, (text, score)]
27
+ if len(line) >= 2 and isinstance(line[1], (list, tuple)):
28
+ box = line[0]
29
+ text = line[1][0]
30
+ score = line[1][1]
31
+
32
+ # Cas 2 : [box, text, score]
33
+ elif len(line) >= 3:
34
+ box = line[0]
35
+ text = line[1]
36
+ score = line[2]
37
+
38
+ else:
39
+ continue
40
+
41
+ if score < 0.5 or not text.strip():
42
  continue
43
 
44
+ xs = [p[0] for p in box]
45
+ ys = [p[1] for p in box]
46
 
47
  words.append({
48
  "text": text.strip(),
49
+ "x": min(xs),
50
+ "y": min(ys),
51
+ "w": max(xs) - min(xs),
52
+ "h": max(ys) - min(ys),
53
  })
54
 
55
+ # 1️⃣ Détecter la colonne Description
56
  header = next(
57
  (w for w in words if "description" in w["text"].lower()),
58
  None
 
71
  if x_min <= w["x"] <= x_max and w["y"] > y_min
72
  ]
73
 
74
+ # 3️⃣ Regroupement par lignes
75
  lines = {}
76
  for w in column_words:
77
  key = int(w["y"] // 18)
 
83
  w["text"] for w in sorted(lines[k], key=lambda x: x["x"])
84
  )
85
 
 
86
  low = line.lower()
87
  if any(x in low for x in ["vat", "gross", "net", "total", "each"]):
88
  continue
 
91
 
92
  raw_lines.append(line)
93
 
94
+ # 4️⃣ Fusion multilignes
95
  final = []
96
  buffer = ""
97
 
 
106
  if buffer:
107
  final.append(buffer.strip())
108
 
109
+ return "\n".join(final) if final else "⚠️ Aucun texte extrait."
 
 
 
110
 
111
 
 
 
 
 
112
  demo = gr.Interface(
113
  fn=extract_descriptions,
114
+ inputs=gr.Image(type="pil"),
115
+ outputs=gr.Textbox(lines=20),
116
+ title="Extraction colonne Description – PaddleOCR (Stable)"
 
 
 
 
117
  )
118
 
119
+ demo.launch(server_name="0.0.0.0", server_port=7860)