On my learning quest I have been curious about recognizing people’s faces. The input will be family photos as I am hoping to figure out how is in a given photo. My current plan involves two steps. First is recognizing where the faces are within the image. The second is to figure out who it is. Let us see how far I get.

Using OpenCV to recognize where there is a face

I am hoping to get a 90% match on faces within OpenCV. This should be relatively easy in Python without much fuss. Dusting off my old facial recognition project I realized I have the connection to my current photo application complete and detecting my son’s face on an easy photo. Now time to krank it up.

Or rather, time to figure out how to scale the output image down. The images are larger than my laptop monitor. Lucky for me OpenCV makes this really easy:

def opencv_image_resize_height_respecting_aspect( image, height ):
    # initialize the dimensions of the image to be resized and
    # grab the image size
    dim = None
    width = None
    (h, w) = image.shape[:2]

    # if both the width and height are None, then return the
    # original image
    if width is None and height is None:
        print("No height and width...")
        return image

    # check to see if the width is None
    if width is None:
        # calculate the ratio of the height and construct the
        # dimensions
        r = height / float(h)
        dim = (int(w * r), height)

    # otherwise, the height is None
    else:
        # calculate the ratio of the width and construct the
        # dimensions
        r = width / float(w)
        dim = (width, int(h * r))

    # resize the image
    resized = cv2.resize(image, dim, interpolation=cv2.INTER_LANCZOS4)

    # return the resized image
    return resized

After spot checking approximately 10 images of varraying complexity the HAAR Cascade classifier seems to only be effecitve on detecting mug-shot like photos. Otherwise I seem to need another approach.

Using face_recognition

face_recognition looks like a promising library. I am wondering what I can do with it. To get this installed on OSX I needed to add the CMake CLI to the path via export PATH=/Applications/CMake.app/Contents/bin:$PATH. Unforuantely my time is up with this experiment, so tune next for “no one here” or “robot overload face detection”.