Esempio n. 1
0
	def trainAAMObject(self):
		try :
			from menpo.feature import fast_dsift
		except :
			pass

		#detector = load_dlib_frontal_face_detector()

		def load_image(i):
		    i = i.crop_to_landmarks_proportion(0.5)
		    if i.n_channels == 3:
		        i = i.as_greyscale()
		    # This step is actually quite important! If we are using
		    # an AAM and a PiecewiseAffine transform then we need
		    # to ensure that our triangulation is sensible so that
		    # we don't end up with ugly skinny triangles. Luckily,
		    # we provide a decent triangulation in the landmarks
		    # package.
		    labeller(i, 'PTS', ibug_face_68_trimesh)
		    return i

		training_images_path = Path(pathToTrainset) 
		training_images = [load_image(i) for i in mio.import_images(training_images_path, verbose=True)]

		aam = HolisticAAM(
		    training_images,
		    group='ibug_face_68_trimesh',
		    scales=(0.5, 1.0),
		    diagonal=150,
		    max_appearance_components=200,
		    max_shape_components=20,
		    verbose=True
		)

		pickle.dump(aam, open(AAMFile, "wb"))
Esempio n. 2
0
 def train(self, train_img_path):
     train_imgs = self.load_database(train_img_path)
     train_imgs = self.equalize_hist(train_imgs)
     if self.algo == 'aam':
         trainer = HolisticAAM(train_imgs,
                               group='PTS',
                               verbose=True,
                               diagonal=120,
                               scales=(0.5, 1.0))
         self.fitter = LucasKanadeAAMFitter(trainer,
                                            n_shape=[6, 12],
                                            n_appearance=0.5)
         mio.export_pickle(self.fitter, self.model_filename)
         print('aam model trained and exported!')
     elif self.algo == 'asm':
         trainer = CLM(train_imgs,
                       group='PTS',
                       verbose=True,
                       diagonal=120,
                       scales=(0.5, 1.0))
         self.fitter = GradientDescentCLMFitter(trainer, n_shape=[6, 12])
         mio.export_pickle(self.fitter, self.model_filename)
         print('asm model trained and exported!')
     else:
         ValueError('algorithm must be aam or asm!')
Esempio n. 3
0
 def holisticAAM(self, training_images):
     aam = HolisticAAM(training_images,
                       group='face_ibug_68_trimesh',
                       diagonal=150,
                       scales=(0.5, 1.0),
                       holistic_features=fast_dsift,
                       verbose=True,
                       max_shape_components=20,
                       max_appearance_components=150)
Esempio n. 4
0
def train(training_images):
    aam = HolisticAAM(training_images,
                      reference_shape=None,
                      diagonal=150,
                      scales=(0.5, 1.0),
                      holistic_features=igo,
                      verbose=True)
    print(aam)
    return aam
Esempio n. 5
0
def train_AAM(training_images, feature=igo):
    aam = HolisticAAM(training_images,
                      reference_shape=None,
                      diagonal=150,
                      scales=(0.5, 1.0),
                      holistic_features=feature,
                      verbose=True,
                      max_shape_components=20,
                      max_appearance_components=150)
    print(aam)
    return aam
def AAMModel(path, max_shape=None, max_appearance=None):
    training_images = []
    for img in print_progress(mio.import_images(path, verbose=True)):
        labeller(img, 'PTS', face_ibug_68_to_face_ibug_68_trimesh)
        training_images.append(img)
    aam_model = HolisticAAM(training_images,
                            group='face_ibug_68_trimesh',
                            scales=(0.5, 1.0),
                            holistic_features=fast_dsift,
                            verbose=True,
                            max_shape_components=max_shape,
                            max_appearance_components=max_appearance)
    return aam_model, training_images
Esempio n. 7
0
def train_aam(path_to_images):
    training_images = mio.import_images(path_to_images, verbose=True)
    training_images = training_images.map(process)
    aam = HolisticAAM(training_images, 
                        group='PTS', 
                        diagonal=150,
                      scales=(0.5, 1.0), verbose=True,
                      #holistic_features=double_igo,
                      max_shape_components=40, max_appearance_components=300
                     )
    fitter = LucasKanadeAAMFitter(aam, 
                                  lk_algorithm_cls=AlternatingInverseCompositional,
                                  n_shape=[10, 40], n_appearance=[60, 300]
                                 )
    return aam, fitter
