from fastapi import FastAPI, File, UploadFile, HTTPException from fastapi.responses import JSONResponse import shutil import os from models.feature_extractor import extract_features app = FastAPI() @app.get("/") def root(): return {"message": "Feature extraction API is running!"} @app.post("/extractfacialfeatures") async def extract_features_endpoint(file: UploadFile = File(...)): if not file: raise HTTPException(status_code=400, detail="No file provided") # Log file info print(f"Received file: {file.filename}, Content-Type: {file.content_type}") # Save the uploaded file temporarily os.makedirs("temp", exist_ok=True) file_location = f"temp/{file.filename}" with open(file_location, "wb") as buffer: shutil.copyfileobj(file.file, buffer) # Optionally log file size file_size = os.path.getsize(file_location) print(f"Saved file size: {file_size} bytes") try: features = extract_features(file_location) return JSONResponse(content=features) except Exception as e: raise HTTPException(status_code=500, detail=str(e)) finally: os.remove(file_location)