MVP: procedural face + audio-reactive mouth
Browse files- freestream/avatar.py +123 -0
freestream/avatar.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Persona video rungs — procedural face + audio-reactive mouth animation.
|
| 2 |
+
|
| 3 |
+
MVP implements rung 2/3 hybrid honestly: a procedural cartoon portrait
|
| 4 |
+
(generated locally, no model download) whose mouth openness is driven by
|
| 5 |
+
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 |
+
|
| 18 |
+
SIZE = 512
|
| 19 |
+
FPS = 15
|
| 20 |
+
PROCEDURAL_MARKER = "freestream:procedural"
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def _face_params(openness: float) -> dict:
|
| 24 |
+
return {
|
| 25 |
+
"mouth_h": 6 + int(58 * max(0.0, min(1.0, openness))),
|
| 26 |
+
"mouth_w": 96 - int(18 * openness),
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def render_procedural(openness: float = 0.15, size: int = SIZE) -> Image.Image:
|
| 31 |
+
"""Cartoon assistant face; mouth height follows `openness` (0..1)."""
|
| 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 |
+
cx, cy = size // 2, int(size * 0.46)
|
| 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))
|
| 41 |
+
# head
|
| 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 (blink handled by caller passing openness<0 rarely; keep open)
|
| 46 |
+
ey = cy - int(r * 0.25)
|
| 47 |
+
for ex in (cx - int(r * 0.42), cx + int(r * 0.42)):
|
| 48 |
+
d.ellipse([ex - 16, ey - 16, ex + 16, ey + 16], fill=(255, 255, 255, 255))
|
| 49 |
+
d.ellipse([ex - 7, ey - 7, ex + 7, ey + 7], fill=(40, 40, 48, 255))
|
| 50 |
+
# brows
|
| 51 |
+
d.line([cx - int(r * 0.55), ey - 30, cx - int(r * 0.28), ey - 34], fill=(66, 62, 78, 255), width=5)
|
| 52 |
+
d.line([cx + int(r * 0.28), ey - 34, cx + int(r * 0.55), ey - 30], fill=(66, 62, 78, 255), width=5)
|
| 53 |
+
# mouth
|
| 54 |
+
my = cy + int(r * 0.45)
|
| 55 |
+
d.ellipse([cx - p["mouth_w"], my - p["mouth_h"], cx + p["mouth_w"], my + p["mouth_h"]],
|
| 56 |
+
fill=(90, 44, 52, 255))
|
| 57 |
+
return img
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def generate_placeholder(out_path) -> str:
|
| 61 |
+
img = render_procedural(0.15)
|
| 62 |
+
meta = PngImagePlugin.PngInfo()
|
| 63 |
+
meta.add_text("freestream:procedural", "1")
|
| 64 |
+
meta.add_text("freestream:fps", str(FPS))
|
| 65 |
+
out = str(out_path)
|
| 66 |
+
img.save(out, "PNG", pnginfo=meta)
|
| 67 |
+
return out
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def is_procedural(image_path) -> bool:
|
| 71 |
+
try:
|
| 72 |
+
with Image.open(image_path) as im:
|
| 73 |
+
return im.text is not None and PROCEDURAL_MARKER in im.text
|
| 74 |
+
except Exception:
|
| 75 |
+
return False
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def build_mouth_sequence(pcm: np.ndarray, sample_rate: int, fps: int = FPS,
|
| 79 |
+
pre_roll_frames: int = 3, tail_frames: int = 5) -> np.ndarray:
|
| 80 |
+
"""Audio-energy -> mouth openness per video frame, 0..1, shape (n_frames,).
|
| 81 |
+
|
| 82 |
+
pre_roll_frames of closed mouth come first: the avatar starts moving
|
| 83 |
+
~150ms (3 frames @15fps) before audio playout (docs/LATENCY.md #3).
|
| 84 |
+
"""
|
| 85 |
+
frame_n = int(sample_rate / fps)
|
| 86 |
+
n = len(pcm) // frame_n
|
| 87 |
+
if n == 0:
|
| 88 |
+
return np.zeros(pre_roll_frames + tail_frames, dtype=np.float32)
|
| 89 |
+
rms = np.sqrt(np.mean(np.abs(pcm[: n * frame_n].astype(np.float32)) ** 2
|
| 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,
|
| 97 |
+
np.zeros(tail_frames)]).astype(np.float32)
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
class AvatarRenderer:
|
| 101 |
+
"""Publish-side frame factory. Procedural face -> re-render mouth;
|
| 102 |
+
any other portrait -> audio-reactive waveform badge (rung 2)."""
|
| 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 |
+
def frame(self, openness: float) -> Image.Image:
|
| 111 |
+
if self.procedural:
|
| 112 |
+
return render_procedural(openness)
|
| 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
|