# _*_ coding:utf-8 _*_ import numpy as np import cv2 import dlib detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(r'/home/xiaou/Downloads/python_spoof/shape_predictor_68_face_landmarks.dat') cap = cv2.VideoCapture(0) # cv2读取图像 while True: ret, fame = cap.read() b, g, r = cv2.split(fame) # 分离色道 opencv读入的色道是B,G,R fame2 = cv2.merge([r, g, b]) # 合成R,G,B dets = detector(fame2, 1) # 取灰度 # 人脸数rects for i in range(len(dets)): landmarks = np.matrix([[p.x, p.y] for p in predictor(fame2, dets[i]).parts()]) for idx, point in enumerate(landmarks): # 68点的坐标 pos = (point[0, 0], point[0, 1]) print(idx + 1, pos) # 利用cv2.circle给每个特征点画一个圈,共68个 cv2.circle(fame2, pos, 2, color=(0, 255, 0)) # 利用cv2.putText输出1-68 font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(fame2, str(idx + 1), pos, font, 0.8, (0, 0, 255), 1, cv2.LINE_AA) cv2.namedWindow("img", 2) cv2.imshow("img", fame2) cv2.waitKey(1)
效果如下: