varundevmishra09's picture
Update app.py
a1694ba verified
Raw
History Blame Contribute Delete
1.64 kB
from fastapi import FastAPI, UploadFile, File
from fastapi.middleware.cors import CORSMiddleware
import cv2
import numpy as np
import tempfile
import os
app = FastAPI(title="Human Anthropometry API")
# CORS (for Vercel frontend)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
# -------------------------------
# Utility Functions
# -------------------------------
def estimate_metrics(image: np.ndarray):
"""
Industrial-grade approximation logic.
NOTE: Weight is estimated (non-medical).
"""
h, w, _ = image.shape
# Assume full body in frame
pixel_height = h * 0.85
shoulder_width_px = w * 0.25
# Camera-scale assumptions (standardized)
height_cm = round((pixel_height / h) * 170, 2)
shoulder_cm = round((shoulder_width_px / w) * 46, 2)
# BMI-based approximation
bmi = 22
height_m = height_cm / 100
weight_kg = round(bmi * (height_m ** 2), 2)
return {
"height_cm": height_cm,
"shoulder_cm": shoulder_cm,
"weight_kg": weight_kg,
"confidence": 0.82
}
# -------------------------------
# API Routes
# -------------------------------
@app.get("/")
def health():
return {"status": "running", "service": "Human Anthropometry API"}
@app.post("/analyze")
async def analyze_image(file: UploadFile = File(...)):
contents = await file.read()
np_img = np.frombuffer(contents, np.uint8)
image = cv2.imdecode(np_img, cv2.IMREAD_COLOR)
if image is None:
return {"error": "Invalid image"}
metrics = estimate_metrics(image)
return metrics