Download app.py from bestwale/face: direct link, hf CLI and curl.
- Browser
- Download file 2.94 kB
-
https://huggingface.co/spaces/bestwale/face/resolve/main/app.py
- Command line
-
hf download hf://spaces/bestwale/face/app.py
-
curl -L -o app.py https://huggingface.co/spaces/bestwale/face/resolve/main/app.py
2.94 kB
| from flask import Flask, request, jsonify, session, send_from_directory | |
| from flask_cors import CORS | |
| import os, json, base64, io, numpy as np | |
| from PIL import Image | |
| import insightface | |
| from insightface.app import FaceAnalysis | |
| # --- Flask Setup --- | |
| app = Flask(__name__, static_folder="static") | |
| CORS(app) | |
| app.secret_key = os.getenv("SECRET_KEY", "supersecret") | |
| # --- InsightFace Setup --- | |
| app_insight = FaceAnalysis(providers=['CPUExecutionProvider']) | |
| app_insight.prepare(ctx_id=0, nms=0.4) | |
| # --- Simple JSON DB --- | |
| DB_FILE = "students.json" | |
| if os.path.exists(DB_FILE): | |
| with open(DB_FILE, "r") as f: | |
| students_db = json.load(f) | |
| else: | |
| students_db = {} | |
| def save_db(): | |
| with open(DB_FILE, "w") as f: | |
| json.dump(students_db, f) | |
| def b64_to_image(b64str): | |
| if "," in b64str: | |
| b64str = b64str.split(",")[1] | |
| img_data = base64.b64decode(b64str) | |
| img = Image.open(io.BytesIO(img_data)).convert('RGB') | |
| return np.array(img) | |
| # --- Routes --- | |
| def index(): | |
| return send_from_directory("static", "index.html") | |
| def health(): | |
| return jsonify({"status":"ok"}) | |
| def register(): | |
| data = request.json | |
| student_id = data.get("id") | |
| name = data.get("name") | |
| email = data.get("email") | |
| img_b64 = data.get("image") | |
| if not (student_id and name and img_b64): | |
| return jsonify({"error":"id, name, and image required"}), 400 | |
| img = b64_to_image(img_b64) | |
| faces = app_insight.get(img) | |
| if not faces: | |
| return jsonify({"error":"no face detected"}), 400 | |
| emb = faces[0].embedding.tolist() | |
| students_db[student_id] = { | |
| "name": name, | |
| "email": email, | |
| "embedding": emb | |
| } | |
| save_db() | |
| return jsonify({"status":"registered", "id": student_id}) | |
| def login(): | |
| data = request.json | |
| img_b64 = data.get("image") | |
| if not img_b64: | |
| return jsonify({"error":"image required"}), 400 | |
| img = b64_to_image(img_b64) | |
| faces = app_insight.get(img) | |
| if not faces: | |
| return jsonify({"error":"no face detected"}), 400 | |
| emb = faces[0].embedding | |
| best = None | |
| best_dist = float("inf") | |
| for sid, sdata in students_db.items(): | |
| db_emb = np.array(sdata["embedding"]) | |
| dist = np.linalg.norm(db_emb - emb) | |
| if dist < best_dist: | |
| best_dist = dist | |
| best = {"id": sid, "name": sdata["name"], "email": sdata["email"]} | |
| THRESHOLD = 0.8 | |
| if best_dist <= THRESHOLD: | |
| session['student_id'] = best['id'] | |
| return jsonify({"status":"ok", "student": best, "distance": float(best_dist)}), 200 | |
| else: | |
| return jsonify({"status":"no-match", "distance": float(best_dist)}), 401 | |
| def list_students(): | |
| return jsonify(students_db) | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=7860) | |