OpenCV检测面部标志(耳-颏-耳线)

2024-10-17 00:24:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找一个opencv函数(在python中)来检测人脸上的左耳-下巴-右耳(看起来像一个抛物线)。有没有哈尔卡德在做这项工作?我已经知道前脸或眼睛的哈尔卡塞德斯,但我正在寻找更精确的东西。


Tags: 函数opencv眼睛抛物线右耳下巴
1条回答
网友
1楼 · 发布于 2024-10-17 00:24:58

你要找的是所谓的面部地标检测。你可以试试DLIB。DLIB是用C++编写的,但它也有一个Python包装器。

现在使用DLib您可以实现这一点

代码

import cv2
import dlib
import numpy

PREDICTOR_PATH = "/home/zed/dlib/files/shape_predictor_68_face_landmarks.dat"
predictor = dlib.shape_predictor(PREDICTOR_PATH)
cascade_path='haarcascade_frontalface_default.xml'
cascade = cv2.CascadeClassifier(cascade_path)

# #This is using the Dlib Face Detector . Better result more time taking
# def get_landmarks(im):
#     rects = detector(im, 1)
#     rect=rects[0]
#     print type(rect.width())
#     fwd=int(rect.width())
#     if len(rects) == 0:
#         return None,None

#     return np.matrix([[p.x, p.y] for p in predictor(im, rects[0]).parts()]),fwd

def get_landmarks(im):
    rects = cascade.detectMultiScale(im, 1.3,5)
    x,y,w,h =rects[0]
    rect=dlib.rectangle(x,y,x+w,y+h)
    return numpy.matrix([[p.x, p.y] for p in predictor(im, rect).parts()])

def annotate_landmarks(im, landmarks):
    im = im.copy()
    for idx, point in enumerate(landmarks):
        pos = (point[0, 0], point[0, 1])
        cv2.putText(im, str(idx), pos,
                    fontFace=cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,
                    fontScale=0.4,
                    color=(0, 0, 255))
        cv2.circle(im, pos, 3, color=(0, 255, 255))
    return im

im=cv2.imread('face_leo1.jpg')
cv2.imshow('Result',annotate_landmarks(im,get_landmarks(im)))
cv2.waitKey(0)
cv2.destroyAllWindows()

结果

enter image description here

与地标相关的点:

FACE_POINTS = list(range(17, 68))
MOUTH_POINTS = list(range(48, 61))
RIGHT_BROW_POINTS = list(range(17, 22))
LEFT_BROW_POINTS = list(range(22, 27))
RIGHT_EYE_POINTS = list(range(36, 42))
LEFT_EYE_POINTS = list(range(42, 48))
NOSE_POINTS = list(range(27, 35))
JAW_POINTS = list(range(0, 17))
CHIN_POINTS=list(range(6,11))

相关问题 更多 >