zl程序教程

您现在的位置是:首页 >  其他

当前栏目

CV之IA:利用人工智能算法实现图像动画(以让古代皇帝画像以及古代四大美女画像动起来-来模仿偶像胡歌剧中角色表情动作为例-照片嗨起来)案例应用

2023-09-14 09:04:43 时间

CV之IA:利用人工智能算法实现图像动画(以让古代皇帝画像以及古代四大美女画像动起来-来模仿偶像胡歌剧中角色表情动作为例-照片嗨起来)案例应用

导读:本论文来自NeurIPS2019,该算法中主要采用一阶运动模型的思想,用一组自学习的关键点和局部仿射变换,建立了复杂运动模型。模型由运动估计模块和图像生成模块两个主要部分组成。首先进行关键点检测,然后根据关键点,进行运动估计,最后使用图像生成模块,生成最终效果。
额,哈哈,不好意思了,又拿我的偶像胡歌下手啦,视频截取来源偶像胡歌在《猎场》中的一角色。

目录

利用人工智能算法让经典图片根据自定义动作嗨起来(将一张静态人像图片转为带表情动作视频)

相关论文

输出结果

利用人工智能算法让古代皇帝画像动起来(模仿偶像胡歌《猎场》剧中角色表情动作)

利用人工智能算法让古代四大美女画像动起来

实现代码

依赖环境

导出当前依赖环境txt文档


作品视频链接
利用人工智能算法,让古代皇帝画像动起来(模仿偶像胡歌《猎场》剧中角色表情动作)
利用人工智能算法让古代美女《西施、王昭君、貂蝉、杨玉环四大美女领衔》画像动起来

利用人工智能算法让经典图片根据自定义动作嗨起来(将一张静态人像图片转为带表情动作视频)

相关论文

Paper:《First Order Motion Model for Image Animation》翻译与解读

输出结果

利用人工智能算法让古代皇帝画像动起来(模仿偶像胡歌《猎场》剧中角色表情动作)

利用人工智能算法让古代四大美女画像动起来

实现代码

import imageio
import torch
from tqdm import tqdm
from animate import normalize_kp
from demo import load_checkpoints
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from skimage import img_as_ubyte
from skimage.transform import resize
import cv2
import os
import argparse

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--input_image", required=True,help="Path to image to animate")
ap.add_argument("-c", "--checkpoint", required=True,help="Path to checkpoint")
ap.add_argument("-v","--input_video", required=False, help="Path to video input")

args = vars(ap.parse_args())

print("[INFO] loading source image and checkpoint...")
source_path = args['input_image']
checkpoint_path = args['checkpoint']
if args['input_video']:
    video_path = args['input_video']
else:
    video_path = None
source_image = imageio.imread(source_path)
source_image = resize(source_image,(256,256))[..., :3]

generator, kp_detector = load_checkpoints(config_path='config/vox-256.yaml', checkpoint_path=checkpoint_path)

if not os.path.exists('output'):
    os.mkdir('output')


relative=True
adapt_movement_scale=True
cpu=False

if video_path:
    cap = cv2.VideoCapture(video_path) 
    print("[INFO] Loading video from the given path")
else:
    cap = cv2.VideoCapture(0)
    print("[INFO] Initializing front camera...")

fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out1 = cv2.VideoWriter('output/Animation_HuGe_02.avi', fourcc, 12, (256*3 , 256), True)

