Python first-order-model实现让照片动起来


Posted in Python onJune 25, 2022

前言

看到一个很有意思的项目,其实在之前就在百度飞浆等平台上看到类似的实现效果。

可以将照片按照视频的表情,动起来。看一下项目给出的效果。

Python first-order-model实现让照片动起来

项目地址:first-order-model项目地址

还是老样子,不管作者给出的种种效果,自己测试一下。

资源下载和安装

我们先看一下README关于项目的基本信息,可以看出除了表情驱动照片,还可以姿态迁移。

Python first-order-model实现让照片动起来

Python first-order-model实现让照片动起来

模型文件提供了线上的下载地址。

Python first-order-model实现让照片动起来

文件很大而且难下,我下好了放到我的云盘上,可以从下面云盘下载。

链接 提取码:ikix

模型文件放到根目录下新建的checkpoint文件夹下。

Python first-order-model实现让照片动起来

Python first-order-model实现让照片动起来

将requirements.txt中的依赖安装一下。

Python first-order-model实现让照片动起来

安装补充 

在测试README中的命令的时候,如果出现一下报错。

Traceback (most recent call last):
  File "demo.py", line 17, in <module>
    from animate import normalize_kp
  File "D:\spyder\first-order-model\animate.py", line 7, in <module>
    from frames_dataset import PairedDataset
  File "D:\spyder\first-order-model\frames_dataset.py", line 10, in <module>
    from augmentation import AllAugmentationTransform
  File "D:\spyder\first-order-model\augmentation.py", line 13, in <module>
    import torchvision
  File "C:\Users\huyi\.conda\envs\fom\lib\site-packages\torchvision\__init__.py", line 2, in <module>
    from torchvision import datasets
  File "C:\Users\huyi\.conda\envs\fom\lib\site-packages\torchvision\datasets\__init__.py", line 9, in <module>
    from .fakedata import FakeData
  File "C:\Users\huyi\.conda\envs\fom\lib\site-packages\torchvision\datasets\fakedata.py", line 3, in <module>
    from .. import transforms
  File "C:\Users\huyi\.conda\envs\fom\lib\site-packages\torchvision\transforms\__init__.py", line 1, in <module>
    from .transforms import *
  File "C:\Users\huyi\.conda\envs\fom\lib\site-packages\torchvision\transforms\transforms.py", line 16, in <module>
    from . import functional as F
  File "C:\Users\huyi\.conda\envs\fom\lib\site-packages\torchvision\transforms\functional.py", line 5, in <module>
    from PIL import Image, ImageOps, ImageEnhance, PILLOW_VERSION
ImportError: cannot import name 'PILLOW_VERSION' from 'PIL' (C:\Users\huyi\.conda\envs\fom\lib\site-packages\PIL\__init__.py)

这个问题主要是我使用的pillow版本过高的原因,如果不想找对应的低版本,可以按照我的方式解决。 

1、修改functional.py代码,将PILLOW_VERSION调整为__version__。

Python first-order-model实现让照片动起来

2、将imageio升级。

pip install --upgrade imageio -i https://pypi.douban.com/simple

3、安装imageio_ffmpeg模块。

pip install imageio-ffmpeg -i https://pypi.douban.com/simple

工具代码验证

官方给出的使用方法我就不重复测试,大家可以按照下面的命令去测试一下。

Python first-order-model实现让照片动起来

这里我推荐一个可视化的库gradio,下面我将demo.py的代码改造了一下。

新的工具文件代码如下:

#!/user/bin/env python
# coding=utf-8
"""
@project : first-order-model
@author  : 剑客阿良_ALiang
@file   : hy_gradio.py
@ide    : PyCharm
@time   : 2022-06-23 14:35:28
"""
import uuid
from typing import Optional
 
import gradio as gr
import matplotlib
 
matplotlib.use('Agg')
import os, sys
import yaml
from argparse import ArgumentParser
from tqdm import tqdm
 
import imageio
import numpy as np
from skimage.transform import resize
from skimage import img_as_ubyte
import torch
from sync_batchnorm import DataParallelWithCallback
 
from modules.generator import OcclusionAwareGenerator
from modules.keypoint_detector import KPDetector
from animate import normalize_kp
from scipy.spatial import ConvexHull
 
