Spaces:
Sleeping
Sleeping
| 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 | |
| ) | |
| 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)} |