Python 在使用dlib检测面部地标后,是否有方法选择面部的特定点?
我正在使用Dlib的68点人脸地标预测器,该预测器有68个点,标记在人脸的不同区域,如下图所示: 我已设法从预测的地标中访问特定点,例如,我可以通过以下方法选择唇角处的点,即面部地标预测器中的第48个点 ' 进口cv2 导入dlib 从google.colab.patches导入cv2_imshowPython 在使用dlib检测面部地标后,是否有方法选择面部的特定点?,python,opencv,image-processing,dlib,facial-landmark-alignment,Python,Opencv,Image Processing,Dlib,Facial Landmark Alignment,我正在使用Dlib的68点人脸地标预测器,该预测器有68个点,标记在人脸的不同区域,如下图所示: 我已设法从预测的地标中访问特定点,例如,我可以通过以下方法选择唇角处的点,即面部地标预测器中的第48个点 ' 进口cv2 导入dlib 从google.colab.patches导入cv2_imshow p = "path_to_shape_predictor_68_face_landmarks.dat" img= cv2.imread('Obama.jpg') gray=cv
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)'
它会在指定区域上绘制一个带有红色小圆的图像。然而;如果我想选择一个不是landmark模型68个点的一部分的点,我如何获得它
这张图片将更详细地阐述:
红色圆圈表示我使用代码访问的点,蓝色圆圈表示所需的点。我可以向您推荐几种解决方案: 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)
完整代码:
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-另一种解决方案是使用较大的面部标志模型,请检查:
3-困难的方法是重新培训和定制您自己的形状检测器:
对于地标索引,我使用了以下参考:
定义要检测的点。你的意思是用鼠标点击一个点,然后你需要得到该点的坐标吗?我需要从给定的面部图像中提取一部分面部,因此我需要指定用于提取特定区域的遮罩函数使用的坐标。尝试使用google face mesh,这给出了468个面部关键点。