-
Notifications
You must be signed in to change notification settings - Fork 4
/
nodes.py
198 lines (160 loc) · 6.37 KB
/
nodes.py
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import argparse
import os
import tempfile
import numpy as np
import torch
from glob import glob
from torchvision.transforms import CenterCrop, Compose, Resize
import sys
current_path = os.path.abspath(os.path.dirname(__file__))
sys.path.append(current_path)
from .gradio_utils.camera_utils import CAMERA_MOTION_MODE, create_relative
from .gradio_utils.utils import vis_camera
from .gradio_utils.motionctrl_cmcm_gradio import build_model, motionctrl_sample
import json
import folder_paths
from PIL import Image
os.environ['KMP_DUPLICATE_LIB_OK']='True'
MOTION_CAMERA_OPTIONS = ["U", "D", "L", "R", "O", "O_0.2x", "O_0.4x", "O_1.0x", "O_2.0x", "O_0.2x", "O_0.2x", "Round-RI", "Round-RI_90", "Round-RI-120", "Round-ZoomIn", "SPIN-ACW-60", "SPIN-CW-60", "I", "I_0.2x", "I_0.4x", "I_1.0x", "I_2.0x", "1424acd0007d40b5", "d971457c81bca597", "018f7907401f2fef", "088b93f15ca8745d", "b133a504fc90a2d1"]
def process_camera(camera_pose_str,frame_length):
RT=json.loads(camera_pose_str)
for i in range(frame_length):
if len(RT)<=i:
RT.append(RT[len(RT)-1])
if len(RT) > frame_length:
RT = RT[:frame_length]
RT = np.array(RT).reshape(-1, 3, 4)
return RT
class LoadMotionCtrlSVDCameraPreset:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"motion_camera": (MOTION_CAMERA_OPTIONS,),
}
}
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("POINTS",)
FUNCTION = "load_motion_camera_preset"
CATEGORY = "motionctrl"
def load_motion_camera_preset(self, motion_camera):
data="[]"
comfy_path = os.path.dirname(folder_paths.__file__)
with open(f'{comfy_path}/custom_nodes/ComfyUI-MotionCtrl-SVD/examples/camera_poses/test_camera_{motion_camera}.json') as f:
data = f.read()
return (data,)
# MODE = ["camera motion control", "object motion control", "camera + object motion control"]
MODE = ["control camera poses", "control object trajectory", "control both camera and object motion"]
RESIZE_MODE = ['Center Crop To 576x1024', 'Keep original spatial ratio']
DIY_MODE = ['Customized Mode 1: First A then B',
'Customized Mode 2: Both A and B',
'Customized Mode 3: RAW Camera Poses']
## load default model
num_frames = 14
num_steps = 25
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using device {device}")
traj_list = []
camera_dict = {
"motion":[],
"mode": "Customized Mode 1: First A then B", # "First A then B", "Both A and B", "Custom"
"speed": 1.0,
"complex": None
}
def process_input_image(input_image, resize_mode):
width, height = 1024, 576
if resize_mode == RESIZE_MODE[0]:
height = 576
width = 1024
w, h = input_image.size
h_ratio = h / height
w_ratio = w / width
if h_ratio > w_ratio:
h = int(h / w_ratio)
if h < height:
h = height
input_image = Resize((h, width))(input_image)
else:
w = int(w / h_ratio)
if w < width:
w = width
input_image = Resize((height, w))(input_image)
transformer = Compose([
# Resize(width),
CenterCrop((height, width)),
])
input_image = transformer(input_image)
else:
w, h = input_image.size
if h > w:
height = 576
width = int(w * height / h)
else:
width = 1024
height = int(h * width / w)
input_image = Resize((height, width))(input_image)
# print(f'input_image size: {input_image.size}')
return input_image
class MotionctrlSVDLoader:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"ckpt_name": (folder_paths.get_filename_list("checkpoints"), {"default": "motionctrl_svd.ckpt"}),
"frame_length": ("INT", {"default": 14}),
"steps": ("INT", {"default": 25}),
}
}
RETURN_TYPES = ("MOTIONCTRLSVD",)
RETURN_NAMES = ("model",)
FUNCTION = "load_checkpoint"
CATEGORY = "motionctrl"
def load_checkpoint(self, ckpt_name, frame_length, steps):
global device
comfy_path = os.path.dirname(folder_paths.__file__)
ckpt_path = folder_paths.get_full_path("checkpoints", ckpt_name)
config_path = os.path.join(comfy_path, 'custom_nodes/ComfyUI-MotionCtrl-SVD/configs/inference/config_motionctrl_cmcm.yaml')
if not os.path.exists(ckpt_path):
os.system(f'wget https://huggingface.co/TencentARC/MotionCtrl/resolve/main/motionctrl_svd.ckpt?download=true -P .')
os.system(f'mv motionctrl_svd.ckpt?download=true {ckpt_path}')
model = build_model(config_path, ckpt_path, device, frame_length, steps)
return (model,)
class MotionctrlSVDSampleSimple:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"model": ("MOTIONCTRLSVD",),
"camera": ("STRING", {"multiline": True, "default":"[[1,0,0,0,0,1,0,0,0,0,1,0.2]]"}),
"image": ("IMAGE",),
"resize_mode" :(RESIZE_MODE,),
"seed": ("INT", {"default": 1234}),
"fps_id": ("INT", {"default": 6, "min": 5, "max": 30}),
"frame_length": ("INT", {"default": 14}),
},
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "run_inference"
CATEGORY = "motionctrl"
def run_inference(self,model,camera,image,resize_mode,seed,fps_id,frame_length):
global device
RT = process_camera(camera,frame_length).reshape(-1,12)
image = 255.0 * image[0].cpu().numpy()
image = Image.fromarray(np.clip(image, 0, 255).astype(np.uint8))
image=process_input_image(image,resize_mode)
return motionctrl_sample(
model=model,
image=image,
RT=RT,
num_frames=frame_length,
fps_id=fps_id,
decoding_t=1,
seed=seed,
sample_num=1,
device=device
)
NODE_CLASS_MAPPINGS = {
"Motionctrl-SVD Sample Simple":MotionctrlSVDSampleSimple,
"Load Motionctrl-SVD Camera Preset":LoadMotionCtrlSVDCameraPreset,
"Load Motionctrl-SVD Checkpoint": MotionctrlSVDLoader,
}