Spaces:
Sleeping
Sleeping
File size: 1,352 Bytes
b2df149 9602c19 b2df149 9602c19 |
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 |
from fastapi import FastAPI, UploadFile, File
from fastapi.responses import JSONResponse
import mediapipe as mp
import numpy as np
from PIL import Image
import io
app = FastAPI()
mp_pose = mp.solutions.pose
pose = mp_pose.Pose(
static_image_mode=True,
model_complexity=1,
enable_segmentation=False
)
@app.post("/analyze_pose")
async def analyze_pose(image: UploadFile = File(...)):
try:
data = await image.read()
pil_image = Image.open(io.BytesIO(data)).convert("RGB")
np_img = np.array(pil_image)
# MediaPipe는 RGB를 사용하지만, OpenCV 기반 변환이 필요할 수 있어 BGR로 변환
# (일반적인 MediaPipe 사용 예제에서는 RGB를 넣지만, cv2 함수를 섞어 쓸 때 주의 필요)
# 작성하신 코드대로라면 BGR로 변환해서 넣고 있습니다.
bgr = np_img[:, :, ::-1]
result = pose.process(bgr)
if not result.pose_landmarks:
return {"landmarks": None}
landmarks = []
for lm in result.pose_landmarks.landmark:
landmarks.append({
"x": lm.x,
"y": lm.y,
"z": lm.z,
"visibility": lm.visibility
})
return {"landmarks": landmarks}
except Exception as e:
return {"error": str(e)} |