Too magical, use Python to realize the popular "Ant Hey" video effects on the whole network!

Preface

The text and pictures in this article are from the Internet and are for learning and communication purposes only, and do not have any commercial use. If you have any questions, please contact us for processing.

PS: If you need Python learning materials, you can click on the link below to get it by yourself

Python free learning materials, codes and exchange answers click to join


Recently, I saw a lot of magical videos of "Ant Hey" on Douyin, and bigwigs from all walks of life joined the battle.

I just saw Baidu's open source project, based on PaddleGAN to realize the migration of expressions, so come and play!

Let's take a look at the results first, the major programming founders sing "Ats, hey"!

 

 

So how do you need to achieve it, let's teach it by hand.

Project address:
https://aistudio.baidu.com/aistudio/projectdetail/1586056

At present, the project is suitable for operation on Windows and Linux, and Mac will make mistakes when calling ffmpeg.
I don't know if it is related to mac not supporting gpu to install paddlepaddle.
First download the source code of PaddleGAN from gitee.

# 从gitee上克隆PaddleGAN代码
git clone https://gitee.com/paddlepaddle/

If you don't have git installed, you can get the file at the end of the article.

After downloading and decompressing, place the PaddleGAN folder in the PyCharm project.

 

 

In this way, you can perform related installation operations in the virtual environment of the PyCharm project.

# 终端打开文件夹
cd PaddleGAN

# 安装相关依赖
pip install -r requirements.txt -i https://mirror.baidu.com/pypi/simple
pip install imageio-ffmpeg -i https://mirror.baidu.com/pypi/simple

The terminal opens the folder and installs the required dependencies.
After all, it is Baidu's open source project, so Baidu's pip source is used, the speed is really fast.
Next, create an output folder, and the generated video will be saved here.

# 打开文件夹
cd applications/

# 新建文件夹
mkdir output

Finally, you need to install Baidu's paddlepaddle and paddlehub.

Paddlepaddle is the foundation, and paddlehub is used to detect faces.

Detect all faces in the "input picture", and then use PaddleGAN to perform expression migration on each face, and finally generate a video.

# 安装库
pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple
pip install paddlehub -i https://mirror.baidu.com/pypi/simple

Now you can run the following commands in the terminal.

export PYTHONPATH=$PYTHONPATH:../PycharmProjects/mayiyahei/PaddleGAN && python -u tools/first-order-demo.py  --driving_video ../PycharmProjects/mayiyahei/MaYiYaHei.MP4  --source_image ../PycharmProjects/mayiyahei/input.jpeg --relative --adapt_scale

 

Among them, "../" should be modified to become your own path.

The command line parameters are described as follows.

driving_video: 驱动视频,视频中人物的表情动作作为待迁移的对象。

source_image: 原始图片,视频中人物的表情动作将迁移到该原始图片中的人物上 。

relative: 指示程序中使用视频和图片中人物关键点的相对坐标还是绝对坐标,建议使用相对坐标,若使用绝对坐标,会导致迁移后人物扭曲变形。

adapt_scale: 根据关键点凸包自适应运动尺度。

Here, you can use the pictures you designed to generate the video you want.

For example, my original picture input.jpeg is a collection of programming founders, as shown below.

 

 

For other video effects, you need to change the driving video, that is, modify driving_video.

In addition, the generated video is without sound, so you need to use FFmpeg to merge the video and audio.

# 视频和音频合并
ffmpeg -i MaYiYaHei.mp4 -i MYYH.mp3 -vcodec copy -acodec copy result.mp4

If you want to generate a GIF, share it with others, such as tricky your good friends.

You can also use FFmpeg to generate GIF.

# 生成GIF
ffmpeg -ss 0 -t 8 -i result.mp4 -s 600*400 -r 15 result.gif

Need to set the video capture time and GIF image size.

to sum up

It should be noted here that when using PaddleGAN to perform movement expression migration on human faces, it takes a lot of time during this period, just wait slowly.
Interested friends can also give it a try.
Reply to the official account [210306] to get all relevant documents

 

Guess you like

Origin blog.csdn.net/pythonxuexi123/article/details/114440571