项目链接:https://aistudio.baidu.com/aistudio/projectdetail/1334806
视频链接:https://www.bilibili.com/video/bv12A411s7aM
当我们形容快的时候我们会怎么说
快出残影!!!
当然残影也并不一定是快的时候才有,印象中郭靖学降龙十八掌的时候也有(咳咳,不小心暴露了什么...)
总之......残影就是很厉害吼,今天我们就来实现残影特效吧!
原理很简单,直接将前几帧的人像给保存起来,然后拼接到当前帧就好了。
为了更美观一点,可以将前几帧的人像给“透明化”一些,效果会更好哦
现在就来实现以下吧(你可以现在下方看到效果)
使用Paddlehub完成残影特效
!pip install paddlehub==1.6.2 -i https://mirror.baidu.com/pypi/simple
import numpy as np
import os
import paddlehub as hub
import cv2
from moviepy.editor import VideoFileClip
from tqdm import tqdm
import copy
os.environ["CUDA_VISIBLE_DEVICES"]="0"
def do_seg(module, frame):
result = module.segmentation(images=[frame],use_gpu=True)
return result[0]['data']
module = hub.Module(name="deeplabv3p_xception65_humanseg")
originname = "test.mp4"
resultname = "test_ngresult.avi"
shadowcount = 9
cap = cv2.VideoCapture(originname)
fps = int(cap.get(cv2.CAP_PROP_FPS))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
framecount = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(resultname, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, (width, height))
tmpres = []
for index in tqdm(range(framecount)):
ret, frame = cap.read()
if not ret:
break
seg_mask = np.around(do_seg(module,frame) / 255)
seg_mask3 = np.repeat(seg_mask[:,:,np.newaxis], 3, axis=2)
background = copy.deepcopy(frame)
stbackground = copy.deepcopy(frame)
if len(tmpres) > shadowcount:
tmpres = tmpres[1:]
# tmpres.append([copy.deepcopy(seg_mask3), copy.deepcopy(cv2.GaussianBlur(seg_mask3 * background,(9,9),0))])
tmpres.append([copy.deepcopy(seg_mask3), copy.deepcopy(seg_mask3 * background)])
thuman = copy.deepcopy(seg_mask3 * background)
if index > len(tmpres):
for fi, [t_mask3, t_human] in enumerate(tmpres):
background = t_human * (fi + 1) / len(tmpres) + t_mask3 * (len(tmpres) - 1 - fi) / len(tmpres) * stbackground + (1 - t_mask3) * background
result = background.astype(np.uint8)
out.write(result)
cap.release()
out.release()
加上声音的部分我就不写了,不是本文主要内容,还要改输出格式,懒......
要改的地方是设定VideoWriter输出的格式,然后用一般的moviepy的VideoFileClip就行
另外提供了几个小视频以供大家测试和扩展思路
不仅如此,还提供了两个脚本大家可以在本地自己试一下,delay_camera.py是使用摄像头的,懂得都懂~
代码非常简单,也就花了......主要是想优化结果浪费了点时间
这里是非常缺钱的九尾妖熊:-),我们下次见/狗头
项目github:https://github.com/ninetailskim/PaddleEGT
我在AI Studio上获得黄金等级,点亮5个徽章,来互关呀~ https://aistudio.baidu.com/aistudio/personalcenter/thirdview/311006