master_adapt_detect/test_optimized_canny.py
2025-10-01 14:32:55 -05:00

99 lines
No EOL
2.9 KiB
Python

#!/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())