科技匠|怎样用 Python 控制图片人物动起来?一文就能 Get( 二 )
kp_detector.load_state_dict(checkpoint['kp_detector'])
if not cpu:
generator = DataParallelWithCallback(generator)
kp_detector = DataParallelWithCallback(kp_detector)
generator.eval
kp_detector.eval
return generator, kp_detector
(2)然后是利用模型创建产生的虚拟图像 , 找到最佳的脸部特征:
def make_animation(source_image, driving_video, generator, kp_detector, relative=True, adapt_movement_scale=True, cpu=False):
with torch.no_grad:
predictions =
source = torch.tensor(source_image[np.newaxis].astype(np.float32)).permute(0, 3, 1, 2)
if not cpu:
source = source.cuda
driving = torch.tensor(np.array(driving_video)[np.newaxis].astype(np.float32)).permute(0, 4, 1, 2, 3)
kp_source = kp_detector(source)
kp_driving_initial = kp_detector(driving[:, :, 0])
for frame_idx in tqdm(range(driving.shape[2])):
driving_frame = driving[:, :, frame_idx]
if not cpu:
driving_frame = driving_frame.cuda
kp_driving = kp_detector(driving_frame)
kp_norm = normalize_kp(kp_source=kp_source, kp_driving=kp_driving,
kp_driving_initial=kp_driving_initial, use_relative_movement=relative,
use_relative_jacobian=relative, adapt_movement_scale=adapt_movement_scale)
out = generator(source, kp_source=kp_source, kp_driving=kp_norm) predictions.append(np.transpose(out['prediction'].data.cpu.numpy, [0, 2, 3, 1])[0])
return predictions
def find_best_frame(source, driving, cpu=False):
import face_alignment
def normalize_kp(kp):
kp = kp - kp.mean(axis=0, keepdims=True)
area = ConvexHull(kp[:, :2]).volume
area = np.sqrt(area)
kp[:, :2] = kp[:, :2] / area
return kp
fa = face_alignment.FaceAlignment(face_alignment.LandmarksType._2D, flip_input=True,
device='cpu' if cpu else 'cuda')
kp_source = fa.get_landmarks(255 * source)[0]
kp_source = normalize_kp(kp_source)
norm = float('inf')
frame_num = 0
for i, image in tqdm(enumerate(driving)):
kp_driving = fa.get_landmarks(255 * image)[0]
kp_driving = normalize_kp(kp_driving)
new_norm = (np.abs(kp_source - kp_driving) ** 2).sum
if new_norm < norm:
norm = new_norm
frame_num = i
return frame_num
(3)接着定义命令行调用参数加载图片、视频等方式:
parser = ArgumentParser
parser.add_argument("--config", required=True, help="path to config")
parser.add_argument("--checkpoint", default='vox-cpk.pth.tar', help="path to checkpoint to restore")
parser.add_argument("--source_image", default='sup-mat/source.png', help="path to source image")
parser.add_argument("--driving_video", default='sup-mat/source.png', help="path to driving video")
parser.add_argument("--result_video", default='result.mp4', help="path to output")
parser.add_argument("--relative", dest="relative", action="store_true", help="use relative or absolute keypoint coordinates")
parser.add_argument("--adapt_scale", dest="adapt_scale", action="store_true", help="adapt movement scale based on convex hull of keypoints")
推荐阅读
- 下个10年,Go能取代Python成为开发者的首选语言吗?
- 怎样不秃顶|期中温习: 二年级上册数学加法、减法和乘法应用题, 附谜底
- 科技日报|塑料微粒影响几何?人类认知仍不足
- 七号人称说科技|华为始料未及?台积电又成了“炮灰”?新风暴说来就来
- 大众新闻|众安科技荣获“今日·保险中介榜”年度Insurtech独角兽
- 雷科技|朋友圈都在秀的“互联网勋章”到底是什么?
- 快科技|小米10至尊纪念版妙享功能升级:手机与Windows电脑合体
- 七号人称说科技|再给华为一颗糖?美国到底在打什么“算盘”?,先给华为一巴掌
- 量子科技,爆发!有项关键技术中国已领先世界
- 暖日科技盈如|这到底是为什么?,俄罗斯华人给出劝告:最好别跟俄罗斯女孩结婚
