""" Simple local FastAPI server for testing face recognition Run this to test the web interface locally """ import sys import os from pathlib import Path # Add the parent directory to Python path sys.path.append(str(Path(__file__).resolve().parent.parent)) from fastapi import FastAPI, Request, File, UploadFile from fastapi.staticfiles import StaticFiles from fastapi.templating import Jinja2Templates from fastapi.responses import HTMLResponse import numpy as np from PIL import Image import uvicorn # Import face recognition from app.Hackathon_setup import face_recognition app = FastAPI(title="Local Face Recognition Test") # Mount static files app.mount("/static", StaticFiles(directory="app/static"), name="static") # Templates templates = Jinja2Templates(directory="app/templates") @app.get("/", response_class=HTMLResponse) async def root(): """Simple HTML interface for testing""" html_content = """ Face Recognition Test

🧠 Face Recognition Test

Upload a face image to test the classification locally.


""" return HTMLResponse(content=html_content) @app.post("/predict") async def predict_face(file: UploadFile = File(...)): """Predict face class from uploaded image""" try: # Save uploaded file contents = await file.read() filename = f"app/static/{file.filename}" with open(filename, 'wb') as f: f.write(contents) # Load and process image img = Image.open(filename) img_array = np.array(img).reshape(img.size[1], img.size[0], 3).astype(np.uint8) # Get face class result = face_recognition.get_face_class(img_array) return f"Predicted Face Class: {result}" except Exception as e: return f"Error: {str(e)}" @app.get("/test") async def test_endpoint(): """Simple test endpoint""" try: from app.Hackathon_setup.face_recognition import CLASS_NAMES import joblib # Test model loading classifier = joblib.load('app/Hackathon_setup/decision_tree_model.sav') scaler = joblib.load('app/Hackathon_setup/face_recognition_scaler.sav') return { "status": "success", "class_names": CLASS_NAMES, "classifier_classes": classifier.classes_.tolist(), "scaler_features": scaler.n_features_in_ } except Exception as e: return {"status": "error", "message": str(e)} if __name__ == "__main__": print("Starting local Face Recognition test server...") print("Open your browser and go to: http://localhost:8000") print("Press Ctrl+C to stop the server") uvicorn.run(app, host="0.0.0.0", port=8000)