Example #1
0
 def alignment_dict(image):
     """ Set the image to a dict for alignment """
     height, width = image.shape[:2]
     face = DetectedFace(x=0, w=width, y=0, h=height)
     face = face.to_bounding_box()
     return {"image": image,
             "detected_faces": [face]}
Example #2
0
    def _alignments_faces(self, frame_name, image):
        """ Return detected faces from an alignments file.

        Parameters
        ----------
        frame_name: str
            The name of the frame to return the detected faces for
        image: :class:`numpy.ndarray`
            The frame that the detected faces exist in

        Returns
        -------
        list
            List of :class:`lib.faces_detect.DetectedFace` objects
        """
        if not self._check_alignments(frame_name):
            return list()

        faces = self._alignments.get_faces_in_frame(frame_name)
        detected_faces = list()

        for rawface in faces:
            face = DetectedFace()
            face.from_alignment(rawface, image=image)
            detected_faces.append(face)
        return detected_faces
Example #3
0
 def alignment_dict(image):
     """ Set the image to a dict for alignment """
     height, width = image.shape[:2]
     face = DetectedFace(x=0, w=width, y=0, h=height)
     face = face.to_bounding_box()
     return {"image": image,
             "detected_faces": [face]}
Example #4
0
 def extract_one_face(self, alignment, image):
     """ Extract one face from image """
     logger.trace("Extracting one face: (frame: '%s', alignment: %s)",
                  self.current_frame, alignment)
     face = DetectedFace()
     face.from_alignment(alignment, image=image)
     face.load_aligned(image, size=self.size, align_eyes=self.align_eyes)
     return face
Example #5
0
 def load(self):
     """ Load the faces from the alignments file, convert to
     :class:`~lib.faces_detect.DetectedFace`. objects and add to :attr:`_frame_faces`. """
     for key in sorted(self._alignments.data):
         this_frame_faces = []
         for item in self._alignments.data[key]["faces"]:
             face = DetectedFace()
             face.from_alignment(item, with_thumb=True)
             this_frame_faces.append(face)
         self._frame_faces.append(this_frame_faces)
Example #6
0
    def alignments_faces(self, frame, image):
        """ Get the face from alignments file """
        if not self.check_alignments(frame):
            return list()

        faces = self.alignments.get_faces_in_frame(frame)
        detected_faces = list()

        for rawface in faces:
            face = DetectedFace()
            face.from_alignment(rawface, image=image)
            detected_faces.append(face)
        return detected_faces
Example #7
0
    def alignments_faces(self, frame, image):
        """ Get the face from alignments file """
        if not self.check_alignments(frame):
            return list()

        faces = self.alignments.get_faces_in_frame(frame)
        detected_faces = list()

        for rawface in faces:
            face = DetectedFace()
            face.from_alignment(rawface, image=image)
            detected_faces.append(face)
        return detected_faces
Example #8
0
    def alignments_faces(self, filename, frame):
        """ Get the face from alignments file """
        if not self.check_alignments(frame):
            return None

        faces = self.alignments.get_alignments_for_frame(frame)
        image = self.images.load_one_image(filename)
        detected_faces = list()

        for rawface in faces:
            face = DetectedFace()
            face.from_alignment(rawface, image=image)
            detected_faces.append(face)
        return image, detected_faces
Example #9
0
    def get_faces_alignments(self, filename, image):
        faces_count = 0
        faces = self.faces_detected[os.path.basename(filename)]
        for rawface in faces:
            face = DetectedFace(**rawface)
            face.image = image[face.y:face.y + face.h, face.x:face.x + face.w]
            if self.filter is not None and not self.filter.check(face):
                print('Skipping not recognized face!')
                continue

            yield faces_count, face
            self.num_faces_detected += 1
            faces_count += 1
        if faces_count > 1 and self.arguments.verbose:
            print('Note: Found more than one face in an image!')
            self.verify_output = True
Example #10
0
 def alignment_dict(filename, image):
     """ Set the image to a dict for alignment """
     height, width = image.shape[:2]
     face = DetectedFace(x=0, w=width, y=0, h=height)
     return {"image": image,
             "filename": filename,
             "detected_faces": [face]}
Example #11
0
    def _get_detected_face(alignment):
        """ Convert an alignment dict item to a detected_face object

        Parameters
        ----------
        alignment: dict
            The alignment dict for a face

        Returns
        -------
        :class:`lib.FacesDetect.detected_face`:
            The corresponding detected_face object for the alignment
        """
        detected_face = DetectedFace()
        detected_face.from_alignment(alignment)
        return detected_face
