  
      def reindex_faces(self):
          """ Re-Index the faces """
          reindexed = 0
          for alignment in tqdm(self.alignments.get_alignments_one_image(),
                                desc="Sort alignment indexes",
                                total=self.alignments.count):
              frame, alignments, count, key = alignment
              if count <= 1:
                  continue
 
</s> remove                               total=self.alignments.count):
 </s> add                               total=self.alignments.frames_count): </s> remove         processor = self.alignments.get_one_alignment_index_reverse
 </s> add         processor = self.alignments.yield_original_index_reverse </s> remove         count = sum(1 for val in self.alignments.alignments.values() if val)
 </s> add         count = sum(1 for val in self.alignments.data.values() if val) </s> remove         dst = os.path.splitext(self.file)[0]
        dst += ".{}".format(self.serializer.ext)
        self.backup_alignments()

        print("Saving alignments to {}".format(dst))
        with open(dst, self.serializer.woptions) as align:
            align.write(self.serializer.marshal(self.alignments))

    def backup_alignments(self):
        """ Backup copy of old alignments """
        if not os.path.isfile(self.file):
            return
        now = datetime.now().strftime("%Y%m%d_%H%M%S")
        src = self.file
        dst = src.split(".")
        dst[0] += "_" + now + "."
        dst = dst[0] + dst[1]
        print("Backing up original alignments to {}".format(dst))
        os.rename(src, dst)

    def get_alignments_one_image(self):
        """ Return the face alignments for one image """
        for frame_fullname, alignments in self.alignments.items():
            frame_name = frame_fullname[:frame_fullname.rindex(".")]
            number_alignments = len(alignments)
            yield frame_name, alignments, number_alignments, frame_fullname

    @staticmethod
    def get_one_alignment_index_reverse(image_alignments, number_alignments):
        """ Return the correct original index for
            alignment in reverse order """
        for idx, _ in enumerate(reversed(image_alignments)):
            original_idx = number_alignments - 1 - idx
            yield original_idx

    def get_alignments_for_frame(self, frame):
        """ Return the alignments for the selected frame """
        return self.alignments.get(frame, list())

    def frame_in_alignments(self, frame):
        """ Return true if frame exists in alignments file """
        return bool(self.alignments.get(frame, -1) != -1)

    def frame_has_faces(self, frame):
        """ Return true if frame exists and has faces """
        return bool(self.alignments.get(frame, list()))

    def frame_has_multiple_faces(self, frame):
        """ Return true if frame exists and has faces """
        if not frame:
            return False
        return bool(len(self.alignments.get(frame, list())) > 1)

    def get_full_frame_name(self, frame):
        """ Return a frame with extension for when the extension is
            not known """
        return next(key for key in self.alignments.keys()
                    if key.startswith(frame))

    def count_alignments_in_frame(self, frame):
        """ Return number of alignments within frame """
        return len(self.alignments.get(frame, list()))

    def delete_alignment_at_index(self, frame, idx):
        """ Delete the face alignment for given frame at given index """
        idx = int(idx)
        if idx + 1 > self.count_alignments_in_frame(frame):
            return False
        del self.alignments[frame][idx]
        return True

    def add_alignment(self, frame, alignment):
        """ Add a new alignment for a frame and return it's index """
        self.alignments[frame].append(alignment)
        return self.count_alignments_in_frame(frame) - 1

    def update_alignment(self, frame, idx, alignment):
        """ Replace an alignment for given frame and index """
        self.alignments[frame][idx] = alignment

    def get_rotated(self):
        """ Return list of keys for alignments containing
            rotated frames """
        keys = list()
        for key, val in self.alignments.items():
            if any(alignment.get("r", None) for alignment in val):
                keys.append(key)
        return keys

    def rotate_existing_landmarks(self, frame, dimensions):
        """ Backwards compatability fix. Rotates the landmarks to
            their correct position and sets r to 0 """
        for alignment in self.alignments.get(frame, list()):
            angle = alignment.get("r", 0)
            if not angle:
                return
            rotation_matrix = self.get_original_rotation_matrix(dimensions,
                                                                angle)
            rotate_landmarks(alignment, rotation_matrix)
            del alignment["r"]

    @staticmethod
    def get_original_rotation_matrix(dimensions, angle):
        """ Calculate original rotation matrix and invert """
        height, width = dimensions
        center = (width/2, height/2)
        rotation_matrix = cv2.getRotationMatrix2D(center, -1.0*angle, 1.)

        abs_cos = abs(rotation_matrix[0, 0])
        abs_sin = abs(rotation_matrix[0, 1])
        rotated_width = int(height*abs_sin + width*abs_cos)
        rotated_height = int(height*abs_cos + width*abs_sin)
        rotation_matrix[0, 2] += rotated_width/2 - center[0]
        rotation_matrix[1, 2] += rotated_height/2 - center[1]

        return rotation_matrix
 </s> add         self.backup()
        super().save() </s> remove         extracted = self.extracted_faces
        size = extracted.size

        faces = extracted.get_faces_for_frame(frame)

        landmarks_xy = [alignment["landmarksXY"] for alignment in alignments]
        landmarks = [
            {"landmarksXY": aligned}
            for aligned
            in extracted.get_aligned_landmarks_for_frame(frame, landmarks_xy)]

        return FacesDisplay(faces, landmarks, size, self.interface)
 </s> add         faces = self.extracted_faces.get_faces_in_frame(frame)
        landmarks = [{"landmarksXY": face.aligned_landmarks}
                     for face in self.extracted_faces.faces]
        return FacesDisplay(faces,
                            landmarks,
                            self.extracted_faces.size,
                            self.interface)
        for alignment in tqdm(self.alignments.yield_faces(),
  
      def reindex_faces(self):
          """ Re-Index the faces """
          reindexed = 0
        for alignment in tqdm(self.alignments.yield_faces(),
                                desc="Sort alignment indexes",
                                total=self.alignments.count):
              frame, alignments, count, key = alignment
              if count <= 1:
                  continue
keep keep keep keep replace keep keep keep keep keep
 Fix convert-adjust (#531)

* Add dimensions to alignments + refactor

* Add frame_dims + funcs to DetectFaces. Add alignments lib

* Convert Adjust working

* Refactor and tidy up 
tools/lib_alignments/jobs.py