使用Python+OpenCV+Dlib实现人脸检测与人脸特征关键点识别
发布时间
阅读量:
阅读量


在安装dlib库时必须确保与当前Python版本相匹配以避免安装失败。
最初我是使用Python 3.6版本的库 最后成功安装了Python 3.7版本的dlib库。
向大家展示代码 您可以自行运行一遍 这样能更好地增进学习乐趣。
import cv2
import dlib
# Load the detector
detector = dlib.get_frontal_face_detector()
# Load the predictor
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
# read the image
cap = cv2.VideoCapture(0)
while True:
_, frame = cap.read()
# Convert image into grayscale
gray = cv2.cvtColor(src=frame, code=cv2.COLOR_BGR2GRAY)
# Use detector to find landmarks
faces = detector(gray)
for face in faces:
x1 = face.left() # left point
y1 = face.top() # top point
x2 = face.right() # right point
y2 = face.bottom() # bottom point
# Create landmark object
landmarks = predictor(image=gray, box=face)
# Loop through all the points
for n in range(0, 68):
x = landmarks.part(n).x
y = landmarks.part(n).y
# Draw a circle
cv2.circle(img=frame, center=(x, y), radius=3, color=(0, 255, 0), thickness=-1)
# show the image
cv2.imshow(winname="Face", mat=frame)
# Exit when escape is pressed
if cv2.waitKey(delay=1) == 27:
break
# When everything done, release the video capture and video write objects
cap.release()
# Close all windows
cv2.destroyAllWindows()

哈哈,在我的gongzhong号上看了一篇文章感觉挺有意思的呢。
shape_predictor_68_face_landmarks.dat这个文件其实就是用来匹配68个像素点的工具可以在上直接下载如果大家都没有会员的话也可以私下联系我发送给你哦~
全部评论 (0)
还没有任何评论哟~