Example #12
0
 def load_frames(self):
     """ Load a sample of random frames """
     self.input_images = list()
     for selection in self.random_choice:
         filename = os.path.basename(self.filelist[selection])
         image = self.images.load_one_image(self.filelist[selection])
         # Get first face only
         face = self.alignments.get_faces_in_frame(filename)[0]
         detected_face = DetectedFace()
         detected_face.from_alignment(face, image=image)
         self.input_images.append({"filename": filename,
                                   "image": image,
                                   "detected_faces": [detected_face]})
     self.display.source = self.input_images
     self.display.update_source = True
     logger.debug("Selected frames: %s", [frame["filename"] for frame in self.input_images])
Example #13
0
    def get_faces_alignments(self, filename, image):
        faces_count = 0
        faces = self.faces_detected[os.path.basename(filename)]
        for rawface in faces:
            face = DetectedFace(**rawface)
            face.image = image[face.y : face.y + face.h, face.x : face.x + face.w]
            if self.filter is not None and not self.filter.check(face):
                print('Skipping not recognized face!')
                continue

            yield faces_count, face
            self.num_faces_detected += 1
            faces_count += 1
        if faces_count > 1 and self.arguments.verbose:
            print('Note: Found more than one face in an image!')
            self.verify_output = True
Example #14
0
    def detect_faces(self, filename, image):
        """ Extract the face from a frame (If alignments file not found) """
        inp = {"filename": filename, "image": image}
        self.extractor.input_queue.put(inp)
        faces = next(self.extractor.detected_faces())

        landmarks = faces["landmarks"]
        detected_faces = faces["detected_faces"]
        final_faces = list()

        for idx, face in enumerate(detected_faces):
            detected_face = DetectedFace()
            detected_face.from_bounding_box(face)
            detected_face.landmarksXY = landmarks[idx]
            final_faces.append(detected_face)
        return final_faces
Example #15
0
    def detect_faces(self, filename, image):
        """ Extract the face from a frame (If alignments file not found) """
        inp = {"filename": filename,
               "image": image}
        self.extractor.input_queue.put(inp)
        faces = next(self.extractor.detected_faces())

        landmarks = faces["landmarks"]
        detected_faces = faces["detected_faces"]
        final_faces = list()

        for idx, face in enumerate(detected_faces):
            detected_face = DetectedFace()
            detected_face.from_bounding_box(face)
            detected_face.landmarksXY = landmarks[idx]
            final_faces.append(detected_face)
        return final_faces
Example #16
0
    def get_faces_alignments(self, filename, image):
        """ Retrieve the face alignments from an image """
        faces_count = 0
        faces = self.faces_detected[os.path.basename(filename)]
        for rawface in faces:
            face = DetectedFace(**rawface)
            # Rotate the image if necessary
            if face.r != 0:
                image = Utils.rotate_image_by_angle(image, face.r)
            face.image = image[face.y : face.y + face.h, face.x : face.x + face.w]
            if self.filter and not self.filter.check(face):
                if self.args.verbose:
                    print("Skipping not recognized face!")
                continue

            yield faces_count, face
            self.num_faces_detected += 1
            faces_count += 1
        if faces_count > 1 and self.args.verbose:
            print("Note: Found more than one face in an image! File: %s" % filename)
            self.verify_output = True
Example #17
0
    def get_faces_alignments(self, filename, image):
        """ Retrieve the face alignments from an image """
        faces_count = 0
        faces = self.faces_detected[os.path.basename(filename)]
        for rawface in faces:
            face = DetectedFace(**rawface)
            # Rotate the image if necessary
            if face.r != 0:
                image = Utils.rotate_image_by_angle(image, face.r)
            face.image = image[face.y : face.y + face.h, face.x : face.x + face.w]
            if self.filter and not self.filter.check(face):
                if self.args.verbose:
                    print("Skipping not recognized face!")
                continue

            yield faces_count, face
            self.num_faces_detected += 1
            faces_count += 1
        if faces_count > 1 and self.args.verbose:
            print("Note: Found more than one face in an image! File: {}".format(filename))
            self.verify_output = True
Example #18
0
 def extract_one_face(self, alignment, image):
     """ Extract one face from image """
     logger.trace("Extracting one face: (frame: '%s', alignment: %s)",
                  self.current_frame, alignment)
     face = DetectedFace()
     face.from_alignment(alignment, image=image)
     face.load_aligned(image, size=self.size, align_eyes=self.align_eyes)
     return face
Example #19
0
 def _add_remove_faces(cls, alignments, faces):
     """ On a revert, ensure that the alignments and detected face object counts for each frame
     are in sync. """
     num_alignments = len(alignments)
     num_faces = len(faces)
     if num_alignments == num_faces:
         retval = False
     elif num_alignments > num_faces:
         faces.extend([DetectedFace() for _ in range(num_faces, num_alignments)])
         retval = True
     else:
         del faces[num_alignments:]
         retval = True
     return retval
Example #20
0
 def _convert_frame(frame, convert_colors=True):
     if convert_colors:
         frame = cv2.cvtColor(
             frame,
             cv2.COLOR_BGR2RGB)  # Swap RGB to BGR to work with OpenCV
     for face in DetectedFace(frame, "cnn"):
         if (not face_filter) or (face_filter and filter.check(face)):
             frame = converter.patch_image(frame, face)
             frame = frame.astype(numpy.float32)
     if convert_colors:
         frame = cv2.cvtColor(
             frame,
             cv2.COLOR_BGR2RGB)  # Swap RGB to BGR to work with OpenCV
     return frame
Example #21
0
 def initialize(self):
     """ Update changed parameters """
     frame = self.interface.get_frame_name()
     if frame == self.media["frame_id"]:
         return
     logger.debug("Initialize frame: '%s'", frame)
     self.media["frame_id"] = frame
     self.media["image"] = self.frames.load_image(frame)
     self.dims = None
     self.center = None
     self.last_move = None
     self.mouse_state = None
     self.media["bounding_box"] = DetectedFace()
     self.media["bounding_box_orig"] = None
Example #22
0
File: media.py Project: aejot/fs
 def extract_one_face(self, alignment, image):
     """ Extract one face from image """
     face = DetectedFace()
     face.from_alignment(alignment, image=image)
     face.load_aligned(image,
                       size=self.size,
                       padding=self.padding,
                       align_eyes=self.align_eyes)
     return face
Example #23
0
 def align_face(faces, align_eyes, size, padding=48):
     """ Align the detected face """
     final_faces = list()
     image = faces["image"]
     landmarks = faces["landmarks"]
     detected_faces = faces["detected_faces"]
     for idx, face in enumerate(detected_faces):
         detected_face = DetectedFace()
         detected_face.from_dlib_rect(face, image)
         detected_face.landmarksXY = landmarks[idx]
         detected_face.frame_dims = image.shape[:2]
         detected_face.load_aligned(image,
                                    size=size,
                                    padding=padding,
                                    align_eyes=align_eyes)
         final_faces.append(detected_face)
     faces["detected_faces"] = final_faces
Example #24
0
    def get_faces_alignments(self, filename, image):
        """ Retrieve the face alignments from an image """
        faces_count = 0
        faces = self.faces_detected[os.path.basename(filename)]
        for rawface in faces:
            face = DetectedFace(**rawface)
            # Rotate the image if necessary
            # NB: Rotation of landmarks now occurs at extract stage
            # This is here for legacy alignments
            if face.r != 0:
                image, _ = rotate_image_by_angle(image, face.r)
            face.image = image[face.y:face.y + face.h, face.x:face.x + face.w]
            if self.filter and not self.filter.check(face):
                if self.args.verbose:
                    print("Skipping not recognized face!")
                continue

            yield faces_count, face
            self.num_faces_detected += 1
            faces_count += 1
        if faces_count > 1 and self.args.verbose:
            print("Note: Found more than one face in "
                  "an image! File: {}".format(filename))
            self.verify_output = True
Example #25
0
 def transform_landmarks(self, alignments):
     """ For each face transform landmarks and return """
     landmarks = dict()
     for _, faces, _, _ in alignments.yield_faces():
         for face in faces:
             detected_face = DetectedFace()
             detected_face.from_alignment(face)
             detected_face.load_aligned(None, size=self.size, align_eyes=False)
             landmarks[detected_face.hash] = detected_face.aligned_landmarks
     return landmarks
Example #26
0
 def align_face(self, faces, align_eyes, size, filename):
     """ Align the detected face and add the destination file path """
     final_faces = list()
     image = faces["image"]
     landmarks = faces["landmarks"]
     detected_faces = faces["detected_faces"]
     for idx, face in enumerate(detected_faces):
         detected_face = DetectedFace()
         detected_face.from_dlib_rect(face, image)
         detected_face.landmarksXY = landmarks[idx]
         detected_face.load_aligned(image, size=size, align_eyes=align_eyes)
         final_faces.append({"file_location": self.output_dir / Path(filename).stem,
                             "face": detected_face})
     faces["detected_faces"] = final_faces
