Posture state machine + device-work animation (laptop spring, typing bursts, gaze/blink)
Browse files- freestream/avatar.py +161 -19
freestream/avatar.py
CHANGED
|
@@ -6,12 +6,23 @@ the synthesized audio's per-frame energy. With a user-provided portrait it
|
|
| 6 |
degrades to an audio-reactive waveform badge (rung 2) rather than
|
| 7 |
pretending to animate someone's face without a portrait model.
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
The generated PNG carries tEXt metadata `freestream:procedural=1` so the
|
| 10 |
bot knows it can re-render the mouth.
|
| 11 |
"""
|
| 12 |
|
| 13 |
from __future__ import annotations
|
| 14 |
|
|
|
|
|
|
|
|
|
|
| 15 |
import numpy as np
|
| 16 |
from PIL import Image, PngImagePlugin
|
| 17 |
|
|
@@ -27,14 +38,27 @@ def _face_params(openness: float) -> dict:
|
|
| 27 |
}
|
| 28 |
|
| 29 |
|
| 30 |
-
def
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
p = _face_params(openness)
|
| 33 |
img = Image.new("RGBA", (size, size), (234, 240, 245, 255))
|
| 34 |
from PIL import ImageDraw
|
| 35 |
|
| 36 |
d = ImageDraw.Draw(img)
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
r = int(size * 0.28)
|
| 39 |
# shoulders
|
| 40 |
d.ellipse([cx - r * 2, cy + r, cx + r * 2, cy + r * 3], fill=(58, 110, 165, 255))
|
|
@@ -42,18 +66,56 @@ def render_procedural(openness: float = 0.15, size: int = SIZE) -> Image.Image:
|
|
| 42 |
d.ellipse([cx - r, cy - r, cx + r, cy + r], fill=(243, 213, 187, 255))
|
| 43 |
# hair
|
| 44 |
d.ellipse([cx - r, cy - r - 14, cx + r, cy - r + 42], fill=(66, 62, 78, 255))
|
| 45 |
-
# eyes
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
for ex in (cx - int(r * 0.42), cx + int(r * 0.42)):
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
fill=(90, 44, 52, 255))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
return img
|
| 58 |
|
| 59 |
|
|
@@ -90,7 +152,6 @@ def build_mouth_sequence(pcm: np.ndarray, sample_rate: int, fps: int = FPS,
|
|
| 90 |
.reshape(n, frame_n), axis=1))
|
| 91 |
scale = max(rms.max(), 1e-6)
|
| 92 |
open_seq = np.clip(rms / scale * 1.4, 0.0, 1.0)
|
| 93 |
-
# light smoothing so the mouth doesn't jitter
|
| 94 |
kernel = np.ones(3) / 3.0
|
| 95 |
open_seq = np.convolve(open_seq, kernel, mode="same")
|
| 96 |
return np.concatenate([np.zeros(pre_roll_frames), open_seq,
|
|
@@ -98,26 +159,107 @@ def build_mouth_sequence(pcm: np.ndarray, sample_rate: int, fps: int = FPS,
|
|
| 98 |
|
| 99 |
|
| 100 |
class AvatarRenderer:
|
| 101 |
-
"""Publish-side frame factory
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
def __init__(self, image_path=None):
|
| 105 |
self.procedural = image_path is None or is_procedural(image_path)
|
| 106 |
self.base = None
|
| 107 |
if not self.procedural:
|
| 108 |
self.base = Image.open(image_path).convert("RGBA").resize((SIZE, SIZE))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
if self.procedural:
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
import copy
|
| 114 |
|
| 115 |
img = self.base.copy()
|
| 116 |
from PIL import ImageDraw
|
| 117 |
|
| 118 |
d = ImageDraw.Draw(img)
|
| 119 |
-
# audio-reactive waveform badge (rung 2), bottom strip
|
| 120 |
bar_h = int(4 + 26 * openness)
|
|
|
|
| 121 |
d.rectangle([40, SIZE - 70, SIZE - 40, SIZE - 40], fill=(20, 22, 28, 200))
|
| 122 |
d.rectangle([60, SIZE - 55 - bar_h, 100, SIZE - 55], fill=(80, 200, 120, 255))
|
|
|
|
|
|
|
| 123 |
return img
|
|
|
|
| 6 |
degrades to an audio-reactive waveform badge (rung 2) rather than
|
| 7 |
pretending to animate someone's face without a portrait model.
|
| 8 |
|
| 9 |
+
Device-work states (the Animate/ACE/ClawOffice pattern — see
|
| 10 |
+
docs/LATENCY.md "Avatar posture state machine"): the renderer is a posture
|
| 11 |
+
state machine (idle / thinking / working / speaking) plus continuous
|
| 12 |
+
channels (gaze, blink, head tilt). In `working` mode the avatar pulls a
|
| 13 |
+
laptop up from below (spring-eased entrance, mild overshoot), tilts her
|
| 14 |
+
head toward it, and types in Poisson-ish bursts with keystroke pulses and
|
| 15 |
+
screen glow — driven live by agent.tool activity in the bot.
|
| 16 |
+
|
| 17 |
The generated PNG carries tEXt metadata `freestream:procedural=1` so the
|
| 18 |
bot knows it can re-render the mouth.
|
| 19 |
"""
|
| 20 |
|
| 21 |
from __future__ import annotations
|
| 22 |
|
| 23 |
+
import random
|
| 24 |
+
import time
|
| 25 |
+
|
| 26 |
import numpy as np
|
| 27 |
from PIL import Image, PngImagePlugin
|
| 28 |
|
|
|
|
| 38 |
}
|
| 39 |
|
| 40 |
|
| 41 |
+
def _ease_out_back(t: float) -> float:
|
| 42 |
+
t = max(0.0, min(1.0, t))
|
| 43 |
+
c1, c3 = 1.70158, 2.70158
|
| 44 |
+
return 1 + c3 * (t - 1) ** 3 + c1 * (t - 1) ** 2
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def render_procedural(openness: float = 0.15, size: int = SIZE, mode: str = "idle",
|
| 48 |
+
device_t: float = 0.0, typing_pulse: float = 0.0,
|
| 49 |
+
blink: float = 0.0, gaze=(0, 0)) -> Image.Image:
|
| 50 |
+
"""Cartoon assistant face; mouth follows `openness`, posture follows
|
| 51 |
+
`mode`. `device_t` (0..1, eased) slides the laptop in; `typing_pulse`
|
| 52 |
+
(0..1) bobs the hands and flashes the screen; `blink` closes the eyes."""
|
| 53 |
p = _face_params(openness)
|
| 54 |
img = Image.new("RGBA", (size, size), (234, 240, 245, 255))
|
| 55 |
from PIL import ImageDraw
|
| 56 |
|
| 57 |
d = ImageDraw.Draw(img)
|
| 58 |
+
working = device_t > 0.05
|
| 59 |
+
tilt = int(14 * device_t) if working else 0 # head dips toward the device
|
| 60 |
+
|
| 61 |
+
cx, cy = size // 2, int(size * 0.46) + (tilt // 2)
|
| 62 |
r = int(size * 0.28)
|
| 63 |
# shoulders
|
| 64 |
d.ellipse([cx - r * 2, cy + r, cx + r * 2, cy + r * 3], fill=(58, 110, 165, 255))
|
|
|
|
| 66 |
d.ellipse([cx - r, cy - r, cx + r, cy + r], fill=(243, 213, 187, 255))
|
| 67 |
# hair
|
| 68 |
d.ellipse([cx - r, cy - r - 14, cx + r, cy - r + 42], fill=(66, 62, 78, 255))
|
| 69 |
+
# eyes: gaze channel + blink + device gaze-down
|
| 70 |
+
gx, gy = gaze
|
| 71 |
+
if working:
|
| 72 |
+
gy += int(6 * device_t)
|
| 73 |
+
ey = cy - int(r * 0.25) + tilt
|
| 74 |
for ex in (cx - int(r * 0.42), cx + int(r * 0.42)):
|
| 75 |
+
ex += gx
|
| 76 |
+
if blink > 0.5:
|
| 77 |
+
d.line([ex - 16, ey, ex + 16, ey], fill=(66, 62, 78, 255), width=4)
|
| 78 |
+
else:
|
| 79 |
+
d.ellipse([ex - 16, ey - 16, ex + 16, ey + 16], fill=(255, 255, 255, 255))
|
| 80 |
+
d.ellipse([ex - 7 + gx // 2, ey - 7 + gy, ex + 7 + gx // 2, ey + 7 + gy],
|
| 81 |
+
fill=(40, 40, 48, 255))
|
| 82 |
+
# brows (rise slightly while working — the "checking output" read)
|
| 83 |
+
brow_dy = -3 if working else 0
|
| 84 |
+
d.line([cx - int(r * 0.55), ey - 30 + brow_dy, cx - int(r * 0.28), ey - 34 + brow_dy],
|
| 85 |
+
fill=(66, 62, 78, 255), width=5)
|
| 86 |
+
d.line([cx + int(r * 0.28), ey - 34 + brow_dy, cx + int(r * 0.55), ey - 30 + brow_dy],
|
| 87 |
+
fill=(66, 62, 78, 255), width=5)
|
| 88 |
+
# mouth (concentrating while working: keep it small)
|
| 89 |
+
mo = openness * (0.25 if working else 1.0)
|
| 90 |
+
pm = _face_params(mo)
|
| 91 |
+
my = cy + int(r * 0.45) + tilt
|
| 92 |
+
d.ellipse([cx - pm["mouth_w"], my - pm["mouth_h"], cx + pm["mouth_w"], my + pm["mouth_h"]],
|
| 93 |
fill=(90, 44, 52, 255))
|
| 94 |
+
|
| 95 |
+
if working:
|
| 96 |
+
# laptop slides up from below with easing (device_t 0..1, overshoot ok)
|
| 97 |
+
dy = int((1.0 - device_t) * 160)
|
| 98 |
+
lx0, lx1 = 76, SIZE - 76
|
| 99 |
+
lid_y = SIZE - 150 + dy
|
| 100 |
+
base_y = SIZE - 60 + dy
|
| 101 |
+
# base
|
| 102 |
+
d.rounded_rectangle([lx0, base_y, lx1, base_y + 26], radius=8,
|
| 103 |
+
fill=(52, 56, 66, 255))
|
| 104 |
+
# lid + screen (glow brightens on keystroke pulses)
|
| 105 |
+
glow = int(150 + 80 * typing_pulse)
|
| 106 |
+
d.rounded_rectangle([lx0 + 14, lid_y, lx1 - 14, base_y - 4], radius=6,
|
| 107 |
+
fill=(36, 40, 50, 255))
|
| 108 |
+
d.rectangle([lx0 + 30, lid_y + 14, lx1 - 30, base_y - 18],
|
| 109 |
+
fill=(90, glow // 2, glow, 255))
|
| 110 |
+
# scanlines
|
| 111 |
+
for sy in range(lid_y + 20, base_y - 24, 9):
|
| 112 |
+
d.line([lx0 + 34, sy, lx1 - 34, sy], fill=(120, 160, 180, 60), width=1)
|
| 113 |
+
# hands (bob on keystrokes)
|
| 114 |
+
hand_dy = int(4 * typing_pulse)
|
| 115 |
+
d.ellipse([cx - 120, base_y - 34 + hand_dy, cx - 70, base_y - 10 + hand_dy],
|
| 116 |
+
fill=(243, 213, 187, 255))
|
| 117 |
+
d.ellipse([cx + 70, base_y - 34 + hand_dy, cx + 120, base_y - 10 + hand_dy],
|
| 118 |
+
fill=(243, 213, 187, 255))
|
| 119 |
return img
|
| 120 |
|
| 121 |
|
|
|
|
| 152 |
.reshape(n, frame_n), axis=1))
|
| 153 |
scale = max(rms.max(), 1e-6)
|
| 154 |
open_seq = np.clip(rms / scale * 1.4, 0.0, 1.0)
|
|
|
|
| 155 |
kernel = np.ones(3) / 3.0
|
| 156 |
open_seq = np.convolve(open_seq, kernel, mode="same")
|
| 157 |
return np.concatenate([np.zeros(pre_roll_frames), open_seq,
|
|
|
|
| 159 |
|
| 160 |
|
| 161 |
class AvatarRenderer:
|
| 162 |
+
"""Publish-side frame factory — a posture state machine with continuous
|
| 163 |
+
channels (gaze, blink, typing cadence), per the ACE/Character-Animator
|
| 164 |
+
recipe in docs/LATENCY.md:
|
| 165 |
+
|
| 166 |
+
idle gaze camera, breathing, blinks
|
| 167 |
+
thinking gaze aversion (up/off-screen), small saccades
|
| 168 |
+
working laptop springs in, gaze down, Poisson typing bursts,
|
| 169 |
+
screen glow on each keystroke
|
| 170 |
+
speaking gaze camera, mouth driven by TTS energy
|
| 171 |
+
"""
|
| 172 |
|
| 173 |
def __init__(self, image_path=None):
|
| 174 |
self.procedural = image_path is None or is_procedural(image_path)
|
| 175 |
self.base = None
|
| 176 |
if not self.procedural:
|
| 177 |
self.base = Image.open(image_path).convert("RGBA").resize((SIZE, SIZE))
|
| 178 |
+
# posture state machine
|
| 179 |
+
self._mode = "idle"
|
| 180 |
+
self._device_pos = 0.0 # 0 (away) .. 1 (on desk), spring-eased
|
| 181 |
+
self._device_vel = 0.0
|
| 182 |
+
self._last_t = time.perf_counter()
|
| 183 |
+
# continuous channels
|
| 184 |
+
self._next_blink = time.perf_counter() + random.uniform(2, 5)
|
| 185 |
+
self._blink_until = 0.0
|
| 186 |
+
self._saccade_until = 0.0
|
| 187 |
+
self._saccade = (0, 0)
|
| 188 |
+
# typing cadence: one-shot keystroke pulses (Cycle-Layers analog)
|
| 189 |
+
self._keystrokes: list[float] = []
|
| 190 |
+
self._press_until = 0.0
|
| 191 |
|
| 192 |
+
# -- channels -------------------------------------------------------------
|
| 193 |
+
def _schedule_burst(self, now: float):
|
| 194 |
+
t = now + random.uniform(0.1, 0.4)
|
| 195 |
+
for _ in range(random.randint(2, 5)):
|
| 196 |
+
self._keystrokes.append(t)
|
| 197 |
+
t += random.uniform(0.08, 0.16)
|
| 198 |
+
|
| 199 |
+
def _gaze(self, mode: str, now: float) -> tuple[int, int]:
|
| 200 |
+
if mode == "working":
|
| 201 |
+
# down at the device, jittering toward the screen every so often
|
| 202 |
+
jx = int(np.sin(now * 2.3) * 2)
|
| 203 |
+
jy = 4 + int(np.sin(now * 1.7) * 1)
|
| 204 |
+
return (jx, jy)
|
| 205 |
+
if mode == "thinking":
|
| 206 |
+
if now > self._saccade_until:
|
| 207 |
+
self._saccade = (random.randint(-6, 2), random.randint(-6, -3))
|
| 208 |
+
self._saccade_until = now + random.uniform(0.3, 1.5)
|
| 209 |
+
return self._saccade
|
| 210 |
+
return (0, 0) # speaking / idle: camera
|
| 211 |
+
|
| 212 |
+
# -- frame ----------------------------------------------------------------
|
| 213 |
+
def frame(self, openness: float, mode: str = "idle") -> Image.Image:
|
| 214 |
if self.procedural:
|
| 215 |
+
now = time.perf_counter()
|
| 216 |
+
dt = min(0.1, now - self._last_t)
|
| 217 |
+
self._last_t = now
|
| 218 |
+
|
| 219 |
+
if mode != self._mode:
|
| 220 |
+
self._mode = mode
|
| 221 |
+
|
| 222 |
+
# device spring (entrance overshoot for free)
|
| 223 |
+
target = 1.0 if mode == "working" else 0.0
|
| 224 |
+
self._device_vel += (target - self._device_pos) * 220 * dt
|
| 225 |
+
self._device_vel *= 0.80
|
| 226 |
+
self._device_pos = max(0.0, min(1.15, self._device_pos + self._device_vel * dt))
|
| 227 |
+
device_t = min(1.0, self._device_pos)
|
| 228 |
+
|
| 229 |
+
# blink channel
|
| 230 |
+
if now >= self._next_blink:
|
| 231 |
+
self._blink_until = now + 0.14
|
| 232 |
+
self._next_blink = now + random.uniform(2.0, 6.0)
|
| 233 |
+
blink = 1.0 if now < self._blink_until else 0.0
|
| 234 |
+
|
| 235 |
+
# typing cadence (Poisson-ish bursts)
|
| 236 |
+
pulse = 0.0
|
| 237 |
+
if mode == "working":
|
| 238 |
+
while self._keystrokes and self._keystrokes[0] <= now:
|
| 239 |
+
self._keystrokes.pop(0)
|
| 240 |
+
self._press_until = now + 0.09
|
| 241 |
+
if now < self._press_until:
|
| 242 |
+
pulse = 1.0
|
| 243 |
+
if len(self._keystrokes) < 3:
|
| 244 |
+
self._schedule_burst(now)
|
| 245 |
+
else:
|
| 246 |
+
self._keystrokes.clear()
|
| 247 |
+
|
| 248 |
+
return render_procedural(
|
| 249 |
+
openness, mode=mode, device_t=device_t, typing_pulse=pulse,
|
| 250 |
+
blink=blink, gaze=self._gaze(mode, now))
|
| 251 |
+
|
| 252 |
+
# non-procedural portrait: rung-2 audio-reactive badge (unchanged)
|
| 253 |
import copy
|
| 254 |
|
| 255 |
img = self.base.copy()
|
| 256 |
from PIL import ImageDraw
|
| 257 |
|
| 258 |
d = ImageDraw.Draw(img)
|
|
|
|
| 259 |
bar_h = int(4 + 26 * openness)
|
| 260 |
+
label = {"working": "working…", "thinking": "thinking…", "speaking": ""}.get(mode, "")
|
| 261 |
d.rectangle([40, SIZE - 70, SIZE - 40, SIZE - 40], fill=(20, 22, 28, 200))
|
| 262 |
d.rectangle([60, SIZE - 55 - bar_h, 100, SIZE - 55], fill=(80, 200, 120, 255))
|
| 263 |
+
if label:
|
| 264 |
+
d.text((120, SIZE - 62), label, fill=(220, 225, 235, 255))
|
| 265 |
return img
|