Files
CenaForge/clipper/tests/test_reaction.py
T
MateuusandClaude Opus 5.5 84f30bbc2d CenaForge: primeira versão pública
Gerador de vídeos narrativos (Godot + FFmpeg), painel Next.js, serviço de voz,
Clipador de lives e Publicador para YouTube, TikTok e Instagram.

Inclui a leitura das visualizações dos posts para o Dashboard e o botão
"Publicar no <rede>" nos cartões das publicações.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 16:35:09 -03:00

354 lines
16 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import json
import random
import pytest
from app.reaction import (
FaceSample, FaceSecond, ReactionParams, label, locate_cam, per_second, reaction_moments, reaction_signal,
seconds_from_json, seconds_to_json, summarize,
)
FPS = 6
def neutral_blend(rng: random.Random, smile: float = 0.05) -> dict[str, float]:
"""Rosto parado: blendshapes baixos com ruído (fala/tique), sorriso no nível pedido."""
n = lambda base: max(0.0, min(1.0, base + rng.uniform(-0.03, 0.03))) # noqa: E731
return {
"mouthSmileLeft": n(smile), "mouthSmileRight": n(smile), "jawOpen": n(0.08),
"eyeWideLeft": n(0.05), "eyeWideRight": n(0.05), "browInnerUp": n(0.05),
"browOuterUpLeft": n(0.04), "browOuterUpRight": n(0.04), "browDownLeft": n(0.05), "browDownRight": n(0.05),
"eyeBlinkLeft": n(0.1), "eyeBlinkRight": n(0.1),
}
def track(duration: int, seed: int = 1, smile: float = 0.05, events: dict | None = None,
absent: range | None = None) -> list[FaceSample]:
"""Quadros a 6 qps. `events`: {segundo: função(blend, rng) → (blend, dx, dy)} para injetar reações."""
rng = random.Random(seed)
events = events or {}
out = []
for i in range(duration * FPS):
t = i / FPS
if absent is not None and int(t) in absent:
out.append(FaceSample(t, False))
continue
blend, dx, dy = neutral_blend(rng, smile), 0.0, 0.0
if int(t) in events:
blend, dx, dy = events[int(t)](blend, rng)
cx = 0.5 + rng.uniform(-0.005, 0.005) + dx
cy = 0.4 + rng.uniform(-0.005, 0.005) + dy
out.append(FaceSample(t, True, cx, cy, 0.3, blend))
return out
def laugh(blend, rng):
blend |= {"mouthSmileLeft": 0.9, "mouthSmileRight": 0.85, "jawOpen": 0.7}
return blend, 0.0, rng.uniform(-0.02, 0.02)
def scream(blend, rng):
blend |= {"mouthSmileLeft": 0.02, "mouthSmileRight": 0.02, "jawOpen": 0.85}
return blend, 0.0, 0.0
def startle(blend, rng):
blend |= {"eyeWideLeft": 0.8, "eyeWideRight": 0.8, "browInnerUp": 0.7, "browOuterUpLeft": 0.6,
"browOuterUpRight": 0.6}
return blend, rng.choice([-1, 1]) * 0.1, rng.uniform(-0.08, 0.08)
# --- série e sinal ------------------------------------------------------------------------------------------------
def test_per_second_shapes_and_none_without_face():
seconds = per_second(track(10, absent=range(4, 6)), 10)
assert [s.t for s in seconds] == list(range(10))
assert seconds[0].present == 1.0 and 0.0 <= seconds[0].smile <= 0.1 and seconds[0].size == 0.3
assert seconds[4].present == 0.0 and seconds[4].smile is None and seconds[4].cx is None
assert seconds[0].motion is not None and seconds[0].motion < 0.5
def test_json_roundtrip_is_compact_and_rounded():
seconds = per_second(track(5, absent=range(2, 3)), 5)
data = seconds_to_json(seconds)
assert "smile" not in data[2] and data[2]["present"] == 0.0
assert all(len(str(v).split(".")[-1]) <= 3 for d in data for v in d.values() if isinstance(v, float))
back = seconds_from_json(json.loads(json.dumps(data)))
assert back[2].smile is None and abs(back[0].cx - seconds[0].cx) < 1e-3
def test_neutral_with_noise_has_no_peak():
seconds = per_second(track(300, seed=3), 300)
assert reaction_moments(seconds) == []
signal = reaction_signal(seconds)
assert max(v for v in signal if v is not None) < ReactionParams().threshold
def test_laugh_is_a_peak_with_right_label_and_onset():
events = {t: laugh for t in range(200, 205)}
seconds = per_second(track(300, events=events), 300)
moments = reaction_moments(seconds)
assert len(moments) == 1
m = moments[0]
assert m.label == "risada" and 200 <= m.peak_s <= 204
assert m.onset_s == 200 # a reação começa exatamente onde o evento começou
assert m.end_s >= 204 and m.strong
def test_constant_smiling_streamer_is_not_a_false_positive():
# Streamer de "neutro sorridente": o sorriso alto é o normal dele, não reação.
seconds = per_second(track(300, seed=5, smile=0.65), 300)
assert reaction_moments(seconds) == []
# E uma risada de verdade nesse mesmo streamer ainda aparece (pela boca).
events = {t: (lambda b, r: (b | {"jawOpen": 0.7, "mouthSmileLeft": 0.8, "mouthSmileRight": 0.8}, 0.0, 0.0))
for t in range(150, 154)}
moments = reaction_moments(per_second(track(300, seed=5, smile=0.65, events=events), 300))
assert [m.label for m in moments] == ["risada"]
def test_scream_versus_laugh():
events = {t: scream for t in range(100, 103)} | {t: laugh for t in range(200, 203)}
moments = reaction_moments(per_second(track(300, events=events), 300))
assert [(m.label, 100 <= m.peak_s <= 102 or 200 <= m.peak_s <= 202) for m in moments] == [
("grito", True), ("risada", True)]
def test_startle_versus_shock():
still = lambda b, r: (startle(b, r)[0], 0.0, 0.0) # noqa: E731 — olhos/sobrancelha sem pulo da cabeça
events = {t: startle for t in range(100, 102)} | {t: still for t in range(200, 202)}
labels = [m.label for m in reaction_moments(per_second(track(300, events=events), 300))]
assert labels == ["susto", "choque"]
def test_seconds_without_face_are_none_and_never_peak():
# Rosto some exatamente na hora em que haveria uma "risada": não pode virar pico.
events = {t: laugh for t in range(150, 155)}
seconds = per_second(track(300, events=events, absent=range(148, 158)), 300)
signal = reaction_signal(seconds)
assert all(signal[t] is None for t in range(148, 158))
assert reaction_moments(seconds) == []
def test_label_without_z_uses_absolute_thresholds():
s = FaceSecond(0, 1.0, 0.5, 0.4, 0.3, smile=0.8, jaw=0.7, surprise=0.1, frown=0.0, blink=0.1, motion=0.1)
assert label(s) == "risada"
s.smile = 0.0
assert label(s) == "grito"
assert label(FaceSecond(1, 0.0)) == "neutro"
assert label(FaceSecond(2, 1.0, smile=0.0, jaw=0.0, surprise=0.0, frown=0.6, motion=0.0)) == "raiva"
def test_summarize_text_for_the_judge():
events = {t: laugh for t in range(200, 204)} | {t: startle for t in range(215, 217)}
seconds = per_second(track(300, events=events), 300)
text = summarize(seconds, 190, 230)
assert text.startswith("risada forte em 1") and "susto" in text and "; " in text
assert summarize(seconds, 20, 60) == "sem reação marcante no rosto"
gone = per_second(track(100, absent=range(0, 100)), 100)
assert summarize(gone, 0, 50) == "rosto do streamer não aparece no trecho"
# --- câmera -------------------------------------------------------------------------------------------------------
W, H = 1920, 1080
def cam_face(rng: random.Random) -> tuple[float, float, float, float, float]:
"""Rosto da câmera no canto inferior esquerdo, com tremida de poucos pixels."""
return (120 + rng.uniform(-4, 4), 700 + rng.uniform(-4, 4), 110, 130, 0.9)
def inside_unit(rect: dict) -> bool:
return (0 <= rect["x"] <= 1 and 0 <= rect["y"] <= 1 and rect["w"] > 0 and rect["h"] > 0
and rect["x"] + rect["w"] <= 1 + 1e-9 and rect["y"] + rect["h"] <= 1 + 1e-9)
def test_locate_cam_stable_corner():
rng = random.Random(1)
frames = [[cam_face(rng)] for _ in range(12)]
result = locate_cam(frames, W, H)
assert result["hasWebcam"] and not result["fullFrame"] and result["confidence"] > 0.7
cam, face = result["cam"], result["faceRect"]
assert inside_unit(cam) and inside_unit(face)
# O rosto fica dentro do retângulo da câmera, que está no canto inferior esquerdo.
assert cam["x"] <= face["x"] and cam["y"] <= face["y"]
assert face["x"] + face["w"] <= cam["x"] + cam["w"] and face["y"] + face["h"] <= cam["y"] + cam["h"]
assert cam["x"] < 0.1 and cam["y"] > 0.4 and cam["w"] < 0.3
def test_locate_cam_ignores_cutscene_face():
rng = random.Random(2)
frames = [[cam_face(rng)] for _ in range(12)]
frames[3].append((900, 300, 200, 240, 0.95)) # rosto do personagem na cutscene, só em 2 de 12 quadros
frames[4].append((905, 302, 198, 238, 0.95))
result = locate_cam(frames, W, H)
assert result["hasWebcam"] and result["faceRect"]["x"] < 0.1
# Só a cutscene (sem câmera): nada.
only = [[] for _ in range(12)]
only[3], only[4] = [(900, 300, 200, 240, 0.95)], [(905, 302, 198, 238, 0.95)]
assert locate_cam(only, W, H)["hasWebcam"] is False
def test_locate_cam_full_frame():
rng = random.Random(3)
frames = [[(760 + rng.uniform(-10, 10), 250, 420, 520, 0.95)] for _ in range(10)]
result = locate_cam(frames, W, H)
assert result["hasWebcam"] and result["fullFrame"] and result["cam"] == {"x": 0, "y": 0, "w": 1, "h": 1}
def test_locate_cam_no_face():
result = locate_cam([[] for _ in range(12)], W, H)
assert result == {"hasWebcam": False, "cam": None, "faceRect": None, "confidence": 0.0, "fullFrame": False}
assert locate_cam([], W, H)["hasWebcam"] is False
# Detecções fracas (score baixo) não contam.
assert locate_cam([[(120, 700, 110, 130, 0.3)] for _ in range(12)], W, H)["hasWebcam"] is False
def test_locate_cam_rect_clipped_and_snapped_to_frame_edges():
# Rosto colado no canto: a expansão passaria da borda do quadro e precisa ser recortada em 0..1.
frames = [[(10, 950, 110, 120, 0.9)] for _ in range(8)]
result = locate_cam(frames, W, H)
assert inside_unit(result["cam"]) and result["cam"]["x"] == 0
# Com bordas persistentes da moldura, o retângulo encaixa nelas.
rng = random.Random(4)
frames = [[cam_face(rng)] for _ in range(12)]
edges = {"xs": [0.02, 0.2, 0.7], "ys": [0.55, 0.95, 0.1]}
cam = locate_cam(frames, W, H, edges=edges)["cam"]
assert cam == {"x": 0.02, "y": 0.55, "w": 0.18, "h": 0.4}
# --- ajustes da bancada (números reais da sessão 3, doc/FACE_BAKEOFF.md) ------------------------------------------
def real_calm(n: int, seed: int = 7) -> list[FaceSecond]:
"""Neutro como o da live real: sorriso por segundo 0,02–0,25 (mediana ~0,1), boca ~0, olho arregalado ~0."""
rng = random.Random(seed)
return [FaceSecond(t, 1.0, 0.5, 0.4, 0.3, smile=rng.uniform(0.02, 0.22), jaw=rng.uniform(0.0, 0.01),
surprise=rng.uniform(0.0, 0.004), frown=rng.uniform(0.01, 0.07), brow=rng.uniform(0.01, 0.05),
blink=0.1, motion=rng.uniform(0.02, 0.15)) for t in range(n)]
def test_real_calm_with_isolated_spikes_has_no_peak():
# No calmo real houve um segundo com sorriso 0,38 e outro com browInnerUp 0,38: não é reação.
seconds = real_calm(300)
seconds[200].smile = 0.38
seconds[250].brow = 0.38
assert reaction_moments(seconds) == []
def test_laugh_without_open_mouth_is_risada():
# Corte 10/12: sorriso 0,84–0,95 por vários segundos com jawOpen ≤ 0,11.
seconds = real_calm(300)
for t in range(200, 210):
seconds[t].smile, seconds[t].jaw = 0.9, 0.05
moments = reaction_moments(seconds)
assert [m.label for m in moments] == ["risada"] and moments[0].onset_s == 200 and moments[0].strong
def test_shout_is_jaw_and_brow_down_without_smile():
# Corte 10, 1628 s: jawOpen 0,42 + browDown forte, sorriso 0,06 naquele segundo.
seconds = real_calm(300)
seconds[200].smile, seconds[200].jaw, seconds[200].frown = 0.06, 0.42, 0.5
seconds[250].frown = 0.6 # franziu de boca fechada
labels = [(m.peak_s, m.label) for m in reaction_moments(seconds)]
assert labels == [(200, "grito"), (250, "raiva")]
def test_confused_brow_inner_up_without_smile():
# Corte 9 (lag): browInnerUp 0,7–0,8 por ~6 s, sorriso baixo, eyeWide ~0.
seconds = real_calm(300)
for t in range(200, 206):
seconds[t].brow = 0.78
moments = reaction_moments(seconds)
assert [m.label for m in moments] == ["confuso"] and moments[0].onset_s == 200
def test_surprise_does_not_need_eye_wide():
# Na live real eyeWide fica ~0: sobrancelha (interna OU externa) levantada é o canal `brow`; parada = confuso,
# com a cabeça se mexendo = susto. Olho arregalado parado continua sendo choque.
blend = {"browOuterUpLeft": 0.7, "browOuterUpRight": 0.7, "eyeWideLeft": 0.0, "eyeWideRight": 0.0}
second = per_second([FaceSample(0.1, True, 0.5, 0.4, 0.3, blend)], 1)[0]
assert second.brow == pytest.approx(0.7) and second.surprise == 0.0
assert label(second) == "confuso"
second.motion = 0.9
assert label(second) == "susto"
eyes = per_second([FaceSample(0.1, True, 0.5, 0.4, 0.3, {"eyeWideLeft": 0.8, "eyeWideRight": 0.8})], 1)[0]
assert label(eyes) == "choque"
def test_motion_alone_or_with_flickering_face_is_not_a_peak():
# Rastreio pulando (rosto em 1/3 do segundo) dava "susto" de intensidade 13 na live real.
seconds = real_calm(300)
seconds[200].present, seconds[200].motion, seconds[200].brow = 0.33, 3.0, 0.2
seconds[250].motion = 3.0 # só movimento, rosto inteiro: ajeitou a cadeira
assert reaction_moments(seconds) == []
def test_old_series_without_brow_still_works():
# Série gravada antes do canal `brow`: o canal fica parado, sem quebrar.
data = seconds_to_json(real_calm(60))
for d in data:
d.pop("brow", None)
assert all(v is not None for v in reaction_signal(seconds_from_json(data)))
def test_summarize_absolute_times_for_the_prompt():
seconds = real_calm(300)
for t in range(200, 205):
seconds[t].smile = 0.9
assert summarize(seconds, 190, 230, relative=False).startswith("risada forte em 20")
def test_locate_cam_leaning_streamer_stays_one_group():
# Câmera grande (rosto ~0,16 do quadro): ele se inclina 0,8 largura de rosto em metade dos quadros.
# Com distância fixa em fração do quadro (ou comparando com o 1º membro) o grupo se partia em dois.
rng = random.Random(9)
frames = []
for i in range(12):
lean = 0.8 * 300 if i % 2 else 0
frames.append([(200 + lean + rng.uniform(-4, 4), 600 + rng.uniform(-4, 4), 300, 340, 0.9)])
result = locate_cam(frames, W, H)
assert result["hasWebcam"] and result["confidence"] > 0.5 # partido em dois daria presença 0,5 → ≤ 0,45
face = result["faceRect"]
center = (face["x"] + face["w"] / 2) * W
assert 200 + 150 + 80 < center < 200 + 150 + 160 # média das duas posições (~350 + 120)
def test_locate_cam_frameless_keeps_forehead():
# Câmera sem moldura (streamer recortado sobre o jogo, banner logo abaixo): o retângulo vem do rosto e deixa
# folga acima da testa — a IA cortava a testa (topo em y 0,532 com o rosto começando em 0,504).
fw, fh = 1280, 720
frames = [[(80 + i % 3, 363, 85, 109, 0.92)] for i in range(12)] # rosto da bancada: x 0,066 y 0,504 h 0,152
result = locate_cam(frames, fw, fh)
cam, face = result["cam"], result["faceRect"]
assert face["y"] - cam["y"] >= 0.5 * face["h"] # testa e cabelo dentro
assert cam["y"] + cam["h"] >= face["y"] + face["h"] + face["h"] # ombros/peito abaixo do queixo
# Borda "falsa" logo acima da testa (ex.: banner) não corta a cabeça.
edges = {"xs": [], "ys": [face["y"] - 0.01]}
assert locate_cam(frames, fw, fh, edges=edges)["cam"]["y"] < face["y"] - 0.3 * face["h"]
def test_two_hours_of_series_is_fast():
# channel_z era O(n²): a API e a pontuação recalculam a live inteira. 2 h sintéticas precisam de < 1 s.
import time
seconds = real_calm(7200, seed=3)
for t in range(4000, 4006):
seconds[t].smile = 0.9
started = time.perf_counter()
moments = reaction_moments(seconds)
assert time.perf_counter() - started < 1.0
assert [m.label for m in moments] == ["risada"] and moments[0].onset_s == 4000
def test_locate_cam_ignores_static_low_score_object():
"""Teste ao vivo de 17/11 (lofigirl, sem webcam): a tela de um notebook desenhado na borda esquerda saiu como
rosto de 24×34 px com score 0,61–0,66 em 8 de 12 quadros e virou câmera. Webcam real: 0,92–0,94."""
fake = (0.0, 415.0, 24.0, 34.0, 0.63)
frames = [[fake], [fake], [fake, (726.0, 253.0, 73.0, 98.0, 0.604)], [], [fake], [], [fake], [fake],
[(606.0, 254.0, 155.0, 197.0, 0.615)], [fake], [], [fake, (605.0, 257.0, 154.0, 193.0, 0.61)]]
assert locate_cam(frames, 1280, 720)["hasWebcam"] is False
real = [[(109.0 + i % 3, 359.0, 82.0, 112.0, 0.93)] for i in range(12)]
assert locate_cam(real, 1280, 720)["hasWebcam"] is True