File size: 3,792 Bytes
795183d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""

Simple test script for Singtel Bill Scanner models

This script demonstrates basic usage without heavy model downloads

"""

from transformers import pipeline
from PIL import Image
import requests
import io

def test_trocr_simple():
    """

    Test TrOCR model with a simple example

    """
    print("Testing TrOCR model...")
    
    try:
        # Create a simple test image with text
        from PIL import Image, ImageDraw, ImageFont
        
        # Create a simple image with text
        img = Image.new('RGB', (300, 100), color='white')
        draw = ImageDraw.Draw(img)
        
        # Draw some text
        try:
            # Try to use a default font
            font = ImageFont.load_default()
        except:
            font = None
            
        draw.text((10, 30), "Total: $123.45", fill='black', font=font)
        draw.text((10, 50), "Due: 2025-07-31", fill='black', font=font)
        
        # Save test image
        img.save("test_bill.png")
        print("Created test bill image: test_bill.png")
        
        # Initialize TrOCR pipeline
        print("Loading TrOCR pipeline...")
        pipe = pipeline("image-to-text", model="microsoft/trocr-base-handwritten")
        
        # Process the test image
        print("Processing test image...")
        result = pipe(img)
        
        print(f"Extracted text: {result[0]['generated_text']}")
        
        return True
        
    except Exception as e:
        print(f"Error in TrOCR test: {e}")
        return False

def quick_usage_demo():
    """

    Show quick usage examples without running heavy models

    """
    print("\n=== Quick Usage Demo ===")
    
    print("\n1. Basic TrOCR Usage:")
    print("""

from transformers import pipeline

from PIL import Image



# Initialize pipeline

pipe = pipeline("image-to-text", model="microsoft/trocr-base-handwritten")



# Process image

image = Image.open("your_bill.jpg")

result = pipe(image)

text = result[0]['generated_text']

    """)
    
    print("\n2. Bill Processing Function:")
    print("""

def process_bill(image_path):

    pipe = pipeline("image-to-text", model="microsoft/trocr-base-handwritten")

    image = Image.open(image_path)

    result = pipe(image)

    

    # Extract bill information

    text = result[0]['generated_text']

    

    # Parse specific information

    total_amount = extract_amount(text)

    due_date = extract_date(text)

    account_number = extract_account(text)

    

    return {

        'total': total_amount,

        'due_date': due_date,

        'account': account_number,

        'raw_text': text

    }

    """)

if __name__ == "__main__":
    print("=== Singtel Bill Scanner - Quick Test ===")
    
    # Show usage demo first
    quick_usage_demo()
    
    # Ask user if they want to run the actual test
    print("\n" + "="*50)
    print("Would you like to run the actual TrOCR test?")
    print("Note: This will download the model (~1.3GB) on first run")
    print("="*50)
    
    user_input = input("Run test? (y/n): ").lower().strip()
    
    if user_input == 'y' or user_input == 'yes':
        success = test_trocr_simple()
        if success:
            print("\n✅ Test completed successfully!")
            print("You can now use the models with your bill images.")
        else:
            print("\n❌ Test failed. Check the error messages above.")
    else:
        print("\nTest skipped. Refer to the usage examples above.")
        print("Run this script again with 'y' when you're ready to test.")
    
    print("\n📚 For detailed documentation, see README.md")
    print("📁 Your models will be cached in ~/.cache/huggingface/ for future use")