本文主要是介绍paddlehub实现人物抠图换背景,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 前言
- 简介
- paddlehub安装
- 功能实现
- 引入库
- 用到的hub库
- 每帧的图像处理
- 结果输出
- 总结
前言
看完文章您将学会:
paddlehub的使用方法
如何用cv2加载图片并保存
如何用cv2逐帧加载视频以及将图片逐帧保存成视频
如何将png格式的图片放入另一张图片
本文涉及paddlehub的人脸检测、图像分割和图像生成三个部分
详细的文档请访问: https://www.paddlepaddle.org.cn/hub
简介
本项目通过人脸检测将人脸遮挡实现打马赛的功能,同时通过风格转换和抠图将人物放置在新的背景下实现换背景的效果。
处理后的图片效果图:
paddlehub安装
pip install paddlehub --upgrade -i https://mirror.baidu.com/pypi/simple
paddlehub 中的模型对版本有要求
请安装最新版本的paddlehub
或者根据需要指定安装版本:
hub install name==version
功能实现
引入库
代码如下:
import paddle
import paddlehub as hub
import numpy as np
from PIL import Image, ImageFilter, ImageDraw
import cv2, osimport matplotlib.pyplot as plt
%matplotlib inlineprint(paddle.__version__)
用到的hub库
代码如下:
#用于人脸检测
face_detection = hub.Module(name="ultra_light_fast_generic_face_detector_1mb_640")
#用于风格转换
stylepro_artistic = hub.Module(name="stylepro_artistic")
#用于扣出人物
humanseg = hub.Module(name="deeplabv3p_xception65_humanseg")
可以查看一下数据的输出格式,本文中默认只考虑一个物体的情况:
# 查看一下使用方法及输出格式, 这里默认一张图片中只有一个目标物
face_detection_res = face_detection.face_detection(images=[cv2.imread('./work/me1.jpg')],paths=None,batch_size=1,use_gpu=False,visualization=False,output_dir=None,confs_threshold=0.5)
# [0]['data']
result = stylepro_artistic.style_transfer( images=[{'content': cv2.imread('./dog.png'),'styles': [cv2.imread('./style/style1.jpg'), cv2.imread('./style/style2.jpg'),cv2.imread('./style/style3.jpg'),cv2.imread('./style/style4.jpg'),cv2.imread('./style/style5.jpg')]}],visualization=True)
# [0]['data']
seg_res = humanseg.segment(images=[cv2.imread('./work/me1.jpg')],paths=None,batch_size=1,use_gpu=False,visualization=True,output_dir='humanseg_output')
# [0]['data']
每帧的图像处理
通过修改precess_img可以实现不同的处理效果。
def process_img(frame_bgr, index=0):'''输入一张图片shape=[H, W, C] 通道为bgr格式'''ratio = 1.4 y_offset = 30num_fps = 150t = 1h, w = frame_bgr.shape[:2]size = frame_bgr.shape[:2]face_detector = MyFaceDetector()face_detection_res = face_detector.face_detection(images=[frame_bgr], use_gpu=False, visualization=False)for box_dict in face_detection_res[0]['data']:box_xyxy = ( int(box_dict['left']), int(box_dict['top']), int(box_dict['right']), int(box_dict['bottom']) )box_xywh = int((box_xyxy[0]+box_xyxy[2])/2), int((box_xyxy[1]+box_xyxy[3])/2), box_xyxy[2]-box_xyxy[0], box_xyxy[3]-box_xyxy[1]correct_box_xywh = box_xywh[0], box_xywh[1]-y_offset, int(box_xywh[2]*ratio), int(box_xywh[3]*ratio)#真实框xyxybox = int(correct_box_xywh[0]-correct_box_xywh[2]/2) if int(correct_box_xywh[0]-correct_box_xywh[2]/2) >= 0 else 0, \int(correct_box_xywh[1]-correct_box_xywh[3]/2) if int(correct_box_xywh[1]-correct_box_xywh[3]/2) >= 0 else 0, \int(correct_box_xywh[0]+correct_box_xywh[2]/2) if int(correct_box_xywh[0]+correct_box_xywh[2]/2) <= size[1] else size[1], \int(correct_box_xywh[1]+correct_box_xywh[3]/2) if int(correct_box_xywh[1]+correct_box_xywh[3]/2) <= size[0] else size[0]dog = cv2.imread('./dog.png', -1) # -1 读取alpha通道dog = cv2.resize(dog, ( box[2]-box[0] if (box[2]-box[0])>0 else 1 , box[3]-box[1] if (box[3]-box[1])>0 else 1) )dog_alpha = dog[:,:,3] != 0dog_alpha = np.repeat(dog_alpha[:,:,np.newaxis], axis=2, repeats=3)human_alpha = humanseg.segmentation(images=[frame_bgr],paths=None,batch_size=1,use_gpu=False,visualization=False,output_dir='humanseg_output')[0]['data']human_alpha = np.repeat(human_alpha[:,:,np.newaxis], axis=2, repeats=3) != 0if index <= num_fps:bg = cv2.imread('./bg1.png')bg = cv2.resize(bg, (w, h))elif index > num_fps and index < (num_fps + t*30):beta = (index-num_fps)/30bg = beta * cv2.imread('./bg2.jpg')/255. + (1 - beta) * cv2.imread('./bg1.jpg')/255.bg = bg * 255bg = bg.astype('uint8')bg = cv2.resize(bg, (w, h))else:bg = cv2.imread('./bg2.png')bg = cv2.resize(bg, (w, h))# 加了这两项后运算时间会大大延长# frame_bgr = stylepro_artistic.style_transfer(images=[{'content': frame_bgr,# 'styles': [cv2.imread('./style/style1.jpg')]# }], use_gpu=False,# visualization=False)[0]['data']# dog = stylepro_artistic.style_transfer(images=[{'content': dog[:,:,:3],# 'styles': [cv2.imread('./style/style1.jpg')]# }], use_gpu=False,# visualization=False)[0]['data']bg[human_alpha] = frame_bgr[human_alpha] #根据alpah矩阵赋值bg[box[1]:box[1]+dog.shape[0], box[0]:box[0]+dog.shape[1], :][dog_alpha] = dog[:,:,:3][dog_alpha]# dog = cv2.imread('./dog.png', -1)# point_boxwh = (point[0], point[1] - (box[3]-box[1])//2 , box[2]-box[0], box[3]-box[1])
#
# point_box = point_boxwh[0]-point_boxwh[2]//2 if (point_boxwh[0]-point_boxwh[2]//2) >= 0 else 0, \
# point_boxwh[1]-point_boxwh[3]//2 if (point_boxwh[1]-point_boxwh[3]//2) >= 0 else 0, \
# point_boxwh[0]+point_boxwh[2]//2 if (point_boxwh[0]+point_boxwh[2]//2) <= size[1] else size[1], \
# point_boxwh[1]+point_boxwh[3]//2 if (point_boxwh[1]+point_boxwh[3]//2) <= size[0] else size[0]
# dog = cv2.resize(dog, ( point_box[2]-point_box[0] if (point_box[2]-point_box[0])>0 else 1, point_box[3]-point_box[1] if (point_box[3]-point_box[1])>0 else 1) )
# alpha_channel = dog[:,:,3] != 0
# alpha_channel = np.repeat(alpha_channel[:,:,np.newaxis], axis=2, repeats=3)
# # assert point_box[1]:point_box[1]+dog.shape[0], point_box[0]: point_box[0]+dog.shape[1]
# frame_bgr[point_box[1]:point_box[1]+dog.shape[0], point_box[0]: point_box[0]+dog.shape[1],:][alpha_channel] = dog[:,:,:3][alpha_channel]return bgdef CutVideo2Image(video_path, img_path):#将视频输出为图像#video_path为输入视频文件路径#img_path为输出图像文件夹路径cap = cv2.VideoCapture(video_path)index = 0while(True):ret,frame = cap.read() if ret:cv2.imwrite(img_path+'/%d.jpg'%index, frame)index += 1else:breakcap.release()class MyFaceDetector(object):"""自定义人脸检测器基于PaddleHub人脸检测模型ultra_light_fast_generic_face_detector_1mb_640,加强稳定人脸检测框"""def __init__(self):self.module = hub.Module(name="ultra_light_fast_generic_face_detector_1mb_640")self.alpha = 0.75self.start_flag =1def face_detection(self,images, use_gpu=False, visualization=False):# 使用GPU运行,use_gpu=True,并且在运行整个教程代码之前设置CUDA_VISIBLE_DEVICES环境变量result = self.module.face_detection(images=images, use_gpu=use_gpu, visualization=visualization)if not result[0]['data']:return resultface = result[0]['data'][0]if self.start_flag == 1:self.left_s = result[0]['data'][0]['left']self.right_s = result[0]['data'][0]['right']self.top_s = result[0]['data'][0]['top']self.bottom_s = result[0]['data'][0]['bottom']self.start_flag=0else:# 加权平均上一帧和当前帧人脸检测框位置,以稳定人脸检测框self.left_s = self.alpha * self.left_s + (1-self.alpha) * face['left'] self.right_s = self.alpha * self.right_s + (1-self.alpha) * face['right'] self.top_s = self.alpha * self.top_s + (1-self.alpha) * face['top']self.bottom_s = self.alpha * self.bottom_s + (1-self.alpha) * face['bottom'] result[0]['data'][0]['left'] = self.left_sresult[0]['data'][0]['right'] = self.right_sresult[0]['data'][0]['top'] = self.top_sresult[0]['data'][0]['bottom'] = self.bottom_sreturn result
结果输出
def generate_image():# 打开摄像头# capture = cv2.VideoCapture(0) capture = cv2.VideoCapture('./test.mp4')fps = capture.get(cv2.CAP_PROP_FPS)size = (int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)),int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)))# 将预测结果写成视频video_writer = cv2.VideoWriter('result.mp4', cv2.VideoWriter_fourcc(*'mp4v'), fps, size)print(f'fps={fps}, size={size}')index = 0while True:# frame_rgb即视频的一帧数据ret, frame_bgr = capture.read() #从capture中读取帧# 按q键即可退出# cv2.imwrite('./work/me1.jpg', frame_bgr)# breakif cv2.waitKey(1) & 0xFF == ord('q'):breakif frame_bgr is None:break# cv2.imwrite('./work'+'/%d.jpg'%index, frame_bgr)index += 1#图像处理frame_bgr = process_img(frame_bgr, index)video_writer.write(frame_bgr) #写入帧# frame_rgb = cv2.cvtColor(frame_bgr, cv2.COLOR_RGB2BGR) # cv2.COLOR_RGB2BGR就是把0, 2 通道互换# yield frame_rgbcapture.release()video_writer.release()cv2.destroyAllWindows()generate_image()
总结
视频中人物的抠图效果需要提升,可以通过在cv2中进一步处理提升画面效果。
另外声音需要后期合成后单独加入,后续我会想办法把声音加上。
总的来看,paddlehub将一些主流的模型集成起来做成相应的接口,当需要时直接调用即可,使用起来也非常方便。这极大的降低了网络的使用门槛,只需要少量的代码即可实现复杂的功能。
这篇关于paddlehub实现人物抠图换背景的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!