[英]Is there a way to select a specific point at the face after detecting facial landmarks using dlib?
I am using Dlib's 68 point face landmark predictor, which has 68 points that are marked on various regions of the face shown in the picture below:我正在使用 Dlib 的 68 点人脸界标预测器,它有 68 个点标记在人脸的各个区域,如下图所示:
I have managed to access particular points from the predicted landmarks, for example, I can select a point that is at the corner of the lip which is the 48th point in the facial landmark predictor by the following ' import cv2 import dlib from google.colab.patches import cv2_imshow我已经设法从预测的地标中访问特定点,例如,我可以 select 一个位于唇角的点,它是面部地标预测器中的第 48 个点,通过以下“import cv2 import dlib from google.colab” .patches 导入 cv2_imshow
p = "path_to_shape_predictor_68_face_landmarks.dat"
img= cv2.imread('Obama.jpg')
gray=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(p)
face = detector(gray)
# Get the shape using the predictor
landmarks=predictor(gray, face)
# Defining x and y coordinates of a specific point
x=landmarks.part(48).x
y=landmarks.part(48).y
# Drawing a circle
cv2.circle(img, (x, y), 6, (0, 0, 255), -1)
cv2_imshow(img)'
It results in an image with a red small circle drawn on the specified region.它会在指定区域上绘制一个带有红色小圆圈的图像。 However;
然而; if I want to select a point that is not a part of the 68 points of the landmark's model, how can I obtain it?
如果我想 select 一个点不属于地标的 model 的 68 个点的一部分,我该如何获得它?
This picture will elaborate it more:这张图会更详细的说明:
The red circle indicates the point that I have accessed using the code and the blue circle shows the desired point.红色圆圈表示我使用代码访问的点,蓝色圆圈表示所需的点。
There are several solutions I may suggest to you:我可能会向您建议几种解决方案:
1- The easy way is to use trigonometry and geometry, for instance to calculate pupil of left eye: 1-简单的方法是使用三角学和几何学,例如计算左眼的瞳孔:
pupil_x = int((abs(landmarks.part(39).x + landmarks.part(36).x)) / 2) # The midpoint of a line Segment between eye's corners in x axis
pupil_y = int((abs(landmarks.part(39).y + landmarks.part(36).y)) / 2) # The midpoint of a line Segment between eye's corners in y axis
pupil_coordination = (pupil_x, pupil_y)
Full code:完整代码:
import cv2
import dlib
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_81_face_landmarks.dat")
img = cv2.imread("test.jpg")
(h, w, _) = img.shape
h2 = 600
w2 = int(h2 * h / w)
img = cv2.resize(img , (h2, w2))
img = cv2.flip(img , 1)
gray = cv2.cvtColor(img , cv2.COLOR_BGR2GRAY)
faces = detector(gray)
face = faces[0]
landmarks = predictor(gray, face)
pupil_x = int((abs(landmarks.part(39).x + landmarks.part(36).x)) / 2)
pupil_y = int((abs(landmarks.part(39).y + landmarks.part(36).y)) / 2)
pupil_coordination = (pupil_x, pupil_y)
cv2.circle(img, pupil_coordination, 6, (0, 0, 255), -1)
cv2.imshow('Show', img )
cv2.waitKey(0)
cv2.destroyAllWindows()
2- Other solution is to use larger facial landmarks model, check this on: 81 Facial Landmarks Shape Predictor 2- 其他解决方案是使用更大的面部标志 model,请查看: 81 Facial Landmarks Shape Predictor
3- The hard way is to retrain and customize your own shape detector: Train a face landmarking model 3- 困难的方法是重新训练和自定义您自己的形状检测器:训练面部标记 model
For landmarks index I used the following reference: Face reference points对于地标索引,我使用了以下参考:人脸参考点
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.