Search code examples
pythonopencvcomputer-visionface-recognitiondlib

Dlib + OpenCV Draw Points Landsmark


I would like to draw contour of the cheek, as in the image below: Example image

I am using OpenCV and Dlib to detect the landmarks, And I do not know how to manipulate the Dlib points. Does anyone know how I can make the contour on the cheek?

Here is my code:

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()

This is my output:

Output image


Solution

  • One fast non-optimized approach based on Adrian Rosebrock's blog 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()
    

    Output Image:

    enter image description here