Esempio n. 1
0
 def rotate_image(self, image, rotation, reverse=False):
     """ Rotate the image forwards or backwards """
     if rotation == 0:
         return image
     if not reverse:
         self.rotation_height, self.rotation_width = image.shape[:2]
         image, _ = rotate_image_by_angle(image, rotation)
     else:
         image, _ = rotate_image_by_angle(
             image,
             rotation * -1,
             rotated_width=self.rotation_width,
             rotated_height=self.rotation_height)
     return image
Esempio n. 2
0
 def compile_reprocess(processed, detect_images, angle):
     """ Rotate images which did not find a face for reprocessing """
     indexes = list()
     to_detect = list()
     for idx, faces in enumerate(processed):
         if faces:
             continue
         image = detect_images[idx]
         rot_image, rot_matrix = rotate_image_by_angle(image, angle)
         to_detect.append(rot_image)
         indexes.append(idx)
     return to_detect, indexes, rot_matrix
Esempio n. 3
0
    def process_single_image(self, filename):
        """ Detect faces in an image. Rotate the image the specified amount
            until at least one face is found, or until image rotations are
            depleted.
            Once at least one face has been detected, pass to
            process_single_face to process the individual faces """
        retval = filename, list()
        try:
            image = Utils.cv2_read_write('read', filename)

            for angle in self.images.rotation_angles:
                currentimage, rotation_matrix = rotate_image_by_angle(
                    image, angle)
                faces = self.faces.get_faces(currentimage, angle)
                process_faces = [[idx, face] for idx, face in faces]
                if not process_faces:
                    continue

                if angle != 0 and self.args.verbose:
                    print("found face(s) by rotating image "
                          "{} degrees".format(angle))
                if angle != 0:
                    process_faces = [[
                        idx, rotate_landmarks(face, rotation_matrix)
                    ] for idx, face in process_faces]

                if process_faces:
                    break

            final_faces = [
                self.process_single_face(idx, face, filename, image)
                for idx, face in process_faces
            ]

            retval = filename, final_faces
        except Exception as err:
            if self.args.verbose:
                print("Failed to extract from image: "
                      "{}. Reason: {}".format(filename, err))
            raise
        return retval
Esempio n. 4
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
Esempio n. 5
0
 def rotate_image(image, angle):
     """ Rotate the image by given angle and return
         Image with rotation matrix """
     if angle == 0:
         return image, None
     return rotate_image_by_angle(image, angle)