使用dlib在python中保存带有面部标志的图像?

时间:2019-02-04 17:09:18

标签: python opencv3.0 dlib

有没有一种方法可以使用python使用dlib保存带有面部标志的图像?因为我只找到了用于c ++的draw_rectangle函数!

2 个答案:

答案 0 :(得分:1)

获取面部点后,可以在原始图像上绘制它,如下所示:

def annotate_landmarks(im, landmarks):
    CIRCLE_SIZE = 1
    FONT_SCALE = 1
    THICKNESS_S = 1
    im = im.copy()
        #0-16: head
    for idx, point in enumerate(landmarks[0:17]):
        pos = (point[0, 0], point[0, 1])
        #cv2.putText(im, str(idx), pos, fontFace=cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale=FONT_SCALE, color=(0, 0, 255))
        cv2.circle(im, pos, CIRCLE_SIZE, color=(255, 0, 0), thickness=THICKNESS_S)

    #17-21: left eye brow
    #22-26: right eye brow
    for idx, point in enumerate(landmarks[17:27]):
        pos = (point[0, 0], point[0, 1])
        #cv2.putText(im, str(idx), pos,fontFace=cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,fontScale=FONT_SCALE,color=(0, 0, 255))
        cv2.circle(im, pos, CIRCLE_SIZE, color=(0, 255, 0), thickness=THICKNESS_S)

    #27-35: nose
    for idx, point in enumerate(landmarks[27:36]):
        pos = (point[0, 0], point[0, 1])
        #cv2.putText(im, str(idx), pos,fontFace=cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,fontScale=FONT_SCALE,color=(0, 0, 255))
        cv2.circle(im, pos, CIRCLE_SIZE, color=(0, 0, 255), thickness=THICKNESS_S)

    #36-41: left eye
    #42-47: right eye
    for idx, point in enumerate(landmarks[36:48]):
        pos = (point[0, 0], point[0, 1])
        #cv2.putText(im, str(idx), pos,fontFace=cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,fontScale=FONT_SCALE,color=(0, 0, 255))
        cv2.circle(im, pos, CIRCLE_SIZE, color=(0, 255, 255), thickness=THICKNESS_S)

    #48-68: lips
    for idx, point in enumerate(landmarks[48:68]):
        pos = (point[0, 0], point[0, 1])
        #cv2.putText(im, str(idx), pos,fontFace=cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,fontScale=FONT_SCALE,color=(0, 0, 255))
        cv2.circle(im, pos, CIRCLE_SIZE, color=(255, 0, 255), thickness=THICKNESS_S)
    return im

答案 1 :(得分:0)

这就是我的工作方式。

.....................
shape = predictor(img, d)
vec= np.empty([68,2], dtype=int)
for b in range(68):
    vec[b][0] = shape.part(b).x
    vec[b][1] = shape.part(b).y

 landmarks_img=annotate_landmarks(img,vec)
 cv2.imwrite("landmarked-"+os.path.basename(f),landmarks_img) #save image

我在其中更改了 annotate_landmarks()

pos = (point[0], point[1])

代替:

pos = (point[0, 0], point[0, 1])