Esempio n. 1
0
    def sort_face_yaw(self):
        """ Sort by estimated face yaw angle """
        logger.info("Sorting by estimated face yaw angle..")
        self._loader = FacesLoader(
            self._args.input_dir)  # TODO This should be set in init
        filenames = []
        yaws = []
        for filename, image, metadata in tqdm(self._loader.load(),
                                              desc="Classifying Faces...",
                                              total=self._loader.count,
                                              leave=False):
            if not metadata:
                msg = (
                    "The images to be sorted do not contain alignment data. Images must have "
                    "been generated by Faceswap's Extract process.\nIf you are sorting an "
                    "older faceset, then you should re-extract the faces from your source "
                    "alignments file to generate this data.")
                raise FaceswapError(msg)
            alignments = metadata["alignments"]
            aligned_face = AlignedFace(np.array(alignments["landmarks_xy"],
                                                dtype="float32"),
                                       image=image,
                                       centering="legacy",
                                       is_aligned=True)
            filenames.append(filename)
            yaws.append(aligned_face.pose.yaw)

        logger.info("Sorting...")
        matched_list = list(zip(filenames, yaws))
        img_list = sorted(matched_list,
                          key=operator.itemgetter(1),
                          reverse=True)
        return img_list
Esempio n. 2
0
    def sort_face(self):
        """ Sort by identity similarity """
        logger.info("Sorting by identity similarity...")
        self._loader = FacesLoader(
            self._args.input_dir)  # TODO This should be set in init
        filenames = []
        preds = []
        for filename, image, metadata in tqdm(self._loader.load(),
                                              desc="Classifying Faces...",
                                              total=self._loader.count,
                                              leave=False):
            if not metadata:
                msg = (
                    "The images to be sorted do not contain alignment data. Images must have "
                    "been generated by Faceswap's Extract process.\nIf you are sorting an "
                    "older faceset, then you should re-extract the faces from your source "
                    "alignments file to generate this data.")
                raise FaceswapError(msg)
            alignments = metadata["alignments"]
            face = AlignedFace(np.array(alignments["landmarks_xy"],
                                        dtype="float32"),
                               image=image,
                               centering="legacy",
                               size=self._vgg_face.input_size,
                               is_aligned=True).face
            filenames.append(filename)
            preds.append(self._vgg_face.predict(face))

        logger.info("Sorting by ward linkage...")

        indices = self._vgg_face.sorted_similarity(np.array(preds),
                                                   method="ward")
        img_list = np.array(filenames)[indices]
        return img_list
Esempio n. 3
0
    def sort_face(self):
        """ Sort by identity similarity """
        logger.info("Sorting by identity similarity...")

        self._loader = FacesLoader(self._args.input_dir)  # TODO This should be set in init
        ratio = _EXTRACT_RATIOS["legacy"] / _EXTRACT_RATIOS["head"]
        filenames = []
        preds = []
        no_hash = 0
        for filename, image, hsh in tqdm(self._loader.load(),
                                         desc="Classifying Faces...",
                                         total=self._loader.count):
            if self._alignments is not None and self._alignments.version != 1.0:
                face = self._alignments.hashes_to_alignment.get(hsh)
                if face:
                    image = AlignedFace(face["landmarks_xy"],
                                        image=image,
                                        centering="legacy",
                                        size=self._vgg_face.input_size,
                                        is_aligned=True).face
                elif image.shape[0] != image.shape[1]:
                    logger.warning("Skipping image '%s' as it is not square (probably not a "
                                   "face)", filename)
                    continue
                else:  # Center crop the image and add count to warning count
                    center = image.shape[0] // 2
                    crop = slice(center - int(center * ratio), center + int(center * ratio))
                    image = image[crop, crop, :]
                    no_hash += 1

            filenames.append(filename)
            preds.append(self._vgg_face.predict(image))

        logger.info("Sorting by ward linkage...")

        indices = self._vgg_face.sorted_similarity(np.array(preds), method="ward")
        img_list = np.array(filenames)[indices]

        if no_hash:
            logger.warning("%s image(s) were not found in the alignments file. This will likely "
                           "result in sub-par sorting results, so you should check the output "
                           "carefully", no_hash)

        return img_list
Esempio n. 4
0
 def __init__(self, arguments):
     self._args = arguments
     self.changes = None
     self.serializer = None
     self._vgg_face = None
     self._loader = FacesLoader(self._args.input_dir)