shayansjm commited on
Commit
c7e8402
·
verified ·
1 Parent(s): ca6f8c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -21
app.py CHANGED
@@ -1,26 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  def process_bank_form(image):
2
  if image is None:
3
  return "Please upload an image."
4
 
5
- # Convert PIL to RGB numpy array
6
- img = image.convert("RGB")
7
- img_array = np.array(img)
8
-
9
- # NEWEST API (3.0+):
10
- # Do not pass 'cls=True' here.
11
- # It is already handled because we set 'use_angle_cls=True' in the initializer.
12
- result = ocr.ocr(img_array)
13
-
14
- if not result or result[0] is None:
15
- return "No text detected. Try a clearer photo or a darker pen."
16
-
17
- # Process and combine text
18
- extracted_text = []
19
- for line in result[0]:
20
- text = line[1][0] # The recognized string
21
- confidence = line[1][1] # Confidence score
22
 
23
- if confidence > 0.4:
24
- extracted_text.append(text)
25
-
26
- return "\n".join(extracted_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from paddleocr import PaddleOCR
3
+ from PIL import Image
4
+ import numpy as np
5
+ import logging
6
+
7
+ # Disable internal logging to prevent "Invalid File Descriptor" noise
8
+ logging.getLogger("ppocr").setLevel(logging.ERROR)
9
+
10
+ # 1. BARE MINIMAL INITIALIZATION
11
+ # In 3.0+, just set the version and language.
12
+ # Angle classification is handled internally.
13
+ try:
14
+ ocr = PaddleOCR(
15
+ lang='en',
16
+ ocr_version='PP-OCRv4',
17
+ use_angle_cls=True
18
+ )
19
+ except Exception as e:
20
+ print(f"Startup Error: {e}")
21
+
22
  def process_bank_form(image):
23
  if image is None:
24
  return "Please upload an image."
25
 
26
+ try:
27
+ # 2. Standardize Image Format
28
+ # Converting to RGB and then a Numpy array prevents "Invalid Format" errors
29
+ img = image.convert("RGB")
30
+ img_array = np.array(img)
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
+ # 3. Safe Inference
33
+ # Removed 'cls=True' to fix the TypeError you saw earlier.
34
+ # PaddleOCR 3.0+ handles the pipeline automatically.
35
+ result = ocr.ocr(img_array)
36
+
37
+ if not result or result[0] is None:
38
+ return "No text detected. Try a closer photo with better lighting."
39
+
40
+ # 4. Extract Text
41
+ extracted_text = []
42
+ for line in result[0]:
43
+ # line[1][0] is the text string
44
+ text_str = str(line[1][0])
45
+ confidence = float(line[1][1])
46
+
47
+ # Lower confidence threshold slightly for handwriting (0.35)
48
+ if confidence > 0.35:
49
+ extracted_text.append(text_str)
50
+
51
+ return "\n".join(extracted_text)
52
+
53
+ except Exception as e:
54
+ return f"Runtime Error: {str(e)}\nTip: Try a smaller image or restart the Space."
55
+
56
+ # 5. UI Setup
57
+ demo = gr.Interface(
58
+ fn=process_bank_form,
59
+ inputs=gr.Image(type="pil", label="Bank Form Image"),
60
+ outputs=gr.Textbox(label="Extracted Content", lines=20),
61
+ title="🏦 Global Bank Form OCR",
62
+ description="Optimized for English handwriting and printed text on CPU."
63
+ )
64
+
65
+ if __name__ == "__main__":
66
+ # max_threads=1 is safer for free CPU tiers to prevent memory crashes
67
+ demo.launch(max_threads=1)