Dlib + OpenCV抽奖点地标

时间:2018-08-27 01:20:58

标签: python opencv computer-vision face-recognition dlib

我想绘制脸颊的轮廓,如下图所示: Example image

我正在使用 OpenCV Dlib 来检测界标,而且我不知道如何操作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()

这是我的输出:

Output image

1 个答案:

答案 0 :(得分:1)

一种基于Adrian Rosebrock博客文章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()

输出图像:

enter image description here