Python实现高级电影特效

为了方便,我们全都使用pip安装:

pip install pillow

pip install opencv-python

pip install moviepy

# 安装paddlepaddle

python -m pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple

# 安装paddlehub

pip install -i https://mirror.baidu.com/pypi/simple paddlehub

代码实现

我们先看看导入外汇返佣的一些模块:

import cv2

扫描二维码关注公众号,回复: 11074375 查看本文章

import math

import numpy as np

from PIL import Image

import paddlehub as hub

from moviepy.editor import *

我们按照上面的步骤,一步一步来。

逐帧提取图像

这就需要使用到我们的opencv了,具体代码如下:

def getFrame(video_name, save_path):

"""

传入视频名称,将图像帧保存到save_path下

"""

# 读取视频

video = cv2.VideoCapture(video_name)

# 获取视频帧率

fps = video.get(cv2.CAP_PROP_FPS)

# 获取画面大小

width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))

height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))

size = (width, height)

# 获取帧数

frame_num = str(video.get(7))

name = int(math.pow(10, len(frame_num)))

ret, frame = video.read()

while ret:

cv2.imwrite(save_path + str(name) + '.jpg', frame)

ret, frame = video.read()

name += 1

video.release()

return fps, size

批量抠图

批量抠图需要使用到我们的paddhub模型库,而抠图的实现也只需要几行代码:

def getHumanseg(frames):

"""

对frames路径下所以图片进行抠图

"""

# 加载模型库

humanseg = hub.Module(name='deeplabv3p_xception65_humanseg')

# 遍历路径下文件

files = [frames + i for i in os.listdir(frames)]

# 抠图

humanseg.segmentation(data={'image': files})

我们调用该方法后会在目录下生成humanseg_output目录,抠好的图像就在里面。

合成图像(影分身)

这里需要使用到我们的Pillow模块,该模块中提供了图像粘贴的函数:

def setImageBg(humanseg, bg_im):

"""

将抠好的图和背景图片合并

:param humanseg:

:param bg_im:

:return:

"""

# 读取透明图片

im = Image.open(humanseg)

# 分离色道

r, g, b, a = im.split()

# 在图片右边粘贴一个人物分身

bg_im.paste(im, (bg_im.size[0]//3, 0), mask=a)

# 在图片左边粘贴一个人物分身

bg_im.paste(im, (-bg_im.size[0]//3, 0), mask=a)

# 将图形转换成opencv能正常读取的类型,并返回

return np.array(bg_im.convert('RGB'))[:, :, ::-1]

写入视频

写入视频的操作同样是OpenCV来实现的:

def writeVideo(humanseg_path, frames, fps, size):

"""

传入抠好的人像,和原图像,以及原视频帧率,大小,写入新视频

"""

# 写入视频

fourcc = cv2.VideoWriter_fourcc(*'mp4v')

out = cv2.VideoWriter('green.mp4', fourcc, fps, size)

# 将每一帧设置背景

humanseg = [humanseg_path + i for i in os.listdir(humanseg_path)]

frames = [frames + i for i in os.listdir(frames)]

for i in range(humanseg.__len__()):

# 读取原图像

bg_im = Image.open(frames[i])

# 设置分身

im_array = setImageBg(humanseg[i], bg_im)

# 写入视频

out.write(im_array)

out.release()

混流

我们混流的操作就是先获取音频,然后再混流,而音频我们只需要读取原视频的音频即可:

def getMusic(video_name):

"""

获取指定视频的音频

"""

# 读取视频文件

video = VideoFileClip(video_name)

# 返回音频

return video.audio

其中VideoFileClip是moviepy中的一个视频处理的类。下面我们来添加音乐:

def addMusic(video_name, audio):

"""实现混流,给video_name添加音频"""

# 读取视频

video = VideoFileClip(video_name)

# 设置视频的音频

video = video.set_audio(audio)

# 保存新的视频文件

video.write_videofile(output_video)

output_video是我们自己定义的一个存放文件保存路径的变量,需要注意,该全路径(路径+名称)不能和原视频相同。

实现特效

也就是将整个流程整合到一起:

def changeVideoScene(video_name):

"""

:param video_name: 视频的文件

:param bgname: 背景图片

:return:

"""

# 读取视频中每一帧画面

fps, size = getFrame(video_name, frames)

# 批量抠图

getHumanseg(frames)

# 将画面一帧帧写入视频

writeVideo(humanseg_path, frames, fps, size)

# 混流

addMusic('green.mp4', getMusic(video_name))

在上面有些变量我们还没有定义,我们在main函数中定义一下:

if __name__ == '__main__':

# 当前项目根目录

BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "."))

# 每一帧画面保存的地址

frames = BASE_DIR + '\\frames\\'

# 抠好的图片位置

humanseg_path = BASE_DIR + '\\humanseg_output\\'

# 最终视频的保存路径

output_video = BASE_DIR + '\\result.mp4'

# 创建文件夹

if not os.path.exists(frames):

os.makedirs(frames)

if not os.path.exists(background_path):

os.makedirs(background_path)

# 给视频添加特效

changeVideoScene('jntm.mp4')

猜你喜欢

转载自www.cnblogs.com/benming/p/12760394.html