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]}
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
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
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)
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
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
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
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]}
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
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])
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
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
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
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
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
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
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
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
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
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
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
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
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)
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
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
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
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)
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)))
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)
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])
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)