Example #27
0
    def load_aligned_face(self):
        """ Align the faces for vgg_face input """
        for filename, face in self.filters.items():
            logger.debug("Loading aligned face: '%s'", filename)
            bounding_box = face["detected_faces"][0]
            image = face["image"]
            landmarks = face["landmarks"][0]

            detected_face = DetectedFace()
            detected_face.from_bounding_box_dict(bounding_box, image)
            detected_face.landmarksXY = landmarks
            detected_face.load_aligned(image, size=224)
            face["face"] = detected_face.aligned_face
            del face["image"]
            logger.debug("Loaded aligned face: ('%s', shape: %s)", filename,
                         face["face"].shape)
Example #28
0
 def to_detected_face(image, dlib_rects):
     """ Convert list of dlib rectangles to a
         list of DetectedFace objects
         and add the cropped face """
     retval = list()
     for d_rect in dlib_rects:
         if not isinstance(
                 d_rect,
                 dlib.rectangle):  # pylint: disable=c-extension-no-member
             retval.append(list())
             continue
         detected_face = DetectedFace()
         detected_face.from_dlib_rect(d_rect)
         detected_face.image_to_face(image)
         detected_face.frame_dims = image.shape[:2]
         retval.append(detected_face)
     return retval
Example #29
0
 def align_face(self, faces, align_eyes, size, filename):
     """ Align the detected face and add the destination file path """
     final_faces = list()
     image = faces["image"]
     landmarks = faces["landmarks"]
     detected_faces = faces["detected_faces"]
     for idx, face in enumerate(detected_faces):
         detected_face = DetectedFace()
         detected_face.from_bounding_box(face, image)
         detected_face.landmarksXY = landmarks[idx]
         detected_face.load_aligned(image, size=size, align_eyes=align_eyes)
         final_faces.append({"file_location": self.output_dir / Path(filename).stem,
                             "face": detected_face})
     faces["detected_faces"] = final_faces
Example #30
0
 def to_detected_face(image, dlib_rects):
     """ Convert list of dlib rectangles to a
         list of DetectedFace objects
         and add the cropped face """
     retval = list()
     for d_rect in dlib_rects:
         if not isinstance(d_rect, dlib.rectangle):
             retval.append(list())
             continue
         detected_face = DetectedFace()
         detected_face.from_dlib_rect(d_rect)
         detected_face.image_to_face(image)
         retval.append(detected_face)
     return retval
Example #31
0
    def add(self, frame_index, pnt_x, width, pnt_y, height):
        """ Add a :class:`~lib.faces_detect.DetectedFace` object to the current frame with the
        given dimensions.

        Parameters
        ----------
        frame_index: int
            The frame that the face is being set for
        pnt_x: int
            The left point of the bounding box
        width: int
            The width of the bounding box
        pnt_y: int
            The top point of the bounding box
        height: int
            The height of the bounding box
        """
        face = DetectedFace()
        faces = self._faces_at_frame_index(frame_index)
        faces.append(face)
        face_index = len(faces) - 1

        self.bounding_box(frame_index, face_index, pnt_x, width, pnt_y, height, aligner="cv2-dnn")
        self._tk_face_count_changed.set(True)
Example #32
0
 def to_detected_face(left, top, right, bottom):
     """ Return a :class:`~lib.faces_detect.DetectedFace` object for the bounding box """
     return DetectedFace(x=int(round(left)),
                         w=int(round(right - left)),
                         y=int(round(top)),
                         h=int(round(bottom - top)))
Example #33
0
 def alignment_dict(image):
     """ Set the image to a dict for alignment """
     height, width = image.shape[:2]
     face = DetectedFace(x=0, w=width, y=0, h=height)
Example #34
0
 def alignment_dict(filename, image):
     """ Set the image to an ExtractMedia object for alignment """
     height, width = image.shape[:2]
     face = DetectedFace(x=0, w=width, y=0, h=height)
     return ExtractMedia(filename, image, detected_faces=[face])
Example #35
0
 def extract_one_face(self, alignment, image):
     """ Extract one face from image """
     face = DetectedFace()
     face.from_alignment(alignment, image=image)
     return self.extractor.extract(image, face, self.size, self.align_eyes)