Update app.py
Browse files
app.py
CHANGED
|
@@ -1,100 +1,105 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify, session, send_from_directory
|
| 2 |
-
from flask_cors import CORS
|
| 3 |
-
import os, json, base64, io, numpy as np
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import insightface
|
| 6 |
-
from insightface.app import FaceAnalysis
|
| 7 |
-
|
| 8 |
-
# --- Flask Setup ---
|
| 9 |
-
app = Flask(__name__, static_folder="static")
|
| 10 |
-
CORS(app)
|
| 11 |
-
app.secret_key = os.getenv("SECRET_KEY", "supersecret")
|
| 12 |
-
|
| 13 |
-
# --- InsightFace Setup ---
|
| 14 |
-
app_insight = FaceAnalysis(providers=['CPUExecutionProvider'])
|
| 15 |
-
app_insight.prepare(ctx_id=0, nms=0.4)
|
| 16 |
-
|
| 17 |
-
# --- Simple JSON DB ---
|
| 18 |
-
DB_FILE = "students.json"
|
| 19 |
-
if os.path.exists(DB_FILE):
|
| 20 |
-
with open(DB_FILE, "r") as f:
|
| 21 |
-
students_db = json.load(f)
|
| 22 |
-
else:
|
| 23 |
-
students_db = {}
|
| 24 |
-
|
| 25 |
-
def save_db():
|
| 26 |
-
with open(DB_FILE, "w") as f:
|
| 27 |
-
json.dump(students_db, f)
|
| 28 |
-
|
| 29 |
-
def b64_to_image(b64str):
|
| 30 |
-
if "," in b64str:
|
| 31 |
-
b64str = b64str.split(",")[1]
|
| 32 |
-
img_data = base64.b64decode(b64str)
|
| 33 |
-
img = Image.open(io.BytesIO(img_data)).convert('RGB')
|
| 34 |
-
return np.array(img)
|
| 35 |
-
|
| 36 |
-
# --- Routes ---
|
| 37 |
-
@app.route("/")
|
| 38 |
-
def index():
|
| 39 |
-
return send_from_directory("static", "index.html")
|
| 40 |
-
|
| 41 |
-
@app.route("/health")
|
| 42 |
-
def health():
|
| 43 |
-
return jsonify({"status":"ok"})
|
| 44 |
-
|
| 45 |
-
@app.route("/register", methods=["POST"])
|
| 46 |
-
def register():
|
| 47 |
-
data = request.json
|
| 48 |
-
student_id = data.get("id")
|
| 49 |
-
name = data.get("name")
|
| 50 |
-
email = data.get("email")
|
| 51 |
-
img_b64 = data.get("image")
|
| 52 |
-
if not (student_id and name and img_b64):
|
| 53 |
-
return jsonify({"error":"id, name, and image required"}), 400
|
| 54 |
-
|
| 55 |
-
img = b64_to_image(img_b64)
|
| 56 |
-
faces = app_insight.get(img)
|
| 57 |
-
if not faces:
|
| 58 |
-
return jsonify({"error":"no face detected"}), 400
|
| 59 |
-
emb = faces[0].embedding.tolist()
|
| 60 |
-
|
| 61 |
-
students_db[student_id] = {
|
| 62 |
-
"name": name,
|
| 63 |
-
"email": email,
|
| 64 |
-
"embedding": emb
|
| 65 |
-
}
|
| 66 |
-
save_db()
|
| 67 |
-
return jsonify({"status":"registered", "id": student_id})
|
| 68 |
-
|
| 69 |
-
@app.route("/login", methods=["POST"])
|
| 70 |
-
def login():
|
| 71 |
-
data = request.json
|
| 72 |
-
img_b64 = data.get("image")
|
| 73 |
-
if not img_b64:
|
| 74 |
-
return jsonify({"error":"image required"}), 400
|
| 75 |
-
|
| 76 |
-
img = b64_to_image(img_b64)
|
| 77 |
-
faces = app_insight.get(img)
|
| 78 |
-
if not faces:
|
| 79 |
-
return jsonify({"error":"no face detected"}), 400
|
| 80 |
-
emb = faces[0].embedding
|
| 81 |
-
|
| 82 |
-
best = None
|
| 83 |
-
best_dist = float("inf")
|
| 84 |
-
for sid, sdata in students_db.items():
|
| 85 |
-
db_emb = np.array(sdata["embedding"])
|
| 86 |
-
dist = np.linalg.norm(db_emb - emb)
|
| 87 |
-
if dist < best_dist:
|
| 88 |
-
best_dist = dist
|
| 89 |
-
best = {"id": sid, "name": sdata["name"], "email": sdata["email"]}
|
| 90 |
-
|
| 91 |
-
THRESHOLD = 0.8
|
| 92 |
-
if best_dist <= THRESHOLD:
|
| 93 |
-
session['student_id'] = best['id']
|
| 94 |
-
return jsonify({"status":"ok", "student": best, "distance": float(best_dist)}), 200
|
| 95 |
-
else:
|
| 96 |
-
return jsonify({"status":"no-match", "distance": float(best_dist)}), 401
|
| 97 |
-
|
| 98 |
-
@app.route("/students", methods=["GET"])
|
| 99 |
-
def list_students():
|
| 100 |
-
return jsonify(students_db)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, session, send_from_directory
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
import os, json, base64, io, numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import insightface
|
| 6 |
+
from insightface.app import FaceAnalysis
|
| 7 |
+
|
| 8 |
+
# --- Flask Setup ---
|
| 9 |
+
app = Flask(__name__, static_folder="static")
|
| 10 |
+
CORS(app)
|
| 11 |
+
app.secret_key = os.getenv("SECRET_KEY", "supersecret")
|
| 12 |
+
|
| 13 |
+
# --- InsightFace Setup ---
|
| 14 |
+
app_insight = FaceAnalysis(providers=['CPUExecutionProvider'])
|
| 15 |
+
app_insight.prepare(ctx_id=0, nms=0.4)
|
| 16 |
+
|
| 17 |
+
# --- Simple JSON DB ---
|
| 18 |
+
DB_FILE = "students.json"
|
| 19 |
+
if os.path.exists(DB_FILE):
|
| 20 |
+
with open(DB_FILE, "r") as f:
|
| 21 |
+
students_db = json.load(f)
|
| 22 |
+
else:
|
| 23 |
+
students_db = {}
|
| 24 |
+
|
| 25 |
+
def save_db():
|
| 26 |
+
with open(DB_FILE, "w") as f:
|
| 27 |
+
json.dump(students_db, f)
|
| 28 |
+
|
| 29 |
+
def b64_to_image(b64str):
|
| 30 |
+
if "," in b64str:
|
| 31 |
+
b64str = b64str.split(",")[1]
|
| 32 |
+
img_data = base64.b64decode(b64str)
|
| 33 |
+
img = Image.open(io.BytesIO(img_data)).convert('RGB')
|
| 34 |
+
return np.array(img)
|
| 35 |
+
|
| 36 |
+
# --- Routes ---
|
| 37 |
+
@app.route("/")
|
| 38 |
+
def index():
|
| 39 |
+
return send_from_directory("static", "index.html")
|
| 40 |
+
|
| 41 |
+
@app.route("/health")
|
| 42 |
+
def health():
|
| 43 |
+
return jsonify({"status":"ok"})
|
| 44 |
+
|
| 45 |
+
@app.route("/register", methods=["POST"])
|
| 46 |
+
def register():
|
| 47 |
+
data = request.json
|
| 48 |
+
student_id = data.get("id")
|
| 49 |
+
name = data.get("name")
|
| 50 |
+
email = data.get("email")
|
| 51 |
+
img_b64 = data.get("image")
|
| 52 |
+
if not (student_id and name and img_b64):
|
| 53 |
+
return jsonify({"error":"id, name, and image required"}), 400
|
| 54 |
+
|
| 55 |
+
img = b64_to_image(img_b64)
|
| 56 |
+
faces = app_insight.get(img)
|
| 57 |
+
if not faces:
|
| 58 |
+
return jsonify({"error":"no face detected"}), 400
|
| 59 |
+
emb = faces[0].embedding.tolist()
|
| 60 |
+
|
| 61 |
+
students_db[student_id] = {
|
| 62 |
+
"name": name,
|
| 63 |
+
"email": email,
|
| 64 |
+
"embedding": emb
|
| 65 |
+
}
|
| 66 |
+
save_db()
|
| 67 |
+
return jsonify({"status":"registered", "id": student_id})
|
| 68 |
+
|
| 69 |
+
@app.route("/login", methods=["POST"])
|
| 70 |
+
def login():
|
| 71 |
+
data = request.json
|
| 72 |
+
img_b64 = data.get("image")
|
| 73 |
+
if not img_b64:
|
| 74 |
+
return jsonify({"error":"image required"}), 400
|
| 75 |
+
|
| 76 |
+
img = b64_to_image(img_b64)
|
| 77 |
+
faces = app_insight.get(img)
|
| 78 |
+
if not faces:
|
| 79 |
+
return jsonify({"error":"no face detected"}), 400
|
| 80 |
+
emb = faces[0].embedding
|
| 81 |
+
|
| 82 |
+
best = None
|
| 83 |
+
best_dist = float("inf")
|
| 84 |
+
for sid, sdata in students_db.items():
|
| 85 |
+
db_emb = np.array(sdata["embedding"])
|
| 86 |
+
dist = np.linalg.norm(db_emb - emb)
|
| 87 |
+
if dist < best_dist:
|
| 88 |
+
best_dist = dist
|
| 89 |
+
best = {"id": sid, "name": sdata["name"], "email": sdata["email"]}
|
| 90 |
+
|
| 91 |
+
THRESHOLD = 0.8
|
| 92 |
+
if best_dist <= THRESHOLD:
|
| 93 |
+
session['student_id'] = best['id']
|
| 94 |
+
return jsonify({"status":"ok", "student": best, "distance": float(best_dist)}), 200
|
| 95 |
+
else:
|
| 96 |
+
return jsonify({"status":"no-match", "distance": float(best_dist)}), 401
|
| 97 |
+
|
| 98 |
+
@app.route("/students", methods=["GET"])
|
| 99 |
+
def list_students():
|
| 100 |
+
return jsonify(students_db)
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
if __name__ == "__main__":
|
| 104 |
+
app.run(host="0.0.0.0", port=7860)
|
| 105 |
+
|