利用百度 PaddlePaddle 平台的 PaddleHub 工具实现一键抠图,结合 opencv 等工具进行视频合成创作。
本视频为2.0版本,相比于第一版,自认为还是有所提升的。。。。。
1.0版本链接如下:
AI人像抠图及视频合成:让你体验复仇者联盟的终局之战
全套代码:
# 首先安装所需的包
# pip install paddlehub==1.6.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
# pip install moviepy -i https://pypi.tuna.tsinghua.edu.cn/simple
# 这里指定从清华源下载,速度快
import cv2
from PIL import Image
import numpy as np
import os
import paddlehub as hub
from moviepy.editor import *
# 从视频提取图片
def video2img(video_path, out_path):
cap = cv2.VideoCapture(video_path)
i=1
while True:
ret, frame = cap.read()
if frame is None:
break
else:
cv2.imwrite(out_path + str(i) + ".jpg", frame)
i+=1
return
# 抠图
def img2seg(img_path, out_path):
# load model
module = hub.Module(name="deeplabv3p_xception65_humanseg")
# config
test_img_path = [os.path.join(img_path, f_name) for f_name in os.listdir(img_path)]
input_dict = {
"image": test_img_path}
results = module.segmentation(data=input_dict, output_dir=out_path)
# 合成图片
def blend_images(fore_image, base_image, out_path, img_num