该内容介绍了基于PaddleHub人脸关键点检测的脸颊物品粘贴项目。先说明相关参数,接着阐述实现步骤:安装并导入PaddleHub及PaddleGAN,定义物品粘贴函数,进行图像动漫化处理,最后根据单侧或双侧需求实现物品粘贴并展示。还提及报错原因及解决方案。

基于PaddleHub人脸关键的检测的脸颊物品粘贴
效果展示

声明:以上均为展示,请不要上升到国家/球队/个人层面
数据写入
In [5]genre = 2 # 0:左边单侧 1:右边单侧 2:双侧flagimg = './agt.webp' # 贴图地址faceimg = './p2c_photo.webp' # 人物地址toflagimg = './bx.webp' # 另外一侧贴图地址(右侧)登录后复制
1.0、PaddleHub及PaddleGAN导入及函数定义
In [ ]!pip install -U paddlehub!hub install face_landmark_localization==1.0.2# 安装PaddleGAN的pip包,即可使用api预测方式!pip install --upgrade ppgan!pip install dlib==19.22.0 -i https://pypi.douban.com/simple登录后复制In [ ]
import paddlehub as hubimport cv2import osface_landmark = hub.Module(name="face_landmark_localization")result = face_landmark.keypoint_detection(images=[cv2.imread(faceimg)])登录后复制In [9]
import numpy as npfrom PIL import Imageimport cv2import numpy as npfrom math import sqrtdef sjb_hand(result1, flagimg, faceimg, select): result1 = result[0]['data'][0] pts = [] def around(select): order = [[2, 30, 3, 31], [30, 31, 16, 15]] order = order[select] for o in order: tx = int(result1[o-1][0]) ty = int(result1[o-1][1]) pts.append([tx, ty]) x = [] y = [] for pt in pts: x.append(pt[0]) y.append(pt[1]) # x = tuple(x) # y = tuple(y) x1 = min(x) y1 = min(y) x2 = max(x) y2 = max(y) # print(x1, x2, y1, y2) return x1, x2, y1, y2 x1, x2, y1, y2 = around(select) img = Image.open(flagimg) # print(((x2 - x1) // 2), int((y2 - y1) * 1.2)) reim=img.resize((((x2 - x1) // 2), int((y2 - y1) * 1.2))) # 宽*高 reim.save('newflagimg.webp') im = cv2.imread(faceimg) obj = cv2.imread('newflagimg.webp') mask = 255 * np.ones(obj.shape, obj.dtype) # print(im.shape, obj.shape) center = (int(x1+(x2-x1)//2), y1+(y2-y1)//2) mixed_clone = cv2.seamlessClone(obj, im, mask, center, cv2. NORMAL_CLONE) cv2.imwrite("inputimg.webp", mixed_clone)登录后复制2.0 图像动漫化处理
In [10]#生成动画头像from ppgan.apps import Photo2CartoonPredictor%cd /home/aistudiop2c = Photo2CartoonPredictor(output_path='/home/aistudio/result/')p2c.run(faceimg)登录后复制
3.0 物品粘贴实现并展示
In [11]if genre == 0: sjb_hand(result, flagimg, './result/p2c_photo.webp', 0) os.rename("./inputimg.webp", './newphoto.webp') sjb_hand(result, flagimg, './result/p2c_cartoon.webp', 0) os.rename("./inputimg.webp", './newcartoon.webp')elif genre == 1: sjb_hand(result, flagimg, './result/p2c_photo.webp', 1) os.rename("./inputimg.webp", './newphoto.webp') sjb_hand(result, flagimg, './result/p2c_cartoon.webp', 1) os.rename("./inputimg.webp", './newcartoon.webp')elif genre == 2: tofaceimg = "inputimg.webp" sjb_hand(result, flagimg, './result/p2c_photo.webp', 0) sjb_hand(result, toflagimg, tofaceimg, 1) os.rename("./inputimg.webp", './newphoto.webp') sjb_hand(result, flagimg, './result/p2c_cartoon.webp', 0) sjb_hand(result, toflagimg, tofaceimg, 1) os.rename("./inputimg.webp", './newcartoon.webp')from PIL import ImageImage.open('newcartoon.webp')登录后复制登录后复制In [12]
from PIL import ImageImage.open('newphoto.webp')登录后复制登录后复制
项目报错原因预测及解决方案
1、 dlib安装失败,请在GPU环境进行
2、项目失败,文件不存在。请查看是否添加图片并修改有关的地址链接
3、2.0动漫处理报错,原因极有可能是1中的安装失败或者图片脸部不够明显,建议更换GPU然后换图片
4、其他,可以留言进行询问,会第一时间回复
