Create Stunning Video Ghosting Effects with PaddlePaddle’s DeepLabV3p Model
Learn how to generate cinematic ghosting effects in videos by leveraging PaddlePaddle’s PaddleHub deep learning library and the pretrained deeplabv3p_xception65 model for semantic segmentation, with step‑by‑step code, environment setup, and practical testing on classic martial‑arts footage.
Creation Background
Inspired by iconic martial‑arts novels and the striking visual style of modern action films, the author explores how to reproduce the dramatic "ghost" effect seen in fight scenes using deep learning.
The technique is demonstrated on a classic duel from a martial‑arts series, showing how the effect enhances visual storytelling.
Algorithm Implementation Process
Environment : Baidu AIStudio deep‑learning platform, Python 3.7, PaddlePaddle 1.8.4.
Step 1: Install PaddleHub
!pip install paddlehub==1.6.2 -i https://mirror.baidu.com/pypi/simpleStep 2: Import required packages
import numpy as np
import os
import paddlehub as hub
import cv2
from moviepy.editor import VideoFileClip
from tqdm import tqdm
import copyStep 3: Set GPU device (single‑GPU environment) os.environ["CUDA_VISIBLE_DEVICES"] = "0" Step 4: Write inference code that uses the pretrained deeplabv3p_xception65 model to create the ghosting effect
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" # original video
resultname = "test_result.mp4" # output with ghost effect
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, fourcc, 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:]
# store current mask and blurred background for later blending
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()The deeplabv3p_xception65 model performs semantic segmentation to separate human figures from the background. By blending the current frame with several previously processed frames, a translucent “shadow” trail is created, producing the desired ghosting visual effect.
Actual Test Results
The method was applied to a classic duel scene, demonstrating a convincing ghosting overlay that accentuates motion.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
