python dlib opencv 人脸68点特征检测

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

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

pip install dlib==19.7.

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

下载解压到项目中

代码

 import numpy as np
import cv2 as cv
import dlib detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('dlib/shape_predictor_68_face_landmarks.dat') def Detect_face(camera_idx):
# camera_idx: 电脑自带摄像头或者usb摄像头
cv.namedWindow('detect')
cap = cv.VideoCapture(camera_idx) while cap.isOpened():
cv.namedWindow('detect', cv.WINDOW_AUTOSIZE)
ok, frame = cap.read()
# frame = cv.flip(frame, 1, dst=None)
if not ok:
break
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
rects = detector(gray, 0)
for i in range(len(rects)):
landmarks = np.matrix([[p.x, p.y] for p in predictor(frame, rects[i]).parts()])
for idx, point in enumerate(landmarks):
pos = (point[0, 0], point[0, 1])
# print(idx, pos)
cv.circle(frame, pos, 1, color=(0, 255, 0))
font = cv.FONT_HERSHEY_SIMPLEX
cv.putText(frame, str(idx + 1), pos, font, 0.4, (0, 255, 255), 1, cv.LINE_AA)
cv.imshow('detect', frame)
c = cv.waitKey(10)
if c & 0xFF == ord('q'):
break
cap.release()
cv.destroyAllWindows() if __name__ == '__main__':
Detect_face(0)

效果图

真好使啊~~~

上一篇:android 数据重构(仿淘宝浏览记录,足迹)


下一篇:Spring 定时器