python dlib opencv 人脸68点特征检测

不得不感慨,现在现成的东西太多了,直接拿来用就行了

dlib安装(指定版本安装,避免踩坑)

pip install dlib==19.7.0

dlib中训练好的文件http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2

下载解压到项目中

代码

 1 import numpy as np
 2 import cv2 as cv
 3 import dlib
 4 
 5 detector = dlib.get_frontal_face_detector()
 6 predictor = dlib.shape_predictor('dlib/shape_predictor_68_face_landmarks.dat')
 7 
 8 
 9 def Detect_face(camera_idx):
10     # camera_idx: 电脑自带摄像头或者usb摄像头
11     cv.namedWindow('detect')
12     cap = cv.VideoCapture(camera_idx)
13 
14     while cap.isOpened():
15         cv.namedWindow('detect', cv.WINDOW_AUTOSIZE)
16         ok, frame = cap.read()
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             for idx, point in enumerate(landmarks):
25                 pos = (point[0, 0], point[0, 1])
26                 # print(idx, pos)
27                 cv.circle(frame, pos, 1, color=(0, 255, 0))
28                 font = cv.FONT_HERSHEY_SIMPLEX
29                 cv.putText(frame, str(idx + 1), pos, font, 0.4, (0, 255, 255), 1, cv.LINE_AA)
30         cv.imshow('detect', frame)
31         c = cv.waitKey(10)
32         if c & 0xFF == ord('q'):
33             break
34     cap.release()
35     cv.destroyAllWindows()
36 
37 
38 if __name__ == '__main__':
39     Detect_face(0)

效果图

 

真好使啊~~~

转载于:https://www.cnblogs.com/MC-Curry/p/10301015.html

你可能感兴趣的:(python dlib opencv 人脸68点特征检测)