if sys.version_info[0] < 3:
    raise Exception("You must use Python 3 or higher. Recommended version is Python 3.7")
 
 
def load_checkpoints(config_path, checkpoint_path, cpu=False):
    with open(config_path) as f:
        config = yaml.load(f)
 
    generator = OcclusionAwareGenerator(**config['model_params']['generator_params'],
                                        **config['model_params']['common_params'])
    if not cpu:
        generator.cuda()
 
    kp_detector = KPDetector(**config['model_params']['kp_detector_params'],
                             **config['model_params']['common_params'])
    if not cpu:
        kp_detector.cuda()
 
    if cpu:
        checkpoint = torch.load(checkpoint_path, map_location=torch.device('cpu'))
    else:
        checkpoint = torch.load(checkpoint_path)
 
    generator.load_state_dict(checkpoint['generator'])
    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
 
 
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
 
 
def h_interface(input_image: str):
    parser = ArgumentParser()
    opt = parser.parse_args()
    opt.config = "./config/vox-256.yaml"
    opt.checkpoint = "./checkpoint/vox-cpk.pth.tar"
    opt.source_image = input_image
    opt.driving_video = "./data/input/ts.mp4"
    opt.result_video = "./data/result/{}.mp4".format(uuid.uuid1().hex)
    opt.relative = True
    opt.adapt_scale = True
    opt.cpu = True
    opt.find_best_frame = False
    opt.best_frame = False
    # source_image = imageio.imread(opt.source_image)
    source_image = opt.source_image
    reader = imageio.get_reader(opt.driving_video)
    fps = reader.get_meta_data()['fps']
    driving_video = []
    try:
        for im in reader:
            driving_video.append(im)
    except RuntimeError:
        pass
    reader.close()
 
    source_image = resize(source_image, (256, 256))[..., :3]
    driving_video = [resize(frame, (256, 256))[..., :3] for frame in driving_video]
    generator, kp_detector = load_checkpoints(config_path=opt.config, checkpoint_path=opt.checkpoint, cpu=opt.cpu)
 
    if opt.find_best_frame or opt.best_frame is not None:
        i = opt.best_frame if opt.best_frame is not None else find_best_frame(source_image, driving_video, cpu=opt.cpu)
        print("Best frame: " + str(i))
        driving_forward = driving_video[i:]
        driving_backward = driving_video[:(i + 1)][::-1]
        predictions_forward = make_animation(source_image, driving_forward, generator, kp_detector,
                                             relative=opt.relative, adapt_movement_scale=opt.adapt_scale, cpu=opt.cpu)
        predictions_backward = make_animation(source_image, driving_backward, generator, kp_detector,
                                              relative=opt.relative, adapt_movement_scale=opt.adapt_scale, cpu=opt.cpu)
        predictions = predictions_backward[::-1] + predictions_forward[1:]
    else:
        predictions = make_animation(source_image, driving_video, generator, kp_detector, relative=opt.relative,
                                     adapt_movement_scale=opt.adapt_scale, cpu=opt.cpu)
    imageio.mimsave(opt.result_video, [img_as_ubyte(frame) for frame in predictions], fps=fps)
    return opt.result_video
 
 
if __name__ == "__main__":
    demo = gr.Interface(h_interface, inputs=[gr.Image(shape=(500, 500))], outputs=[gr.Video()])
 
    demo.launch()
    # h_interface("C:\\Users\\huyi\\Desktop\\xx3.jpg")

代码说明

1、将原demo.py中的main函数内容,重新编辑为h_interface方法,输入是想要驱动的图片。

2、其中driving_video参数使用了我自己录制的一段表情视频ts.mp4,我建议在使用的时候可以自己用手机录制一段替换。

3、使用gradio来生成方法的页面,下面会展示给大家看。

4、使用uuid为结果视频命名。

执行结果如下

Running on local URL:  http://127.0.0.1:7860/
To create a public link, set `share=True` in `launch()`.

打开本地的地址:http://localhost:7860/

可以看到我们实现的交互界面如下:

Python first-order-model实现让照片动起来

我们上传一下我准备的样例图片,提交制作。

Python first-order-model实现让照片动起来

看一下执行的日志,如下图。

Python first-order-model实现让照片动起来

看一下制作结果。

Python first-order-model实现让照片动起来

