根据开发者社区自己也来一个
https://mp.weixin.qq.com/s/-ZFwcdwAvJb2YSkpJS7v-A
点击 https://marketplace.huaweicloud.com/markets/aihub/datasets/detail/?content_id=00bc20c3-2a00-4231-bdfd-dfa3eb62a46d 下载胡大大提供的数据集
一,先创建一个桶(用于储存数据,也可以理解为云存储)
桶;名全局唯一(意思可以理解为桶名只能有一个,不能有相同的)
创建完成
下载
耐心等待几分钟即可
登陆ModelArts控制台https://www.huaweicloud.com/product/modelarts.html -> 开发环境 -> Notebook -> 创建
名称:任意设置参数:python3-公共资源池-GPU-云硬盘EVS
创建Notebook
可以选择免费的版本,但是免费的要排队哦~
点击提交(圈起来的是重点·~)由于我买过套餐,所以显示的是0元
创建步骤我就直接省略了,直接启动以及创建好的,初次创建只要选择好GPU一般都不会出现什么问题,如果选择cpu可能会出现内存耗尽的问题,所以建议选择GPU~
测试一下是否能运行
print("huawei cloud")
#下载代码,华为云代码托管平台再拉取
!git clone https://codehub.devcloud.cn-north-4.huaweicloud.com/ai-pome-free00001/first-order-model.git
# 此处牛刀小试--用 Moxing 下载文件
import moxing as mox
# 此处需要替换您的 OBS 地址(根据实际自己的桶地址)
mox.file.copy_parallel('obs://modelarts-lab/first-order-motion-model/first-order-motion-model-20210226T075740Z-001.zip' , 'first-order-motion-model.zip')
mox.file.copy_parallel('obs://modelarts-lab/first-order-motion-model/02.mp4' , '02.mp4')
# 解压
!unzip first-order-motion-model.zip
# 模版视频
!mv 02.mp4 first-order-motion-model/
切换到first-order-model
目录,然后将 source_image_path
中的路径替换为”您的脸”所在的路径,脸的照片可以直接通过 Notebook 的文件上传功能上传。
当然您还可以将默认的“蚂蚁呀嘿”视频替换为您自定义的视频,格式为 mp4。一路执行可以查看到合成前的预览。
cd first-order-model
import imageio
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from skimage.transform import resize
from IPython.display import HTML
import warnings
warnings.filterwarnings("ignore")
# 此处替换为您的图片路径,图片最好为 256*256,这里默认为普京大帝
source_image_path = '/home/ma-user/work/first-order-motion-model/02.png'
source_image = imageio.imread(source_image_path)
# 此处可替换为您的视频路径,这里默认为“蚂蚁牙黑”
reader_path = '/home/ma-user/work/first-order-motion-model/02.mp4'
reader = imageio.get_reader(reader_path)
# 调整图片和视频大小为 256x256
source_image = resize(source_image, (256, 256))[..., :3]
fps = reader.get_meta_data()['fps']
driving_video = []
try:
for im in reader:
driving_video.append(im)
except RuntimeError:
pass
reader.close()
driving_video = [resize(frame, (256, 256))[..., :3] for frame in driving_video]
def display(source, driving, generated=None):
fig = plt.figure(figsize=(8 + 4 * (generated is not None), 6))
ims = []
for i in range(len(driving)):
cols = [source]
cols.append(driving[i])
if generated is not None:
cols.append(generated[i])
im = plt.imshow(np.concatenate(cols, axis=1), animated=True)
plt.axis('off')
ims.append([im])
ani = animation.ArtistAnimation(fig, ims, interval=50, repeat_delay=1000)
plt.close()
return ani
HTML(display(source_image, driving_video).to_html5_video())
from demo import load_checkpoints
generator, kp_detector = load_checkpoints(config_path='config/vox-256.yaml',
checkpoint_path='/home/ma-user/work/first-order-motion-model/vox-cpk.pth.tar')
from demo import make_animation
from skimage import img_as_ubyte
predictions = make_animation(source_image, driving_video, generator, kp_detector, relative=True)
# 保存结果视频
imageio.mimsave('../generated.mp4', [img_as_ubyte(frame) for frame in predictions], fps=fps)
# 在 Notebook 根目录能找,/home/ma-user/work/
HTML(display(source_image, driving_video, predictions).to_html5_video())
# 安装视频剪辑神器 moviepy
!pip install moviepy
# 为生成的视频加上源视频声音
from moviepy.editor import *
videoclip_1 = VideoFileClip(reader_path)
videoclip_2 = VideoFileClip("../generated.mp4")
audio_1 = videoclip_1.audio
videoclip_3 = videoclip_2.set_audio(audio_1)
videoclip_3.write_videofile("../result.mp4", audio_codec="aac")
# 还可以给视频加水印
video = VideoFileClip("../result.mp4")
# 水印图片请自行上传
logo = (ImageClip("/home/ma-user/work/first-order-motion-model/water.png")
.set_duration(video.duration) # 水印持续时间
.resize(height=50) # 水印的高度,会等比缩放
.margin(right=0, top=0, opacity=1) # 水印边距和透明度
.set_pos(("left","top"))) # 水印的位置
final = CompositeVideoClip([video, logo])
final.write_videofile("../result_water.mp4", audio_codec="aac")
final_reader = imageio.get_reader("../result_water.mp4")
fps = final_reader.get_meta_data()['fps']
result_water_video = []
try:
for im in final_reader:
result_water_video.append(im)
except RuntimeError:
pass
reader.close()
result_water_video = [resize(frame, (256, 256))[..., :3] for frame in result_water_video]
HTML(display(source_image, driving_video, result_water_video).to_html5_video())
我自己调用了它自己里面的一张图片来测试
#自带修改地址
/home/ma-user/work/first-order-motion-model/doll-07.png
评论(0)