【问题标题】:OpenCV detect face landmarks (ear-chin-ear line)OpenCV 检测人脸地标(耳-下-耳线)
【发布时间】:2016-09-09 16:32:28
【问题描述】:

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

【问题讨论】:

    标签: python opencv face-detection


    【解决方案1】:

    您正在寻找的是所谓的面部特征检测。你可以试试 DLIB 。 DLIB 是用 C++ 编写的,但它也有一个 python 包装器。Install Instructions

    现在使用 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()
    

    结果

    与地标相关的点:

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

    【讨论】:

    • 但我在rect=dlib.rectangle(x,y,x+w,y+h) 即Boost.Python.ArgumentError: Python argument types in rectangle.__init__(rectangle, numpy.int32, numpy.int32, numpy.int32, numpy.int32) did not match C++ signature: __init__(_object*, long left, long top, long right, long bottom) __init__(_object*) 行收到了一个错误您是否已经收到此错误并知道如何解决它?
    • 我没有遇到任何类似这样的错误,但我遇到了很多问题并提出了这个解决方案并且它有效。 :/你有正确的级联路径??和预测器??
    • 我通过写x,y,w,h =rects[0].astype(long)解决了这个问题
    • 现在可以用了吗?我添加了另一种使用 dlib facedetector 而不是 Opencv haar cascade one 的方法
    • 是的,它有效。显然,级联给了int32 矩形,而dlib 矩形方法(用C++ 编写)期望long 整数
    【解决方案2】:

    还有一个易于使用的 dlib 包装器,称为 face_recognition。

    from face_recognition import load_image_file, face_locations
    
    # load to numpy array
    img_array = load_image_file("img.jpg")
    
    # returns a list with a dict for each face
    # each face-dict contains landmarks for features like chin, nose.. etc
    landmarks = face_locations(img_array)
    

    【讨论】:

      猜你喜欢
      • 2019-12-01
      • 2021-07-17
      • 2012-02-04
      • 2013-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-31
      • 2012-04-15
      相关资源
      最近更新 更多