Update app.py
Browse files
app.py
CHANGED
|
@@ -1,74 +1,67 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from paddleocr import PaddleOCR
|
| 3 |
-
from PIL import Image, ImageOps
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
-
#
|
| 7 |
try:
|
| 8 |
ocr = PaddleOCR(
|
| 9 |
lang='en',
|
| 10 |
-
ocr_version='PP-OCRv4',
|
| 11 |
-
use_angle_cls=True
|
|
|
|
| 12 |
)
|
| 13 |
except Exception as e:
|
| 14 |
-
print(f"
|
| 15 |
|
| 16 |
-
def
|
| 17 |
if image is None:
|
| 18 |
return "Please upload an image."
|
| 19 |
|
| 20 |
try:
|
| 21 |
-
#
|
| 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)
|
| 25 |
-
enhancer = ImageEnhance.Contrast(img)
|
| 26 |
-
img = enhancer.enhance(1.5) # Boost contrast by 50%
|
| 27 |
-
|
| 28 |
img_array = np.array(img)
|
| 29 |
|
| 30 |
-
#
|
| 31 |
result = ocr.ocr(img_array)
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
if not result or result[0] is None:
|
| 35 |
-
return "No text detected. Try a closer photo
|
| 36 |
|
| 37 |
extracted_text = []
|
|
|
|
| 38 |
for line in result[0]:
|
| 39 |
-
# Each 'line' is [[
|
| 40 |
-
# We
|
| 41 |
-
if
|
| 42 |
-
|
| 43 |
-
confidence = float(line[1][1])
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
if not extracted_text:
|
| 50 |
-
return "
|
| 51 |
|
| 52 |
return "\n".join(extracted_text)
|
| 53 |
|
| 54 |
except Exception as e:
|
| 55 |
-
return f"
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
gr.
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 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)
|