## 创作背景

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

## 算法实现过程

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

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

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)):
if not ret:
break
background = copy.deepcopy(frame)
stbackground = copy.deepcopy(frame)
tmpres = tmpres[1:]
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()```