|
"""
|
|
Final verification test - Models are downloaded, let's test them!
|
|
"""
|
|
|
|
from transformers import pipeline
|
|
from PIL import Image
|
|
import time
|
|
|
|
def test_downloaded_models():
|
|
"""Test the downloaded models quickly"""
|
|
print("π TESTING DOWNLOADED MODELS")
|
|
print("=" * 40)
|
|
|
|
try:
|
|
|
|
print("π₯ Loading TrOCR model...")
|
|
start_time = time.time()
|
|
|
|
pipe = pipeline("image-to-text", model="microsoft/trocr-base-handwritten")
|
|
|
|
load_time = time.time() - start_time
|
|
print(f"β
Model loaded in {load_time:.1f} seconds")
|
|
|
|
|
|
print("\nπ Processing test image...")
|
|
|
|
if Image.open("test_singtel_bill.png"):
|
|
img = Image.open("test_singtel_bill.png")
|
|
print("β
Test image loaded successfully")
|
|
|
|
|
|
start_time = time.time()
|
|
result = pipe(img)
|
|
process_time = time.time() - start_time
|
|
|
|
extracted_text = result[0]['generated_text']
|
|
|
|
print(f"β
Processing completed in {process_time:.1f} seconds")
|
|
print(f"\nπ EXTRACTED TEXT:")
|
|
print(f"'{extracted_text}'")
|
|
|
|
|
|
print(f"\nπ§ TESTING BILL PARSING:")
|
|
|
|
|
|
import re
|
|
|
|
|
|
if "singtel" in extracted_text.lower():
|
|
print("β
'Singtel' detected in text")
|
|
else:
|
|
print("β 'Singtel' not clearly detected")
|
|
|
|
|
|
numbers = re.findall(r'\d+', extracted_text)
|
|
if numbers:
|
|
print(f"β
Numbers detected: {numbers}")
|
|
else:
|
|
print("β No numbers detected")
|
|
|
|
|
|
currency = re.findall(r'\$[\d.]+', extracted_text)
|
|
if currency:
|
|
print(f"β
Currency amounts: {currency}")
|
|
else:
|
|
print("β οΈ No currency patterns detected")
|
|
|
|
print(f"\nπ SUCCESS! Your TrOCR model is working!")
|
|
return True
|
|
|
|
else:
|
|
print("β Could not load test image")
|
|
return False
|
|
|
|
except Exception as e:
|
|
print(f"β Error: {e}")
|
|
return False
|
|
|
|
def test_real_bill_processing():
|
|
"""Show how to process a real bill"""
|
|
print(f"\nπ― READY FOR REAL BILLS!")
|
|
print("=" * 30)
|
|
print("To process your Singtel bill:")
|
|
print()
|
|
print("1. πΈ Take a clear photo of your bill")
|
|
print("2. πΎ Save it as 'my_bill.jpg' in this folder")
|
|
print("3. π Run this code:")
|
|
print()
|
|
print("```python")
|
|
print("from transformers import pipeline")
|
|
print("from PIL import Image")
|
|
print()
|
|
print("# Load the model (fast now - already downloaded!)")
|
|
print("pipe = pipeline('image-to-text', model='microsoft/trocr-base-handwritten')")
|
|
print()
|
|
print("# Process your bill")
|
|
print("image = Image.open('my_bill.jpg')")
|
|
print("result = pipe(image)")
|
|
print("text = result[0]['generated_text']")
|
|
print()
|
|
print("print(f'Extracted: {text}')")
|
|
print("```")
|
|
print()
|
|
print("4. π§ Use singtel_scanner.py for advanced parsing!")
|
|
|
|
if __name__ == "__main__":
|
|
print("FINAL MODEL VERIFICATION")
|
|
print("Models are downloaded - testing now!")
|
|
print("=" * 50)
|
|
|
|
success = test_downloaded_models()
|
|
|
|
if success:
|
|
test_real_bill_processing()
|
|
print(f"\nβ
COMPLETE! Your Singtel Bill Scanner is ready to use!")
|
|
print(f"β‘ All future runs will be instant (models cached)")
|
|
else:
|
|
print(f"\nβ Something went wrong. Check the error above.")
|
|
|
|
print("\n" + "=" * 50)
|
|
input("Press Enter to finish...")
|
|
|