opencv圖像融合(大頭)
阿新 • • 發佈:2019-02-11
scl 上下 camera med inf ron 思路 landmark for
單純的變大再覆蓋上去,頭部檢測信息不夠全,效果實在是太差,就不多說了,只是按照自己的思路玩一玩,沒有達到抖音上那麽好的效果
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 ifnot 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,下巴down25 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()
效果
opencv圖像融合(大頭)