1.关键点检测
人脸关键点检测是人脸识别和分析领域中的关键一步,它是诸如自动人脸识别、表情分析、三维人脸重建及三维动画等其它人脸相关问题的前提和突破口。该 PaddleHub Module 的模型转换自 github.com/lsy17096535… ,支持同一张图中的多个人脸检测。
2.检测关键点
很简单,通过PaddleHub的face_landmark_localization模块进行关键点检测
import paddlehub as hub import cv2 import os face_landmark = hub.Module(name="face_landmark_localization") result = face_landmark.keypoint_detection(images=[cv2.imread('1.jpg')], output_dir='face_landmark_output', visualization=False) # 左上:2 # 右上:16 # 最下中:9 # print(result) data = result[0]['data'][0] print(data) print(len(data)) print("左上:2 ", data[1]) print("右上:16 ", data[15]) print("最下中:9 ", data[8]) width = int(data[15][0] - data[1][0])+50 height = int(data[8][1] - data[29][1]) # dx = int(data[1][0] + width / 2) # dy = int(data[8][1] - height / 2) dx = int(data[1][0])-10 dy = int(data[1][1]) print(width, height, dx, dy)
3.合成
主要是根据检测的脸的大小resize口罩,最后进行图层合并
# 第二步:合成 def merge(face_pic, w, h, dx, dy): mask_path = 'mask.png' face = cv2.imread(face_pic, cv2.IMREAD_COLOR) # 捕获图像1 print('face pic shape: ', face.shape) # 口罩 # IMREAD_UNCHANGED If set, return the loaded image as is (with alpha channel, otherwise it gets cropped). # 因此Png必须是4通道的IMREAD_UNCHANGED mask = cv2.imread(mask_path, cv2.IMREAD_UNCHANGED) # mask = cv2.resize(mask, [w, h]) mask = cv2.resize(mask, (w, h)) rows, cols, channels = mask.shape print('mask shape: ', rows, cols, channels) roi = face[dy:dy + rows, dx:dx + cols] print('dx + rows, dy + cols', dy + rows, dx + cols) for i in range(rows): for j in range(cols): if not (mask[i, j][3] == 0): # 透明的意思 roi[i, j][0] = mask[i, j][0] roi[i, j][1] = mask[i, j][1] roi[i, j][2] = mask[i, j][2] face[dy:dy + rows, dx:dx + cols] = roi merge_img_path = 're_' + face_pic cv2.imwrite(merge_img_path, face) return 're_' + face_pic merge('1.jpg', width, height, dx, dy)
4.效果