                  vram["device"],
                  int(vram["free"]),
                  int(vram["total"])))
          return int(vram["card_id"]), int(vram["free"]), int(vram["total"])
  
  
  class Extract():
      """ Based on the original https://www.reddit.com/r/deepfakes/
          code sample + contribs """
  
      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
  
      @staticmethod
      def transform(image, mat, size, padding=0):
          """ Transform Image """
          matrix = mat * (size - 2 * padding)
          matrix[:, 2] += padding
          return cv2.warpAffine(image, matrix, (size, size))
  
      @staticmethod
      def transform_points(points, mat, size, padding=0):
          """ Transform points along matrix """
          matrix = mat * (size - 2 * padding)
          matrix[:, 2] += padding
          points = np.expand_dims(points, axis=1)
          points = cv2.transform(points, matrix, points.shape)
          points = np.squeeze(points)
          return points
  
      @staticmethod
      def get_feature_mask(aligned_landmarks_68, size,
                           padding=0, dilation=30):
          """ Return the face feature mask """
          scale = size - 2*padding
          translation = padding
          pad_mat = np.matrix([[scale, 0.0, translation],
                               [0.0, scale, translation]])
          aligned_landmarks_68 = np.expand_dims(aligned_landmarks_68, axis=1)
          aligned_landmarks_68 = cv2.transform(aligned_landmarks_68,
                                               pad_mat,
                                               aligned_landmarks_68.shape)
          aligned_landmarks_68 = np.squeeze(aligned_landmarks_68)
  
          (l_start, l_end) = FACIAL_LANDMARKS_IDXS["left_eye"]
          (r_start, r_end) = FACIAL_LANDMARKS_IDXS["right_eye"]
          (m_start, m_end) = FACIAL_LANDMARKS_IDXS["mouth"]
          (n_start, n_end) = FACIAL_LANDMARKS_IDXS["nose"]
          (lb_start, lb_end) = FACIAL_LANDMARKS_IDXS["left_eyebrow"]
          (rb_start, rb_end) = FACIAL_LANDMARKS_IDXS["right_eyebrow"]
          (c_start, c_end) = FACIAL_LANDMARKS_IDXS["chin"]
  
          l_eye_points = aligned_landmarks_68[l_start:l_end].tolist()
          l_brow_points = aligned_landmarks_68[lb_start:lb_end].tolist()
          r_eye_points = aligned_landmarks_68[r_start:r_end].tolist()
          r_brow_points = aligned_landmarks_68[rb_start:rb_end].tolist()
          nose_points = aligned_landmarks_68[n_start:n_end].tolist()
          chin_points = aligned_landmarks_68[c_start:c_end].tolist()
          mouth_points = aligned_landmarks_68[m_start:m_end].tolist()
          l_eye_points = l_eye_points + l_brow_points
          r_eye_points = r_eye_points + r_brow_points
          mouth_points = mouth_points + nose_points + chin_points
  
          l_eye_hull = cv2.convexHull(np.array(l_eye_points).reshape(
              (-1, 2)).astype(int)).flatten().reshape((-1, 2))
          r_eye_hull = cv2.convexHull(np.array(r_eye_points).reshape(
              (-1, 2)).astype(int)).flatten().reshape((-1, 2))
          mouth_hull = cv2.convexHull(np.array(mouth_points).reshape(
              (-1, 2)).astype(int)).flatten().reshape((-1, 2))
  
          mask = np.zeros((size, size, 3), dtype=float)
          cv2.fillConvexPoly(mask, l_eye_hull, (1, 1, 1))
          cv2.fillConvexPoly(mask, r_eye_hull, (1, 1, 1))
          cv2.fillConvexPoly(mask, mouth_hull, (1, 1, 1))
  
          if dilation > 0:
              kernel = np.ones((dilation, dilation), np.uint8)
              mask = cv2.dilate(mask, kernel, iterations=1)
  
          return mask
 
</s> remove         return self.extractor.extract(image, face, self.size, self.align_eyes)

    def original_roi(self, matrix):
        """ Return the original ROI of an extracted face """
        points = np.array([[0, 0], [0, self.size - 1],
                           [self.size - 1, self.size - 1],
                           [self.size - 1, 0]], np.int32)
        points = points.reshape((-1, 1, 2))
 </s> add         face.load_aligned(image,
                          size=self.size,
                          padding=self.padding,
                          align_eyes=self.align_eyes)
        return face </s> remove         mat = matrix * (self.size - 2 * self.padding)
        mat[:, 2] += self.padding
        mat = cv2.invertAffineTransform(mat)
        return [cv2.transform(points, mat)]

    def get_faces_for_frame(self, frame, update=False):
 </s> add     def get_faces_in_frame(self, frame, update=False): </s> remove     def smooth_mask(self,img_old,img_new):
        w,h,c = img_new.shape
        crop = slice(0,w)
        mask = numpy.zeros_like(img_new)
        mask[h//15:-h//15,w//15:-w//15,:] = 255
        mask = cv2.GaussianBlur(mask,(15,15),10)
        img_new[crop,crop] = mask/255*img_new + (1-mask/255)*img_old
 </s> add     @staticmethod
    def smooth_mask(old_face, new_face):
        """ Smooth the mask """
        width, height, _ = new_face.shape
        crop = slice(0, width)
        mask = np.zeros_like(new_face)
        mask[height // 15:-height // 15, width // 15:-width // 15, :] = 255
        mask = cv2.GaussianBlur(mask,  # pylint: disable=no-member
                                (15, 15),
                                10)
        new_face[crop, crop] = (mask / 255 * new_face +
                                (1 - mask / 255) * old_face) </s> remove     mat_umeyama = umeyama(numpy.array(face.landmarks_as_xy()[17:]), landmarks_2D, True)[0:2]
 </s> add     """ Return the alignment Matrix """
    mat_umeyama = umeyama(np.array(face.landmarks_as_xy()[17:]),
                          LANDMARKS_2D,
                          True)[0:2]
 </s> remove         for idx, face in enumerate(output_item["detected_faces"]):
            resized_face = output_item["resized_faces"][idx]
            dims = resized_face.shape[:2]
            size = dims[0]
            t_mat = output_item["t_mats"][idx]

            aligned_landmarks = extractor.transform_points(
                face.landmarksXY,
                t_mat, size, 48)
 </s> add         for face in output_item["detected_faces"]:
            aligned_landmarks = face.aligned_landmarks
            resized_face = face.aligned_face
            size = face.aligned["size"]
                  vram["device"],
                  int(vram["free"]),
                  int(vram["total"])))
          return int(vram["card_id"]), int(vram["free"]), int(vram["total"])















































































keep keep keep keep replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace
 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 
plugins/extract/align/_base.py