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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from PIL import Image, ImageStat
|
| 3 |
+
from engine.hoodie_rules import AOP_HOODIE_MAPPING_RULES
|
| 4 |
+
from engine.hf_processors import run_optional_ai_processors
|
| 5 |
+
|
| 6 |
+
def norm(name):
|
| 7 |
+
return name.lower().replace(" ", "_")
|
| 8 |
+
|
| 9 |
+
def pct_box(w, h, x1, y1, x2, y2):
|
| 10 |
+
return (int(w * x1), int(h * y1), int(w * x2), int(h * y2))
|
| 11 |
+
|
| 12 |
+
def region_score(img):
|
| 13 |
+
stat = ImageStat.Stat(img.convert("RGB"))
|
| 14 |
+
contrast = sum(stat.stddev) / 3
|
| 15 |
+
saturation_hint = max(stat.mean) - min(stat.mean)
|
| 16 |
+
brightness = sum(stat.mean) / 3
|
| 17 |
+
return contrast + saturation_hint + (255 - abs(140 - brightness)) * 0.12
|
| 18 |
+
|
| 19 |
+
def analyze_regions(image, prompt, ai_results=None):
|
| 20 |
+
ai_results = ai_results or {}
|
| 21 |
+
w, h = image.size
|
| 22 |
+
p = (prompt or "").lower()
|
| 23 |
+
|
| 24 |
+
base = [
|
| 25 |
+
("PRIMARY_FOCAL_REGION", pct_box(w, h, 0.18, 0.10, 0.82, 0.82)),
|
| 26 |
+
("SECONDARY_FOCAL_REGION", pct_box(w, h, 0.04, 0.00, 0.75, 0.50)),
|
| 27 |
+
("ENVIRONMENT_REGION", pct_box(w, h, 0.00, 0.00, 1.00, 1.00)),
|
| 28 |
+
("LOWER_ANCHOR_REGION", pct_box(w, h, 0.05, 0.58, 0.95, 1.00)),
|
| 29 |
+
("LEFT_EDGE_FLOW_REGION", pct_box(w, h, 0.00, 0.04, 0.38, 0.96)),
|
| 30 |
+
("RIGHT_EDGE_FLOW_REGION", pct_box(w, h, 0.62, 0.04, 1.00, 0.96)),
|
| 31 |
+
]
|
| 32 |
+
|
| 33 |
+
regions = []
|
| 34 |
+
for region_type, box in base:
|
| 35 |
+
crop = image.crop(box)
|
| 36 |
+
score = region_score(crop)
|
| 37 |
+
|
| 38 |
+
if region_type == "PRIMARY_FOCAL_REGION":
|
| 39 |
+
if any(k in p for k in ["face", "head", "person", "character", "dj", "main", "center"]):
|
| 40 |
+
score += 45
|
| 41 |
+
|
| 42 |
+
if region_type == "SECONDARY_FOCAL_REGION":
|
| 43 |
+
if any(k in p for k in ["eye", "monster", "upper", "teeth", "dramatic"]):
|
| 44 |
+
score += 35
|
| 45 |
+
|
| 46 |
+
if region_type == "LOWER_ANCHOR_REGION":
|
| 47 |
+
if any(k in p for k in ["pocket", "deck", "table", "lower", "base", "ground", "smoke"]):
|
| 48 |
+
score += 30
|
| 49 |
+
|
| 50 |
+
if region_type == "ENVIRONMENT_REGION":
|
| 51 |
+
if any(k in p for k in ["pattern", "repeat", "tile", "smoke", "flame", "abstract"]):
|
| 52 |
+
score += 30
|
| 53 |
+
|
| 54 |
+
regions.append({
|
| 55 |
+
"type": region_type,
|
| 56 |
+
"box": box,
|
| 57 |
+
"score": round(score, 2),
|
| 58 |
+
})
|
| 59 |
+
|
| 60 |
+
for category, result in ai_results.items():
|
| 61 |
+
if not result.get("ok"):
|
| 62 |
+
continue
|
| 63 |
+
for det in result.get("detections", []):
|
| 64 |
+
det_type = str(det.get("type", category)).lower()
|
| 65 |
+
box = det.get("box")
|
| 66 |
+
if not box or len(box) != 4:
|
| 67 |
+
continue
|
| 68 |
+
|
| 69 |
+
region_type = None
|
| 70 |
+
if det_type in ["face", "person", "object"]:
|
| 71 |
+
region_type = "PRIMARY_FOCAL_REGION"
|
| 72 |
+
elif det_type in ["text", "ocr"]:
|
| 73 |
+
region_type = "TEXT_REGION"
|
| 74 |
+
elif det_type in ["logo"]:
|
| 75 |
+
region_type = "LOGO_REGION"
|
| 76 |
+
|
| 77 |
+
if region_type:
|
| 78 |
+
regions.append({
|
| 79 |
+
"type": region_type,
|
| 80 |
+
"box": tuple(map(int, box)),
|
| 81 |
+
"score": float(det.get("score", 85)),
|
| 82 |
+
"source": "hf_optional_ai",
|
| 83 |
+
})
|
| 84 |
+
|
| 85 |
+
return sorted(regions, key=lambda r: r["score"], reverse=True)
|
| 86 |
+
|
| 87 |
+
def cover_resize(img, target_w, target_h, zoom_out=False):
|
| 88 |
+
src_w, src_h = img.size
|
| 89 |
+
scale = max(target_w / src_w, target_h / src_h)
|
| 90 |
+
|
| 91 |
+
if zoom_out:
|
| 92 |
+
scale *= 0.92
|
| 93 |
+
|
| 94 |
+
new_w, new_h = int(src_w * scale), int(src_h * scale)
|
| 95 |
+
resized = img.resize((new_w, new_h), Image.LANCZOS)
|
| 96 |
+
|
| 97 |
+
canvas = Image.new("RGBA", (max(target_w, new_w), max(target_h, new_h)), (0, 0, 0, 0))
|
| 98 |
+
canvas.paste(resized, ((canvas.width - new_w) // 2, (canvas.height - new_h) // 2))
|
| 99 |
+
|
| 100 |
+
left = (canvas.width - target_w) // 2
|
| 101 |
+
top = (canvas.height - target_h) // 2
|
| 102 |
+
return canvas.crop((left, top, left + target_w, top + target_h))
|
| 103 |
+
|
| 104 |
+
def tile_texture(img, target_w, target_h):
|
| 105 |
+
tile_size = max(300, min(900, target_w, target_h))
|
| 106 |
+
tile = cover_resize(img, tile_size, tile_size)
|
| 107 |
+
out = Image.new("RGBA", (target_w, target_h))
|
| 108 |
+
for x in range(0, target_w, tile.width):
|
| 109 |
+
for y in range(0, target_h, tile.height):
|
| 110 |
+
out.paste(tile, (x, y))
|
| 111 |
+
return out.crop((0, 0, target_w, target_h))
|
| 112 |
+
|
| 113 |
+
def choose_region(piece_name, regions):
|
| 114 |
+
rule = AOP_HOODIE_MAPPING_RULES.get(norm(piece_name))
|
| 115 |
+
|
| 116 |
+
if not rule:
|
| 117 |
+
return regions[0], "safe_cover"
|
| 118 |
+
|
| 119 |
+
for preferred in rule["preferred"]:
|
| 120 |
+
for region in regions:
|
| 121 |
+
if region["type"] == preferred and region["type"] not in rule["forbidden"]:
|
| 122 |
+
return region, rule["strategy"]
|
| 123 |
+
|
| 124 |
+
for region in regions:
|
| 125 |
+
if region["type"] not in rule["forbidden"]:
|
| 126 |
+
return region, rule["strategy"]
|
| 127 |
+
|
| 128 |
+
env = next((r for r in regions if r["type"] == "ENVIRONMENT_REGION"), regions[0])
|
| 129 |
+
return env, "fallback_texture"
|
| 130 |
+
|
| 131 |
+
def apply_strategy(image, region, strategy, target_w, target_h):
|
| 132 |
+
crop = image.crop(region["box"]).convert("RGBA")
|
| 133 |
+
|
| 134 |
+
if strategy in ["texture_band", "vertical_texture", "dark_texture", "fallback_texture"]:
|
| 135 |
+
return tile_texture(crop, target_w, target_h)
|
| 136 |
+
|
| 137 |
+
if strategy == "centered_focal":
|
| 138 |
+
return cover_resize(crop, target_w, target_h, zoom_out=True)
|
| 139 |
+
|
| 140 |
+
return cover_resize(crop, target_w, target_h)
|
| 141 |
+
|
| 142 |
+
def map_all(image_path, pieces, prompt, use_optional_ai=False):
|
| 143 |
+
image = Image.open(image_path).convert("RGBA")
|
| 144 |
+
|
| 145 |
+
ai_results = run_optional_ai_processors(image_path, prompt) if use_optional_ai else {}
|
| 146 |
+
regions = analyze_regions(image, prompt, ai_results)
|
| 147 |
+
|
| 148 |
+
outputs = {}
|
| 149 |
+
decisions = []
|
| 150 |
+
warnings = []
|
| 151 |
+
used_boxes = {}
|
| 152 |
+
|
| 153 |
+
for piece_name, size in pieces.items():
|
| 154 |
+
target_w, target_h = size
|
| 155 |
+
region, strategy = choose_region(piece_name, regions)
|
| 156 |
+
|
| 157 |
+
mapped = apply_strategy(image, region, strategy, target_w, target_h)
|
| 158 |
+
outputs[piece_name] = mapped
|
| 159 |
+
|
| 160 |
+
decision = {
|
| 161 |
+
"piece": piece_name,
|
| 162 |
+
"region": region["type"],
|
| 163 |
+
"strategy": strategy,
|
| 164 |
+
"box": region["box"],
|
| 165 |
+
"score": region["score"],
|
| 166 |
+
"output_size": [target_w, target_h],
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
decisions.append(decision)
|
| 170 |
+
|
| 171 |
+
key = str(region["box"])
|
| 172 |
+
used_boxes[key] = used_boxes.get(key, 0) + 1
|
| 173 |
+
|
| 174 |
+
risky_small = norm(piece_name) in [
|
| 175 |
+
"left_sleeve",
|
| 176 |
+
"right_sleeve",
|
| 177 |
+
"collar",
|
| 178 |
+
"left_cuff",
|
| 179 |
+
"right_cuff",
|
| 180 |
+
"waistband",
|
| 181 |
+
"kangaroo_pocket",
|
| 182 |
+
]
|
| 183 |
+
|
| 184 |
+
if risky_small and region["type"] in ["TEXT_REGION", "LOGO_REGION", "PRIMARY_FOCAL_REGION"]:
|
| 185 |
+
warnings.append("Readable text or primary focal content was detected on a risky small panel.")
|
| 186 |
+
|
| 187 |
+
front_decisions = [d for d in decisions if norm(d["piece"]) in ["front", "front_body"]]
|
| 188 |
+
if front_decisions and front_decisions[0]["region"] != "PRIMARY_FOCAL_REGION":
|
| 189 |
+
warnings.append("Main focal point was not placed on the front panel.")
|
| 190 |
+
|
| 191 |
+
if used_boxes and max(used_boxes.values()) > 3:
|
| 192 |
+
warnings.append("Too many panels are using the same crop. Design may look repetitive.")
|
| 193 |
+
|
| 194 |
+
return outputs, {
|
| 195 |
+
"prompt": prompt,
|
| 196 |
+
"plain_engine_logic": [
|
| 197 |
+
"Big panels get meaning.",
|
| 198 |
+
"Small panels get texture.",
|
| 199 |
+
"Risky seams get atmosphere.",
|
| 200 |
+
"Pockets get lower anchors.",
|
| 201 |
+
"Cuffs get color bands.",
|
| 202 |
+
"Hoods get continuation, not the whole story.",
|
| 203 |
+
],
|
| 204 |
+
"regions": regions,
|
| 205 |
+
"decisions": decisions,
|
| 206 |
+
"warnings": warnings,
|
| 207 |
+
"optional_ai_processors": ai_results,
|
| 208 |
+
}
|