Пример #1
0
    def load_aligned(self, image, size=256, align_eyes=False, dtype=None):
        """ No need to load aligned information for all uses of this
            class, so only call this to load the information for easy
            reference to aligned properties for this face """
        # Don't reload an already aligned face:
        if self.aligned:
            logger.trace(
                "Skipping alignment calculation for already aligned face")
        else:
            logger.trace(
                "Loading aligned face: (size: %s, align_eyes: %s, dtype: %s)",
                size, align_eyes, dtype)
            padding = int(size * self.extract_ratio) // 2
            self.aligned["size"] = size
            self.aligned["padding"] = padding
            self.aligned["align_eyes"] = align_eyes
            self.aligned["matrix"] = get_align_mat(self, size, align_eyes)
            self.aligned["face"] = None
        if image is not None and self.aligned["face"] is None:
            logger.trace("Getting aligned face")
            face = AlignerExtract().transform(image, self.aligned["matrix"],
                                              size, padding)
            self.aligned["face"] = face if dtype is None else face.astype(
                dtype)

        logger.trace(
            "Loaded aligned face: %s",
            {key: val
             for key, val in self.aligned.items() if key != "face"})
Пример #2
0
    def load_reference_face(self,
                            image,
                            size=64,
                            coverage_ratio=0.625,
                            dtype=None):
        """ Return a face in the correct dimensions for reference to the output from a NN

            Coverage ratio should be the ratio of the extracted image that was used for
            training """
        logger.trace(
            "Loading reference face: (size: %s, coverage_ratio: %s, dtype: %s)",
            size, coverage_ratio, dtype)

        self.reference["size"] = size
        self.reference["padding"] = self.padding_from_coverage(
            size, coverage_ratio)
        self.reference["matrix"] = get_align_mat(self,
                                                 size,
                                                 should_align_eyes=False)

        face = np.clip(
            AlignerExtract().transform(image, self.reference["matrix"], size,
                                       self.reference["padding"])[:, :, :3] /
            255.0, 0.0, 1.0)
        self.reference["face"] = face if dtype is None else face.astype(dtype)

        logger.trace("Loaded reference face. (face_shape: %s, matrix: %s)",
                     self.reference_face.shape, self.reference_matrix)
Пример #3
0
    def load_feed_face(self, image, size=64, coverage_ratio=0.625, dtype=None):
        """ Align a face in the correct dimensions for feeding into a model.

        Parameters
        ----------
        image: numpy.ndarray
            The image that contains the face to be aligned
        size: int
            The size of the face in pixels to be fed into the model
        coverage_ratio: float, optional
            the ratio of the extracted image that was used for training. Default: `0.625`
        dtype: str, optional
            Optionally set a ``dtype`` for the final face to be formatted in. Default: ``None``

        Notes
        -----
        This method must be executed to get access to the following `properties`:
            - :func:`feed_face`
            - :func:`feed_interpolators`
        """
        logger.trace("Loading feed face: (size: %s, coverage_ratio: %s, dtype: %s)",
                     size, coverage_ratio, dtype)

        self.feed["size"] = size
        self.feed["padding"] = self._padding_from_coverage(size, coverage_ratio)
        self.feed["matrix"] = get_align_mat(self)

        face = AlignerExtract().transform(image, self.feed["matrix"], size, self.feed["padding"])
        face = np.clip(face[:, :, :3] / 255., 0., 1.)
        self.feed["face"] = face if dtype is None else face.astype(dtype)

        logger.trace("Loaded feed face. (face_shape: %s, matrix: %s)",
                     self.feed_face.shape, self._feed_matrix)
Пример #4
0
    def extract(self, image, face, size):
        if face.landmarks == None:
            print("Warning! landmarks not found. Switching to crop!")
            return cv2.resize(face.image, (size, size))

        alignment = get_align_mat( face )
        return self.transform( image, alignment, size, 48 )
Пример #5
0
    def extract(self, image, face, size):
        if face.landmarks == None:
            print("Warning! landmarks not found. Switching to crop!")
            return cv2.resize(face.image, (size, size))

        alignment = get_align_mat(face)
        return self.transform(image, alignment, size, 48)
Пример #6
0
 def load_aligned(self, image, size=256, padding=48, align_eyes=False):
     """ No need to load aligned information for all uses of this
         class, so only call this to load the information for easy
         reference to aligned properties for this face """
     self.aligned["size"] = size
     self.aligned["padding"] = padding
     self.aligned["align_eyes"] = align_eyes
     self.aligned["matrix"] = get_align_mat(self, size, align_eyes)
     self.aligned["face"] = AlignerExtract().transform(
         image, self.aligned["matrix"], size, padding)