Esempio n. 8
0
    def Train(self,
              i_diag=150,
              i_scale=[0.5, 1.0],
              i_max_greyscale_dims=200,
              i_max_shape_dims=20):

        # laterals tuned for performance gain - Sacrifice mouth modes
        self.model = HolisticAAM(
            self.LoadDataset(),
            group='PTS',
            verbose=True,
            holistic_features=float32_fast_dsift,
            diagonal=i_diag,
            scales=i_scale,
            max_appearance_components=i_max_greyscale_dims,
            max_shape_components=i_max_shape_dims)

        self.fitter = LucasKanadeAAMFitter(self.model,
                                           n_shape=[5, 15],
                                           n_appearance=[50, 150])
test_images = load_dataset(path_to_Dset / 'Test_R/Test_R', 0.5, max_images=5)
visualize_images(test_images)
from menpo.feature import imgfeature, igo


@imgfeature
def custom_double_igo(image):
    return igo(igo(image))


custom_double_igo(training_images[0]).view()
from menpofit.aam import HolisticAAM

aam = HolisticAAM(training_images,
                  group='PTS',
                  verbose=True,
                  holistic_features=custom_double_igo,
                  diagonal=120,
                  scales=(0.5, 1.0))

print(aam)

aam.view_aam_widget()

from menpofit.aam import LucasKanadeAAMFitter

fitter = LucasKanadeAAMFitter(aam, n_shape=[6, 12], n_appearance=0.5)

from menpofit.fitter import noisy_shape_from_bounding_box

fitting_results = []
Esempio n. 10
0
    def extract_save_features(self, files):
        r"""
        Uses the input files as train AAMs and store the resulting pickle on the disk
        Parameters
        ----------
        files

        Returns
        -------

        """

        # 1. fetch all video frames, attach landmarks
        frames = mio.import_video(files[0],
                                  landmark_resolver=self._myresolver,
                                  normalize=True,
                                  exact_frame_count=True)

        # frames = frames.map(AAMFeature._preprocess)
        idx_above_thresh, idx_lip_opening = landmark_filter(
            files[0],
            self._landmarkDir,
            threshold=self._confidence_thresh,
            keep=self._kept_frames)

        frames = frames[idx_above_thresh]
        frames = frames[idx_lip_opening]
        frames = frames.map(attach_semantic_landmarks)

        if self._greyscale is True:
            frames = frames.map(convert_to_grayscale)

        # initial AAM training
        if self._warpType == 'holistic':
            aam = HolisticAAM(frames,
                              group=self._landmarkGroup,
                              holistic_features=self._features,
                              reference_shape=None,
                              diagonal=self._diagonal,
                              scales=self._scales,
                              max_shape_components=self._max_shape_components,
                              max_appearance_components=self._max_appearance_components,
                              verbose=False)
        elif self._warpType == 'patch':
            aam = PatchAAM(frames,
                           group=self._landmarkGroup,
                           holistic_features=self._features,
                           diagonal=self._diagonal,
                           scales=self._scales,
                           max_shape_components=self._max_shape_components,
                           max_appearance_components=self._max_appearance_components,
                           patch_shape=self._extractOpts['patch_shape'],
                           verbose=False)

        else:
            raise Exception('Unknown warp type. Did you mean holistic/patch ?')

        frame_buffer = LazyList.init_from_iterable([])
        buffer_len = 256
        for idx, file in enumerate(files[1:]):
            # useful to check progress
            with open('./run/log_' + self._outModelName + '.txt', 'w') as log:
                log.write(str(idx) + ' ' + file + '\n')

            frames = mio.import_video(file,
                                      landmark_resolver=self._myresolver,
                                      normalize=True,
                                      exact_frame_count=True)
            idx_above_thresh, idx_lip_opening = landmark_filter(
                file,
                landmark_dir=self._landmarkDir,
                threshold=self._confidence_thresh,
                keep=self._kept_frames)

            frames = frames[idx_above_thresh]
            frames = frames[idx_lip_opening]
            frames = frames.map(attach_semantic_landmarks)
            if self._greyscale is True:
                frames = frames.map(convert_to_grayscale)

            frame_buffer += frames
            if len(frame_buffer) > buffer_len:
                # 2. retrain AAM
                aam.increment(frame_buffer,
                              group=self._landmarkGroup,
                              shape_forgetting_factor=1.0,
                              appearance_forgetting_factor=1.0,
                              verbose=False,
                              batch_size=None)
                del frame_buffer
                frame_buffer = LazyList.init_from_iterable([])
            else:
                pass

        if len(frame_buffer) != 0:  #
            # deplete remaining frames
            aam.increment(frame_buffer,
                          group=self._landmarkGroup,
                          shape_forgetting_factor=1.0,
                          appearance_forgetting_factor=1.0,
                          verbose=False,
                          batch_size=None)
            del frame_buffer

        mio.export_pickle(obj=aam, fp=self._outDir + self._outModelName, overwrite=True, protocol=4)
        landmarks = landmarks[MOUTH_OUTLINE_POINTS + MOUTH_INNER_POINTS]

        landmarks_pc = menpo.shape.PointCloud(landmarks)

        # The image will have two sets of lanmarks, PTS with the labeleld points and dlib_0 with the
        # original face bounding box.
        i.landmarks['PTS'] = landmarks_pc  #lm_lbl

        # Crop to lips
        i = i.crop_to_landmarks(group='PTS', boundary=10)  #dlib_0 for box
        i = i.resize(IMAGE_SIZE)

        images.append(i)

    #visualize_images(images)

    # IMPORTANT! Adjust these parameters, they are currently set to low to avoid MemoryError on my laptop
    # max_shape_components=20, max_appearance_components=150 features: mphog, sparse_hog, fast_dsift
    aam = HolisticAAM(images,
                      diagonal=None,
                      scales=1,
                      group='PTS',
                      holistic_features=igo,
                      verbose=True,
                      max_shape_components=20,
                      max_appearance_components=150)  # 20, 150
    log(aam)
    outpath = os.path.join(path_save, speaker + "-aam.pkl")
    menpo.io.export_pickle(aam, outpath, overwrite=True, protocol=2)
    print("\n\nSaved AAM to: ", outpath)
