#!/usr/bin/env python3 """ Test script for optimized Canny detection method """ import sys import os import cv2 import numpy as np from pathlib import Path from panel_splitter import PanelSplitter def test_14_panel_splitting(): """Test 14-panel splitting with optimized Canny detection""" print("=" * 60) print("TESTING 14-PANEL SPLITTING WITH OPTIMIZED CANNY") print("=" * 60) # Initialize splitter with debug mode splitter = PanelSplitter(debug=True) # Target layout file layout_path = Path("layouts") / "6786505.jpg" if not layout_path.exists(): print(f"โŒ ERROR: Layout file {layout_path} not found!") return False print(f"๐Ÿ“ Testing with: {layout_path.name}") # Load and examine the image image = cv2.imread(str(layout_path)) if image is None: print(f"โŒ ERROR: Could not load image {layout_path}") return False height, width = image.shape[:2] print(f"๐Ÿ“ Image dimensions: {width}x{height}") # Test with target count 14 target_count = 14 print(f"\n๐ŸŽฏ Testing with target count: {target_count}") print("โ”€" * 40) # Split the layout splits = splitter.split_panels(str(layout_path), target_count) print(f"\n๐Ÿ“Š RESULTS:") print(f"Generated {len(splits)} splits (target: {target_count})") # Check if we got exactly 14 panels success = len(splits) == target_count if success: print(f"โœ… SUCCESS: Generated exactly {target_count} splits!") else: print(f"โŒ FAILURE: Generated {len(splits)} splits instead of {target_count}") # Save split images if len(splits) > 0: splits_dir = Path("test_splits") splits_dir.mkdir(exist_ok=True) for i, split in enumerate(splits): split_filename = splits_dir / f"6786505_14panel_split_{i+1:02d}.jpg" cv2.imwrite(str(split_filename), split['image']) print(f"\n๐Ÿ’พ Saved {len(splits)} split images to test_splits/") # Show split details print("\n๐Ÿ“‹ Split details:") for i, split in enumerate(splits): x, y, w, h = split['bounds'] print(f" Split {i+1:2d}: [{x:4d}, {y:4d}, {w:4d}, {h:4d}] conf={split['confidence']:.3f}") print("\n" + "=" * 60) if success: print("๐ŸŽ‰ TEST PASSED: 14-panel splitting is working!") else: print("โŒ TEST FAILED: 14-panel splitting needs adjustment") print("=" * 60) return success def main(): """Main test function""" print("๐Ÿงช STARTING OPTIMIZED CANNY 14-PANEL TEST") success = test_14_panel_splitting() if success: print("\n๐ŸŽ‰ SUCCESS: Optimized Canny detection produces exactly 14 panels!") return 0 else: print("\nโŒ FAILURE: Optimized Canny detection needs further tuning") return 1 if __name__ == "__main__": sys.exit(main())