Dlib + OpenCV 绘制点地标

Dlib + OpenCV Draw Points Landsmark

我想画脸颊的轮廓,如下图:

我正在使用 OpenCVDlib 来检测地标,但我不知道如何操作 Dlib 点。有谁知道怎么画脸颊的轮廓吗?

这是我的代码:

import cv2
import dlib
import numpy as np


def imprimePontos (imagem, pontosFaciais):
    for p in pontosFaciais.parts():
        cv2.circle(imagem, (p.x, p.y), 2, (0, 255,0) ,4)

def imprimeNumeros (imagem, pontosFaciais):
    for i, p in enumerate (pontosFaciais.parts()): 
        cv2.putText(imagem, str(i), (p.x, p.y), fonte, .55, (0, 0, 255),1) 

def points (imagem, pontosFaciais): #here where a draw de points
    p68 =[[15, 47, False],
          [47, 28, False],
          [28, 30, False],
          [30, 12, False]]

    for k in range(0, len(p68)):
        pontos = []
        for i in range(p68[k] [0], p68[k][1] + 1):
            ponto = [pontosFaciais.part(i).x, pontosFaciais.part(i).y]
            pontos.append(ponto)
        pontos = np.array(pontos, dtype=np.int32)
        cv2.polylines(imagem, [pontos], p68 [k][2], (255, 0, 0), 2)

fonte = cv2.FONT_HERSHEY_COMPLEX_SMALL
imagem = cv2.imread('1.jpg')
detectorface = dlib.get_frontal_face_detector()
detectorpontosfaciais = 
dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
facesDetectadas = detectorface(imagem, 2)

for face in facesDetectadas:
    pontos = detectorpontosfaciais(imagem, face)
    print(pontos.parts())
    #imprimePontos(imagem, pontos)
    #imprimeNumeros(imagem, pontos)
    points(imagem, pontos)


cv2.imshow("Bucheca", imagem)
cv2.waitKey()
cv2.destroyAllWindows()

这是我的输出:

一种基于 Adrian Rosebrock 博客的快速非优化方法 post https://www.pyimagesearch.com/2017/04/10/detect-eyes-nose-lips-jaw-dlib-opencv-python/

from collections import OrderedDict
import numpy as np
import cv2
import dlib
import imutils

CHEEK_IDXS = OrderedDict([("left_cheek", (1,2,3,4,5,48,49,31)),
                        ("right_cheek", (11,12,13,14,15,35,53,54))
                         ])

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

img = cv2.imread('tom.jpg')
img = imutils.resize(img, width=600)

overlay = img.copy()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

detections = detector(gray, 0)
for k,d in enumerate(detections):
    shape = predictor(gray, d)
    for (_, name) in enumerate(CHEEK_IDXS.keys()):
        pts = np.zeros((len(CHEEK_IDXS[name]), 2), np.int32) 
        for i,j in enumerate(CHEEK_IDXS[name]): 
            pts[i] = [shape.part(j).x, shape.part(j).y]

        pts = pts.reshape((-1,1,2))
        cv2.polylines(overlay,[pts],True,(0,255,0),thickness = 2)

    cv2.imshow("Image", overlay)
    cv2.waitKey(0)
    if cv2.waitKey(1) & 0xFF == ord('q'): 
        break
cv2.destroyAllWindows()

输出图像: