opencv图像融合(大头)
单纯的变大再覆盖上去,头部检测信息不够全,效果实在是太差,就不多说了,只是按照自己的思路玩一玩,没有达到抖音上那么好的效果
1 import cv2 as cv 2 import numpy as np 3 import dlib 4 5 6 detector = dlib.get_frontal_face_detector() 7 predictor = dlib.shape_predictor('../dlib/shape_predictor_68_face_landmarks.dat') 8 9 10 def big_head(camera_idx): 11 cap = cv.VideoCapture(camera_idx) 12 while cap.isOpened(): 13 cv.namedWindow('big_head', cv.WINDOW_AUTOSIZE) 14 ok, frame = cap.read() 15 # 镜像反转 16 if camera_idx == 0 or camera_idx == 1: 17 frame = cv.flip(frame, 1, dst=None) 18 if not ok: 19 break 20 gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) 21 rects = detector(gray, 0) 22 for i in range(len(rects)): 23 landmarks = np.matrix([[p.x, p.y] for p in predictor(frame, rects[i]).parts()]) 24 # 脸部中心点face_center,下巴down 25 face_center = (landmarks[29][0, 0], landmarks[29][0, 1]) 26 down = (landmarks[8][0, 0], landmarks[8][0, 1]) 27 left = (landmarks[0][0, 0], landmarks[0][0, 1]) 28 right = (landmarks[16][0, 0], landmarks[0][0, 1]) 29 # 上下偏移量y_offset,为了框出尽可能全的头部信息,边框扩大 30 y_offset = int(1.2*abs(face_center[1]-down[1])) 31 rect_start = (int(0.8*left[0]), face_center[1]-y_offset) 32 rect_end = (int(1.2*right[0]), face_center[1]+y_offset) 33 print(rect_start, rect_end) 34 face = frame[rect_start[1]: rect_end[1], rect_start[0]: rect_end[0]] 35 # 放大比例k_size 36 k_size = 1.1 37 size = (int(k_size*(rect_end[0]-rect_start[0])), int(k_size*(rect_end[1]-rect_start[1]))) 38 face = cv.resize(face, size, interpolation=cv.INTER_CUBIC) 39 face_mask = 255 * np.ones(face.shape, face.dtype) 40 output = cv.seamlessClone(face, frame, face_mask, face_center, cv.NORMAL_CLONE) 41 cv.imshow('face', face) 42 43 cv.rectangle(frame, rect_start, rect_end, (0, 0, 255), -1) 44 # cv.circle(frame, face_center, 1, (0, 0, 255), -1) 45 46 cv.imshow('big_head', output) 47 c = cv.waitKey(10) 48 if c & 0xFF == ord('q'): 49 break 50 cap.release() 51 cv.destroyAllWindows() 52 53 54 if __name__ == '__main__': 55 video = '../video/face.mp4' 56 big_head(video) 57 # test()
效果