Update app.py
Browse files
app.py
CHANGED
|
@@ -13,58 +13,57 @@ def process_bank_form(image):
|
|
| 13 |
return "Please upload an image."
|
| 14 |
|
| 15 |
try:
|
| 16 |
-
# 1.
|
| 17 |
if ocr_instance is None:
|
| 18 |
ocr_instance = PaddleOCR(
|
| 19 |
lang='en',
|
| 20 |
-
ocr_version='PP-
|
| 21 |
use_angle_cls=True
|
| 22 |
)
|
| 23 |
|
| 24 |
-
# 2.
|
| 25 |
img = image.convert("RGB")
|
| 26 |
-
img = ImageOps.exif_transpose(img)
|
| 27 |
img_array = np.array(img)
|
| 28 |
|
| 29 |
-
# 3.
|
| 30 |
result = ocr_instance.ocr(img_array)
|
| 31 |
|
| 32 |
-
# 4.
|
| 33 |
-
if not result or not isinstance(result, list) or len(result) == 0:
|
| 34 |
-
return "No text detected."
|
| 35 |
|
| 36 |
extracted_text = []
|
| 37 |
|
| 38 |
-
# result[0] is the list of detected lines
|
| 39 |
for line in result[0]:
|
| 40 |
-
# Each line
|
| 41 |
if isinstance(line, list) and len(line) >= 2:
|
| 42 |
content = line[1] # This should be [text, confidence]
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
if isinstance(content, (list, tuple)) and len(content)
|
| 46 |
text_val = content[0]
|
| 47 |
|
| 48 |
-
#
|
| 49 |
if text_val and isinstance(text_val, str):
|
| 50 |
extracted_text.append(text_val.strip())
|
| 51 |
|
| 52 |
if not extracted_text:
|
| 53 |
-
return "AI
|
| 54 |
|
| 55 |
return "\n".join(extracted_text)
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
-
|
| 59 |
-
return f"Recognition Error: {str(e)}\nTip: This usually happens with low-quality scans. Try cropping to a smaller area."
|
| 60 |
|
| 61 |
# UI
|
| 62 |
demo = gr.Interface(
|
| 63 |
fn=process_bank_form,
|
| 64 |
-
inputs=gr.Image(type="pil"),
|
| 65 |
-
outputs=gr.Textbox(label="
|
| 66 |
-
title="🏦
|
| 67 |
)
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
-
demo.launch()
|
|
|
|
| 13 |
return "Please upload an image."
|
| 14 |
|
| 15 |
try:
|
| 16 |
+
# 1. LAZY LOAD MODEL (Standard 2026 init)
|
| 17 |
if ocr_instance is None:
|
| 18 |
ocr_instance = PaddleOCR(
|
| 19 |
lang='en',
|
| 20 |
+
ocr_version='PP-OCRv5', # Using latest v5 for best handwriting
|
| 21 |
use_angle_cls=True
|
| 22 |
)
|
| 23 |
|
| 24 |
+
# 2. IMAGE STANDARDIZATION
|
| 25 |
img = image.convert("RGB")
|
| 26 |
+
img = ImageOps.exif_transpose(img) # Prevents 'sideways' photo errors
|
| 27 |
img_array = np.array(img)
|
| 28 |
|
| 29 |
+
# 3. RUN OCR
|
| 30 |
result = ocr_instance.ocr(img_array)
|
| 31 |
|
| 32 |
+
# 4. DEEP DEFENSIVE PARSING (Fixes 'index out of range')
|
| 33 |
+
if not result or not isinstance(result, list) or len(result) == 0 or result[0] is None:
|
| 34 |
+
return "No text detected. Try a closer, clearer photo."
|
| 35 |
|
| 36 |
extracted_text = []
|
| 37 |
|
| 38 |
+
# result[0] is the list of detected lines/boxes
|
| 39 |
for line in result[0]:
|
| 40 |
+
# Each 'line' must be a list: [ [coordinates], [text, confidence] ]
|
| 41 |
if isinstance(line, list) and len(line) >= 2:
|
| 42 |
content = line[1] # This should be [text, confidence]
|
| 43 |
|
| 44 |
+
# Check if 'content' has a list with at least the text string
|
| 45 |
+
if isinstance(content, (list, tuple)) and len(content) > 0:
|
| 46 |
text_val = content[0]
|
| 47 |
|
| 48 |
+
# Ensure it's a valid string and skip if empty
|
| 49 |
if text_val and isinstance(text_val, str):
|
| 50 |
extracted_text.append(text_val.strip())
|
| 51 |
|
| 52 |
if not extracted_text:
|
| 53 |
+
return "AI found text boxes but couldn't read characters. Try cropping the image."
|
| 54 |
|
| 55 |
return "\n".join(extracted_text)
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
+
return f"System Error: {str(e)}\nTip: If it persists, use 'Factory Reboot' in Space Settings."
|
|
|
|
| 59 |
|
| 60 |
# UI
|
| 61 |
demo = gr.Interface(
|
| 62 |
fn=process_bank_form,
|
| 63 |
+
inputs=gr.Image(type="pil", label="Upload Bank Form"),
|
| 64 |
+
outputs=gr.Textbox(label="Extracted Text", lines=20),
|
| 65 |
+
title="🏦 Global Bank OCR"
|
| 66 |
)
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
+
demo.launch(max_threads=1)
|