Download classify_face_shape.py from EthanCui/face-shape-measurement-reference: direct link, hf CLI and curl.
- Browser
- Download file 6.36 kB
-
https://huggingface.co/datasets/EthanCui/face-shape-measurement-reference/resolve/main/classify_face_shape.py
- Command line
-
hf download hf://datasets/EthanCui/face-shape-measurement-reference/classify_face_shape.py
-
curl -L -o classify_face_shape.py https://huggingface.co/datasets/EthanCui/face-shape-measurement-reference/resolve/main/classify_face_shape.py
6.36 kB
| #!/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()) | |