Instructions to use 23f1000805/face-expression-v3 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Py-Feat
How to use 23f1000805/face-expression-v3 with Py-Feat:
# No code snippets available yet for this library. # To use this model, check the repository files and the library's documentation. # Want to help? PRs adding snippets are welcome at: # https://github.com/huggingface/huggingface.js
- Notebooks
- Google Colab
- Kaggle
Download inference.py from 23f1000805/face-expression-v3: direct link, hf CLI and curl.
- Browser
- Download file 2.03 kB
-
https://huggingface.co/23f1000805/face-expression-v3/resolve/main/inference.py
- Command line
-
hf download hf://23f1000805/face-expression-v3/inference.py
-
curl -L -o inference.py https://huggingface.co/23f1000805/face-expression-v3/resolve/main/inference.py
2.03 kB
| import json | |
| import sys | |
| from pathlib import Path | |
| import numpy as np | |
| import pandas as pd | |
| from scipy.optimize import linear_sum_assignment | |
| from safetensors.numpy import load_file | |
| class ExpressionMatcher: | |
| def __init__(self, src="."): | |
| p = Path(src) | |
| if not p.is_dir(): | |
| from huggingface_hub import snapshot_download | |
| p = Path(snapshot_download(src)) | |
| self.cfg = json.load(open(p / "config.json")) | |
| self.w = load_file(str(p / "model.safetensors"))["weights"].astype(np.float64) | |
| self.cols = self.cfg["feature_columns"] | |
| self.labels = self.cfg["labels"] | |
| def inputs(self, df): | |
| raw = df[self.cols].values.astype(float) | |
| rk = df[self.cols].rank(method="average").values | |
| if self.cfg["rank_mode"] == "minmax": | |
| rk = (rk - 1) / (len(df) - 1) | |
| else: | |
| rk = rk / len(df) | |
| return np.hstack([raw, rk]) | |
| def scores(self, df): | |
| return self.inputs(df) @ self.w.T | |
| def predict(self, df): | |
| if len(df) != 10: | |
| raise ValueError("need the 10 photos of one person, got %d" % len(df)) | |
| s = self.scores(df) | |
| r, c = linear_sum_assignment(-s) | |
| out = [None] * len(df) | |
| for i in range(len(r)): | |
| out[r[i]] = self.labels[c[i]] | |
| return out | |
| def predict_table(self, df): | |
| df = df.reset_index(drop=True) | |
| key = "person_id" if "person_id" in df.columns else None | |
| parts = [] | |
| groups = df.groupby(key) if key else [(0, df)] | |
| for pid, grp in groups: | |
| res = grp.copy() | |
| res["predicted_expression"] = self.predict(grp) | |
| parts.append(res) | |
| keep = [c for c in ("person_id", "photo_id", "predicted_expression") if c in parts[0].columns] | |
| return pd.concat(parts)[keep] | |
| if __name__ == "__main__": | |
| csv = sys.argv[1] | |
| src = sys.argv[2] if len(sys.argv) > 2 else "." | |
| m = ExpressionMatcher(src) | |
| print(m.predict_table(pd.read_csv(csv)).to_string(index=False)) | |