Update app.py
Browse files
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
|
| 7 |
-
#
|
| 8 |
-
ocr = PaddleOCR(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def process_bank_form(image):
|
| 11 |
if image is None:
|
| 12 |
return "Please upload an image."
|
| 13 |
|
| 14 |
-
# Convert
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
-
# Run OCR
|
| 18 |
-
#
|
| 19 |
result = ocr.ocr(img_array, cls=True)
|
| 20 |
|
| 21 |
if not result or result[0] is None:
|
| 22 |
-
return "No text
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
for line in result[0]:
|
| 27 |
-
|
| 28 |
-
confidence = line[1][1]
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
return "\n".join(
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 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()
|