订阅
纠错
加入自媒体

使用Python+OpenCV+Dlib实现人脸检测与人脸特征关键点识别

2020-08-12 10:12
磐创AI
关注

到目前为止,我们在检测人脸方面做得很好,但是我们仍然需要一些工作来提取所有特征(地标)。接下来让我们开始吧。步骤3:识别人脸特征你喜欢魔术吗?到目前为止,DLib的工作方式相当神奇,只需几行代码我们就可以实现很多,而现在我们遇到了一个全新的问题,它还会继续这么简单吗?回答是肯定的!原来DLib提供了一个名为shape_predictor()的函数,它将为我们提供所有的魔法,但是需要一个预先训练的模型才能工作。有几种模型可以与shape_predictor一起工作,我正在使用的模型可以在这里下载,也可以尝试其他模型。让我们看看新代码现在是什么样子import cv2import dlib# Load the detectordetector = dlib.get_frontal_face_detector()# Load the predictorpredictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")# read the imageimg = cv2.imread("face.jpg")# Convert image into grayscalegray = cv2.cvtColor(src=img, code=cv2.COLOR_BGR2GRAY)# Use detector to find landmarksfaces = 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    # Look for the landmarks    landmarks = predictor(image=gray, box=face)    x = landmarks.part(27).x    y = landmarks.part(27).y    # Draw a circle    cv2.circle(img=img, center=(x, y), radius=5, color=(0, 255, 0), thickness=-1)# show the imagecv2.imshow(winname="Face", mat=img)# Wait for a key press to exitcv2.waitKey(delay=0)# Close all windowscv2.destroyAllWindows()像以前一样,我们总是在同一个代码上构建代码,现在使用我们的预测函数为每个人脸找到特征。但现在我还在做一些奇怪的事情,比如如下代码的数值27是用来干嘛的?landmarks = predictor(image=gray, box=face)x = landmarks.part(27).xy = landmarks.part(27).y我们的预测函数会返回一个包含68个点的对象,根据我们之前看到的图片,如果你注意到的话,会发现点27正好在眼睛之间,所以如果所有的计算正确,你应该看到一个绿点在眼睛之间,如下图所示:

我们已经很接近了,现在让我们渲染所有的点,而不是只渲染一个:import cv2import numpy as npimport dlib# Load the detectordetector = dlib.get_frontal_face_detector()# Load the predictorpredictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")# read the imageimg = cv2.imread("face.jpg")# Convert image into grayscalegray = cv2.cvtColor(src=img, code=cv2.COLOR_BGR2GRAY)# Use detector to find landmarksfaces = 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=img, center=(x, y), radius=3, color=(0, 255, 0), thickness=-1)# show the imagecv2.imshow(winname="Face", mat=img)# Delay between every framcv2.waitKey(delay=0)# Close all windowscv2.destroyAllWindows()

但是如果你对所有的点都不感兴趣呢?实际上,你可以调整你的范围间隔来获得上面术语表中指定的任何特征,就像我在这里做的那样:

<上一页  1  2  3  下一页>  
声明: 本文由入驻维科号的作者撰写,观点仅代表作者本人,不代表OFweek立场。如有侵权或其他问题,请联系举报。

发表评论

0条评论,0人参与

请输入评论内容...

请输入评论/评论长度6~500个字

您提交的评论过于频繁,请输入验证码继续

暂无评论

暂无评论

人工智能 猎头职位 更多
扫码关注公众号
OFweek人工智能网
获取更多精彩内容
文章纠错
x
*文字标题:
*纠错内容:
联系邮箱:
*验 证 码:

粤公网安备 44030502002758号