shayansjm commited on
Commit
1210071
·
verified ·
1 Parent(s): 26fdeb9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -41
app.py CHANGED
@@ -1,74 +1,67 @@
1
  import gradio as gr
2
  from paddleocr import PaddleOCR
3
- from PIL import Image, ImageOps, ImageEnhance
4
  import numpy as np
5
 
6
- # 1. Clean Initialization for 2026 (PaddleOCR 3.0+)
7
  try:
8
  ocr = PaddleOCR(
9
  lang='en',
10
- ocr_version='PP-OCRv4', # Stable & accurate for handwriting
11
- use_angle_cls=True
 
12
  )
13
  except Exception as e:
14
- print(f"Initialization Error: {e}")
15
 
16
- def process_bank_form(image):
17
  if image is None:
18
  return "Please upload an image."
19
 
20
  try:
21
- # 2. ENHANCE HANDWRITING (Pre-processing)
22
- # We sharpen and increase contrast to help the AI see light pen marks
23
  img = image.convert("RGB")
24
- img = ImageOps.exif_transpose(img) # Fix auto-rotation from phones
25
- enhancer = ImageEnhance.Contrast(img)
26
- img = enhancer.enhance(1.5) # Boost contrast by 50%
27
-
28
  img_array = np.array(img)
29
 
30
- # 3. RUN OCR
31
  result = ocr.ocr(img_array)
32
 
33
- # 4. SAFE PARSING (Fixes 'string index out of range')
34
- if not result or result[0] is None:
35
- return "No text detected. Try a closer photo or darker ink."
36
 
37
  extracted_text = []
 
38
  for line in result[0]:
39
- # Each 'line' is [[box_coords], [text, confidence]]
40
- # We must verify the text content exists before accessing it
41
- if len(line) > 1 and len(line[1]) > 0:
42
- text_str = str(line[1][0]).strip()
43
- confidence = float(line[1][1])
44
 
45
- # If the string is empty, skip it to avoid the index error
46
- if text_str and confidence > 0.30:
47
- extracted_text.append(text_str)
 
 
 
 
48
 
49
  if not extracted_text:
50
- return "AI found text boxes but couldn't read the words. Try more light."
51
 
52
  return "\n".join(extracted_text)
53
 
54
  except Exception as e:
55
- return f"Processing Error: {str(e)}\nTip: If the error persists, try a smaller/cropped image."
56
 
57
- # 5. UI Setup
58
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
59
- gr.Markdown("# 🏦 English Bank Form Reader (Stable v3.0)")
60
- gr.Markdown("Optimized for handwriting and noisy document scans.")
61
-
62
- with gr.Row():
63
- with gr.Column():
64
- input_img = gr.Image(type="pil", label="Bank Form Photo")
65
- submit_btn = gr.Button("Extract All Text", variant="primary")
66
- with gr.Column():
67
- #output_text = gr.Textbox(label="Extracted Content", lines=20, show_copy_button=True)
68
- output_text = gr.Textbox(label="Result", lines=20)
69
-
70
- submit_btn.click(fn=process_bank_form, inputs=input_img, outputs=output_text)
71
 
72
  if __name__ == "__main__":
73
- # max_threads=1 prevents memory overlap on free CPU tiers
74
  demo.launch(max_threads=1)
 
1
  import gradio as gr
2
  from paddleocr import PaddleOCR
3
+ from PIL import Image, ImageOps
4
  import numpy as np
5
 
6
+ # Initializing with explicit CPU settings for Hugging Face
7
  try:
8
  ocr = PaddleOCR(
9
  lang='en',
10
+ ocr_version='PP-OCRv4',
11
+ use_angle_cls=True,
12
+ use_gpu=False # Force CPU to prevent memory-related runtime errors
13
  )
14
  except Exception as e:
15
+ print(f"Startup error: {e}")
16
 
17
+ def safe_ocr_process(image):
18
  if image is None:
19
  return "Please upload an image."
20
 
21
  try:
22
+ # 1. Correct image orientation (common phone photo fix)
 
23
  img = image.convert("RGB")
24
+ img = ImageOps.exif_transpose(img)
 
 
 
25
  img_array = np.array(img)
26
 
27
+ # 2. Run OCR
28
  result = ocr.ocr(img_array)
29
 
30
+ # 3. Defensive Parsing (The Fix for 'Index Out of Range')
31
+ if not result or not isinstance(result, list) or result[0] is None:
32
+ return "No text detected. Try a closer, clearer photo."
33
 
34
  extracted_text = []
35
+ # PaddleOCR result is a list of lists: result[0] contains the lines
36
  for line in result[0]:
37
+ # Each 'line' is [[box], [text, confidence]]
38
+ # We check if 'line' has exactly two elements and if the text part is valid
39
+ if isinstance(line, list) and len(line) >= 2:
40
+ text_data = line[1] # This is [text_string, confidence_score]
 
41
 
42
+ if isinstance(text_data, (list, tuple)) and len(text_data) >= 1:
43
+ raw_string = text_data[0]
44
+ confidence = text_data[1]
45
+
46
+ # Ensure it's a non-empty string and confidence is okay
47
+ if raw_string and isinstance(raw_string, str) and confidence > 0.3:
48
+ extracted_text.append(raw_string.strip())
49
 
50
  if not extracted_text:
51
+ return "Detection found boxes, but couldn't read the words. Try more light."
52
 
53
  return "\n".join(extracted_text)
54
 
55
  except Exception as e:
56
+ return f"Final Stability Error: {str(e)}\nTip: This usually happens if the image is too large for the CPU. Try cropping the form."
57
 
58
+ # Simple, reliable UI
59
+ demo = gr.Interface(
60
+ fn=safe_ocr_process,
61
+ inputs=gr.Image(type="pil", label="Bank Form Image"),
62
+ outputs=gr.Textbox(label="Extracted Text", lines=20),
63
+ title="🏦 Stable Bank OCR"
64
+ )
 
 
 
 
 
 
 
65
 
66
  if __name__ == "__main__":
 
67
  demo.launch(max_threads=1)