Пример #7
0
    def load_aligned(self, image, size=256, align_eyes=False, dtype=None):
        """ Align a face from a given image.

        Aligning a face is a relatively expensive task and is not required for all uses of
        the :class:`~lib.faces_detect.DetectedFace` object, so call this function explicitly to
        load an aligned face.

        This method plugs into :mod:`lib.aligner` to perform face alignment based on this face's
        ``landmarks_xy``. If the face has already been aligned, then this function will return
        having performed no action.

        Parameters
        ----------
        image: numpy.ndarray
            The image that contains the face to be aligned
        size: int
            The size of the output face in pixels
        align_eyes: bool, optional
            Optionally perform additional alignment to align eyes. Default: `False`
        dtype: str, optional
            Optionally set a ``dtype`` for the final face to be formatted in. Default: ``None``

        Notes
        -----
        This method must be executed to get access to the following `properties`:
            - :func:`original_roi`
            - :func:`aligned_landmarks`
            - :func:`aligned_face`
            - :func:`adjusted_interpolators`
        """
        if self.aligned:
            # Don't reload an already aligned face
            logger.trace(
                "Skipping alignment calculation for already aligned face")
        else:
            logger.trace(
                "Loading aligned face: (size: %s, align_eyes: %s, dtype: %s)",
                size, align_eyes, dtype)
            padding = int(size * self._extract_ratio) // 2
            self.aligned["size"] = size
            self.aligned["padding"] = padding
            self.aligned["align_eyes"] = align_eyes
            self.aligned["matrix"] = get_align_mat(self, size, align_eyes)
            self.aligned["face"] = None
        if image is not None and self.aligned["face"] is None:
            logger.trace("Getting aligned face")
            face = AlignerExtract().transform(image, self.aligned["matrix"],
                                              size, padding)
            self.aligned["face"] = face if dtype is None else face.astype(
                dtype)

        logger.trace(
            "Loaded aligned face: %s",
            {key: val
             for key, val in self.aligned.items() if key != "face"})
Пример #8
0
    def patch_image( self, image, face_detected):
        size = 64
        image_size = image.shape[1], image.shape[0]

        mat = numpy.array(get_align_mat(face_detected)).reshape(2,3) * size

        new_face = self.get_new_face(image,mat,size)

        image_mask = self.get_image_mask( image, new_face, face_detected, mat, image_size )

        return self.apply_new_face(image, new_face, image_mask, mat, image_size, size)
Пример #9
0
    def patch_image( self, image, face_detected ):
        size = 64
        image_size = image.shape[1], image.shape[0]

        mat = numpy.array(get_align_mat(face_detected)).reshape(2,3) * size

        new_face = self.get_new_face(image,mat,size)

        image_mask = self.get_image_mask( image, new_face, face_detected, mat, image_size )

        return self.apply_new_face(image, new_face, image_mask, mat, image_size, size)
Пример #10
0
    def load_feed_face(self,
                       image,
                       size=64,
                       coverage_ratio=0.625,
                       dtype=None,
                       is_aligned_face=False):
        """ Align a face in the correct dimensions for feeding into a model.

        Parameters
        ----------
        image: numpy.ndarray
            The image that contains the face to be aligned
        size: int
            The size of the face in pixels to be fed into the model
        coverage_ratio: float, optional
            the ratio of the extracted image that was used for training. Default: `0.625`
        dtype: str, optional
            Optionally set a ``dtype`` for the final face to be formatted in. Default: ``None``
        is_aligned_face: bool, optional
            Indicates that the :attr:`image` is an aligned face rather than a frame.
            Default: ``False``

        Notes
        -----
        This method must be executed to get access to the following `properties`:
            - :func:`feed_face`
            - :func:`feed_interpolators`
        """
        logger.trace(
            "Loading feed face: (size: %s, coverage_ratio: %s, dtype: %s, "
            "is_aligned_face: %s)", size, coverage_ratio, dtype,
            is_aligned_face)

        self.feed["size"] = size
        self.feed["padding"] = self._padding_from_coverage(
            size, coverage_ratio)
        self.feed["matrix"] = get_align_mat(self)
        if is_aligned_face:
            original_size = image.shape[0]
            interp = cv2.INTER_CUBIC if original_size < size else cv2.INTER_AREA
            face = cv2.resize(image, (size, size), interpolation=interp)
        else:
            face = AlignerExtract().transform(image, self.feed["matrix"], size,
                                              self.feed["padding"])
        self.feed["face"] = face if dtype is None else face.astype(dtype)

        logger.trace("Loaded feed face. (face_shape: %s, matrix: %s)",
                     self.feed_face.shape, self.feed_matrix)
Пример #11
0
 def load_aligned(self, image, size=256, padding=48, align_eyes=False):
     """ No need to load aligned information for all uses of this
         class, so only call this to load the information for easy
         reference to aligned properties for this face """
     logger.trace(
         "Loading aligned face: (size: %s, padding: %s, align_eyes: %s)",
         size, padding, align_eyes)
     self.aligned["size"] = size
     self.aligned["padding"] = padding
     self.aligned["align_eyes"] = align_eyes
     self.aligned["matrix"] = get_align_mat(self, size, align_eyes)
     self.aligned["face"] = AlignerExtract().transform(
         image, self.aligned["matrix"], size, padding)
     logger.trace(
         "Loaded aligned face: %s",
         {key: val
          for key, val in self.aligned.items() if key != "face"})