Esempio n. 12
0
def fit(path_to_images, path_to_test, c, r, w):
    training_images = []
    for img in print_progress(mio.import_images(path_to_images, verbose=True)):
        # convert to greyscale
        if img.n_channels == 3:
            img = img.as_greyscale()
        # crop to landmarks bounding box with an extra 20% padding
        img = img.crop_to_landmarks_proportion(0.2)
        # rescale image if its diagonal is bigger than 400 pixels
        d = img.diagonal()
        if d > 1000:
            img = img.rescale(1000.0 / d)
        # define a TriMesh which will be useful for Piecewise Affine Warp of HolisticAAM
    # labeller(img, 'PTS', face_ibug_68_to_face_ibug_68_trimesh)
    # append to list
        training_images.append(img)

    # ## Training ribcage - Patch
    # from menpofit.aam import PatchAAM
    # from menpo.feature import fast_dsift
    #
    # patch_aam = PatchAAM(training_images, group='PTS', patch_shape=[(15, 15), (23, 23)],
    #                      diagonal=500, scales=(0.5, 1.0), holistic_features=fast_dsift,
    #                      max_shape_components=20, max_appearance_components=150,
    #                      verbose=True)

    ## Training ribcage - Holistic

    patch_aam = HolisticAAM(training_images,
                            group='PTS',
                            diagonal=500,
                            scales=(0.5, 1.0),
                            holistic_features=fast_dsift,
                            verbose=True,
                            max_shape_components=20,
                            max_appearance_components=150)

    ## Prediction

    fitter = LucasKanadeAAMFitter(patch_aam,
                                  lk_algorithm_cls=WibergInverseCompositional,
                                  n_shape=[5, 20],
                                  n_appearance=[30, 150])

    image = mio.import_image(path_to_test)

    #initialize box

    adjacency_matrix = np.array([
        [0, 1, 0, 0],
        [0, 0, 1, 0],
        [0, 0, 0, 1],
        [1, 0, 0, 0],
    ])
    # points = np.array([[0,0], [0,2020], [2020, 2020], [2020, 0]])
    points = np.array([[r - w / 2, c - w / 2], [r - w / 2, c + w / 2],
                       [r + w / 2, c + w / 2], [r + w / 2, c - w / 2]])
    graph = PointDirectedGraph(points, adjacency_matrix)
    box = graph.bounding_box()

    # initial bbox
    initial_bbox = box

    # fit image
    result = fitter.fit_from_bb(image, initial_bbox, max_iters=[15, 5])

    pts = result.final_shape.points
    return pts
Esempio n. 13
0
import menpo.io as mio
from menpofit.aam import HolisticAAM
from menpo.feature import fast_dsift


def process(image, crop_proportion=0.2, max_diagonal=400):
    if image.n_channels == 3:
        image = image.as_greyscale()
    image = image.crop_to_landmarks_proportion(crop_proportion)
    d = image.diagonal()
    if d > max_diagonal:
        image = image.rescale(float(max_diagonal) / d)
    #labeller(image, 'PTS', face_ibug_68_to_face_ibug_68_trimesh)
    return image


path_to_images = '../../../data/lfpw/trainset_34/'
training_images = mio.import_images(path_to_images, verbose=True)
training_images = training_images.map(process)

aam = HolisticAAM(training_images,
                  holistic_features=fast_dsift,
                  verbose=True,
                  scales=1,
                  max_shape_components=16,
                  max_appearance_components=104)
mio.export_pickle(aam, "aam.pkl", overwrite=True, protocol=4)