nex_animation/backend/tests/test_multi_view_reference_c...

124 lines
7.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"""三视图不能和定妆照同时入模:一张三视图会被模型当成「好几个真人」。
项目 9《自检-异步调度验证》实测的第 7 个根因:紫小慧有定妆照(asset 170),
素材库里还有一张三视图(asset 171,纸上并排站着她的正面/侧面/背面)。
`collect_reference_details` 过去把一个人的每种素材统统收集,于是画首帧时
qwen-image 同时看到「一个西装女人 + 三个西装女人」,直接照着多出来的角度又画了一个人:
镜头 03 的每一帧右后方都站着一名穿同款深蓝西装的女性。
质检连判三轮「人数超出设定:背景女子」,返工话术写得再狠也没用——
多余的人不是文字描述来的,是从参考图里克隆来的。
现在每个人物只送一张身份参照图(定妆图 > 服装 > 表情 > 三视图),
只有缺定妆照的人物才退到三视图,并显式提醒模型「三视图是同一个人」。
顺带修掉关键帧提示词里 style_note 被拼接两遍的问题,以及质检把
「近景/特写构图把人物裁掉一半」当成畸变的问题。
"""
from __future__ import annotations
from app.models import Character, Project, Shot
from app.services import prompt
def _kinds(db, shot) -> list[str]:
return [row["kind"] for row in prompt.collect_reference_details(db, shot)
if row["owner_type"] == "human"]
def _upload(api, pid, cid, kind, make_image_png, name):
return api.post(f"/api/projects/{pid}/characters/{cid}/assets",
files={"file": (f"{kind}.png", make_image_png(480, 270, "0x335577"), "image/png")},
data={"kind": kind, "name": name})["result"]
def test_turnaround_never_joins_when_a_portrait_exists(db, api, studio, make_image_png):
"""有定妆照还附三视图 = 让模型看见三个同款真人,必须只留定妆照。"""
pid, cid = studio["project_id"], studio["character_id"]
api.post(f"/api/projects/{pid}/shots",
json={"title": "求助", "description": "林澈凝视全息星图", "mode": "i2v",
"duration": 2, "character_ids": [cid]})
shot = db.query(Shot).filter(Shot.project_id == pid).order_by(Shot.id).all()[-1]
assert _kinds(db, shot) == ["portrait"]
_upload(api, pid, cid, "turnaround", make_image_png, "林澈三视图")
db.expire_all()
shot = db.get(Shot, shot.id)
rows = [row for row in prompt.collect_reference_details(db, shot) if row["owner_type"] == "human"]
assert [row["kind"] for row in rows] == ["portrait"], "定妆照在的时候三视图不得再入模"
def test_turnaround_is_only_a_fallback_and_gets_flagged(db, api, studio, make_image_png):
"""没有定妆照的人物仍然要用三视图,但要标出来,提示词里得警告「这是同一个人」。"""
pid = studio["project_id"]
cid = api.post(f"/api/projects/{pid}/characters",
json={"name": "俊杰", "role": "配角", "appearance": "高大、和蔼"})["result"]["id"]
_upload(api, pid, cid, "turnaround", make_image_png, "俊杰三视图")
api.post(f"/api/projects/{pid}/shots",
json={"title": "安抚", "description": "俊杰坐在服务台后", "mode": "i2v",
"duration": 2, "character_ids": [cid]})
shot = db.query(Shot).filter(Shot.project_id == pid).order_by(Shot.id).all()[-1]
rows = [row for row in prompt.collect_reference_details(db, shot) if row["owner_id"] == cid]
assert [row["kind"] for row in rows] == ["turnaround"]
assert rows[0]["multi_view"] is True
text = prompt.compose_prompt(db.get(Project, pid), shot, db=db)
assert "多角度三视图" in text and "只允许出现一位" in text
assert "俊杰" in text
def test_one_identity_image_per_person_frees_slots_for_the_scene(db, api, studio, make_image_png):
"""一人一张:素材库里 4 张母版不再把场景母图挤出参考图名额。
旧收集口径把每个人的定妆图/三视图/服装/表情统统列出,项目 9 的镜头 03
光人物就占掉 4 张,模型 `max_reference_images` 只有 4,场景母图被截断丢弃。
"""
pid, cid = studio["project_id"], studio["character_id"]
other = api.post(f"/api/projects/{pid}/characters",
json={"name": "俊杰", "role": "配角", "appearance": "高大、和蔼"})["result"]["id"]
for target, name in ((cid, "林澈"), (other, "俊杰")):
for kind in ("portrait", "turnaround", "costume", "expression"):
_upload(api, pid, target, kind, make_image_png, f"{name}·{kind}")
scene = db.get(Project, pid).scenes[0]
api.post(f"/api/projects/{pid}/scenes/{scene.id}/assets",
files={"file": ("scene.png", make_image_png(480, 270, "0x123456"), "image/png")},
data={"kind": "scene_image", "name": "星舰舰桥母图"})
api.post(f"/api/projects/{pid}/shots",
json={"title": "启航", "description": "林澈与俊杰站在舰桥前", "mode": "i2v",
"duration": 2, "character_ids": [cid, other]})
shot = db.query(Shot).filter(Shot.project_id == pid).order_by(Shot.id).all()[-1]
shot.scenes = [scene]
db.commit()
rows = prompt.collect_reference_details(db, shot)
assert [row["kind"] for row in rows] == ["portrait", "portrait", "scene_image"]
first_frame = prompt.collect_reference_details(db, shot, scene_first=True)
assert [row["kind"] for row in first_frame] == ["scene_image", "portrait", "portrait"]
assert not any(row.get("multi_view") for row in rows), "有定妆照的人不该带三视图入模"
def test_keyframe_prompt_states_the_style_note_only_once(db, api, studio):
"""关键帧提示词里返工要求被拼了两遍,白白挤占模型上下文。"""
pid = studio["project_id"]
api.post(f"/api/projects/{pid}/shots",
json={"title": "求助", "description": "林澈凝视全息星图", "mode": "i2v", "duration": 2})
shot = db.query(Shot).filter(Shot.project_id == pid).order_by(Shot.id).all()[-1]
shot.style_note = "日常真实场景 【返工要求第1轮】背景不得多出旁人"
db.commit()
from app.jobs import handlers
text = handlers._image_asset_prompt(db, db.get(Project, pid), shot, "shot", "keyframe", "")
assert text.count("返工要求第1轮") == 1, text
def test_visual_qc_treats_cropped_composition_as_normal():
"""近景/特写把人物裁掉一半是构图,不是穿帮,不能再拿它当返工理由。"""
from app.services import visual_qc
shot = Shot(id=1, project_id=1, order_index=1, code="s01", title="安抚",
description="俊杰坐在服务台后", shot_size="close_up", camera_angle="over_shoulder")
cast = [{"id": 1, "name": "俊杰", "gender": "男", "appearance": "深蓝西装",
"speaker": False, "mentioned": False}]
text = visual_qc.build_prompt(shot, cast, 4, [])
rule = next(line for line in text.split("\n") if "构图裁切不是瑕疵" in line)
assert "近景、特写、过肩镜头" in rule and "不得因此报 distorted" in rule