姿势即数据:使用 MediaPipe 和 GPT-4o 构建实时 AI 物理治疗师
Source: Dev.to
在家进行物理治疗(PT)是一把双刃剑。一方面,你可以穿着睡衣进行锻炼;另一方面,你根本不知道自己的“深蹲”是更像优雅的鹤,还是折叠的草坪椅。错误的姿势不仅效果不佳,还可能带来危险。
在本教程中,我们将弥合原始 Computer Vision 与 Generative AI 之间的鸿沟。我们将构建一个系统,使用 MediaPipe 进行 real‑time pose estimation(实时姿态估计),将骨骼数据序列化为 JSON,并通过 WebSockets 将其传输到 GPT‑4o‑mini,从而提供专业级的纠正反馈。无论你对 AI‑driven fitness(AI 驱动的健身)、human‑computer interaction(人机交互)还是 real‑time multimodal LLMs(实时多模态大语言模型)感兴趣,本指南都涵盖了现代视觉‑到‑文本流水线的完整技术栈。
架构:从像素到处方
我们如何将视频流转化为可操作的医疗建议?秘诀在于将“姿势视为数据”。我们不将原始视频帧发送给大型语言模型(这既昂贵又慢),而是提取骨骼关节的三维坐标,并发送运动的数学表示。
graph TD
A[User Webcam] -->|Video Frame| B(MediaPipe Pose)
B -->|3D Landmarks| C{Data Processor}
C -->|JSON Skeleton| D[FastAPI WebSocket]
D -->|Contextual Prompt| E[GPT‑4o‑mini]
E -->|Corrective Text/Speech| F[Frontend UI]
F -->|Real‑time Feedback| A
前置条件
要跟随操作,您需要:
- MediaPipe – 高保真人体追踪。
- OpenCV – 视频流处理。
- FastAPI / WebSockets – 低延迟通信。
- OpenAI SDK – 访问 GPT‑4o‑mini 的推理能力。
步骤 1:使用 MediaPipe 提取骨骼关键点
首先,我们需要从视频流中提取 33 个关键的骨骼标记点(肩部、膝盖、踝部等)。MediaPipe 为每个标记点提供 (x, y, z) 坐标。
import cv2
import mediapipe as mp
mp_pose = mp.solutions.pose
pose = mp_pose.Pose(static_image_mode=False, min_detection_confidence=0.5)
def get_skeletal_data(frame):
# Convert the BGR image to RGB
image_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = pose.process(image_rgb)
if not results.pose_landmarks:
return None
# Extract key points relevant to the exercise (e.g., squat)
landmarks = results.pose_landmarks.landmark
data = {
"left_knee": {"x": landmarks[25].x, "y": landmarks[25].y, "z": landmarks[25].z},
"right_knee": {"x": landmarks[26].x, "y": landmarks[26].y, "z": landmarks[26].z},
"hip": {"x": landmarks[24].x, "y": landmarks[24].y}
}
return data
第2步:实时管道(WebSockets)
由于我们需要“实时”反馈,不能等到标准的 REST 请求完成。我们将使用 WebSockets 将关键点数据流式传输到后端。
from fastapi import FastAPI, WebSocket
import json
app = FastAPI()
@app.websocket("/ws/rehab")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
while True:
# Receive skeletal data from the frontend/client
client_data = await websocket.receive_text()
skeleton = json.loads(client_data)
# Trigger GPT‑4o analysis every 30 frames or on specific movement triggers
feedback = analyze_pose_with_gpt(skeleton)
await websocket.send_text(feedback)
第3步:喂入“物理治疗师”(GPT‑4o‑mini)
魔法发生在提示中。我们向大型语言模型提供坐标数据,并让它充当 物理治疗师。
import openai
def analyze_pose_with_gpt(skeleton_data):
prompt = f"""
You are a professional Physical Therapist.
Analyze these 3D coordinates of a patient performing a squat:
{skeleton_data}
If the knee y‑coordinate is higher than the hip y‑coordinate, they aren't deep enough.
If the knees are converging (check x‑coordinates), warn about valgus stress.
Provide a 1‑sentence concise correction.
"""
response = openai.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
max_tokens=50
)
return response.choices[0].message.content
专业提示:高级实现模式
虽然上述方法可以让你快速得到 MVP,但要打造可投入生产的医疗或健身应用,还必须处理:
- 抖动与遮挡(当身体部位被遮挡时)
- 时序分析(将当前帧与最近的 10 帧进行比较)
可以考虑使用卡尔曼滤波器对关键点进行平滑,或在视觉任务中优化大语言模型(LLM)的 token 使用。想深入了解,查看 WellAlly 博客——这是帮助开发者从“炫酷演示”迈向“稳健 AI 产品”的绝佳资源。
结论
通过将 pose as data 视为数据,我们将“视觉”和“智能”解耦。MediaPipe 负责空间几何的繁重计算,而 GPT‑4o‑mini 则提供细致的人类指令。
接下来?
- 添加 TTS(文本转语音) – 使用 OpenAI 的 Whisper 或 ElevenLabs,使 “AI Coach” 能实时与用户对话。
- 时序逻辑 – 发送坐标序列,让 AI 能分析运动的 节奏,而不仅是静态帧。
你正在构建 AI 驱动的健身项目吗?分享你的进展吧!🚀
健康领域?在下方留言或分享你的仓库!
让我们一起构建运动的未来。
