forked from TencentARC/StereoCrafter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdepth_splatting_inference_npz_iter.py
More file actions
371 lines (285 loc) · 16.2 KB
/
depth_splatting_inference_npz_iter.py
File metadata and controls
371 lines (285 loc) · 16.2 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
'''
huggingface-cli download svjack/Genshin-Impact-Novel-Video Genshin-Impact-Cutness-video1.zip --repo-type dataset --local-dir .
unzip Genshin-Impact-Cutness-video1.zip
##### choose this
python depth_splatting_inference_npz_iter.py \
--pre_trained_path ./weights/stable-video-diffusion-img2vid-xt-1-1\
--unet_path ./weights/DepthCrafter --process_length 64 \
--input_path test_videos0 \
--output_path test_videos0_splatting_64
python npz_to_video.py --input_folder test_videos0_splatting_64 --output_folder test_videos0_splatting_64_video
!pip install -r requirements.txt
python inpainting_inference_iter.py \
--pre_trained_path ./weights/stable-video-diffusion-img2vid-xt-1-1 \
--unet_path ./weights/StereoCrafter \
--input_video_path test_videos0_splatting_64_video --output_video_path test_videos0_splatting_64_video_3D
#####
mkdir test_videos1 && cp "Genshin-Impact-Cutness-video1/[P057]璃月 群玉阁大战 哥哥.mp4" test_videos1
python split_video.py --input_path test_videos1 --frames_per_segment 64 --output_path test_videos1_64_sp --skip_short_segments
python depth_splatting_inference_npz_iter.py \
--pre_trained_path ./weights/stable-video-diffusion-img2vid-xt-1-1\
--unet_path ./weights/DepthCrafter --process_length 64 \
--input_path test_videos1_64_sp \
--output_path test_videos1_splatting_64_sp
python npz_to_video.py --input_folder test_videos1_splatting_64_sp --output_folder test_videos1_splatting_64_sp_video
!pip install -r requirements.txt
python inpainting_inference_iter.py \
--pre_trained_path ./weights/stable-video-diffusion-img2vid-xt-1-1 \
--unet_path ./weights/StereoCrafter \
--input_video_path test_videos1_splatting_64_sp_video --output_video_path test_videos1_splatting_64_sp_video_3D
python connect_video.py --input_folder test_videos1_splatting_64_sp_video_3D --output_folder test_videos1_splatting_64_sp_video_3D_connect
#python spatial_split_video.py --input_video_path test_videos1_splatting_64_sp_video_3D_connect/final_sbs.mp4 --split_direction vertical
python add_audio_with_speed_adjustment.py "test_videos1/[P057]璃月 群玉阁大战 哥哥.mp4" test_videos1_splatting_64_sp_video_3D_connect/final_anaglyph.mp4 test_videos1_splatting_64_sp_video_3D_connect/final_anaglyph_audio.mp4
python add_audio_with_speed_adjustment.py "test_videos1/[P057]璃月 群玉阁大战 哥哥.mp4" test_videos1_splatting_64_sp_video_3D_connect/final_sbs.mp4 test_videos1_splatting_64_sp_video_3D_connect/final_sbs_audio.mp4
python produce_hf_video_dataset.py --input_folder test_videos1_splatting_64_sp_video_3D \
--output_folder test_videos1_splatting_64_sp_video_3D_anaglyph_upload \
--group_by_type _anaglyph.mp4 \
--connected_video test_videos1_splatting_64_sp_video_3D_connect/final_anaglyph_audio.mp4
python produce_hf_video_dataset.py --input_folder test_videos1_splatting_64_sp_video_3D \
--output_folder test_videos1_splatting_64_sp_video_3D_sbs_upload \
--group_by_type _sbs.mp4 \
--connected_video test_videos1_splatting_64_sp_video_3D_connect/final_sbs_audio.mp4
huggingface-cli upload svjack/Genshin-Impact-LiYue-QunYuGe-War-3D-Anaglyph-Video test_videos1_splatting_64_sp_video_3D_anaglyph_upload --repo-type dataset
huggingface-cli upload svjack/Genshin-Impact-LiYue-QunYuGe-War-3D-STS-Video test_videos1_splatting_64_sp_video_3D_sbs_upload --repo-type dataset
#####
huggingface-cli download svjack/Genshin-Impact-Novel-Video "原神角色EP.zip" --repo-type dataset --local-dir .
unzip 原神角色EP.zip
mkdir test_videos2 && cp "原神角色EP/《原神》EP - 如风如露之思.mp4" test_videos2
python split_video.py --input_path test_videos2 --frames_per_segment 64 --output_path test_videos2_64_sp --skip_short_segments
python depth_splatting_inference_npz_iter.py \
--pre_trained_path ./weights/stable-video-diffusion-img2vid-xt-1-1\
--unet_path ./weights/DepthCrafter --process_length 64 \
--input_path test_videos2_64_sp \
--output_path test_videos2_splatting_64_sp
python npz_to_video.py --input_folder test_videos2_splatting_64_sp --output_folder test_videos2_splatting_64_sp_video
!pip install -r requirements.txt
python inpainting_inference_iter.py \
--pre_trained_path ./weights/stable-video-diffusion-img2vid-xt-1-1 \
--unet_path ./weights/StereoCrafter \
--input_video_path test_videos2_splatting_64_sp_video --output_video_path test_videos2_splatting_64_sp_video_3D
python connect_video.py --input_folder test_videos2_splatting_64_sp_video_3D --output_folder test_videos2_splatting_64_sp_video_3D_connect
#python spatial_split_video.py --input_video_path test_videos2_splatting_64_sp_video_3D_connect/final_sbs.mp4 --split_direction vertical
python add_audio_with_speed_adjustment.py "test_videos2/《原神》EP - 如风如露之思.mp4" test_videos2_splatting_64_sp_video_3D_connect/final_anaglyph.mp4 test_videos2_splatting_64_sp_video_3D_connect/final_anaglyph_audio.mp4
python add_audio_with_speed_adjustment.py "test_videos2/《原神》EP - 如风如露之思.mp4" test_videos2_splatting_64_sp_video_3D_connect/final_sbs.mp4 test_videos2_splatting_64_sp_video_3D_connect/final_sbs_audio.mp4
python produce_hf_video_dataset.py --input_folder test_videos2_splatting_64_sp_video_3D \
--output_folder test_videos2_splatting_64_sp_video_3D_anaglyph_upload \
--group_by_type _anaglyph.mp4 \
--connected_video test_videos2_splatting_64_sp_video_3D_connect/final_anaglyph_audio.mp4
python produce_hf_video_dataset.py --input_folder test_videos2_splatting_64_sp_video_3D \
--output_folder test_videos2_splatting_64_sp_video_3D_sbs_upload \
--group_by_type _sbs.mp4 \
--connected_video test_videos2_splatting_64_sp_video_3D_connect/final_sbs_audio.mp4
huggingface-cli upload svjack/Genshin-Impact-Nahida-EP-3D-Anaglyph-Video test_videos2_splatting_64_sp_video_3D_anaglyph_upload --repo-type dataset
huggingface-cli upload svjack/Genshin-Impact-Nahida-EP-3D-STS-Video test_videos2_splatting_64_sp_video_3D_sbs_upload --repo-type dataset
##### 【原神真人版】如果现实世界与提瓦特接轨..._fix.mp4
#####.\BBDown.exe https://www.bilibili.com/video/BV1py4y1t7u7
mkdir test_videos3 && cp "【原神真人版】如果现实世界与提瓦特接轨..._fix.mp4" test_videos3
python split_video.py --input_path test_videos3 --frames_per_segment 64 --output_path test_videos3_64_sp --skip_short_segments
python depth_splatting_inference_npz_iter.py \
--pre_trained_path ./weights/stable-video-diffusion-img2vid-xt-1-1\
--unet_path ./weights/DepthCrafter --process_length 64 \
--input_path test_videos3_64_sp \
--output_path test_videos3_splatting_64_sp
python npz_to_video.py --input_folder test_videos3_splatting_64_sp --output_folder test_videos3_splatting_64_sp_video
!pip install -r requirements.txt
python inpainting_inference_iter.py \
--pre_trained_path ./weights/stable-video-diffusion-img2vid-xt-1-1 \
--unet_path ./weights/StereoCrafter \
--input_video_path test_videos3_splatting_64_sp_video --output_video_path test_videos3_splatting_64_sp_video_3D
python connect_video.py --input_folder test_videos3_splatting_64_sp_video_3D --output_folder test_videos3_splatting_64_sp_video_3D_connect
python add_audio_with_speed_adjustment.py "【原神真人版】如果现实世界与提瓦特接轨..._fix.mp4" test_videos3_splatting_64_sp_video_3D_connect/final_anaglyph.mp4 test_videos3_splatting_64_sp_video_3D_connect/final_anaglyph_audio.mp4
python add_audio_with_speed_adjustment.py "【原神真人版】如果现实世界与提瓦特接轨..._fix.mp4" test_videos3_splatting_64_sp_video_3D_connect/final_sbs.mp4 test_videos3_splatting_64_sp_video_3D_connect/final_sbs_audio.mp4
python produce_hf_video_dataset.py --input_folder test_videos3_splatting_64_sp_video_3D \
--output_folder test_videos3_splatting_64_sp_video_3D_anaglyph_upload \
--group_by_type _anaglyph.mp4 \
--connected_video test_videos3_splatting_64_sp_video_3D_connect/final_anaglyph_audio.mp4
python produce_hf_video_dataset.py --input_folder test_videos3_splatting_64_sp_video_3D \
--output_folder test_videos3_splatting_64_sp_video_3D_sbs_upload \
--group_by_type _sbs.mp4 \
--connected_video test_videos3_splatting_64_sp_video_3D_connect/final_sbs_audio.mp4
huggingface-cli upload svjack/Genshin-Impact-RealWorld-Teyvat-Reality-3D-Anaglyph-Video test_videos3_splatting_64_sp_video_3D_anaglyph_upload --repo-type dataset
huggingface-cli upload svjack/Genshin-Impact-RealWorld-Teyvat-Reality-3D-STS-Video test_videos3_splatting_64_sp_video_3D_sbs_upload --repo-type dataset
'''
import gc
import os
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from tqdm import tqdm
import argparse
from diffusers.training_utils import set_seed
from decord import VideoReader, cpu
from dependency.DepthCrafter.depthcrafter.depth_crafter_ppl import DepthCrafterPipeline
from dependency.DepthCrafter.depthcrafter.unet import DiffusersUNetSpatioTemporalConditionModelDepthCrafter
from dependency.DepthCrafter.depthcrafter.utils import vis_sequence_depth, read_video_frames
from Forward_Warp import forward_warp
save_grid = True
class DepthCrafterDemo:
def __init__(
self,
unet_path: str,
pre_trained_path: str,
cpu_offload: str = "model",
):
unet = DiffusersUNetSpatioTemporalConditionModelDepthCrafter.from_pretrained(
unet_path,
low_cpu_mem_usage=True,
torch_dtype=torch.float16,
)
self.pipe = DepthCrafterPipeline.from_pretrained(
pre_trained_path,
unet=unet,
torch_dtype=torch.float16,
variant="fp16",
)
if cpu_offload is not None:
if cpu_offload == "sequential":
self.pipe.enable_sequential_cpu_offload()
elif cpu_offload == "model":
self.pipe.enable_model_cpu_offload()
else:
raise ValueError(f"Unknown cpu offload option: {cpu_offload}")
else:
self.pipe.to("cuda")
try:
self.pipe.enable_xformers_memory_efficient_attention()
except Exception as e:
print(e)
print("Xformers is not enabled")
self.pipe.enable_attention_slicing()
def infer(
self,
input_video_path: str,
output_video_path: str,
process_length: int = -1,
num_denoising_steps: int = 8,
guidance_scale: float = 1.2,
window_size: int = 70,
overlap: int = 25,
max_res: int = 1024,
dataset: str = "open",
target_fps: int = -1,
seed: int = 42,
track_time: bool = False,
save_depth: bool = True,
):
set_seed(seed)
frames, target_fps, original_height, original_width = read_video_frames(
input_video_path,
process_length,
target_fps,
max_res,
dataset,
)
with torch.inference_mode():
res = self.pipe(
frames,
height=frames.shape[1],
width=frames.shape[2],
output_type="np",
guidance_scale=guidance_scale,
num_inference_steps=num_denoising_steps,
window_size=window_size,
overlap=overlap,
track_time=track_time,
).frames[0]
res = res.sum(-1) / res.shape[-1]
tensor_res = torch.tensor(res).unsqueeze(1).float().contiguous().cuda()
res = F.interpolate(tensor_res, size=(original_height, original_width), mode='bilinear', align_corners=False)
res = res.cpu().numpy()[:,0,:,:]
res = (res - res.min()) / (res.max() - res.min())
vis = vis_sequence_depth(res)
save_path = os.path.join(
os.path.dirname(output_video_path), os.path.splitext(os.path.basename(output_video_path))[0]
)
os.makedirs(os.path.dirname(save_path), exist_ok=True)
if save_depth:
np.savez_compressed(save_path + ".npz", depth=res)
np.savez_compressed(save_path + "_depth_vis.npz", video=vis)
return res, vis
class ForwardWarpStereo(nn.Module):
def __init__(self, eps=1e-6, occlu_map=False):
super(ForwardWarpStereo, self).__init__()
self.eps = eps
self.occlu_map = occlu_map
self.fw = forward_warp()
def forward(self, im, disp):
im = im.contiguous()
disp = disp.contiguous()
weights_map = disp - disp.min()
weights_map = (1.414) ** weights_map
flow = -disp.squeeze(1)
dummy_flow = torch.zeros_like(flow, requires_grad=False)
flow = torch.stack((flow, dummy_flow), dim=-1)
res_accum = self.fw(im * weights_map, flow)
mask = self.fw(weights_map, flow)
mask.clamp_(min=self.eps)
res = res_accum / mask
if not self.occlu_map:
return res
else:
ones = torch.ones_like(disp, requires_grad=False)
occlu_map = self.fw(ones, flow)
occlu_map.clamp_(0.0, 1.0)
occlu_map = 1.0 - occlu_map
return res, occlu_map
def process_video(
input_video_path: str,
output_video_path: str,
depthcrafter_demo: DepthCrafterDemo,
max_disp: float = 20.0,
process_length: int = -1
):
video_depth, depth_vis = depthcrafter_demo.infer(
input_video_path,
output_video_path,
process_length,
)
if save_grid:
vid_reader = VideoReader(input_video_path, ctx=cpu(0))
original_fps = vid_reader.get_avg_fps()
input_frames = vid_reader[:process_length].asnumpy() / 255.0
if process_length != -1 and process_length < len(input_frames):
input_frames = input_frames[:process_length]
stereo_projector = ForwardWarpStereo(occlu_map=True).cuda()
left_video = torch.tensor(input_frames).permute(0, 3, 1, 2).float().contiguous().cuda()
disp_map = torch.tensor(video_depth).unsqueeze(1).float().contiguous().cuda()
disp_map = disp_map * 2.0 - 1.0
disp_map = disp_map * max_disp
right_video, occlusion_mask = stereo_projector(left_video, disp_map)
right_video = right_video.cpu().permute(0, 2, 3, 1).numpy()
occlusion_mask = occlusion_mask.cpu().permute(0, 2, 3, 1).numpy().repeat(3, axis=-1)
video_grid_top = np.concatenate([input_frames, depth_vis], axis=2)
video_grid_bottom = np.concatenate([occlusion_mask, right_video], axis=2)
video_grid = np.concatenate([video_grid_top, video_grid_bottom], axis=1)
save_path = os.path.join(
os.path.dirname(output_video_path), os.path.splitext(os.path.basename(output_video_path))[0]
)
np.savez_compressed(save_path + "_video_grid.npz", video_grid=video_grid)
print(f"Finished processing {input_video_path}")
def main():
parser = argparse.ArgumentParser(description="Process videos using DepthCrafter.")
parser.add_argument("--input_path", type=str, required=True, help="Path to the input video or directory containing videos.")
parser.add_argument("--output_path", type=str, required=True, help="Path to the output directory.")
parser.add_argument("--unet_path", type=str, required=True, help="Path to the UNet model.")
parser.add_argument("--pre_trained_path", type=str, required=True, help="Path to the pre-trained model.")
parser.add_argument("--max_disp", type=float, default=20.0, help="Maximum disparity value.")
parser.add_argument("--process_length", type=int, default=-1, help="Number of frames to process.")
args = parser.parse_args()
depthcrafter_demo = DepthCrafterDemo(
unet_path=args.unet_path,
pre_trained_path=args.pre_trained_path,
cpu_offload = "sequential"
)
if os.path.isfile(args.input_path):
video_files = [args.input_path]
else:
video_files = [os.path.join(args.input_path, f) for f in os.listdir(args.input_path) if f.endswith(('.mp4', '.avi', '.mov'))]
os.makedirs(args.output_path, exist_ok=True)
for video_file in tqdm(video_files, desc="Processing videos"):
output_video_name = os.path.basename(video_file).replace(" ", "_")
output_video_path = os.path.join(args.output_path, output_video_name)
process_video(video_file, output_video_path, depthcrafter_demo, args.max_disp, args.process_length)
if __name__ == "__main__":
main()