Example #1
0
def betaface_recognition(image_dict):
    """ Given a message contained a cropped head, send it to BetaFace
        API for face recognition. This can take anywhere from 1 to 10 seconds.
    """
    try:
        api = BetaFaceAPI()

        # Get image from message, save it to disk and
        # pass it on to BetaFace API
        image = base64_to_image(image_dict['head_image']['image'],
                                int(image_dict['head_image']['width']),
                                int(image_dict['head_image']['height']))
        temp_path = random_file_name('jpg', "FR_")
        logger.info("Generated random path to save image: %s" % temp_path)
        image = image.rotate(180)
        image.save(temp_path)

        logger.info("Saved image to disk at path %s" % temp_path)

        matches = api.recognize_faces(temp_path, 'amilab.ro')
        logger.info("Received recognized faces from BetaFace API %r" % matches)

        #os.remove(str(temp_path))
        #logger.info("Removed image from disk (%s)" % str(temp_path))

        return matches
    except:
        logger.exception("Failed to recognize faces via BetaFace API")
        return {}
Example #2
0
def crop_face_from_image(image):
    """ Return a cropped face from a given image. """

    # Try to detect faces in the image - if there is None,
    # there is nothing to do. Otherwise, keep only the first
    # face regardless of how many were detected.
    faces = voting_face_detect(image)
    if len(faces) == 0:
        return None

    (x, y, w, h) = faces[0][0]
    logger.info("Found face with size %d x %d" % (w, h))

    # Take the first detected face and scale the rectangle
    # 2 times around its center.
    face_rect = (x, y, x + w, y + h)
    face_rect = rectangle_scale(*face_rect, factor = 2)
    logger.info("Doubling both dimensions to %d x %d" % (face_rect[2] - face_rect[0],
                                                         face_rect[3] - face_rect[1]))

    # Intersect this with the image rectangle
    image_rect = (0, 0, image.size[0], image.size[1])
    cropping_rect = rectangle_intersection(face_rect, image_rect)
    logger.info("Final cropping rect has dimensions %d x %d" % (cropping_rect[2] - cropping_rect[0],
                                                                cropping_rect[3] - cropping_rect[1]))
    logger.info("Cropping rect: %.2f %.2f %.2f %.2f" % (cropping_rect[0],
                                                        cropping_rect[1],
                                                        cropping_rect[2],
                                                        cropping_rect[3]))

    cropping_rect = (int(cropping_rect[0]), int(cropping_rect[1]),
                     int(cropping_rect[2]), int(cropping_rect[3]))
    cropped_image = image.crop(cropping_rect)

    # Save both images and log this so we can debug more easily
    temp_path_full_image = random_file_name('jpg')
    temp_path_face_image = random_file_name('jpg')
    image.save(temp_path_full_image)
    cropped_image.save(temp_path_face_image)
    logger.info("Saved full image at %s" % temp_path_full_image)
    logger.info("Saved full image at %s" % temp_path_face_image)

    return cropped_image