shayansjm commited on
Commit
141e334
·
verified ·
1 Parent(s): da6be28

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -23
app.py CHANGED
@@ -2,43 +2,57 @@ import gradio as gr
2
  from paddleocr import PaddleOCR
3
  from PIL import Image
4
  import numpy as np
 
5
 
6
- # Initialize PaddleOCR (English language, with handwriting support enabled)
7
- # This will download the models on the first run
8
- ocr = PaddleOCR(use_angle_cls=True, lang='en', rec_algorithm='SVTR_LCNet')
 
 
 
 
 
9
 
10
  def process_bank_form(image):
11
  if image is None:
12
  return "Please upload an image."
13
 
14
- # Convert PIL to numpy array for PaddleOCR
15
- img_array = np.array(image.convert("RGB"))
 
16
 
17
- # Run OCR
18
- # det=True (finds text), rec=True (reads text), cls=True (handles rotated text)
19
  result = ocr.ocr(img_array, cls=True)
20
 
21
  if not result or result[0] is None:
22
- return "No text detected. Try a clearer photo."
23
 
24
- # Format the output
25
- full_text = []
26
  for line in result[0]:
27
- text = line[1][0] # The actual words
28
- confidence = line[1][1] # How sure the AI is
29
- if confidence > 0.5: # Filter out "visual noise"
30
- full_text.append(text)
 
 
31
 
32
- return "\n".join(full_text)
33
 
34
- # Build the Interface
35
- demo = gr.Interface(
36
- fn=process_bank_form,
37
- inputs=gr.Image(type="pil", label="Upload Bank Form"),
38
- outputs=gr.Textbox(label="Extracted Text (Handwritten & Printed)", lines=20),
39
- title="🏦 Advanced Bank Form OCR",
40
- description="Uses PP-OCRv5 to detect messy handwriting and printed text on a CPU."
41
- )
 
 
 
 
 
42
 
43
  if __name__ == "__main__":
44
  demo.launch()
 
2
  from paddleocr import PaddleOCR
3
  from PIL import Image
4
  import numpy as np
5
+ import os
6
 
7
+ # Initialize OCR with specific settings for handwriting and forms
8
+ # rec_algorithm='SVTR_LCNet' is the secret for messy handwriting
9
+ ocr = PaddleOCR(
10
+ use_angle_cls=True,
11
+ lang='en',
12
+ show_log=False,
13
+ rec_algorithm='SVTR_LCNet'
14
+ )
15
 
16
  def process_bank_form(image):
17
  if image is None:
18
  return "Please upload an image."
19
 
20
+ # Pre-processing: Convert to RGB and then to a format Paddle understands
21
+ img = image.convert("RGB")
22
+ img_array = np.array(img)
23
 
24
+ # Run the OCR pipeline
25
+ # We use 'cls=True' to fix upside-down or tilted phone photos automatically
26
  result = ocr.ocr(img_array, cls=True)
27
 
28
  if not result or result[0] is None:
29
+ return "No text found. Try taking a photo with more light or a darker pen."
30
 
31
+ # Extracting and cleaning the text
32
+ extracted_lines = []
33
  for line in result[0]:
34
+ text_content = line[1][0]
35
+ confidence = line[1][1]
36
+
37
+ # Only keep text if the AI is reasonably sure (avoids random symbols)
38
+ if confidence > 0.4:
39
+ extracted_lines.append(text_content)
40
 
41
+ return "\n".join(extracted_lines)
42
 
43
+ # Create the Gradio Interface
44
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
45
+ gr.Markdown("# 🏦 Bank Form Handwriting Reader")
46
+ gr.Markdown("Upload a photo of your bank form. This system is tuned for English handwritten and printed text.")
47
+
48
+ with gr.Row():
49
+ with gr.Column():
50
+ input_img = gr.Image(type="pil", label="Bank Form Photo")
51
+ submit_btn = gr.Button("Read Form", variant="primary")
52
+ with gr.Column():
53
+ output_text = gr.Textbox(label="Extracted Text", lines=20, show_copy_button=True)
54
+
55
+ submit_btn.click(fn=process_bank_form, inputs=input_img, outputs=output_text)
56
 
57
  if __name__ == "__main__":
58
  demo.launch()