简体   繁体   English

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

[英]save image with face landmarks using dlib in python?

Is there a way to save image with the overlay of facial landmarks with dlib using python? 有没有一种方法可以使用python使用dlib保存带有面部标志的图像? Because I've found only the draw_rectangle function which is for c++! 因为我只找到了用于c ++的draw_rectangle函数!

After getting the facial points, you can draw it on original image, like this: 获得面部点之后,可以将其绘制在原始图像上,如下所示:

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

This is how it worked for me. 这就是它为我工作的方式。

.....................
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

where I made this change to annotate_landmarks() 我在此处对annotate_landmarks()进行了更改

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

instead of : 代替 :

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 dlib 面部标志裁剪面部 - Cropping face using dlib facial landmarks 使用 dlib 进行人脸地标检测 - Face landmarks detection with dlib Python 使用 dlib 裁剪人脸图像 - Python cropped face image using dlib 在使用 dlib 检测面部标志后,有没有办法 select 面部特定点? - Is there a way to select a specific point at the face after detecting facial landmarks using dlib? python Dlib 将人脸描述符保存到文件中以备不时之需 - python Dlib save face descriptor into file for ulterior use 使用 opencv dnn 人脸检测器检测检测到的人脸图像中的人脸标志 - Detect facial landmarks inside a detected face image using opencv dnn face detector 使用python使用Dlib的人脸检测器检测多个人脸 - Detect multiple faces using Dlib's face detector using python Matplotlib - 绘制没有背景图像的人脸地标 - Matplotlib - plot face landmarks with no background image 如何在 Python 脚本(Flask 服务器)中保存 dlib.get_frontal_face_detector() 的 output (dlib.rectangles)? - How to save dlib.get_frontal_face_detector() 's output (dlib.rectangles) in a Python Script (Flask Server)? 预测器出错 = dlib.shape_predictor(“shape_predictor_68_face_landmarks.dat”) RuntimeError: - Having error in predictor = dlib.shape_predictor(“shape_predictor_68_face_landmarks.dat”) RuntimeError:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM