File size: 6,310 Bytes
014cef7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | import io
import math
import os
import tempfile
import uuid
import zipfile
import cv2
import mediapipe as mp
from fastapi import FastAPI, File, UploadFile, HTTPException
from fastapi.responses import StreamingResponse, JSONResponse
from mediapipe.tasks.python import BaseOptions
from mediapipe.tasks.python.vision import (
PoseLandmarker,
PoseLandmarkerOptions,
RunningMode,
)
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
MODEL_PATH = os.path.join(SCRIPT_DIR, "pose_landmarker_heavy.task")
# Landmark indices
LEFT_SHOULDER = 11
RIGHT_SHOULDER = 12
LEFT_HIP = 23
RIGHT_HIP = 24
TARGETS = {
"front": 0,
"front_45_clockwise": 45,
"right_side": 90,
"back_45_clockwise": 135,
"back": 180,
"back_45_anticlockwise": -135,
"left_side": -90,
"front_45_anticlockwise": -45,
}
app = FastAPI(title="Pose Frame Extractor API")
def estimate_body_angle(world_landmarks):
ls = world_landmarks[LEFT_SHOULDER]
rs = world_landmarks[RIGHT_SHOULDER]
lh = world_landmarks[LEFT_HIP]
rh = world_landmarks[RIGHT_HIP]
s_dx = ls.x - rs.x
s_dz = ls.z - rs.z
h_dx = lh.x - rh.x
h_dz = lh.z - rh.z
dx = (s_dx + h_dx) / 2
dz = (s_dz + h_dz) / 2
angle_rad = math.atan2(dz, dx)
return math.degrees(angle_rad)
def angle_distance(a, b):
diff = (a - b + 180) % 360 - 180
return abs(diff)
def process_video(video_path: str):
"""Process video and return dict of pose_name -> (png_bytes, metadata)."""
if not os.path.exists(MODEL_PATH):
raise HTTPException(status_code=500, detail="Pose model file not found on server.")
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
raise HTTPException(status_code=400, detail="Cannot open uploaded video.")
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
fps = cap.get(cv2.CAP_PROP_FPS)
if fps == 0:
cap.release()
raise HTTPException(status_code=400, detail="Invalid video: 0 FPS detected.")
options = PoseLandmarkerOptions(
base_options=BaseOptions(model_asset_path=MODEL_PATH),
running_mode=RunningMode.VIDEO,
num_poses=1,
min_pose_detection_confidence=0.5,
min_pose_presence_confidence=0.5,
min_tracking_confidence=0.5,
)
landmarker = PoseLandmarker.create_from_options(options)
best = {
name: {"diff": float("inf"), "frame": None, "angle": None, "frame_idx": -1}
for name in TARGETS
}
frame_idx = 0
while True:
ret, frame = cap.read()
if not ret:
break
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=rgb)
timestamp_ms = int(frame_idx * 1000 / fps)
results = landmarker.detect_for_video(mp_image, timestamp_ms)
if results.pose_world_landmarks and len(results.pose_world_landmarks) > 0:
world_lms = results.pose_world_landmarks[0]
angle = estimate_body_angle(world_lms)
for name, target in TARGETS.items():
diff = angle_distance(angle, target)
if diff < best[name]["diff"]:
best[name] = {
"diff": diff,
"frame": frame.copy(),
"angle": angle,
"frame_idx": frame_idx,
}
frame_idx += 1
cap.release()
landmarker.close()
# Encode frames as PNGs
results_out = {}
for name, target_angle in TARGETS.items():
info = best[name]
if info["frame"] is None:
continue
suffix = "" if info["diff"] <= 15 else "_approx"
filename = f"{name}{suffix}.png"
_, buf = cv2.imencode(".png", info["frame"])
results_out[filename] = {
"png_bytes": buf.tobytes(),
"frame_idx": info["frame_idx"],
"detected_angle": round(info["angle"], 1),
"target_angle": target_angle,
"error": round(info["diff"], 1),
}
return results_out, total_frames, fps
@app.post("/extract-poses")
async def extract_poses(video: UploadFile = File(...)):
"""Upload a video and get back a ZIP of extracted pose frames."""
# Save uploaded video to a temp file
suffix = os.path.splitext(video.filename or "video.mp4")[1]
with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as tmp:
tmp.write(await video.read())
tmp_path = tmp.name
try:
results, total_frames, fps = process_video(tmp_path)
finally:
os.unlink(tmp_path)
if not results:
raise HTTPException(status_code=422, detail="No poses detected in video.")
# Build a ZIP in memory
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
for filename, data in results.items():
zf.writestr(filename, data["png_bytes"])
zip_buffer.seek(0)
return StreamingResponse(
zip_buffer,
media_type="application/zip",
headers={"Content-Disposition": "attachment; filename=pose_frames.zip"},
)
@app.post("/extract-poses-json")
async def extract_poses_json(video: UploadFile = File(...)):
"""Upload a video and get back JSON metadata (no images, just info)."""
suffix = os.path.splitext(video.filename or "video.mp4")[1]
with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as tmp:
tmp.write(await video.read())
tmp_path = tmp.name
try:
results, total_frames, fps = process_video(tmp_path)
finally:
os.unlink(tmp_path)
summary = []
for filename, data in results.items():
summary.append({
"filename": filename,
"frame_idx": data["frame_idx"],
"detected_angle": data["detected_angle"],
"target_angle": data["target_angle"],
"error_degrees": data["error"],
})
return JSONResponse({
"video_info": {
"total_frames": total_frames,
"fps": round(fps, 1),
"duration_seconds": round(total_frames / fps, 2),
},
"poses": summary,
})
@app.get("/health")
async def health():
return {"status": "ok", "model_loaded": os.path.exists(MODEL_PATH)}
|