import sys from pathlib import 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.middleware.cors import CORSMiddleware from app.config import settings from app.Hackathon_setup import face_recognition, exp_recognition import numpy as np from PIL import Image app = FastAPI( title=settings.PROJECT_NAME, openapi_url=f"{settings.API_V1_STR}/openapi.json" ) # Mount static folder app.mount("/static", StaticFiles(directory="app/static"), name="static") # Templates folder templates = Jinja2Templates(directory="app/templates") #################################### Home Page #################################### @app.get("/") async def root(request: Request): return templates.TemplateResponse("index.html", {"request": request}) #################################### Face Similarity #################################### @app.get("/similarity/") async def similarity_root(request: Request): return templates.TemplateResponse("similarity.html", {"request": request}) @app.post("/predict_similarity/") async def predict_similarity(request: Request, file1: UploadFile = File(...), file2: UploadFile = File(...)): # Save uploaded images path1 = f'app/static/{file1.filename}' path2 = f'app/static/{file2.filename}' contents1 = await file1.read() with open(path1, 'wb') as f: f.write(contents1) contents2 = await file2.read() with open(path2, 'wb') as f: f.write(contents2) # Open images as RGB img1 = np.array(Image.open(path1).convert("RGB")) img2 = np.array(Image.open(path2).convert("RGB")) # Compute similarity result = face_recognition.get_similarity(img1, img2) return templates.TemplateResponse("predict_similarity.html", { "request": request, "result": result, # <-- only this line changed "simi_filename1": f"/static/{file1.filename}", "simi_filename2": f"/static/{file2.filename}" }) #################################### Face Recognition #################################### @app.get("/face_recognition/") async def face_recognition_root(request: Request): return templates.TemplateResponse("face_recognition.html", {"request": request}) @app.post("/predict_face_recognition/") async def predict_face_recognition(request: Request, file3: UploadFile = File(...)): path = f'app/static/{file3.filename}' contents = await file3.read() with open(path, 'wb') as f: f.write(contents) img = np.array(Image.open(path).convert("RGB")) result = face_recognition.get_face_class(img) return templates.TemplateResponse("predict_face_recognition.html", { "request": request, "result": result, "face_rec_filename": f"/static/{file3.filename}" }) #################################### Expression Recognition #################################### @app.get("/expr_recognition/") async def expr_recognition_root(request: Request): return templates.TemplateResponse("expr_recognition.html", {"request": request}) @app.post("/predict_expr_recognition/") async def predict_expr_recognition(request: Request, file4: UploadFile = File(...)): path = f'app/static/{file4.filename}' contents = await file4.read() with open(path, 'wb') as f: f.write(contents) img = np.array(Image.open(path).convert("RGB")) result = exp_recognition.get_expression(img) return templates.TemplateResponse("predict_expr_recognition.html", { "request": request, "result": result, "expr_rec_filename": f"/static/{file4.filename}" }) #################################### CORS Middleware #################################### if settings.BACKEND_CORS_ORIGINS: app.add_middleware( CORSMiddleware, allow_origins=[str(origin) for origin in settings.BACKEND_CORS_ORIGINS], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) #################################### Run App #################################### if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8001)