#1.该代码用于提取人脸68个关键点 #2.并提取嘴部mask # coding=utf-8 import cv2 import dlib import numpy as np # path = "2.jpg" path = "face.png" img = cv2.imread(path) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 人脸分类器 detector = dlib.get_frontal_face_detector() # 获取人脸检测器 predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") # JAW_POINTS = list(range(0, 17)) # FACE_POINTS = list(range(17, 68)) # BROW_POINTS = list(range(17, 27)) # NOSE_POINTS = list(range(27, 36)) # EYE_POINTS = list(range(36, 48)) # MOUTH_POINTS = list(range(48, 61)) dets = detector(gray, 1) for face in dets: shape = predictor(img, face) # 寻找人脸的68个标定点 # 遍历所有点,打印出其坐标,并圈出来 for idx,pt in enumerate(shape.parts()): pt_pos = (pt.x, pt.y) cv2.circle(img, pt_pos, 2, (0, 255, 0), 1) font=cv2.FONT_HERSHEY_SCRIPT_SIMPLEX cv2.putText(img,str(idx),pt_pos,font,0.4,(0,0,255),1,cv2.LINE_AA) cv2.imshow("image", img) h, w, c = img.shape # 手工绘制ROI区域 mask = np.zeros((h, w), dtype=np.uint8) list_x = [] list_y = [] for idx, pt in enumerate(shape.parts()): if idx >= 48 and idx <= 59: list_x.append(pt.x) list_y.append(pt.y) pts = np.vstack((list_x, list_y)).astype(np.int32).T cv2.fillPoly(mask, [pts], (255), 8, 0) cv2.imshow("mask", mask) cv2.waitKey(0) cv2.destroyAllWindows()
提取人脸68个特征点并提取嘴部mask--2020-12-18任务
最新推荐文章于 2023-03-17 12:50:07 发布