<commit_before>"""Face."""

from PIL import Image, ImageFilter
import numpy as np
import time
import os


class Face(object):

    """Face from database."""

    def __init__(self, path):
        """Construct Face."""
        self.path = path
        self.original = Image.open(path)
        self.img = self.face()

        filename = os.path.basename(path)
        self.id = int(filename.split('_')[0])
        self.sub_id = int(filename.split('_')[1])

    def __str__(self):
        """Return string representation of Face."""
        return "{img.id:3} {img.sub_id}".format(img=self)

    def edges(self):
        """Find edges of image."""
        smooth = self.original.filter(ImageFilter.BLUR)
        edges = smooth.filter(ImageFilter.FIND_EDGES)
        blur = edges.filter(ImageFilter.BLUR)
        return blur

    @property
    def size(self):
        """Return size of image in (x, y)."""
        return self.img.size

    @property
    def vector(self):
        """Return column vector of image intensities."""
        size = self.size
        matrix = []
        for y in range(size[1]):
            matrix.append([self.img.getpixel((x, y)) for x in range(size[0])])

        matrix = np.matrix(matrix)
        return matrix.reshape((size[0] * self.size[1], 1))

    def face(self):
        """Return face."""
        img = self.edges()
        leftside_face = self._get_left_median(img)
        rightside_face = self._get_right_median(img)

        face_center = (leftside_face + rightside_face) / 2
        left_edge_crop = face_center - self.original.size[1] / 2
        right_edge_crop = face_center + self.original.size[1] / 2

        return self.original.crop(
            [left_edge_crop, 0, right_edge_crop, self.original.size[1]]
        )

    def _get_left_median(self, img):
        """Return the mean of the left side of the image."""
        width = img.size[0]
        height = img.size[1]

        left_most_points = []
        for y in range(int(height / 5), int(6 * height / 7)):
            row = [
                (img.getpixel((x, y)), x)
                for x in range(10, width / 2)
            ]
            sorted_row = sorted(row)

            left_most_points.append(sorted_row[-1][1])

        return int(np.median(left_most_points))

    def _get_right_median(self, img):
        """Return the mean of the right side of the image."""
        width = img.size[0]
        height = img.size[1]
<commit_msg>Resize image for memory and speed<commit_after>"""Face."""

from PIL import Image, ImageFilter
import numpy as np
import time
import os


class Face(object):

    """Face from database."""

    def __init__(self, path):
        """Construct Face."""
        self.path = path
        original = Image.open(path)
        original = original.resize((int(s / 4) for s in original.size))
        self.img = self.face(original)

        filename = os.path.basename(path)
        self.id = int(filename.split('_')[0])
        self.sub_id = int(filename.split('_')[1])

    def __str__(self):
        """Return string representation of Face."""
        return "{img.id:3} {img.sub_id}".format(img=self)

    @staticmethod
    def edges(img):
        """Find edges of image."""
        smooth = img.filter(ImageFilter.BLUR)
        edges = smooth.filter(ImageFilter.FIND_EDGES)
        blur = edges.filter(ImageFilter.BLUR)
        return blur

    @property
    def size(self):
        """Return size of image in (x, y)."""
        return self.img.size

    @property
    def vector(self):
        """Return column vector of image intensities."""
        size = self.size
        matrix = []
        for y in range(size[1]):
            matrix.append([self.img.getpixel((x, y)) for x in range(size[0])])

        matrix = np.matrix(matrix)
        return matrix.reshape((size[0] * self.size[1], 1))

    def face(self, img):
        """Return face."""
        edges = Face.edges(img)
        leftside_face = self._get_left_median(edges)
        rightside_face = self._get_right_median(edges)

        face_center = (leftside_face + rightside_face) / 2
        left_edge_crop = face_center - img.size[1] / 2
        right_edge_crop = face_center + img.size[1] / 2

        return img.crop(
            [left_edge_crop, 0, right_edge_crop, img.size[1]]
        )

    def _get_left_median(self, img):
        """Return the mean of the left side of the image."""
        width = img.size[0]
        height = img.size[1]

        left_most_points = []
        for y in range(int(height / 5), int(6 * height / 7)):
            row = [
                (img.getpixel((x, y)), x)
                for x in range(10, width / 2)
            ]
            sorted_row = sorted(row)

            left_most_points.append(sorted_row[-1][1])

        return int(np.median(left_most_points))

    def _get_right_median(self, img):
        """Return the mean of the right side of the image."""
        width = img.size[0]
        height = img.size[1]

{'fraction_non_alphanumeric': 0.13274336283185842, 'fraction_numerical': 0.011061946902654867, 'mean_word_length': 2.1355611601513242, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5050425', 'n_tokens_mistral': 1595, 'n_tokens_neox': 1526, 'n_words': 386}