由于上传不了视频,我将视频转成了gif。

Python first-order-model实现让照片动起来

还是蛮有意思的,具体的参数调优我就不弄了,大家可能根据需要调整我提供的方法里面的参数。

以上就是Python first-order-model实现让照片动起来的详细内容,更多关于Python 照片动起来的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
使用wxpython实现的一个简单图片浏览器实例
Jul 10 Python
利用python3随机生成中文字符的实现方法
Nov 24 Python
Python可变参数*args和**kwargs用法实例小结
Apr 27 Python
flask框架使用orm连接数据库的方法示例
Jul 16 Python
Python 打印中文字符的三种方法
Aug 14 Python
对pyqt5多线程正确的开启姿势详解
Jun 14 Python
使用python实现简单五子棋游戏
Jun 18 Python
python通过http下载文件的方法详解
Jul 26 Python
python脚本实现音频m4a格式转成MP3格式的实例代码
Oct 09 Python
关于多元线性回归分析——Python&amp;SPSS
Feb 24 Python
python爬虫容易学吗
Jun 02 Python
使用Python爬取小姐姐图片(beautifulsoup法)
Feb 11 Python
python热力图实现的完整实例
彻底弄懂Python中的回调函数(callback)
Jun 25 #Python
利用Python实现翻译HTML中的文本字符串
Jun 21 #Python
使用scrapy实现增量式爬取方式
Jun 21 #Python
python+opencv实现目标跟踪过程
Jun 21 #Python
使用opencv-python如何打开USB或者笔记本前置摄像头
Python+DeOldify实现老照片上色功能
You might like
php加密解密函数authcode的用法详细解析
2013/10/28 PHP
php正则匹配html中带class的div并选取其中内容的方法
2015/01/13 PHP
Linux操作系统安装LAMP环境
2015/06/26 PHP
EasyUi tabs的高度与宽度根据IE窗口的变化自适应代码
2010/10/26 Javascript
javascript克隆对象深度介绍
2012/11/20 Javascript
jquery 倒计时效果实现秒杀思路
2013/09/11 Javascript
基于JS实现Android,iOS一个手势动画效果
2016/04/27 Javascript
javascript创建含数字字母的随机字符串方法总结
2016/08/01 Javascript
卸载安装Node.js与npm过程详解
2016/08/15 Javascript
微信小程序开发实战教程之手势解锁
2016/11/18 Javascript
angularJS+requireJS实现controller及directive的按需加载示例
2017/02/20 Javascript
详解有关easyUI的拖动操作中droppable,draggable用法例子
2017/06/03 Javascript
Javascript实现基本运算器
2017/07/15 Javascript
基于node.js express mvc轻量级框架实践
2017/09/14 Javascript
vue实现移动端悬浮窗效果
2018/12/01 Javascript
[35:55]完美世界DOTA2联赛PWL S3 Rebirth vs CPG 第一场 12.11
2020/12/13 DOTA
python自动安装pip
2014/04/24 Python
Python实现的合并两个有序数组算法示例
2019/03/04 Python
Python实现一个数组除以一个数的例子
2019/07/20 Python
在Python中使用MongoEngine操作数据库教程实例
2019/12/03 Python
经济实惠的名牌太阳镜和眼镜:Privé Revaux
2021/02/07 全球购物
师范生教师实习自我鉴定
2013/09/27 职场文书
父亲的菜园教学反思
2014/02/13 职场文书
综合实践活动总结
2014/05/05 职场文书
精神文明建设标语
2014/06/16 职场文书
2014乡镇班子个人对照检查材料思想汇报
2014/09/26 职场文书
上班时间打瞌睡检讨书
2014/09/26 职场文书
教师自我剖析材料(四风问题)
2014/09/30 职场文书
2014最新实习证明模板
2014/10/02 职场文书
个人融资协议书范本两则
2014/10/15 职场文书
群众路线教育实践活动实施方案
2014/10/31 职场文书
2014年党建工作总结
2014/11/11 职场文书
2015年学校教务处工作总结
2015/05/11 职场文书
2015年度高中教师工作总结
2015/05/26 职场文书
详细聊聊vue中组件的props属性
2021/11/02 Vue.js
JavaScript中时间格式化新思路toLocaleString()
2021/11/07 Javascript