示例#1
0
    def __init__(self, path, face_identifier, landmarks_detector, face_detector=None, no_show=False):
        path = osp.abspath(path)
        self.fg_path = path
        self.no_show = no_show
        paths = []
        if osp.isdir(path):
            paths = [osp.join(path, f) for f in os.listdir(path) \
                      if f.split('.')[-1] in self.IMAGE_EXTENSIONS]
        else:
            log.error("Wrong face images database path. Expected a " \
                      "path to the directory containing %s files, " \
                      "but got '%s'" % \
                      (" or ".join(self.IMAGE_EXTENSIONS), path))

        if len(paths) == 0:
            log.error("The images database folder has no images.")

        self.database = []
        for num, path in enumerate(paths):
            label = osp.splitext(osp.basename(path))[0]
            image = cv2.imread(path, flags=cv2.IMREAD_COLOR)

            assert len(image.shape) == 3, \
                "Expected an input image in (H, W, C) format"
            assert image.shape[2] in [3, 4], \
                "Expected BGR or BGRA input"

            orig_image = image.copy()
            image = image.transpose((2, 0, 1)) # HWC to CHW
            image = np.expand_dims(image, axis=0)

            if face_detector:
                face_detector.start_async(image)
                rois = face_detector.get_roi_proposals(image)
                if len(rois) < 1:
                    log.warning("Not found faces on the image '%s'" % (path))
            else:
                w, h = image.shape[-1], image.shape[-2]
                rois = [FaceDetector.Result([0, 0, 0, 0, 0, w, h])]

            for i, roi in enumerate(rois):
                r = [roi]
                landmarks_detector.start_async(image, r)
                landmarks = landmarks_detector.get_landmarks()

                face_identifier.start_async(image, r, landmarks)
                descriptor = face_identifier.get_descriptors()[0]

                if face_detector:
                    mm = self.check_if_face_exist(descriptor, face_identifier.get_threshold())
                    if mm < 0:
                        crop = orig_image[int(roi.position[1]):int(roi.position[1]+roi.size[1]), \
                               int(roi.position[0]):int(roi.position[0]+roi.size[0])]
                        name = self.ask_to_save(crop)
                        self.dump_faces(crop, descriptor, name)
                else:
                    log.debug("Adding label {} to the gallery.".format(label))
                    self.add_item(descriptor, label)
示例#2
0
    def __init__(self,
                 path,
                 face_identifier,
                 landmarks_detector,
                 face_detector=None,
                 no_show=False):
        path = osp.abspath(path)
        self.fg_path = path
        self.no_show = no_show
        paths = []
        if osp.isdir(path):
            paths = [
                osp.join(path, f) for f in os.listdir(path)
                if f.split('.')[-1] in self.IMAGE_EXTENSIONS
            ]
        else:
            log.error("Wrong face images database path. Expected a "
                      "path to the directory containing %s files, "
                      "but got '%s'" %
                      (" or ".join(self.IMAGE_EXTENSIONS), path))

        if len(paths) == 0:
            log.error("The images database folder has no images.")

        self.database = []
        for path in paths:
            label = osp.splitext(osp.basename(path))[0]
            image = cv2.imread(path, flags=cv2.IMREAD_COLOR)

            orig_image = image.copy()

            if face_detector:
                rois = face_detector.infer((image, ))
                if len(rois) < 1:
                    log.warning(
                        "Not found faces on the image '{}'".format(path))
            else:
                w, h = image.shape[1], image.shape[0]
                rois = [FaceDetector.Result([0, 0, 0, 0, 0, w, h])]

            for roi in rois:
                r = [roi]
                landmarks = landmarks_detector.infer((image, r))

                face_identifier.start_async(image, r, landmarks)
                descriptor = face_identifier.get_descriptors()[0]

                if face_detector:
                    mm = self.check_if_face_exist(
                        descriptor, face_identifier.get_threshold())
                    if mm < 0:
                        crop = orig_image[
                            int(roi.position[1]):int(roi.position[1] +
                                                     roi.size[1]),
                            int(roi.position[0]):int(roi.position[0] +
                                                     roi.size[0])]
                        name = self.ask_to_save(crop)
                        self.dump_faces(crop, descriptor, name)
                else:
                    log.debug("Adding label {} to the gallery.".format(label))
                    self.add_item(descriptor, label)