Spaces:
Runtime error
Runtime error
File size: 9,132 Bytes
840c040 d63503f 840c040 d63503f 840c040 d63503f 840c040 d63503f 840c040 d63503f 840c040 d63503f 840c040 d63503f 840c040 d63503f 840c040 d63503f 840c040 d63503f 840c040 | 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | """
Face Verification API - Python backend for face enrollment and authentication.
Replaces FaceIO with a self-hosted solution using face_recognition library.
"""
import base64
import io
import os
import json
import math
from typing import Optional, List
from contextlib import asynccontextmanager
import numpy as np
import face_recognition
from PIL import Image
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Configuration
FACE_MATCH_THRESHOLD = float(os.getenv("FACE_MATCH_THRESHOLD", "0.6"))
ALLOWED_ORIGINS = os.getenv("ALLOWED_ORIGINS", "*").split(",")
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Application lifespan handler."""
print("๐ Face Verification API starting...")
print(f" Match threshold: {FACE_MATCH_THRESHOLD}")
yield
print("๐ Face Verification API shutting down...")
app = FastAPI(
title="Face Verification API",
description="Self-hosted face enrollment and authentication service",
version="1.0.0",
lifespan=lifespan
)
# Configure CORS
app.add_middleware(
CORSMiddleware,
allow_origins=ALLOWED_ORIGINS if ALLOWED_ORIGINS[0] != "*" else ["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Request/Response Models
class EnrollRequest(BaseModel):
"""Request model for face enrollment."""
image_base64: str
user_id: str
metadata: Optional[dict] = None
class EnrollResponse(BaseModel):
"""Response model for face enrollment."""
success: bool
face_id: str
face_embedding: List[float]
message: str
class VerifyRequest(BaseModel):
"""Request model for face verification."""
image_base64: str
stored_embedding: List[float]
user_id: Optional[str] = None
class VerifyResponse(BaseModel):
"""Response model for face verification."""
success: bool
match: bool
confidence: float
distance: float
message: str
class HealthResponse(BaseModel):
"""Response model for health check."""
status: str
version: str
def decode_base64_image(base64_string: str) -> np.ndarray:
"""
Decode a base64 image string to a numpy array for face_recognition.
Supports both raw base64 and data URI format.
"""
# Remove data URI prefix(es) if present
# Handle cases like "data:image/jpeg;base64,data:image/png;base64,..."
if "," in base64_string:
base64_string = base64_string.split(",")[-1]
# Clean up the string and fix padding
base64_string = base64_string.strip()
padding = len(base64_string) % 4
if padding > 0:
base64_string += "=" * (4 - padding)
try:
image_bytes = base64.b64decode(base64_string)
image = Image.open(io.BytesIO(image_bytes))
# Convert to RGB if necessary (face_recognition requires RGB)
if image.mode != "RGB":
image = image.convert("RGB")
return np.array(image)
except Exception as e:
raise HTTPException(
status_code=400,
detail=f"Invalid image data: {str(e)}"
)
def extract_face_encoding(image: np.ndarray) -> np.ndarray:
"""
Extract face encoding from an image.
Returns the 128-dimensional face encoding.
"""
# Detect face locations
face_locations = face_recognition.face_locations(image, model="hog")
if len(face_locations) == 0:
raise HTTPException(
status_code=400,
detail="No face detected in the image. Please ensure your face is clearly visible."
)
if len(face_locations) > 1:
raise HTTPException(
status_code=400,
detail="Multiple faces detected. Please ensure only one face is visible."
)
# Extract face encoding
face_encodings = face_recognition.face_encodings(image, face_locations)
if len(face_encodings) == 0:
raise HTTPException(
status_code=400,
detail="Could not extract face features. Please try again with better lighting."
)
return face_encodings[0]
def face_distance_to_confidence(face_distance: float, face_match_threshold: float) -> float:
"""
Convert a face distance to a confidence score between 0.0 and 1.0.
Based on the reference implementation from face_recognition docs.
"""
if face_distance > face_match_threshold:
range_val = 1.0 - face_match_threshold
linear_val = (1.0 - face_distance) / (range_val * 2.0)
return max(0.0, min(1.0, linear_val))
range_val = face_match_threshold
linear_val = 1.0 - (face_distance / (range_val * 2.0))
return max(0.0, min(1.0, linear_val + ((1.0 - linear_val) * math.pow((linear_val - 0.5) * 2, 0.2))))
def compare_faces(stored_embedding: List[float], new_embedding: np.ndarray) -> tuple:
"""
Compare two face embeddings and return match result and confidence.
"""
stored_array = np.asarray(stored_embedding, dtype=np.float64)
if stored_array.shape != (128,) or not np.isfinite(stored_array).all():
raise HTTPException(
status_code=400,
detail="Invalid stored embedding values. Expected 128 numeric values."
)
# Calculate face distance (lower = more similar)
face_distance = float(face_recognition.face_distance([stored_array], new_embedding)[0])
# Convert distance to confidence (invert so higher = more confident match)
confidence = face_distance_to_confidence(face_distance, FACE_MATCH_THRESHOLD)
# Check if faces match based on threshold
is_match = face_distance <= FACE_MATCH_THRESHOLD
return is_match, float(confidence), face_distance
@app.get("/health", response_model=HealthResponse)
async def health_check():
"""Health check endpoint."""
return HealthResponse(
status="healthy",
version="1.0.0"
)
@app.post("/enroll", response_model=EnrollResponse)
async def enroll_face(request: EnrollRequest):
"""
Enroll a new face - extract and return face embedding for storage.
The embedding should be stored in the database by the client app.
"""
try:
# Decode image
image = decode_base64_image(request.image_base64)
# Extract face encoding
face_encoding = extract_face_encoding(image)
# Generate a simple face ID (timestamp-based)
import time
face_id = f"face_{request.user_id}_{int(time.time() * 1000)}"
# Convert encoding to list for JSON serialization
embedding_list = face_encoding.tolist()
return EnrollResponse(
success=True,
face_id=face_id,
face_embedding=embedding_list,
message="Face enrolled successfully. Store the embedding securely."
)
except HTTPException:
raise
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"Face enrollment failed: {str(e)}"
)
@app.post("/verify", response_model=VerifyResponse)
async def verify_face(request: VerifyRequest):
"""
Verify a face against a stored embedding.
Returns whether the faces match and the confidence score.
"""
try:
# Validate stored embedding
if not request.stored_embedding or len(request.stored_embedding) != 128:
raise HTTPException(
status_code=400,
detail="Invalid stored embedding. Expected 128-dimensional vector."
)
# Decode image
image = decode_base64_image(request.image_base64)
# Extract face encoding from new image
face_encoding = extract_face_encoding(image)
# Compare faces
is_match, confidence, distance = compare_faces(request.stored_embedding, face_encoding)
if is_match:
message = f"Face verified successfully (confidence: {confidence:.1%})"
else:
message = f"Face verification failed - faces do not match (confidence: {confidence:.1%})"
return VerifyResponse(
success=is_match,
match=is_match,
confidence=confidence,
distance=distance,
message=message
)
except HTTPException:
raise
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"Face verification failed: {str(e)}"
)
@app.get("/")
async def root():
"""Root endpoint with API info."""
return {
"name": "Face Verification API",
"version": "1.0.0",
"endpoints": {
"/health": "Health check",
"/enroll": "Enroll a new face (POST)",
"/verify": "Verify a face against stored embedding (POST)"
}
}
if __name__ == "__main__":
import uvicorn
port = int(os.getenv("PORT", "8000"))
uvicorn.run(app, host="0.0.0.0", port=port)
|