在 python 中使用 dlib 保存带有面部特征的图像?
save image with face landmarks using dlib in python?
有没有一种方法可以使用 python 使用 dlib 保存带有面部特征点叠加的图像?因为我只找到了 c++ 的 draw_rectangle 函数!
得到面部点后,可以在原图上画出来,像这样:
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
这就是我的工作方式。
.....................
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])
有没有一种方法可以使用 python 使用 dlib 保存带有面部特征点叠加的图像?因为我只找到了 c++ 的 draw_rectangle 函数!
得到面部点后,可以在原图上画出来,像这样:
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
这就是我的工作方式。
.....................
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])