ClothIron / utils /analysis.py
Mihir Mithani
Initial commit: AI Virtual Garment Ironing System
c7159bc
Raw
History Blame Contribute Delete
5.03 kB
"""
Analysis & Reporting Utilities
Generates human-readable wrinkle analysis reports.
"""
from __future__ import annotations
from datetime import datetime
FABRIC_IRONING_TIPS = {
"cotton": "Cotton responds best to medium-high heat with steam. Use circular motions.",
"linen": "Linen irons best while slightly damp. Use high heat and press firmly.",
"silk": "Silk needs low heat and a pressing cloth. Avoid steam directly on fabric.",
"denim": "Denim needs high heat, iron inside-out to preserve colour.",
"polyester": "Use low heat only β€” polyester melts at high temperatures.",
"wool": "Use a damp pressing cloth and medium heat. Never press directly.",
"synthetic blend": "Use low-to-medium heat; check label for fibre percentages.",
"auto-detect": "Settings have been automatically optimised for the detected fabric.",
}
INTENSITY_DESCRIPTIONS = {
"light": "Light touch β€” removes surface creasing only.",
"medium": "Standard press β€” removes most wrinkles while keeping natural drape.",
"professional press": "Full press β€” crisp, sharp finish suitable for formal wear.",
}
def analyze_wrinkles(
wrinkle_score: float,
zones: list[dict],
labels: list[str],
fabric: str,
intensity: str,
) -> dict:
"""Aggregate analysis data into a structured report dict."""
level = "Low" if wrinkle_score < 25 else ("Medium" if wrinkle_score < 55 else "High")
improvement = _estimate_improvement(wrinkle_score, intensity)
return {
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M"),
"detected_garments": labels or ["clothing item"],
"fabric": fabric,
"intensity": intensity,
"wrinkle_score": wrinkle_score,
"wrinkle_level": level,
"zones": zones,
"improvement": improvement,
"fabric_tip": FABRIC_IRONING_TIPS.get(fabric.lower(), FABRIC_IRONING_TIPS["auto-detect"]),
"intensity_desc": INTENSITY_DESCRIPTIONS.get(
intensity.lower(), INTENSITY_DESCRIPTIONS["medium"]
),
}
def generate_analysis_report(analysis: dict) -> str:
"""Format analysis dict into a readable text report."""
score = analysis["wrinkle_score"]
level = analysis["wrinkle_level"]
fab = analysis["fabric"]
zones = analysis["zones"]
garments = ", ".join(analysis["detected_garments"])
impr = analysis["improvement"]
bar = _progress_bar(score, width=20)
lines = [
"═" * 48,
" GARMENT ANALYSIS REPORT",
f" {analysis['timestamp']}",
"═" * 48,
"",
f" Detected garment : {garments}",
f" Fabric type : {fab}",
f" Ironing intensity: {analysis['intensity']}",
"",
"── Wrinkle Assessment ──────────────────────────",
f" Score : {score:.1f} / 100 ({level})",
f" [{bar}]",
"",
"── Zone Breakdown ──────────────────────────────",
]
for z in zones:
indicator = "πŸ”΄" if z["level"] == "high" else ("🟑" if z["level"] == "medium" else "🟒")
lines.append(f" {indicator} {z['name']:<22} {z['score']:.1f} pts [{z['level']}]")
lines += [
"",
"── Ironing Result ──────────────────────────────",
f" Expected improvement: {impr:.0f}%",
f" {analysis['intensity_desc']}",
"",
"── Fabric Tips ─────────────────────────────────",
f" {analysis['fabric_tip']}",
"",
"── What Was Preserved ──────────────────────────",
" βœ“ Background and non-garment areas",
" βœ“ Face, hair, skin tones",
" βœ“ Garment colour and pattern",
" βœ“ Logos, embroidery, buttons",
" βœ“ Structural folds from body posture",
" βœ“ Natural fabric drape and gravity pull",
" βœ“ Original lighting and shadows",
"",
"═" * 48,
]
return "\n".join(lines)
# ── helpers ───────────────────────────────────────────────────────────────────
def _estimate_improvement(score: float, intensity: str) -> float:
intensity_factor = {"light": 0.45, "medium": 0.72, "professional press": 0.92}
factor = intensity_factor.get(intensity.lower(), 0.72)
return min(score * factor, 97.0)
def _progress_bar(value: float, width: int = 20, filled: str = "β–ˆ", empty: str = "β–‘") -> str:
filled_n = int(round(value / 100.0 * width))
return filled * filled_n + empty * (width - filled_n)