""" 姿势引导图:按均匀的动作幅度,程序化画出每一帧的“人偶”姿势,作为 Codex 生成连续帧的参考。 只用文字描述时,模型常把相邻帧画得忽大忽小、左右脚画反;给它一张逐帧的人偶图, 帧间距和左右都由程序决定。被拍者的左侧肢体 = 橙色(在画面右侧),右侧肢体 = 蓝色,躯干/头 = 灰色。 用法:python3 coach_pose_guide.py <输出.png> """ import math import sys from PIL import Image, ImageDraw W, H = 1672, 941 # Codex 生成 16:9 图的实际尺寸 ORANGE = (255, 122, 0) BLUE = (30, 107, 255) GRAY = (150, 156, 166) HEAD_FILL = (205, 209, 216) # 人偶比例(单位 = 站姿身高,原点 = 两脚中点的地面,y 向上) HEAD_TOP, HEAD_R = 1.0, 0.085 NECK = 0.80 SH_Y, SH_HALF = 0.775, 0.105 HIP_Y, HIP_HALF = 0.49, 0.062 THIGH, SHIN = 0.235, 0.225 UPPER_ARM, FOREARM, HAND = 0.155, 0.14, 0.05 FOOT = 0.06 def outward(length, deg_from_down, side): """从竖直向下开始、向身体外侧转 deg 度的向量;side +1 = 被拍者左侧(画面右侧)""" a = math.radians(deg_from_down) return side * length * math.sin(a), -length * math.cos(a) class Pose: def __init__(self): self.hip_drop = 0.0 self.stance = 0.07 # 两脚间距的一半 self.arm = {s: dict(abd=6.0, elbow=0.0, fwd=0.0) for s in (1, -1)} self.leg = {s: dict(abd=0.0, lift=0.0) for s in (1, -1)} def draw_pose(d: ImageDraw.ImageDraw, p: Pose, cx: float, ground: float, scale: float): def P(x, y): return cx + x * scale, ground - y * scale lw = max(4, int(scale * 0.028)) def joints(points, col, hand=None): d.line([P(*q) for q in points], fill=col, width=lw, joint="curve") for q in points[:-1]: x, y = P(*q) d.ellipse([x - lw * 0.7, y - lw * 0.7, x + lw * 0.7, y + lw * 0.7], fill=col) if hand: x, y = P(*hand) d.ellipse([x - lw, y - lw, x + lw, y + lw], fill=col) hip_y, sh_y = HIP_Y - p.hip_drop, SH_Y - p.hip_drop for s, col in ((1, ORANGE), (-1, BLUE)): leg = p.leg[s] hip = (s * HIP_HALF, hip_y) if leg["lift"] > 0: # 抬膝:大腿向前抬(正面投影变短),小腿自然下垂;膝盖高度随 lift 线性上升 th = math.acos(1 - leg["lift"]) knee = (hip[0] + s * 0.01, hip[1] - THIGH * math.cos(th)) ankle = (knee[0] + s * 0.005, knee[1] - SHIN * 0.97) else: dx, dy = outward(THIGH + SHIN, leg["abd"], s) ankle = (s * p.stance + dx, max(0.035, hip[1] + dy)) bend = s * 0.012 * p.hip_drop / 0.05 if p.hip_drop else 0 knee = ((hip[0] + ankle[0]) / 2 + bend, (hip[1] + ankle[1]) / 2 + 0.004) joints([hip, knee, ankle, (ankle[0] + s * FOOT * 0.6, ankle[1] - 0.03)], col) d.polygon([P(-SH_HALF, sh_y), P(SH_HALF, sh_y), P(HIP_HALF * 1.25, hip_y - 0.01), P(-HIP_HALF * 1.25, hip_y - 0.01)], fill=GRAY) d.line([P(0, sh_y), P(0, NECK - p.hip_drop + 0.03)], fill=GRAY, width=int(lw * 1.6)) hx, hy = P(0, HEAD_TOP - HEAD_R * 1.25 - p.hip_drop) r = HEAD_R * scale d.ellipse([hx - r * 0.86, hy - r, hx + r * 0.86, hy + r], fill=HEAD_FILL, outline=GRAY, width=2) for s, col in ((1, ORANGE), (-1, BLUE)): arm = p.arm[s] sh = (s * SH_HALF, sh_y) shrink = 1 - 0.75 * arm["fwd"] ex, ey = outward(UPPER_ARM * shrink, arm["abd"], s) elbow = (sh[0] + ex, sh[1] + ey) if arm["elbow"] > 0: # 屈肘前摆:前臂向身体内侧、向上折,正面看拳头抬到胸前 bend = math.radians(arm["elbow"]) wrist = (elbow[0] - s * FOREARM * math.sin(bend) * 0.55 + s * 0.01, elbow[1] - FOREARM * math.cos(bend)) hand = (wrist[0] - s * 0.012, wrist[1] + 0.03 * math.sin(bend)) else: wx, wy = outward(FOREARM * shrink, arm["abd"], s) wrist = (elbow[0] + wx, elbow[1] + wy) hx2, hy2 = outward(HAND * shrink, arm["abd"], s) hand = (wrist[0] + hx2, wrist[1] + hy2) joints([sh, elbow, wrist, hand], col, hand=hand) def jack(a: float, side: int) -> Pose: """低冲击开合跳:双臂经体侧举过头顶成 V,side 侧腿向外点地,支撑腿微屈""" p = Pose() p.stance = 0.045 for s in (1, -1): p.arm[s] = dict(abd=8 + 157 * a, elbow=0, fwd=0.0) p.leg[side]["abd"] = 26 * a p.hip_drop = (THIGH + SHIN) * (1 - math.cos(math.radians(26 * a))) * 0.9 return p def raise_(a: float) -> Pose: """站姿侧平举:只有手臂动,从体侧抬到与肩同高""" p = Pose() for s in (1, -1): p.arm[s] = dict(abd=6 + 84 * a, elbow=0, fwd=0.0) return p def sheet(poses, rows, cols, fig_h, out): img = Image.new("RGB", (W, H), "white") d = ImageDraw.Draw(img) cell_w, cell_h = W / cols, H / rows for i, p in enumerate(poses): r, c = divmod(i, cols) draw_pose(d, p, cell_w * (c + 0.5), cell_h * (r + 1) - cell_h * 0.06, fig_h) img.save(out) GUIDES = { # 动作: (帧数, 行, 列, 人偶身高, 幅度 → 姿势) "raise": (18, 3, 6, 262, raise_), "jack": (18, 3, 6, 262, lambda a: jack(a, 1)), } if __name__ == "__main__": move, out = sys.argv[1], sys.argv[2] n, rows, cols, fig_h, pose_at = GUIDES[move] sheet([pose_at(i / (n - 1)) for i in range(n)], rows, cols, fig_h, out) print("已生成", out)