该项目基于PaddleHub的deeplabv3p_xception65_humanseg模型实现人像分割,可将人物从原始采访视频中抠出并贴合到背景视频。流程为:安装环境后,对背景和人像视频切片,平移人像,用模型抠像,合并抠像与背景图,最终合成带音乐的视频。
☞☞☞AI 智能聊天, 问答助手, AI 智能搜索, 免费无限量使用 DeepSeek R1 模型☜☜☜

!pip install -U paddlehub -i https://pypi.tuna.tsinghua.edu.cn/simple
import numpy as npimport matplotlib.image as mpimgimport matplotlib.pyplot as pltimport osfrom PIL import Imageimport paddlehub as hubimport cv2from moviepy.editor import VideoFileClipimport shutil
os.environ["CUDA_VISIBLE_DEVICES"]="0"#GPU模式
#背景视频转jpgdef video_to_pic():
if os.path.exists('./background_pic'):
shutil.rmtree('./background_pic')#删除文件夹以及文件
os.mkdir('./background_pic')#创建文件夹
index = 0
cap = cv2.VideoCapture('./background.mp4')
ret,frame = cap.read() while ret:
cv2.imwrite('./background_pic/%d.jpg'%index, frame)
index += 1
ret,frame = cap.read()
cap.release() print('Video cut finish, all %d frame' % index)
video_to_pic()Video cut finish, all 434 frame
#人像视频转jpgdef video_to_pic():
if os.path.exists('./video_pic'):
shutil.rmtree('./video_pic')#删除文件夹以及文件
os.mkdir('./video_pic')#创建文件夹
index = 0
cap = cv2.VideoCapture('./xxm.mp4')
ret,frame = cap.read() while ret:
cv2.imwrite('./video_pic/%d.jpg'%index, frame)
index += 1
ret,frame = cap.read()
cap.release() print('Video cut finish, all %d frame' % index)
video_to_pic()Video cut finish, all 319 frame
#图片平移def tl(img_path):
'''x轴平移输出两张jpg图片,dest1,dest2'''
img = cv2.imread(img_path)
rows,cols, channel = img.shape# 平移矩阵M:[[1,0,x],[0,1,y]]
M1 = np.float32([[1,0,400],[0,1,0]]) #M2 = np.float32([[1,0,-400],[0,1,0]])
dest1 = cv2.warpAffine(img,M1,(cols,rows), borderValue=(255, 255, 255))#仿射平移
#dest2 = cv2.warpAffine(img,M2,(cols,rows), borderValue=(255, 255, 255))
cv2.imwrite('./dest1'+'/'+file,dest1)#保存图片
# cv2.imwrite('./dest2'+'/'+file,dest2)
# print('保存平移图片成功')#保存平移后的图片(背景)#filename_list = os.listdir('background_pic')#if os.path.exists('dest2'):# shutil.rmtree('dest2')#os.mkdir('dest2')#for file in filename_list:# tl('./background_pic'+'/'+file)#print("文件已创建")#保存平移后的图片(人像)filename_list = os.listdir('video_pic')if os.path.exists('dest1'):
shutil.rmtree('dest1')
os.mkdir('dest1')for file in filename_list:
tl('./video_pic'+'/'+file)print("文件已创建")文件已创建
#扣出平移后图片中的人像def seg_pic(path,savepath):
i = 0
module= hub.Module(name="deeplabv3p_xception65_humanseg")
pic_path = path if os.path.exists(savepath):
shutil.rmtree(savepath)
os.mkdir(savepath)
filename_list = os.listdir(pic_path)
filename_list.sort(key=lambda x: int(x[:-4]))#图片排序
for fname in filename_list:
result = module.segmentation(images=[cv2.imread(pic_path+'/'+fname)], visualization=True, output_dir=savepath,use_gpu=True) #print('已处理处理',pic_path,'图片:', i)
i+=1
filename_list = os.listdir(savepath)
filename_list.sort()
i=0
for file in filename_list:
os.rename(savepath+'/'+file,savepath+'/'+str(i)+'.jpg')#(修改前目录名,修改后名)
i+=1
print('抠图完成')#左边原图路径,右边抠图输出路径seg_pic('dest1','res_dest1')#seg_pic('dest2','res_dest2')Download https://bj.bcebos.com/paddlehub/paddlehub_dev/deeplabv3p_xception65_humanseg.tar.gz [##################################################] 100.00% Decompress /home/aistudio/.paddlehub/tmp/tmpg981rhya/deeplabv3p_xception65_humanseg.tar.gz [##################################################] 100.00%
[2021-09-27 09:46:31,837] [ INFO] - Successfully installed deeplabv3p_xception65_humanseg-1.1.2 [2021-09-27 09:46:31,974] [ WARNING] - The _initialize method in HubModule will soon be deprecated, you can use the __init__() to handle the initialization of the object
抠图完成
#将扣出的人像与原图合并def blend_images(fore_path, base_path ,savepath):
"""
将抠出的人物图像换背景
fore_image: 前景图片,抠出的人物图片
base_image: 背景图片
"""
filename_list = os.listdir(fore_path) for fname in filename_list:
fore_image = fore_path + '/' + fname
base_image = base_path + '/' + fname # 读入图片
base_image = Image.open(base_image).convert('RGB')
fore_image = Image.open(fore_image).resize(base_image.size) # 图片加权合成
scope_map = np.array(fore_image)[:,:,-1] / 255
scope_map = scope_map[:,:,np.newaxis]
scope_map = np.repeat(scope_map, repeats=3, axis=2)
res_image = np.multiply(scope_map, np.array(fore_image)[:,:,:3]) + np.multiply((1-scope_map), np.array(base_image))
#保存图片
res_image = Image.fromarray(np.uint8(res_image))
res_image.save(savepath + '/' + fname)
savepath = 'result_picture'if os.path.exists(savepath):
shutil.rmtree(savepath)
os.mkdir(savepath)
blend_images('res_dest1', 'background_pic', savepath)#删除多余文件以免混淆shutil.rmtree('dest1')#shutil.rmtree('dest2')shutil.rmtree('res_dest1')#shutil.rmtree('res_dest2')print("图片成功合成")图片成功合成
#图片变视频加配乐def merge_to_mp4():
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video= cv2.VideoCapture('./background.mp4')
fps = video.get(cv2.CAP_PROP_FPS)
img = Image.open('./result_picture/1.jpg')#需要整合成视频的图片路径
out = cv2.VideoWriter('result.mp4', fourcc, fps, (img.size[0], img.size[1]))
files = os.listdir('./result_picture')
files.sort(key=lambda x: int(x[:-4])) for i in files:
img = cv2.imread('./result_picture/'+i)
out.write(img)#保存帧
out.release() print('ok!')def add_audio():
video_origin = VideoFileClip('xxm.mp4')
audio=video_origin.audio
video_result = VideoFileClip('./result.mp4')
video_result = video_result.set_audio(audio)
video_result.write_videofile('成品.mp4')
print('ok!')
merge_to_mp4()
add_audio()ok! Moviepy - Building video 成品.mp4. MoviePy - Writing audio in 成品TEMP_MPY_wvf_snd.mp3
<br/>
MoviePy - Done. Moviepy - Writing video 成品.mp4
<br/>
Moviepy - Done ! Moviepy - video ready 成品.mp4 ok!
以上就是数字文创:冰冰老婆带你去旅游---基于paddlehub的人像分割与视频合成的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号