File size: 6,554 Bytes
5cb23ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""16:9 night beach: Anima + charref keyframe, then the character frozen and the background redrawn (inverted WeVisOne matte)."""
import json, sys, time, urllib.request
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent))
from gen_h3 import API, DATA, post, upload  # noqa: E402

CHAR = "c0040"
TAGS = "1girl, solo, blue hair, double bun, school uniform, serafuku, petite"
KF_W, KF_H, W, H = 1344, 768, 832, 480
SCENE = "from behind, standing on the beach, looking at the horizon, night, starry sky, full moon, moonlight reflecting on the sea, waves, wide shot, scenery"
MOTION = "waves rolling onto the shore, stars twinkling, moonlight shimmering on the water, wind"


def graph(ref_image, prefix, denoise, grow, seed, noref=False, blur=1):
    neg = "worst quality, low quality, blurry, text, watermark"
    return {
        "1": {"class_type": "UNETLoader", "inputs": {"unet_name": "anima/anima-turbo-v1.0-int8-convrot.safetensors", "weight_dtype": "default"}},
        "2": {"class_type": "CLIPLoader", "inputs": {"clip_name": "qwen_3_06b_base.safetensors", "type": "stable_diffusion", "device": "default"}},
        "3": {"class_type": "VAELoader", "inputs": {"vae_name": "qwen_image_vae.safetensors"}},
        "4": {"class_type": "LoadImage", "inputs": {"image": ref_image}},
        "5": {"class_type": "AnimaRefAdapter", "inputs": {"model": ["1", 0], "vae": ["3", 0], "image": ["4", 0], "lora_name": "anima_ref_adapter_v2_r64.safetensors",
                                                          "ref_guidance": 1.0, "correction": "none"}},
        "6": {"class_type": "CLIPTextEncode", "inputs": {"clip": ["2", 0], "text": f"masterpiece, best quality, score_7, safe, {TAGS}, {SCENE}"}},
        "7": {"class_type": "CLIPTextEncode", "inputs": {"clip": ["2", 0], "text": neg}},
        # keyframe
        "10": {"class_type": "EmptyLatentImage", "inputs": {"width": KF_W, "height": KF_H, "batch_size": 1}},
        "11": {"class_type": "KSampler", "inputs": {"model": ["5", 0], "positive": ["6", 0], "negative": ["7", 0], "latent_image": ["10", 0], "seed": seed,
                                                    "steps": 8, "cfg": 1.0, "sampler_name": "euler", "scheduler": "simple", "denoise": 1.0}},
        "12": {"class_type": "VAEDecode", "inputs": {"vae": ["3", 0], "samples": ["11", 0]}},
        "13": {"class_type": "SaveImage", "inputs": {"images": ["12", 0], "filename_prefix": prefix + "_keyframe"}},
        "14": {"class_type": "ImageScale", "inputs": {"image": ["12", 0], "upscale_method": "lanczos", "width": W, "height": H, "crop": "disabled"}},
        # background-only redraw over 16 latent frames
        "20": {"class_type": "VAEEncode", "inputs": {"pixels": ["14", 0], "vae": ["3", 0]}},
        **{str(i): {"class_type": "LatentConcat", "inputs": {"samples1": [str(i - 1), 0], "samples2": [str(i - 1), 0], "dim": "t"}} for i in range(21, 25)},
        "25": {"class_type": "WeVisOneVLLoader", "inputs": {"clip_name": "wevisone_vl_4b_int8_convrot.safetensors"}},
        "26": {"class_type": "WeVisOneVLPerceive", "inputs": {"clip": ["25", 0], "image": ["14", 0], "task": "matting", "query": "character",
                                                              "max_length": 1024, "individual_masks": False}},
        "27": {"class_type": "ThresholdMask", "inputs": {"mask": ["26", 3], "value": 0.3}},
        "28": {"class_type": "GrowMask", "inputs": {"mask": ["27", 0], "expand": grow, "tapered_corners": True}},
        # soft edge, so the frozen border does not keep a ring of the keyframe's background
        "40": {"class_type": "MaskToImage", "inputs": {"mask": ["28", 0]}},
        "41": {"class_type": "ImageBlur", "inputs": {"image": ["40", 0], "blur_radius": blur, "sigma": blur / 2}},
        "42": {"class_type": "ImageToMask", "inputs": {"image": ["41", 0], "channel": "red"}},
        "29": {"class_type": "InvertMask", "inputs": {"mask": ["42", 0]}},
        "30": {"class_type": "SetLatentNoiseMask", "inputs": {"samples": ["24", 0], "mask": ["29", 0]}},
        # the background pass without the reference or character tags: with them the redrawn area grows another copy of her
        "31": {"class_type": "AnimaMotionModule", "inputs": {"model": ["1", 0] if noref else ["5", 0], "motion_name": "r2v_20clips/motion_02000.safetensors"}},
        "32": {"class_type": "CLIPTextEncode", "inputs": {"clip": ["2", 0], "text": f"masterpiece, best quality, score_7, safe, {'no humans, ' if noref else TAGS + ', '}{SCENE.replace('from behind, standing on the beach, looking at the horizon, ', '') if noref else SCENE}, {MOTION}, anime screencap"}},
        "33": {"class_type": "KSampler", "inputs": {"model": ["31", 0], "positive": ["32", 0], "negative": ["7", 0], "latent_image": ["30", 0], "seed": seed,
                                                    "steps": 8, "cfg": 1.0, "sampler_name": "euler", "scheduler": "simple", "denoise": denoise}},
        "34": {"class_type": "LatentCutToBatch", "inputs": {"samples": ["33", 0], "dim": "t", "slice_size": 1}},
        "35": {"class_type": "VAEDecode", "inputs": {"vae": ["3", 0], "samples": ["34", 0]}},
        "36": {"class_type": "CreateVideo", "inputs": {"images": ["35", 0], "fps": 8.0}},
        "37": {"class_type": "SaveVideo", "inputs": {"video": ["36", 0], "filename_prefix": prefix, "format": "mp4", "codec": "h264"}},
        "38": {"class_type": "MaskToImage", "inputs": {"mask": ["29", 0]}},
        "39": {"class_type": "SaveImage", "inputs": {"images": ["38", 0], "filename_prefix": prefix + "_mask"}},
    }


def main():
    ref = upload(DATA / CHAR / "ref.png")
    noref = "noref" in sys.argv[1:]
    grow = next((int(a[5:]) for a in sys.argv[1:] if a.startswith("grow=")), 16)
    blur = next((int(a[5:]) for a in sys.argv[1:] if a.startswith("blur=")), 1)
    for denoise in map(float, [a for a in sys.argv[1:] if a[0].isdigit()] or ["0.8"]):
        prefix = f"anima_motion_eval/beach{'_noref' if noref else ''}_d{int(denoise * 100)}_g{grow}b{blur}"
        pid = post("/prompt", json.dumps({"prompt": graph(ref, prefix, denoise, grow, 21, noref, blur)}).encode(), {"Content-Type": "application/json"})["prompt_id"]
        t = time.time()
        while pid not in (h := json.load(urllib.request.urlopen(f"{API}/history/{pid}"))):
            time.sleep(2)
        s = h[pid]["status"]
        print(prefix, s["status_str"], f"{time.time() - t:.0f}s", flush=True)
        if s["status_str"] != "success":
            print(json.dumps(s["messages"])[-1500:])


if __name__ == "__main__":
    main()