<commit_before># !/usr/bin/env python
# -*- coding: utf-8 -*-

"""Generate an EigenFace from a few faces found in images."""

from SimpleCV import Image, HaarCascade, ImageSet


_SOURCE_DIR = 'data/source_images/'
_INTERMEDIATE_DIR = 'data/intermediate_images/'


def get_faces(image, haar_cascade):
    """Get all faces in the image.
    image (Image) - Image object to find faces in.
                    Has to be valid.
    haar_cascade (HaarCascade) - A valid HaarCascade feature detector.
    Returns list with all faces found in image. Empty list if no faces
        were found.
    """
    assert isinstance(image, Image)
    assert isinstance(haar_cascade, HaarCascade)

    detected_features = image.findHaarFeatures(haar_cascade)

    # We don't use ImageSet here because this list will usually be concatenated
    # with something else and we want to avoid the overhead.
    # Simply extend an existing ImageSet and you'll get there.
    return [feature.crop() for feature in detected_features]


def _clear_intermediate_data():
    import os
    filelist = [file_ for file_ in os.listdir(_INTERMEDIATE_DIR)
                if file_.endswith('.png')]
    for file_ in filelist:
        os.remove(_INTERMEDIATE_DIR + file_)


def pre_process_data():
    """Pre-process the data folder to find all images and crop all faces.
    WARNING: This will clear all pngs in the intermediate_images directory!
    """
    images = ImageSet(directory=_SOURCE_DIR)
    face_haar_cascade = HaarCascade('face2.xml')
    cropped_images = ImageSet()
    for image in images:
        cropped_images.extend(get_faces(image, face_haar_cascade))

    _clear_intermediate_data()
    cropped_images.save(_INTERMEDIATE_DIR)


def test_get_faces():
    """Test get faces against an image without any faces and one with faces."""
    face_haar_cascade = HaarCascade('face2.xml')
    results = [(Image(_SOURCE_DIR + 'false.jpg'), 0),
               (Image(_SOURCE_DIR + 'photo2.jpg'), 2)]
    for result in results:
        faces = get_faces(result[0], face_haar_cascade)
        assert len(faces) == result[1]


if __name__ == '__main__':
    # use `python -m py.test eigen_avatar.py` to test!
    pre_process_data()

<commit_msg>Resized faces to the same size<commit_after># !/usr/bin/env python
# -*- coding: utf-8 -*-

"""Generate an EigenFace from a few faces found in images."""

from SimpleCV import Image, HaarCascade, ImageSet
from collections import namedtuple


_SOURCE_DIR = 'data/source_images/'
_INTERMEDIATE_DIR = 'data/intermediate_images/'

size = namedtuple('Size', ['width', 'height'])
_IMAGE_SIZE = size(128, 128)


def get_faces(image, haar_cascade):
    """Get all faces in the image.
    image (Image) - Image object to find faces in.
                    Has to be valid.
    haar_cascade (HaarCascade) - A valid HaarCascade feature detector.
    Returns list with all faces found in image. Empty list if no faces
        were found.
    """
    assert isinstance(image, Image)
    assert isinstance(haar_cascade, HaarCascade)

    detected_features = image.findHaarFeatures(haar_cascade)

    # We don't use ImageSet here because this list will usually be concatenated
    # with something else and we want to avoid the overhead.
    # Simply extend an existing ImageSet and you'll get there.
    return [feature.crop() for feature in detected_features]


def _clear_intermediate_data():
    import os
    filelist = [file_ for file_ in os.listdir(_INTERMEDIATE_DIR)
                if file_.endswith('.png')]
    for file_ in filelist:
        os.remove(_INTERMEDIATE_DIR + file_)


def pre_process_data():
    """Pre-process the data folder to find all images and crop all faces.
    WARNING: This will clear all pngs in the intermediate_images directory!
    """
    images = ImageSet(directory=_SOURCE_DIR)
    face_haar_cascade = HaarCascade('face2.xml')
    cropped_images = []
    for image in images:
        cropped_images.extend(get_faces(image, face_haar_cascade))

    resized_images = ImageSet()
    for image in cropped_images:
        resized_images.append(image.resize(_IMAGE_SIZE.width,
                                           _IMAGE_SIZE.height))

    _clear_intermediate_data()
    resized_images.save(_INTERMEDIATE_DIR)


def test_get_faces():
    """Test get faces against an image without any faces and one with faces."""
    face_haar_cascade = HaarCascade('face2.xml')
    results = [(Image(_SOURCE_DIR + 'false.jpg'), 0),
               (Image(_SOURCE_DIR + 'photo2.jpg'), 2)]
    for result in results:
        faces = get_faces(result[0], face_haar_cascade)
        assert len(faces) == result[1]


if __name__ == '__main__':
    # use `python -m py.test eigen_avatar.py` to test!
    pre_process_data()


{'fraction_non_alphanumeric': 0.09192494201981868, 'fraction_numerical': 0.004638414505587181, 'mean_word_length': 3.262353998203055, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5539296', 'n_tokens_mistral': 1463, 'n_tokens_neox': 1427, 'n_words': 451}