132 lines
No EOL
4.6 KiB
Python
132 lines
No EOL
4.6 KiB
Python
"""
|
|
Simple Panel Splitter for Master Adapt Detect
|
|
|
|
This module provides a simple panel splitting approach that evenly divides layouts
|
|
into panels based on the number of panels detected by OpenAI analysis. It's designed
|
|
for use with hybrid mode as an alternative to split-advanced.
|
|
"""
|
|
|
|
import cv2
|
|
import numpy as np
|
|
from typing import List, Tuple, Optional, Dict
|
|
import os
|
|
from pathlib import Path
|
|
|
|
|
|
class SimplePanelSplitter:
|
|
"""
|
|
Simple panel splitter that evenly divides layouts into panels based on OpenAI analysis count.
|
|
|
|
This splitter assumes panels are arranged in a grid pattern and divides the image
|
|
evenly based on the detected panel count. It's optimized for speed and simplicity
|
|
rather than precision.
|
|
"""
|
|
|
|
def __init__(self, debug: bool = False):
|
|
"""
|
|
Initialize the simple panel splitter.
|
|
|
|
Args:
|
|
debug: Enable debug output and visualization
|
|
"""
|
|
self.debug = debug
|
|
|
|
def _determine_grid_layout(self, panel_count: int) -> Tuple[int, int]:
|
|
"""
|
|
Determine the grid layout for vertical splitting of horizontal layouts.
|
|
For wide horizontal layouts, this creates even vertical sections.
|
|
|
|
Args:
|
|
panel_count: Number of panels to arrange
|
|
|
|
Returns:
|
|
Tuple of (rows, cols) - always (1, panel_count) for horizontal splitting
|
|
"""
|
|
if panel_count <= 0:
|
|
return (1, 1)
|
|
else:
|
|
# Always split horizontally into vertical sections
|
|
# This creates even vertical strips across the width
|
|
return (1, panel_count)
|
|
|
|
def split_panels(self, layout_path: str, panel_count: int) -> List[Dict]:
|
|
"""
|
|
Split a layout image into individual panels using simple even division.
|
|
|
|
Args:
|
|
layout_path: Path to the layout image
|
|
panel_count: Number of panels to split into (from OpenAI analysis)
|
|
|
|
Returns:
|
|
List of dictionaries containing image data and metadata
|
|
"""
|
|
if self.debug:
|
|
print(f"SimplePanelSplitter: Splitting {layout_path} into {panel_count} panels")
|
|
|
|
# Load the image
|
|
image = cv2.imread(layout_path)
|
|
if image is None:
|
|
print(f"Error: Could not load image {layout_path}")
|
|
return []
|
|
|
|
height, width = image.shape[:2]
|
|
|
|
# Determine grid layout
|
|
rows, cols = self._determine_grid_layout(panel_count)
|
|
if self.debug:
|
|
print(f"SimplePanelSplitter: Using {rows}x{cols} grid layout")
|
|
|
|
# Calculate panel dimensions
|
|
panel_height = height // rows
|
|
panel_width = width // cols
|
|
|
|
splits = []
|
|
panel_index = 0
|
|
|
|
# Extract panels in row-major order
|
|
for row in range(rows):
|
|
for col in range(cols):
|
|
if panel_index >= panel_count:
|
|
break
|
|
|
|
# Calculate panel boundaries
|
|
y_start = row * panel_height
|
|
y_end = (row + 1) * panel_height if row < rows - 1 else height
|
|
x_start = col * panel_width
|
|
x_end = (col + 1) * panel_width if col < cols - 1 else width
|
|
|
|
# Extract panel
|
|
panel_image = image[y_start:y_end, x_start:x_end]
|
|
|
|
# Create panel dictionary in expected format
|
|
panel_dict = {
|
|
'image': panel_image,
|
|
'bounds': (x_start, y_start, x_end - x_start, y_end - y_start),
|
|
'confidence': 1.0, # High confidence for simple even division
|
|
'method': 'simple_even_division'
|
|
}
|
|
splits.append(panel_dict)
|
|
|
|
if self.debug:
|
|
print(f"SimplePanelSplitter: Extracted panel {panel_index + 1} "
|
|
f"at ({x_start},{y_start}) to ({x_end},{y_end})")
|
|
|
|
panel_index += 1
|
|
|
|
if panel_index >= panel_count:
|
|
break
|
|
|
|
if self.debug:
|
|
print(f"SimplePanelSplitter: Successfully split into {len(splits)} panels")
|
|
|
|
return splits
|
|
|
|
def cleanup_split_files(self, splits: List[Dict]) -> None:
|
|
"""
|
|
Clean up method for compatibility - simple splitter doesn't create files.
|
|
|
|
Args:
|
|
splits: List of split panel dictionaries (no cleanup needed)
|
|
"""
|
|
if self.debug:
|
|
print(f"SimplePanelSplitter: No cleanup needed - {len(splits)} panels processed in memory") |