Search code examples
pythonimage-processingdetection

Detect face, get the boarder of face


I am a new to python. I'm trying to detect face and find the boundaries of the face. How to make it? It can be a number of points that are the border of the face.

I want to obtain something like this:

enter image description here


Solution

  • from http://fideloper.com/facial-detection:

    We can use openCV for facial detection. OpenCV is written in C, but there are bindings for Python and actually 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)
    

    The original image: enter image description here

    With faces detected enter image description here