# AI算法实现武侠小说中的“绝世武功”——动作残影特效！

↑↑↑关注后”

“天池大数据科研平台

## 创作背景

“飞雪连天射白鹿，笑书神侠倚碧鸳”，相信很多90后知道这副对联的含义，这是武侠小说作家金庸先生的作品合集，说实话在所有的作品中我最喜欢的是倚天屠龙记和天龙八部，应该说基本都喜欢，但是这两部中的爱恨情仇最为突出，倚天屠龙记中周芷若和赵敏，张无忌，再到天龙八部中的乔峰。

## 算法实现过程

`!pip install paddlehub==1.6.2 -i https://mirror.baidu.com/pypi/simple`

`import numpy as np //导入数据科学报numpyimport os  import paddlehub as hubimport cv2from 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"   //test.mp4为原始的输入视频resultname = "test_result.mp4"  //test_result.mp4为处理后的残影视频shadowcount = 9cap = 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()`