shayansjm commited on
Commit
d3be5ae
·
verified ·
1 Parent(s): fc9a315

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -20
app.py CHANGED
@@ -13,58 +13,57 @@ def process_bank_form(image):
13
  return "Please upload an image."
14
 
15
  try:
16
- # 1. Initialize if not already done
17
  if ocr_instance is None:
18
  ocr_instance = PaddleOCR(
19
  lang='en',
20
- ocr_version='PP-OCRv4',
21
  use_angle_cls=True
22
  )
23
 
24
- # 2. Standardize Image
25
  img = image.convert("RGB")
26
- img = ImageOps.exif_transpose(img)
27
  img_array = np.array(img)
28
 
29
- # 3. Run OCR
30
  result = ocr_instance.ocr(img_array)
31
 
32
- # 4. DEFENSIvE PARSING (Fixes 'index out of range')
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 MUST be a list with at least 2 elements: [coordinates, [text, conf]]
41
  if isinstance(line, list) and len(line) >= 2:
42
  content = line[1] # This should be [text, confidence]
43
 
44
- # Verify content is a list and has at least 1 element (the text)
45
- if isinstance(content, (list, tuple)) and len(content) >= 1:
46
  text_val = content[0]
47
 
48
- # Finally, ensure it's a valid string before adding
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 saw boxes but couldn't recognize any characters. Try more light."
54
 
55
  return "\n".join(extracted_text)
56
 
57
  except Exception as e:
58
- # Catch the specific error and give better feedback
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="Result", lines=20),
66
- title="🏦 Secure Bank OCR"
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)