cv2_source = cv2.cvtColor(source_image.astype('float32'),cv2.COLOR_BGR2RGB)
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()
    kp_source = kp_detector(source)
    count = 0
    while(True):
        ret, frame = cap.read()
        frame = cv2.flip(frame,1)
        if ret == True:
            
            if not video_path:
                x = 143
                y = 87
                w = 322
                h = 322 
                frame = frame[y:y+h,x:x+w]
            frame1 = resize(frame,(256,256))[..., :3]
            
            if count == 0:
                source_image1 = frame1
                source1 = torch.tensor(source_image1[np.newaxis].astype(np.float32)).permute(0, 3, 1, 2)
                kp_driving_initial = kp_detector(source1)
            
            frame_test = torch.tensor(frame1[np.newaxis].astype(np.float32)).permute(0, 3, 1, 2)

            driving_frame = frame_test
            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])
            im = np.transpose(out['prediction'].data.cpu().numpy(), [0, 2, 3, 1])[0]
            im = cv2.cvtColor(im,cv2.COLOR_RGB2BGR)
            joinedFrame = np.concatenate((cv2_source,im,frame1),axis=1)
            
            cv2.imshow('Test',joinedFrame)
            out1.write(img_as_ubyte(joinedFrame))
            count += 1
            if cv2.waitKey(20) & 0xFF == ord('q'):
                break
        else:
            break
        
    cap.release()
    out1.release()
    cv2.destroyAllWindows()

依赖环境

C:\Users\Administrator>conda activate F:\File_Anaconda\project_py37\envs

(F:\File_Anaconda\project_py37\envs) C:\Users\Administrator>conda list
# packages in environment at F:\File_Anaconda\project_py37\envs:
#
# Name                    Version                   Build  Channel
attrs                     19.3.0                   pypi_0    pypi
backcall                  0.1.0                    pypi_0    pypi
blas                      1.0                         mkl    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
bleach                    3.1.5                    pypi_0    pypi
ca-certificates           2021.10.26           haa95532_2    http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
certifi                   2021.10.8        py37haa95532_0    http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
cffi                      1.11.5                   pypi_0    pypi
cloudpickle               0.5.3                    pypi_0    pypi
colorama                  0.4.3                    pypi_0    pypi
cpuonly                   2.0                           0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch
cudatoolkit               8.0                           4    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
cycler                    0.10.0                   pypi_0    pypi
dask                      0.18.2                   pypi_0    pypi
decorator                 4.3.0                    pypi_0    pypi
defusedxml                0.6.0                    pypi_0    pypi
entrypoints               0.3                      pypi_0    pypi
freetype                  2.10.4               hd328e21_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
imageio                   2.3.0                    pypi_0    pypi
importlib-metadata        1.6.0                    pypi_0    pypi
intel-openmp              2021.4.0          haa95532_3556    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
ipykernel                 5.2.1                    pypi_0    pypi
ipython                   7.14.0                   pypi_0    pypi
ipython-genutils          0.2.0                    pypi_0    pypi
jedi                      0.17.0                   pypi_0    pypi
jinja2                    2.11.2                   pypi_0    pypi
jpeg                      9d                   h2bbff1b_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
jsonschema                3.2.0                    pypi_0    pypi
jupyter-client            6.1.3                    pypi_0    pypi
jupyter-core              4.6.3                    pypi_0    pypi
kiwisolver                1.0.1                    pypi_0    pypi
libpng                    1.6.37               h2a8f88b_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
libtiff                   4.2.0                hd0e1b90_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
libuv                     1.40.0               he774522_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
libwebp                   1.2.0                h2bbff1b_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
lz4-c                     1.9.3                h2bbff1b_1    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
markupsafe                1.1.1                    pypi_0    pypi
matplotlib                2.2.2                    pypi_0    pypi
mistune                   0.8.4                    pypi_0    pypi
mkl                       2021.4.0           haa95532_640    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
mkl-service               2.4.0            py37h2bbff1b_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
mkl_fft                   1.3.1            py37h277e83a_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
mkl_random                1.2.2            py37hf11a4ad_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
nbconvert                 5.6.1                    pypi_0    pypi
nbformat                  5.0.6                    pypi_0    pypi
networkx                  2.1                      pypi_0    pypi
ninja                     1.10.2           py37h559b2a2_3    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
notebook                  6.0.3                    pypi_0    pypi
numpy                     1.15.0                   pypi_0    pypi
olefile                   0.46                     py37_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
opencv-python             4.2.0.34                 pypi_0    pypi
openssl                   1.1.1l               h2bbff1b_0    http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
packaging                 20.4                     pypi_0    pypi
pandas                    0.23.4                   pypi_0    pypi
pandocfilters             1.4.2                    pypi_0    pypi
parso                     0.7.0                    pypi_0    pypi
pickleshare               0.7.5                    pypi_0    pypi
pillow                    5.2.0                    pypi_0    pypi
pip                       21.2.4           py37haa95532_0    http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
prometheus-client         0.7.1                    pypi_0    pypi
prompt-toolkit            3.0.5                    pypi_0    pypi
pycparser                 2.18                     pypi_0    pypi
pygit                     0.1                      pypi_0    pypi
pygments                  2.6.1                    pypi_0    pypi
pyparsing                 2.2.0                    pypi_0    pypi
pyrsistent                0.16.0                   pypi_0    pypi
python                    3.7.11               h6244533_0    http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
python-dateutil           2.7.3                    pypi_0    pypi
pytorch                   1.0.1           py3.7_cuda80_cudnn7_1    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch
pytorch-mutex             1.0                         cpu    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch
pytz                      2018.5                   pypi_0    pypi
pywavelets                0.5.2                    pypi_0    pypi
pywin32                   227                      pypi_0    pypi
pywinpty                  0.5.7                    pypi_0    pypi
pyyaml                    5.1                      pypi_0    pypi
pyzmq                     19.0.1                   pypi_0    pypi
scikit-image              0.14.0                   pypi_0    pypi
scikit-learn              0.19.2                   pypi_0    pypi
scipy                     1.1.0                    pypi_0    pypi
send2trash                1.5.0                    pypi_0    pypi
setuptools                58.0.4           py37haa95532_0    http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
six                       1.11.0                   pypi_0    pypi
sqlite                    3.36.0               h2bbff1b_0    http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
terminado                 0.8.3                    pypi_0    pypi
testpath                  0.4.4                    pypi_0    pypi
tk                        8.6.11               h2bbff1b_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
toolz                     0.9.0                    pypi_0    pypi
torchvision               0.2.2                      py_3    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch
tornado                   6.0.4                    pypi_0    pypi
tqdm                      4.24.0                   pypi_0    pypi
traitlets                 4.3.3                    pypi_0    pypi
typing_extensions         3.10.0.2           pyh06a4308_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
vc                        14.2                 h21ff451_1    http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
vs2015_runtime            14.27.29016          h5e58377_2    http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
wcwidth                   0.1.9                    pypi_0    pypi
webencodings              0.5.1                    pypi_0    pypi
wheel                     0.37.0             pyhd3eb1b0_1    http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
wincertstore              0.2              py37haa95532_2    http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
xz                        5.2.5                h62dcd97_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
zipp                      3.1.0                    pypi_0    pypi
zlib                      1.2.11               h62dcd97_4    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
zstd                      1.4.9                h19a0ad4_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main

