from fastapi import FastAPI, UploadFile, File, HTTPException from fastapi.middleware.cors import CORSMiddleware import shutil import os import uuid from gait_analysis import EnhancedGaitAnalyzer import uvicorn app = FastAPI() # Allow CORS for frontend app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Initialize Analyzer analyzer = EnhancedGaitAnalyzer() UPLOAD_DIR = "uploads" if not os.path.exists(UPLOAD_DIR): os.makedirs(UPLOAD_DIR) @app.get("/") def read_root(): return {"message": "Gait Analysis API with OpenPose is running"} @app.post("/analyze") async def analyze_video(file: UploadFile = File(...)): try: # Save uploaded file file_ext = file.filename.split(".")[-1] filename = f"{uuid.uuid4()}.{file_ext}" file_path = os.path.join(UPLOAD_DIR, filename) with open(file_path, "wb") as buffer: shutil.copyfileobj(file.file, buffer) # Process video results = analyzer.process_video(file_path) # Cleanup # os.remove(file_path) # Keep for debugging for now or serve back return {"filename": filename, "results": results} except Exception as e: raise HTTPException(status_code=500, detail=str(e)) if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=8000)