SIngtel-Bill-Scanner / auto_test.py
Cosmo125's picture
Upload 26 files
795183d verified
"""
Auto-running version of the quick test script
This will automatically proceed with the model download and testing
"""
from transformers import pipeline
from PIL import Image, ImageDraw, ImageFont
import sys
def test_trocr_with_progress():
"""
Test TrOCR model with progress updates
"""
print("πŸš€ Starting TrOCR Model Test...")
print("=" * 50)
try:
# Step 1: Create test image
print("πŸ“ Step 1: Creating test bill image...")
img = Image.new('RGB', (400, 150), color='white')
draw = ImageDraw.Draw(img)
# Draw bill-like content
try:
font = ImageFont.load_default()
except:
font = None
# Draw typical bill text
draw.text((20, 20), "SINGTEL BILL", fill='black', font=font)
draw.text((20, 50), "Account: 123-456-789", fill='black', font=font)
draw.text((20, 70), "Total Amount: $123.45", fill='black', font=font)
draw.text((20, 90), "Due Date: 31/07/2025", fill='black', font=font)
draw.text((20, 110), "Thank you for choosing Singtel", fill='black', font=font)
# Save the test image
img.save("test_singtel_bill.png")
print("βœ… Test image created: test_singtel_bill.png")
# Step 2: Initialize model (this is where download happens)
print("\nπŸ“₯ Step 2: Loading TrOCR model...")
print("⏳ Note: First time will download ~1.3GB (please wait...)")
print("This may take 5-10 minutes depending on your internet speed")
print("-" * 50)
# Initialize the pipeline - download happens here
pipe = pipeline("image-to-text", model="microsoft/trocr-base-handwritten")
print("βœ… Model loaded successfully!")
# Step 3: Process the test image
print("\nπŸ” Step 3: Processing test image...")
result = pipe(img)
extracted_text = result[0]['generated_text']
# Display results
print("\n" + "πŸŽ‰ RESULTS:")
print("=" * 30)
print(f"Original text in image:")
print(" SINGTEL BILL")
print(" Account: 123-456-789")
print(" Total Amount: $123.45")
print(" Due Date: 31/07/2025")
print(" Thank you for choosing Singtel")
print()
print(f"AI Extracted text: '{extracted_text}'")
print()
# Step 4: Test with actual processing
print("πŸ”§ Step 4: Testing bill processing functions...")
# Simple parsing test
import re
# Look for amount
amount_match = re.search(r'\$([0-9.]+)', extracted_text)
found_amount = amount_match.group(1) if amount_match else "Not detected"
# Look for numbers (could be account or amounts)
numbers = re.findall(r'\d+', extracted_text)
print(f" Detected amount: ${found_amount}")
print(f" Detected numbers: {numbers}")
print("\nβœ… SUCCESS! The TrOCR model is working correctly!")
print("🎯 You can now process real Singtel bill images!")
return True
except Exception as e:
print(f"\n❌ Error during testing: {e}")
print("\nTroubleshooting tips:")
print("1. Check your internet connection")
print("2. Make sure you have enough disk space (~2GB)")
print("3. Try running again - downloads can sometimes fail")
return False
def show_next_steps():
"""Show what to do after successful test"""
print("\n" + "🎯 NEXT STEPS:")
print("=" * 40)
print("1. βœ… Model is now cached and ready for use")
print("2. πŸ“Έ Take a photo of your Singtel bill")
print("3. πŸš€ Run the full scanner:")
print(" python singtel_scanner.py")
print()
print("4. πŸ”§ Or use the model directly in your code:")
print("""
from transformers import pipeline
pipe = pipeline("image-to-text", model="microsoft/trocr-base-handwritten")
result = pipe(Image.open("your_bill.jpg"))
text = result[0]['generated_text']
""")
print("\n5. πŸ“ Models are cached in: %USERPROFILE%\\.cache\\huggingface\\")
print("6. πŸš€ Future runs will be instant (no re-download)")
if __name__ == "__main__":
print("SINGTEL BILL SCANNER - AUTO TEST")
print("This will automatically download and test the AI model")
print("=" * 60)
# Run the test automatically
success = test_trocr_with_progress()
if success:
show_next_steps()
print("\nπŸŽ‰ CONGRATULATIONS! Your Singtel Bill Scanner is ready!")
else:
print("\nπŸ”§ Setup needs attention. Check the error messages above.")
print("\n" + "=" * 60)
input("Press Enter to exit...")