导出当前依赖环境txt文档

F:\File_Anaconda\project_py37\RealTimeImageAnimation>conda activate F:\File_Anaconda\project_py37\envs

(F:\File_Anaconda\project_py37\envs) F:\File_Anaconda\project_py37\RealTimeImageAnimation>conda list -e > requirements2020.txt


# This file may be used to create an environment using:
# $ conda create --name <env> --file <this file>
# platform: win-64
attrs=19.3.0=pypi_0
backcall=0.1.0=pypi_0
blas=1.0=mkl
bleach=3.1.5=pypi_0
ca-certificates=2021.10.26=haa95532_2
certifi=2021.10.8=py37haa95532_0
cffi=1.11.5=pypi_0
cloudpickle=0.5.3=pypi_0
colorama=0.4.3=pypi_0
cpuonly=2.0=0
cudatoolkit=8.0=4
cycler=0.10.0=pypi_0
dask=0.18.2=pypi_0
decorator=4.3.0=pypi_0
defusedxml=0.6.0=pypi_0
entrypoints=0.3=pypi_0
freetype=2.10.4=hd328e21_0
imageio=2.3.0=pypi_0
importlib-metadata=1.6.0=pypi_0
intel-openmp=2021.4.0=haa95532_3556
ipykernel=5.2.1=pypi_0
ipython=7.14.0=pypi_0
ipython-genutils=0.2.0=pypi_0
jedi=0.17.0=pypi_0
jinja2=2.11.2=pypi_0
jpeg=9d=h2bbff1b_0
jsonschema=3.2.0=pypi_0
jupyter-client=6.1.3=pypi_0
jupyter-core=4.6.3=pypi_0
kiwisolver=1.0.1=pypi_0
libpng=1.6.37=h2a8f88b_0
libtiff=4.2.0=hd0e1b90_0
libuv=1.40.0=he774522_0
libwebp=1.2.0=h2bbff1b_0
lz4-c=1.9.3=h2bbff1b_1
markupsafe=1.1.1=pypi_0
matplotlib=2.2.2=pypi_0
mistune=0.8.4=pypi_0
mkl=2021.4.0=haa95532_640
mkl-service=2.4.0=py37h2bbff1b_0
mkl_fft=1.3.1=py37h277e83a_0
mkl_random=1.2.2=py37hf11a4ad_0
nbconvert=5.6.1=pypi_0
nbformat=5.0.6=pypi_0
networkx=2.1=pypi_0
ninja=1.10.2=py37h559b2a2_3
notebook=6.0.3=pypi_0
numpy=1.15.0=pypi_0
olefile=0.46=py37_0
opencv-python=4.2.0.34=pypi_0
openssl=1.1.1l=h2bbff1b_0
packaging=20.4=pypi_0
pandas=0.23.4=pypi_0
pandocfilters=1.4.2=pypi_0
parso=0.7.0=pypi_0
pickleshare=0.7.5=pypi_0
pillow=5.2.0=pypi_0
pip=21.2.4=py37haa95532_0
prometheus-client=0.7.1=pypi_0
prompt-toolkit=3.0.5=pypi_0
pycparser=2.18=pypi_0
pygit=0.1=pypi_0
pygments=2.6.1=pypi_0
pyparsing=2.2.0=pypi_0
pyrsistent=0.16.0=pypi_0
python=3.7.11=h6244533_0
python-dateutil=2.7.3=pypi_0
pytorch=1.0.1=py3.7_cuda80_cudnn7_1
pytorch-mutex=1.0=cpu
pytz=2018.5=pypi_0
pywavelets=0.5.2=pypi_0
pywin32=227=pypi_0
pywinpty=0.5.7=pypi_0
pyyaml=5.1=pypi_0
pyzmq=19.0.1=pypi_0
scikit-image=0.14.0=pypi_0
scikit-learn=0.19.2=pypi_0
scipy=1.1.0=pypi_0
send2trash=1.5.0=pypi_0
setuptools=58.0.4=py37haa95532_0
six=1.11.0=pypi_0
sqlite=3.36.0=h2bbff1b_0
terminado=0.8.3=pypi_0
testpath=0.4.4=pypi_0
tk=8.6.11=h2bbff1b_0
toolz=0.9.0=pypi_0
torchvision=0.2.2=py_3
tornado=6.0.4=pypi_0
tqdm=4.24.0=pypi_0
traitlets=4.3.3=pypi_0
typing_extensions=3.10.0.2=pyh06a4308_0
vc=14.2=h21ff451_1
vs2015_runtime=14.27.29016=h5e58377_2
wcwidth=0.1.9=pypi_0
webencodings=0.5.1=pypi_0
wheel=0.37.0=pyhd3eb1b0_1
wincertstore=0.2=py37haa95532_2
xz=5.2.5=h62dcd97_0
zipp=3.1.0=pypi_0
zlib=1.2.11=h62dcd97_4
zstd=1.4.9=h19a0ad4_0