Files
audio2motion/common/animation.py
T
2022-02-07 17:28:40 +08:00

262 lines
10 KiB
Python

import moviepy.editor as mpy
import numpy as np
import sys
sys.path.insert(0, "/mnt/data/CODE/Audio2Expression")
from common.landmark import landmark_to_images
def save_animation(data_motion, path_save, suffix, w=128*5, h=72*5, dpi=80):
""" convert data_motion to .mp4 animation
data_motion, shape [B, N, 136, ...]
"""
# no need invert
# anim_clips = inv_standardize(data_motion[:self.nseq_test, :, :], self.motion_scaler)
fps = 30
B, *_ = data_motion.shape
for i in range(B):
idata_pt = data_motion[i, :, :].reshape(-1, 68, 2)
images_pt = landmark_to_images(idata_pt, w=w, h=h, dpi=dpi, norm=True, prex="PT")
duration = idata_pt.shape[0] / fps
# animation = mpy.VideoClip(lambda t: images_pt[int(fps * t + 0.5) - 1,...], duration= duration )
animation = mpy.VideoClip(lambda t: images_pt[int(fps * t + 0.5),...], duration= duration )
ipath_save = path_save.joinpath(f"{suffix}_{i:02d}.mp4")
animation.write_videofile(str(ipath_save), fps=fps, logger=None)
def save_animation_ws_audio(data_motion, path_save, path_audio=None, start_time=None, end_time=None, fps=30, w=256, h=256, dpi=150):
""" convert data_motion to .mp4 animation
data_motion, shape [nsteps, 136]
"""
# fps = 30
nsteps = data_motion.shape[0]
data_motion = data_motion.copy().reshape(nsteps, 68, 2) # nsteps x 68 x 2
image = landmark_to_images(data_motion, w=w, h=h, dpi=dpi, norm=True, prex="PT") # (nsteps, H, W, 3)
duration = nsteps / fps
animation = mpy.VideoClip(lambda t: image[int(fps * t + 0.5),...], duration= duration )
duration = animation.duration
if path_audio is not None:
audioclip = mpy.AudioFileClip(str(path_audio))
# cut-off
if (start_time is not None) and (end_time is not None):
audioclip = audioclip.subclip(start_time, end_time)
# alignment to video
audioclip = audioclip.set_duration(duration)
# add audio to animation
animation = animation.set_audio(audioclip)
animation = animation.set_duration(duration)
# save
animation.write_videofile(str(path_save), fps=fps, logger=None)
def save_animation_grid(data_motion, path_save, path_audio=None, start_time=None, end_time=None, ncol=3, w=128*5, h=72*5, dpi=80):
""" convert data_motion to .mp4 animation
data_motion, shape [N, nsteps, 136]
"""
# no need invert
# anim_clips = inv_standardize(data_motion[:self.nseq_test, :, :], self.motion_scaler)
fps = 30
N, nsteps, _ = data_motion.shape
total_image = []
for i in range(N):
idata_pt = data_motion[i, :, :].reshape(-1, 68, 2) # nsteps x 68 x 2
prex = "ground-truth" if i == 0 else f"prediction-{i-0:02d}"
image = landmark_to_images(idata_pt, w=w, h=h, dpi=dpi, norm=True, prex=prex) # (nsteps, H, W, 3)
total_image.append(image)
print(f"images shape = {image.shape}")
total_image = np.concatenate(total_image, axis=0) # N x nsteps x H x W x 3
total_image = total_image.reshape(N//ncol, ncol, nsteps, h, w, 3)
total_image = total_image.transpose((2, 0, 3, 1, 4, 5)) # nsteps x nrow x H x ncol x W x 3
total_image = total_image.reshape(nsteps, -1, ncol*w, 3) # nsteps x nrow*H x ncol*W x 3
print(f"total images shape = {total_image.shape}")
duration = nsteps / fps
animation = mpy.VideoClip(lambda t: total_image[int(fps * t + 0.5),...], duration= duration )
# animation = mpy.VideoClip(lambda t: images_pt[int(fps * t + 0.5) ,...], duration= duration )
duration = animation.duration
if path_audio is not None:
audioclip = mpy.AudioFileClip(str(path_audio))
# cut-off
if (start_time is not None) and (end_time is not None):
audioclip = audioclip.cutout(start_time, end_time)
# alignment to video
audioclip = audioclip.set_duration(duration)
# add audio to animation
animation = animation.set_audio(audioclip)
animation = animation.set_duration(duration)
# save
animation.write_videofile(str(path_save), fps=fps, logger=None)
def save_animation2(images, path_save):
fps = 30
duration = len(images) / fps
animation = mpy.VideoClip(lambda t: images[int(fps * t + 0.5) - 1], duration= duration )
animation.write_videofile(str(path_save), fps=fps, logger=None)
def save_animation_with_gts(images_list, path_save):
""" convert images_pt to .mp4 animation
images_pt, shape len=N list [224, 224, 3]
images_gt, shape len=N list [224, 224, 3]
"""
# print(f"images_pt shape = {len(images_list[0])}, shape[0]={images_pt[0].shape}")
fps = 30
N = len(images_list[0])
duration = N / fps
print(f"fps = {fps}, N ={N}, duration={duration}")
# assert len(images_pt) == len(images_gt), f"pt and gt imamge size not the same ({len(images_pt)}, {len(images_pt)})"
animations = []
for images in images_list:
animations.append(mpy.VideoClip(lambda t: images[int(fps * t + 0.5) -1], duration= duration))
# mixture & save
animation = mpy.clips_array([animations])
animation.write_videofile(str(path_save), fps=fps, logger=None)
def save_images_with_audio(images, path_audio, path_save):
""" convert images_pt to .mp4 animation
images_pt, shape len=N list [224, 224, 3]
images_gt, shape len=N list [224, 224, 3]
"""
N = len(images)
# load audio
# if isinstance(path_audio, str):
# print(f"load audio form str")
audioclip = mpy.AudioFileClip(str(path_audio))
# elif isinstance(path_audio, np.ndarray):
# print(f"load audio form np.ndarray")
# audioclip = mpy.AudioFileClip(path_audio, fps=16000)
duration = audioclip.duration
# add video
fps = N / duration
animation = mpy.VideoClip(lambda t: images[int(fps * t + 0.5) -1], duration= duration)
animation = animation.set_audio(audioclip) # add audio to animation
# save
animation.write_videofile(str(path_save), fps=fps)
print(f"fps = {fps}, N ={N}, duration={duration} -> save to = {path_save}")
def save_animation_with_gt5(images_pt, images_gt, images_A, images_B, images_C, path_save):
""" convert images_pt to .mp4 animation
images_pt, shape len=N list [224, 224, 3]
images_gt, shape len=N list [224, 224, 3]
"""
# print(f"images_pt shape = {len(images_pt)}, shape[0]={images_pt[0].shape}")
fps = 30
N = len(images_pt)
duration = N / fps
# print(f"fps = {fps}, N ={N}, duration={duration}")
# assert len(images_pt) == len(images_gt), f"pt and gt imamge size not the same ({len(images_pt)}, {len(images_pt)})"
animation_pt = mpy.VideoClip(lambda t: images_pt[int(fps * t + 0.5) -1], duration= duration)
animation_gt = mpy.VideoClip(lambda t: images_gt[int(fps * t + 0.5) -1], duration= duration)
animation_A = mpy.VideoClip(lambda t: images_A[int(fps * t + 0.5) -1], duration= duration)
animation_B = mpy.VideoClip(lambda t: images_B[int(fps * t + 0.5) -1], duration= duration)
animation_C = mpy.VideoClip(lambda t: images_C[int(fps * t + 0.5) -1], duration= duration)
# mixture & save
animation = mpy.clips_array([[animation_pt, animation_gt, animation_A, animation_B, animation_C]])
animation.write_videofile(str(path_save), fps=fps, logger=None)
def save_animation_with_gt2(images_pt, images_gt, path_save, fps=None, path_audio=None, start_time=None, end_time=None):
""" convert images_pt to .mp4 animation
images_pt, shape len= N list [224, 224, 3]
images_gt, shape len= N list [224, 224, 3]
"""
# print(f"images_pt shape = {len(images_pt)}, shape[0]={images_pt[0].shape}")
fps = fps if fps else 30
N = len(images_pt)
duration = N / fps
# print(f"fps = {fps}, N ={N}, duration={duration}")
# assert len(images_pt) == len(images_gt), f"pt and gt imamge size not the same ({len(images_pt)}, {len(images_pt)})"
animation_pt = mpy.VideoClip(lambda t: images_pt[int(fps * t + 0.5) -1], duration= duration)
animation_gt = mpy.VideoClip(lambda t: images_gt[int(fps * t + 0.5) -1], duration= duration)
# mixture & save
animation = mpy.clips_array([[animation_pt, animation_gt]])
if path_audio is not None:
audioclip = mpy.AudioFileClip(str(path_audio))
# cut-off
if (start_time is not None) and (end_time is not None):
print(f"subclip clips {start_time}:{end_time}")
audioclip = audioclip.subclip(start_time, end_time)
# alignment to video
audioclip = audioclip.set_duration(duration)
# add audio to animation
animation = animation.set_audio(audioclip)
animation = animation.set_duration(duration)
animation.write_videofile(str(path_save), fps=fps)
def save_animation_with_gt(data_motion, data_motion_gt, path_save, suffix, w=128*5, h=72*5, dpi=80, maxsave=3):
""" convert data_motion to .mp4 animation
data_motion, shape [B, N, 68, 2]
data_motion_gt, shape [B, N, 68, 2]
"""
fps = 30
B, *_ = data_motion.shape
B = min(B, maxsave)
for i in range(B):
idata_pt = data_motion[i].reshape(-1, 68, 2)
idata_gt = data_motion_gt[i].reshape(-1, 68, 2)
images_pt = landmark_to_images(idata_pt, w=w, h=h, dpi=dpi, norm=True, prex="PT")
images_gt = landmark_to_images(idata_gt, w=w, h=h, dpi=dpi, norm=True, prex="GT")
duration = idata_pt.shape[0] / fps
# animation = mpy.VideoClip(lambda t: images_pt[int(fps * t + 0.5) - 1,...], duration= duration )
animation_pt = mpy.VideoClip(lambda t: images_pt[int(fps * t + 0.5),...], duration= duration)
animation_gt = mpy.VideoClip(lambda t: images_gt[int(fps * t + 0.5),...], duration= duration)
# mixture
animation = mpy.clips_array([[animation_pt, animation_gt]])
ipath_save = path_save.joinpath(f"{suffix}_{i:02d}.mp4")
animation.write_videofile(str(ipath_save), fps=fps, logger=None)