| import sys
|
| sys.path.append('src')
|
|
|
| from preprocessing import load_image, convert_to_grayscale, remove_noise, binarize, preprocess_pipeline
|
| import numpy as np
|
| import matplotlib.pyplot as plt
|
|
|
|
|
| print("Test 1: Loading receipt1.jpg...")
|
| image = load_image('data/raw/receipt1.jpg')
|
| print(f"✅ Success! Image shape: {image.shape}")
|
| print(f" Data type: {image.dtype}")
|
| print(f" Value range: {image.min()} to {image.max()}")
|
|
|
|
|
| print("\nTest 2: Displaying image...")
|
| plt.imshow(image)
|
| plt.title("Loaded Receipt")
|
| plt.axis('off')
|
| plt.show()
|
| print("✅ If you see the receipt image, it worked!")
|
|
|
|
|
| print("\nTest 3: Testing error handling...")
|
| try:
|
| load_image('data/raw/fake_image.jpg')
|
| print("❌ Should have raised FileNotFoundError!")
|
| except FileNotFoundError as e:
|
| print(f"✅ Correctly raised error: {e}")
|
|
|
|
|
| print("\nTest 4: Converting to grayscale...")
|
| gray = convert_to_grayscale(image)
|
| print(f"✅ Success! Grayscale shape: {gray.shape}")
|
| print(f" Original had 3 channels, now has: {len(gray.shape)} dimensions")
|
|
|
|
|
| fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))
|
| ax1.imshow(image)
|
| ax1.set_title("Original (RGB)")
|
| ax1.axis('off')
|
|
|
| ax2.imshow(gray, cmap='gray')
|
| ax2.set_title("Grayscale")
|
| ax2.axis('off')
|
|
|
| plt.tight_layout()
|
| plt.show()
|
|
|
|
|
| print("\nTest 5: Converting already-grayscale image...")
|
| gray_again = convert_to_grayscale(gray)
|
| print(f"✅ Returned without error: {gray_again.shape}")
|
| assert gray_again is gray, "Should return same object if already grayscale"
|
| print("✅ Correctly returned the same image!")
|
|
|
| print("\n🎉 Grayscale tests passed!")
|
|
|
|
|
| print("\nTest 6: Simple binarization...")
|
| binary_simple = binarize(gray, method='simple')
|
| print(f"✅ Success! Binary shape: {binary_simple.shape}")
|
| print(f" Unique values: {np.unique(binary_simple)}")
|
|
|
|
|
| print("\nTest 7: Adaptive binarization...")
|
| binary_adaptive = binarize(gray, method='adaptive', block_size=11, C=2)
|
| print(f"✅ Success! Binary shape: {binary_adaptive.shape}")
|
| print(f" Unique values: {np.unique(binary_adaptive)}")
|
|
|
|
|
| fig, axes = plt.subplots(2, 2, figsize=(12, 10))
|
|
|
| axes[0, 0].imshow(image)
|
| axes[0, 0].set_title("1. Original (RGB)")
|
| axes[0, 0].axis('off')
|
|
|
| axes[0, 1].imshow(gray, cmap='gray')
|
| axes[0, 1].set_title("2. Grayscale")
|
| axes[0, 1].axis('off')
|
|
|
| axes[1, 0].imshow(binary_simple, cmap='gray')
|
| axes[1, 0].set_title("3. Simple Threshold")
|
| axes[1, 0].axis('off')
|
|
|
| axes[1, 1].imshow(binary_adaptive, cmap='gray')
|
| axes[1, 1].set_title("4. Adaptive Threshold")
|
| axes[1, 1].axis('off')
|
|
|
| plt.tight_layout()
|
| plt.show()
|
|
|
|
|
| print("\nTest 8: Testing error handling...")
|
| try:
|
| binarize(image, method='adaptive')
|
| print("❌ Should have raised ValueError!")
|
| except ValueError as e:
|
| print(f"✅ Correctly raised error: {e}")
|
|
|
| print("\n🎉 Binarization tests passed!")
|
|
|
|
|
| print("\nTest 9: Noise removal...")
|
| denoised = remove_noise(gray, kernel_size=3)
|
| print(f"✅ Success! Denoised shape: {denoised.shape}")
|
|
|
|
|
| denoised_light = remove_noise(gray, kernel_size=3)
|
| denoised_heavy = remove_noise(gray, kernel_size=7)
|
|
|
|
|
| fig, axes = plt.subplots(1, 3, figsize=(15, 5))
|
|
|
| axes[0].imshow(gray, cmap='gray')
|
| axes[0].set_title("Original Grayscale")
|
| axes[0].axis('off')
|
|
|
| axes[1].imshow(denoised_light, cmap='gray')
|
| axes[1].set_title("Denoised (kernel=3)")
|
| axes[1].axis('off')
|
|
|
| axes[2].imshow(denoised_heavy, cmap='gray')
|
| axes[2].set_title("Denoised (kernel=7)")
|
| axes[2].axis('off')
|
|
|
| plt.tight_layout()
|
| plt.show()
|
| print(" Notice: kernel=7 is blurrier but removes more noise")
|
|
|
|
|
| print("\nTest 10: Noise removal error handling...")
|
| try:
|
| remove_noise(gray, kernel_size=4)
|
| print("❌ Should have raised ValueError!")
|
| except ValueError as e:
|
| print(f"✅ Correctly raised error: {e}")
|
|
|
| print("\n🎉 Noise removal tests passed!")
|
|
|
|
|
| print("\nTest 11: Full preprocessing pipeline...")
|
|
|
|
|
| full_processed = preprocess_pipeline(image,
|
| steps=['grayscale', 'denoise', 'binarize'],
|
| denoise_kernel=3,
|
| binarize_method='adaptive')
|
| print(f"✅ Full pipeline success! Shape: {full_processed.shape}")
|
|
|
|
|
| clean_processed = preprocess_pipeline(image,
|
| steps=['grayscale', 'binarize'],
|
| binarize_method='adaptive')
|
| print(f"✅ Clean pipeline success! Shape: {clean_processed.shape}")
|
|
|
|
|
| fig, axes = plt.subplots(1, 3, figsize=(15, 5))
|
|
|
| axes[0].imshow(image)
|
| axes[0].set_title("Original")
|
| axes[0].axis('off')
|
|
|
| axes[1].imshow(full_processed, cmap='gray')
|
| axes[1].set_title("Full Pipeline\n(grayscale → denoise → binarize)")
|
| axes[1].axis('off')
|
|
|
| axes[2].imshow(clean_processed, cmap='gray')
|
| axes[2].set_title("Clean Pipeline\n(grayscale → binarize)")
|
| axes[2].axis('off')
|
|
|
| plt.tight_layout()
|
| plt.show()
|
|
|
| print("\n🎉 Pipeline tests passed!")
|
|
|
| print("\n🎉 All tests passed!")
|
|
|