基于dlib库实现人脸特征值提取
一、安装Dlib库管理员运行cmd,使用pip安装pip install dlib二、OpenCV安装使用pip安装pip3 install opencv_python三、提取人脸特征点import dlibimport numpy as npimport cv2predictor = dlib.shape_predictor("D:\\test\\shape_predictor_68_face_
·
一、安装Dlib库
管理员运行cmd,使用pip安装
pip install dlib
二、OpenCV安装
使用pip安装
pip3 install opencv_python
三、提取人脸特征点
import dlib
import numpy as np
import cv2
predictor = dlib.shape_predictor("D:\\test\\shape_predictor_68_face_landmarks.dat")
# predictor_path = "shape_predictor_68_face_landmarks.dat"
detector = dlib.get_frontal_face_detector()
# predictor = dlib.shape_predictor(predictor_path)
points_keys = []
captureframe = cv2.VideoCapture(0, cv2.CAP_DSHOW)
while (1):
ReturnValue, frame = captureframe.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
rects = detector(gray, 1)
for i in range(len(rects)):
landmarks = np.matrix([[p.x, p.y] for p in predictor(gray, rects[i]).parts()])
for idx, point in enumerate(landmarks):
pos = (point[0, 0], point[0, 1])
points_keys.append([point[0, 0], point[0, 1]])
cv2.circle(frame, pos, 2, (255, 0, 0), -1)
cv2.imshow('frame', frame)
cv2.waitKey(5)
运行结果:
四、在眼睛处绘制黑色的实心圆
import dlib
import numpy as np
import cv2
predictor = dlib.shape_predictor("D:\\test\\shape_predictor_68_face_landmarks.dat")
# predictor_path = "shape_predictor_68_face_landmarks.dat"
detector = dlib.get_frontal_face_detector()
# predictor = dlib.shape_predictor(predictor_path)
points_keys = []
captureframe = cv2.VideoCapture(0, cv2.CAP_DSHOW)
while (1):
ReturnValue, frame = captureframe.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
rects = detector(gray, 1)
for i in range(len(rects)):
landmarks = np.matrix([[p.x, p.y] for p in predictor(gray, rects[i]).parts()])
for idx, point in enumerate(landmarks):
pos = (point[0, 0], point[0, 1])
points_keys.append([point[0, 0], point[0, 1]])
cv2.circle(frame, pos, 2, (255, 0, 0), -1)
cv2.imshow('frame', frame)
cv2.waitKey(5)
def painting_sunglasses(img,detector,predictor):
#给人脸带上墨镜
rects = detector(img_gray, 0)
for i in range(len(rects)):
landmarks = np.matrix([[p.x, p.y] for p in predictor(img,rects[i]).parts()])
right_eye_x=0
right_eye_y=0
left_eye_x=0
left_eye_y=0
for i in range(36,42):#右眼范围
#将坐标相加
right_eye_x+=landmarks[i][0,0]
right_eye_y+=landmarks[i][0,1]
#取眼睛的中点坐标
pos_right=(int(right_eye_x/6),int(right_eye_y/6))
cv2.circle(img=img, center=pos_right, radius=30, color=(0,0,0),thickness=-1)
for i in range(42,48):#左眼范围
#将坐标相加
left_eye_x+=landmarks[i][0,0]
left_eye_y+=landmarks[i][0,1]
#取眼睛的中点坐标
pos_left=(int(left_eye_x/6),int(left_eye_y/6))
cv2.circle(img=img, center=pos_left, radius=30, color=(0,0,0),thickness=-1)
运行结果:
五、总结
本文简略地介绍了dlib库与OpenCV,并初步尝试使用dlib库对人脸的特征值进行提取。
六、参考链接
更多推荐
已为社区贡献1条内容
所有评论(0)