Пример #12
0
    def patch_image(self, image, face_detected, size):

        image_size = image.shape[1], image.shape[0]

        mat = numpy.array(
            get_align_mat(face_detected, size,
                          should_align_eyes=False)).reshape(2, 3)
        mat = mat * size

        new_face = self.get_new_face(image, mat, size)

        image_mask = self.get_image_mask(image, new_face,
                                         face_detected.landmarksAsXY(), mat,
                                         image_size)

        return self.apply_new_face(image, new_face, image_mask, mat,
                                   image_size, size)
Пример #13
0
    def patch_image( self, image, face_detected, size ):

        image_size = image.shape[1], image.shape[0]

        mat = numpy.array(get_align_mat(face_detected, size, should_align_eyes=False)).reshape(2,3)

        if "GAN" not in self.trainer:
            mat = mat * size
        else:
            padding = int(48/256*size)
            mat = mat * (size - 2 * padding)
            mat[:,2] += padding

        new_face = self.get_new_face(image,mat,size)

        image_mask = self.get_image_mask( image, new_face, face_detected.landmarks_as_xy(), mat, image_size )

        return self.apply_new_face(image, new_face, image_mask, mat, image_size, size)
Пример #14
0
    def load_reference_face(self, image, size=64, coverage_ratio=0.625, dtype=None):
        """ Return a face in the correct dimensions for reference to the output from a NN

            Coverage ratio should be the ratio of the extracted image that was used for
            training """
        logger.trace("Loading reference face: (size: %s, coverage_ratio: %s, dtype: %s)",
                     size, coverage_ratio, dtype)

        self.reference["size"] = size
        self.reference["padding"] = self.padding_from_coverage(size, coverage_ratio)
        self.reference["matrix"] = get_align_mat(self, size, should_align_eyes=False)

        face = np.clip(AlignerExtract().transform(image,
                                                  self.reference["matrix"],
                                                  size,
                                                  self.reference["padding"])[:, :, :3] / 255.0,
                       0.0, 1.0)
        self.reference["face"] = face if dtype is None else face.astype(dtype)

        logger.trace("Loaded reference face. (face_shape: %s, matrix: %s)",
                     self.reference_face.shape, self.reference_matrix)
Пример #15
0
    def patch_image(self, image, face_detected, size):

        image_size = image.shape[1], image.shape[0]

        mat = numpy.array(
            get_align_mat(face_detected, size,
                          should_align_eyes=False)).reshape(2, 3)

        if "GAN" not in self.trainer:
            mat = mat * size
        else:
            padding = int(48 / 256 * size)
            mat = mat * (size - 2 * padding)
            mat[:, 2] += padding

        new_face = self.get_new_face(image, mat, size)

        image_mask = self.get_image_mask(image, new_face,
                                         face_detected.landmarks_as_xy(), mat,
                                         image_size)

        return self.apply_new_face(image, new_face, image_mask, mat,
                                   image_size, size)
Пример #16
0
    def load_aligned(self, image, size=256, align_eyes=False, dtype=None):
        """ No need to load aligned information for all uses of this
            class, so only call this to load the information for easy
            reference to aligned properties for this face """
        logger.trace("Loading aligned face: (size: %s, align_eyes: %s, dtype: %s)",
                     size, align_eyes, dtype)
        padding = int(size * self.extract_ratio) // 2
        self.aligned["size"] = size
        self.aligned["padding"] = padding
        self.aligned["align_eyes"] = align_eyes
        self.aligned["matrix"] = get_align_mat(self, size, align_eyes)
        if image is None:
            self.aligned["face"] = None
        else:
            face = AlignerExtract().transform(
                image,
                self.aligned["matrix"],
                size,
                padding)
            self.aligned["face"] = face if dtype is None else face.astype(dtype)

        logger.trace("Loaded aligned face: %s", {key: val
                                                 for key, val in self.aligned.items()
                                                 if key != "face"})
Пример #17
0
 def extract(self, image, face, size, align_eyes):
     """ Extract a face from an image """
     alignment = get_align_mat(face, size, align_eyes)
     extracted = self.transform(image, alignment, size, 48)
     return extracted, alignment
Пример #18
0
    def extract(self, image, face, size):
        if face.landmarks == None:
            return cv2.resize(face.image, (size, size))

        alignment = get_align_mat(face)
        return self.transform(image, alignment, size, 48)
Пример #19
0
 def extract(self, image, face, size):
     alignment = get_align_mat(face)
     return self.transform(image, alignment, size, 48)
Пример #20
0
 def extract(self, image, face, size, align_eyes):
     alignment = get_align_mat(face, size, align_eyes)
     extracted = self.transform(image, alignment, size, 48)
     return extracted, alignment