检测脸部,获得脸部边框

时间:2014-04-13 18:06:38

标签: python image-processing detection

我是python的新手。我正试图检测脸部并找到脸部的边界。 怎么做?它可以是面部边界的多个点。

我想获得这样的东西:

enter image description here

1 个答案:

答案 0 :(得分:0)

来自http://fideloper.com/facial-detection

  

我们可以使用openCV进行面部检测。 OpenCV是用C语言编写的,但是   有bindings for Python,实际上是PHP。

import cv2

def detect(path):
    img = cv2.imread(path)
    cascade = cv2.CascadeClassifier("/vagrant/detect/haarcascade_frontalface_alt.xml")
    rects = cascade.detectMultiScale(img, 1.3, 4, cv2.cv.CV_HAAR_SCALE_IMAGE, (20,20))

    if len(rects) == 0:
        return [], img
    rects[:, 2:] += rects[:, :2]
    return rects, img

def box(rects, img):
    for x1, y1, x2, y2 in rects:
        cv2.rectangle(img, (x1, y1), (x2, y2), (127, 255, 0), 2)
    cv2.imwrite('/vagrant/img/detected.jpg', img);

rects, img = detect("/vagrant/img/one.jpg")
box(rects, img)

原始图片: enter image description here

检测到面部 enter image description here

相关问题