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

142 lines
No EOL
4.5 KiB
Python

#!/usr/bin/env python3
"""
Simple test script to verify panel splitting functionality
Tests the panel splitting with 6786505.jpg (horizontal strip layout)
"""
import sys
import os
import cv2
import numpy as np
from pathlib import Path
from panel_splitter import PanelSplitter
def test_simple_splitting():
"""Test splitting without OpenAI guidance"""
print("=" * 60)
print("TESTING SIMPLE PANEL SPLITTING")
print("=" * 60)
# Initialize splitter with debug mode
splitter = PanelSplitter(debug=True)
# Target layout file - this is a horizontal strip with many panels
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 different target counts
test_counts = [5, 8, 10, 12]
for target_count in test_counts:
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"Generated {len(splits)} splits")
if len(splits) > 0:
print("✅ Successfully generated splits!")
# Save split images
splits_dir = Path("test_splits")
splits_dir.mkdir(exist_ok=True)
for i, split in enumerate(splits):
split_filename = splits_dir / f"6786505_target{target_count}_split_{i+1:02d}.jpg"
cv2.imwrite(str(split_filename), split['image'])
print(f" Saved {len(splits)} split images to test_splits/")
# Show 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}")
else:
print("❌ No splits generated")
print("\n" + "=" * 60)
print("🎉 SIMPLE SPLITTING TEST COMPLETED!")
print("Check the test_splits/ directory for generated images.")
print("=" * 60)
return True
def test_individual_methods():
"""Test individual splitting methods"""
print("\n" + "=" * 60)
print("TESTING INDIVIDUAL SPLITTING METHODS")
print("=" * 60)
splitter = PanelSplitter(debug=True)
layout_path = Path("layouts") / "6786505.jpg"
image = cv2.imread(str(layout_path))
if image is None:
print("❌ Could not load image")
return False
target_count = 8
methods = [
splitter._enhanced_gradient_analysis,
splitter._advanced_canny_detection,
splitter._template_matching_method,
splitter._contour_analysis_method,
splitter._texture_analysis_method,
splitter._clustering_method
]
for method in methods:
print(f"\n🔬 Testing {method.__name__}...")
try:
result = method(image, target_count)
if result:
print(f" ✅ Generated {len(result)} boundaries")
for i, boundary in enumerate(result):
bounds = boundary['bounds']
print(f" {i+1}: [{bounds[0]:4d}, {bounds[1]:4d}, {bounds[2]:4d}, {bounds[3]:4d}] conf={boundary['confidence']:.3f}")
else:
print(" ❌ No boundaries generated")
except Exception as e:
print(f" ❌ Error: {e}")
return True
def main():
"""Main test function"""
print("🧪 STARTING SIMPLE PANEL SPLITTING TEST")
# Test basic splitting
simple_success = test_simple_splitting()
# Test individual methods
methods_success = test_individual_methods()
print(f"\n📊 FINAL RESULTS:")
print(f"Simple splitting: {'✅ PASSED' if simple_success else '❌ FAILED'}")
print(f"Individual methods: {'✅ PASSED' if methods_success else '❌ FAILED'}")
if simple_success and methods_success:
print("\n🎉 ALL TESTS PASSED! The panel splitting is working correctly.")
return 0
else:
print("\n❌ Some tests failed. Please check the output above.")
return 1
if __name__ == "__main__":
sys.exit(main())