Spaces:
Runtime error
Runtime error
File size: 10,359 Bytes
c914720 39e0cb0 60fb79c 39e0cb0 60fb79c 39e0cb0 c914720 39e0cb0 c914720 39e0cb0 c914720 39e0cb0 c914720 39e0cb0 60fb79c 39e0cb0 60fb79c 39e0cb0 60fb79c 39e0cb0 60fb79c c914720 39e0cb0 60fb79c 39e0cb0 c914720 39e0cb0 c914720 39e0cb0 c914720 60fb79c c914720 39e0cb0 c914720 60fb79c c914720 39e0cb0 c914720 60fb79c 39e0cb0 60fb79c 39e0cb0 60fb79c 39e0cb0 c914720 39e0cb0 c914720 39e0cb0 c914720 39e0cb0 c914720 39e0cb0 c914720 39e0cb0 c914720 39e0cb0 c914720 39e0cb0 60fb79c 39e0cb0 c914720 60fb79c 39e0cb0 c914720 39e0cb0 c914720 39e0cb0 60fb79c c914720 60fb79c 39e0cb0 c914720 39e0cb0 c914720 39e0cb0 c914720 39e0cb0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | import os
from PIL import Image, ImageStat
Image.MAX_IMAGE_PIXELS = 200_000_000
from engine.hoodie_rules import AOP_HOODIE_MAPPING_RULES
from engine.hf_processors import run_optional_ai_processors
def norm(name):
return name.lower().replace(" ", "_")
def pct_box(w, h, x1, y1, x2, y2):
return (int(w * x1), int(h * y1), int(w * x2), int(h * y2))
def region_score(img):
stat = ImageStat.Stat(img.convert("RGB"))
contrast = sum(stat.stddev) / 3
saturation_hint = max(stat.mean) - min(stat.mean)
brightness = sum(stat.mean) / 3
return contrast + saturation_hint + (255 - abs(140 - brightness)) * 0.12
def analyze_regions(image, prompt, ai_results=None):
ai_results = ai_results or {}
w, h = image.size
p = (prompt or "").lower()
base = [
("PRIMARY_FOCAL_REGION", pct_box(w, h, 0.18, 0.10, 0.82, 0.82)),
("SECONDARY_FOCAL_REGION", pct_box(w, h, 0.04, 0.00, 0.75, 0.50)),
("ENVIRONMENT_REGION", pct_box(w, h, 0.00, 0.00, 1.00, 1.00)),
("LOWER_ANCHOR_REGION", pct_box(w, h, 0.05, 0.58, 0.95, 1.00)),
("LEFT_EDGE_FLOW_REGION", pct_box(w, h, 0.00, 0.04, 0.38, 0.96)),
("RIGHT_EDGE_FLOW_REGION", pct_box(w, h, 0.62, 0.04, 1.00, 0.96)),
]
regions = []
for region_type, box in base:
crop = image.crop(box)
score = region_score(crop)
if region_type == "PRIMARY_FOCAL_REGION":
if any(k in p for k in ["face", "head", "person", "character", "dj", "main", "center", "figure", "subject"]):
score += 45
if region_type == "SECONDARY_FOCAL_REGION":
if any(k in p for k in ["eye", "monster", "upper", "teeth", "dramatic", "background"]):
score += 35
if region_type == "LOWER_ANCHOR_REGION":
if any(k in p for k in ["pocket", "deck", "table", "lower", "base", "ground", "smoke"]):
score += 30
if region_type == "ENVIRONMENT_REGION":
if any(k in p for k in ["pattern", "repeat", "tile", "smoke", "flame", "abstract", "texture"]):
score += 30
if region_type == "LEFT_EDGE_FLOW_REGION":
if any(k in p for k in ["left sleeve", "left edge", "flame", "smoke"]):
score += 20
if region_type == "RIGHT_EDGE_FLOW_REGION":
if any(k in p for k in ["right sleeve", "right edge", "purple", "smoke"]):
score += 20
regions.append({
"type": region_type,
"box": box,
"score": round(score, 2),
"source": "local_region_analysis",
})
for category, result in ai_results.items():
if not result.get("ok"):
continue
for det in result.get("detections", []):
det_type = str(det.get("type", category)).lower()
box = det.get("box")
if not box or len(box) != 4:
continue
region_type = None
if det_type in ["face", "person", "object", "subject"]:
region_type = "PRIMARY_FOCAL_REGION"
elif det_type in ["text", "ocr"]:
region_type = "TEXT_REGION"
elif det_type == "logo":
region_type = "LOGO_REGION"
if region_type:
regions.append({
"type": region_type,
"box": tuple(map(int, box)),
"score": float(det.get("score", 85)),
"source": "hf_optional_ai",
})
return sorted(regions, key=lambda r: r["score"], reverse=True)
def cover_resize_no_padding(img, target_w, target_h):
src_w, src_h = img.size
scale = max(target_w / src_w, target_h / src_h)
new_w, new_h = int(src_w * scale), int(src_h * scale)
resized = img.resize((new_w, new_h), Image.LANCZOS)
left = max(0, (new_w - target_w) // 2)
top = max(0, (new_h - target_h) // 2)
return resized.crop((left, top, left + target_w, top + target_h))
def contain_resize_with_transparency(img, target_w, target_h):
src_w, src_h = img.size
scale = min(target_w / src_w, target_h / src_h)
new_w, new_h = int(src_w * scale), int(src_h * scale)
resized = img.resize((new_w, new_h), Image.LANCZOS)
canvas = Image.new("RGBA", (target_w, target_h), (0, 0, 0, 0))
canvas.paste(resized, ((target_w - new_w) // 2, (target_h - new_h) // 2))
return canvas
def tile_texture(img, target_w, target_h):
tile_size = max(300, min(900, target_w, target_h))
tile = cover_resize_no_padding(img, tile_size, tile_size)
out = Image.new("RGBA", (target_w, target_h))
for x in range(0, target_w, tile.width):
for y in range(0, target_h, tile.height):
out.paste(tile, (x, y))
return out.crop((0, 0, target_w, target_h))
def get_mask_path(template_id, piece_name):
candidates = [
f"template_assets/{template_id}/masks/{piece_name}.png",
f"template_assets/{template_id}/masks/{norm(piece_name)}.png",
f"template_assets/{template_id}/{piece_name}.png",
f"template_assets/{template_id}/{norm(piece_name)}.png",
]
for path in candidates:
if os.path.exists(path):
return path
return None
def apply_piece_mask(mapped_img, template_id, piece_name):
mask_path = get_mask_path(template_id, piece_name)
if not mask_path:
return mapped_img, None
mask_img = Image.open(mask_path).convert("RGBA").resize(mapped_img.size, Image.LANCZOS)
alpha = mask_img.getchannel("A")
out = mapped_img.convert("RGBA")
out.putalpha(alpha)
return out, mask_path
def choose_region(piece_name, regions):
rule = AOP_HOODIE_MAPPING_RULES.get(norm(piece_name))
if not rule:
return regions[0], "safe_cover"
for preferred in rule["preferred"]:
for region in regions:
if region["type"] == preferred and region["type"] not in rule["forbidden"]:
return region, rule["strategy"]
for region in regions:
if region["type"] not in rule["forbidden"]:
return region, rule["strategy"]
env = next((r for r in regions if r["type"] == "ENVIRONMENT_REGION"), regions[0])
return env, "fallback_texture"
def apply_strategy(image, region, strategy, target_w, target_h):
crop = image.crop(region["box"]).convert("RGBA")
if strategy == "texture_band":
return tile_texture(crop, target_w, target_h)
if strategy == "vertical_texture":
return cover_resize_no_padding(crop, target_w, target_h)
if strategy == "dark_texture":
return cover_resize_no_padding(crop, target_w, target_h)
if strategy == "fallback_texture":
return cover_resize_no_padding(crop, target_w, target_h)
if strategy == "centered_focal":
return cover_resize_no_padding(crop, target_w, target_h)
if strategy == "wide_dramatic":
return cover_resize_no_padding(crop, target_w, target_h)
if strategy == "lower_anchor":
return cover_resize_no_padding(crop, target_w, target_h)
return cover_resize_no_padding(crop, target_w, target_h)
def map_all(image_path, template_id, pieces, prompt, use_optional_ai=False):
image = Image.open(image_path).convert("RGBA")
ai_results = run_optional_ai_processors(image_path, prompt) if use_optional_ai else {}
regions = analyze_regions(image, prompt, ai_results)
outputs = {}
decisions = []
warnings = []
used_boxes = {}
mask_usage = {}
for piece_name, size in pieces.items():
target_w, target_h = size
region, strategy = choose_region(piece_name, regions)
mapped = apply_strategy(image, region, strategy, target_w, target_h)
mapped, mask_path = apply_piece_mask(mapped, template_id, piece_name)
outputs[piece_name] = mapped
decision = {
"piece": piece_name,
"region": region["type"],
"strategy": strategy,
"box": region["box"],
"score": region["score"],
"output_size": [target_w, target_h],
"mask_applied": bool(mask_path),
"mask_path": mask_path,
}
decisions.append(decision)
key = str(region["box"])
used_boxes[key] = used_boxes.get(key, 0) + 1
mask_usage[piece_name] = bool(mask_path)
risky_small = norm(piece_name) in [
"left_sleeve",
"right_sleeve",
"collar",
"left_cuff",
"right_cuff",
"waistband",
"kangaroo_pocket",
]
if risky_small and region["type"] in [
"TEXT_REGION",
"LOGO_REGION",
"PRIMARY_FOCAL_REGION",
]:
warnings.append(
f"{piece_name}: readable text, logo, or primary focal content may be on a risky small panel."
)
if not mask_path:
warnings.append(
f"{piece_name}: no piece mask found, output is rectangular. Add template_assets/{template_id}/masks/{piece_name}.png for true shaped AOP output."
)
front_decisions = [
d for d in decisions if norm(d["piece"]) in ["front", "front_body"]
]
if front_decisions and front_decisions[0]["region"] != "PRIMARY_FOCAL_REGION":
warnings.append("Main focal point was not placed on the front panel.")
if used_boxes and max(used_boxes.values()) > 3:
warnings.append("Too many panels are using the same crop. Design may look repetitive.")
if not any(mask_usage.values()):
warnings.append(
"No template masks were applied. Current outputs are rectangular crops, not true garment-shaped production pieces."
)
return outputs, {
"prompt": prompt,
"template_id": template_id,
"plain_engine_logic": [
"Big panels get meaning.",
"Small panels get texture.",
"Risky seams get atmosphere.",
"Pockets get lower anchors.",
"Cuffs get color bands.",
"Hoods get continuation, not the whole story.",
"True AOP piece shape requires alpha masks from template PNGs.",
],
"regions": regions,
"decisions": decisions,
"warnings": warnings,
"optional_ai_processors": ai_results,
} |