Ray1ee01's picture
Upload folder using huggingface_hub
2cf467c verified
Raw
History Blame Contribute Delete
3.74 kB
"""Backward compatibility wrapper for sdf module.
This module re-exports all functions from the sdf subpackage for backward compatibility.
New code should import directly from the sdf subpackage instead.
"""
from layout_system.sdf import optimize
from layout_system.sdf.core import (
binary_to_sdf_norm,
load_binary_mask_from_rgba,
)
from layout_system.sdf.visualization import (
visualize_sdf,
visualize_sdf_norm_and_softmask,
)
if __name__ == "__main__":
import json
# Visualize SDFs
print("Loading images and computing SDFs...")
mask1 = load_binary_mask_from_rgba("chart.png")
mask2 = load_binary_mask_from_rgba("pictogram.png")
sdf1_norm = binary_to_sdf_norm(mask1, pad=16)
sdf2_norm = binary_to_sdf_norm(mask2, pad=16)
print(f"SDF 1 shape: {sdf1_norm.shape}, range: [{sdf1_norm.min():.3f}, {sdf1_norm.max():.3f}]")
print(f"SDF 2 shape: {sdf2_norm.shape}, range: [{sdf2_norm.min():.3f}, {sdf2_norm.max():.3f}]")
# Use a smaller range (0.05) for finer detail around the boundary
visualize_sdf(sdf1_norm, sdf2_norm, mask1, mask2,
save_path="sdf_visualization.png", sdf_range=0.05)
# Visualize SDF normalization and softmask conversion for both images
print("\nVisualizing SDF normalization and softmask conversion...")
# For mask1 (chart) - use a sample bbox covering most of the image
H1, W1 = mask1.shape
bbox1_sample = (W1 * 0.1, H1 * 0.1, W1 * 0.8, H1 * 0.8)
visualize_sdf_norm_and_softmask(sdf1_norm, mask1,
bbox=bbox1_sample,
container_size=(W1, H1),
save_path="sdf_norm_softmask_chart.png")
# For mask2 (pictogram) - use a sample bbox covering most of the image
H2, W2 = mask2.shape
bbox2_sample = (W2 * 0.1, H2 * 0.1, W2 * 0.8, H2 * 0.8)
visualize_sdf_norm_and_softmask(sdf2_norm, mask2,
bbox=bbox2_sample,
container_size=(W2, H2),
save_path="sdf_norm_softmask_pictogram.png")
# Load reference layout from JSON
print("\n" + "="*50)
print("Loading reference layout from simplified.json...")
print("="*50)
json_path = "simplified.json"
with open(json_path, 'r') as f:
json_data = json.load(f)
# Extract reference bboxes from children
reference_bboxes = []
reference_parent_bbox = None
if "children" in json_data and len(json_data["children"]) >= 2:
# Extract parent container bbox
parent_bbox = json_data.get("bbox", {})
reference_parent_bbox = (
parent_bbox.get("x", 0),
parent_bbox.get("y", 0),
parent_bbox.get("width", 1000),
parent_bbox.get("height", 1000)
)
# Extract children bboxes (reference layout)
for child in json_data["children"]:
child_bbox = child.get("bbox", {})
ref_bbox = (
child_bbox.get("x", 0),
child_bbox.get("y", 0),
child_bbox.get("width", 100),
child_bbox.get("height", 100)
)
reference_bboxes.append(ref_bbox)
print(f"Reference parent bbox: {reference_parent_bbox}")
print(f"Reference bboxes: {reference_bboxes}")
# Run optimization
print("\n" + "="*50)
print("Running optimization...")
print("="*50)
optimize(png_list=["chart.png", "pictogram.png"],
reference_bboxes=reference_bboxes,
reference_parent_bbox=reference_parent_bbox,
w_similarity=1000.0)