File size: 6,362 Bytes
9e3f4b2 | 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 | #!/usr/bin/env python3
"""Classify face shape candidates from four tape measurements.
Only relative comparisons between your four numbers are used, because published
face-shape guides state their rules in relative terms. "Roughly equal" uses a
single tie band (--tol, default 3%) that is this tool's own parameter, not a
figure from any published study. Where measurements alone cannot separate two
shapes (e.g. round vs square depends on jaw angle), the tool says so instead
of inventing an answer.
Usage:
python classify_face_shape.py --forehead 14.2 --cheekbones 15.0 \
--jawline 13.6 --length 19.1 [--tol 0.03] [--json]
"""
from __future__ import annotations
import argparse
import json
import sys
WIDTHS = ("forehead", "cheekbones", "jawline")
MANUAL_CHECKS = (
"Manual check (a tape cannot see this): "
"round vs square needs the jaw angle (soft curve vs strong angle); "
"heart vs diamond partly needs chin shape (pointed vs tapered)."
)
def make_rules(tol: float):
"""Rule table: shape -> list of (condition_fn, plain-English condition).
The comparisons mirror the rules published in the guide at
https://faceshapechart.com/ — stated there in relative terms only.
"""
def width(m):
return max(m[k] for k in WIDTHS)
def roughly(a, b, factor=1.0):
return abs(a - b) <= tol * factor * max(a, b)
def widest_is(key, strict=False):
def check(m):
w = width(m)
if m[key] < w * (1 - tol): # clearly below the widest
return False
if strict: # must beat the others outright
return all(m[key] > m[o] * (1 - tol) for o in WIDTHS if o != key)
return True
return check
return {
"oval": [
(widest_is("cheekbones"), "cheekbones widest"),
(lambda m: m["length"] > width(m) * (1 + tol * 0.5), "face longer than wide"),
(lambda m: m["jawline"] < m["cheekbones"] * (1 - tol * 0.5), "jaw narrower than cheekbones"),
],
"round": [
(lambda m: roughly(m["length"], width(m), 1.2), "length roughly equal to width"),
(lambda m: roughly(m["forehead"], m["jawline"], 1.5), "forehead roughly equal to jawline"),
],
"square": [
(lambda m: roughly(m["forehead"], m["cheekbones"], 1.5) and roughly(m["cheekbones"], m["jawline"], 1.5), "forehead, cheekbones, jawline roughly equal"),
(lambda m: roughly(m["length"], width(m), 1.2), "length roughly equal to width"),
],
"heart": [
(widest_is("forehead", strict=True), "forehead clearly widest"),
(lambda m: m["jawline"] < m["forehead"] * (1 - tol * 0.5), "jaw narrower than forehead"),
],
"diamond": [
(widest_is("cheekbones", strict=True), "cheekbones clearly widest"),
(lambda m: m["forehead"] < m["cheekbones"] * (1 - tol * 0.5), "forehead narrower than cheekbones"),
(lambda m: m["jawline"] < m["cheekbones"] * (1 - tol * 0.5), "jaw narrower than cheekbones"),
],
"oblong": [
(lambda m: m["length"] > width(m) * (1 + 4 * tol), "length clearly greater than width"),
(lambda m: roughly(m["forehead"], m["cheekbones"], 1.5) and roughly(m["cheekbones"], m["jawline"], 1.5), "forehead, cheekbones, jawline roughly equal"),
],
"triangle (pear)": [
(widest_is("jawline", strict=True), "jawline clearly widest"),
(lambda m: m["jawline"] > m["cheekbones"] * (1 + tol * 0.5), "jaw wider than cheekbones"),
],
"hourglass": [
(lambda m: m["cheekbones"] >= m["forehead"] * (1 - tol) and m["jawline"] > m["forehead"] * (1 - tol) and m["jawline"] >= width(m) * (1 - 2 * tol), "cheekbones AND jawline both prominent"),
(lambda m: roughly(m["length"], width(m) * 1.1, 2.0), "length balanced against width"),
],
}
def classify(m: dict, tol: float):
rules = make_rules(tol)
results = {}
for shape, conds in rules.items():
matched = [text for fn, text in conds if fn(m)]
failed = [text for fn, text in conds if not fn(m)]
results[shape] = {"score": len(matched), "total": len(conds), "matched": matched, "failed": failed}
return results
def main() -> int:
p = argparse.ArgumentParser(description="Rank face-shape candidates from four measurements.")
p.add_argument("--forehead", type=float, required=True, help="widest width across the forehead")
p.add_argument("--cheekbones", type=float, required=True, help="widest width across the cheekbones")
p.add_argument("--jawline", type=float, required=True, help="width across the jawline")
p.add_argument("--length", type=float, required=True, help="hairline-to-chin length")
p.add_argument("--tol", type=float, default=0.03, help="tie band for 'roughly equal' as a fraction (default 0.03 = 3%%); this tool's own parameter, not a published figure")
p.add_argument("--json", action="store_true", help="print raw JSON")
a = p.parse_args()
if min(a.forehead, a.cheekbones, a.jawline, a.length) <= 0:
p.error("all four measurements must be positive numbers (any consistent unit)")
m = {"forehead": a.forehead, "cheekbones": a.cheekbones, "jawline": a.jawline, "length": a.length}
results = classify(m, a.tol)
if a.json:
print(json.dumps({"measurements": m, "tol": a.tol, "results": results}, indent=2))
return 0
ranked = sorted(results.items(), key=lambda kv: (-kv[1]["score"], kv[0]))
best = ranked[0][1]["score"]
top = [s for s, r in ranked if r["score"] == best]
print("Measurements: forehead=%.4g cheekbones=%.4g jawline=%.4g length=%.4g (tol=%.0f%%)"
% (a.forehead, a.cheekbones, a.jawline, a.length, a.tol * 100))
print()
print("Top candidate(s): %s" % ", ".join(top))
print()
for shape, r in ranked:
tag = "OK " if not r["failed"] else " "
print(" %s[%s] %d/%d matched" % (tag, shape.ljust(16), r["score"], r["total"]))
for t in r["matched"]:
print(" + %s" % t)
for t in r["failed"]:
print(" - %s" % t)
print()
print(MANUAL_CHECKS)
return 0
if __name__ == "__main__":
sys.exit(main())
|