Exemplo n.º 1
0
class EigenFaces():
    def __init__(self, num_components):
        """
        Constructor
        """
        self._num_components = num_components
        self._projections = []

    def train(self, images, labels):
        """
        Train eigenfaces
        """
        print "Train..."
        #copy labels
        self._labels = labels

        #transform the numpe vector to shogun structure
        features = RealFeatures(images)
        #PCA
        self.pca = PCA()
        #set dimension
        self.pca.set_target_dim(self._num_components)
        #compute PCA
        self.pca.init(features)

        for sampleIdx in range(features.get_num_vectors()):
            v = features.get_feature_vector(sampleIdx)
            p = self.pca.apply_to_feature_vector(v)
            self._projections.insert(sampleIdx, p)

        print "Train ok!"

    def predict(self, image):
        """
        Predict the face
        """
        #image as row
        imageAsRow = np.asarray(
            image.reshape(image.shape[0] * image.shape[1], 1), np.float64)
        #project inthe subspace
        p = self.pca.apply_to_feature_vector(
            RealFeatures(imageAsRow).get_feature_vector(0))

        #min value to find the face
        minDist = 1e100
        #class
        minClass = -1
        #search which face is the best match
        for sampleIdx in range(len(self._projections)):
            test = RealFeatures(np.asmatrix(p, np.float64).T)
            projection = RealFeatures(
                np.asmatrix(self._projections[sampleIdx], np.float64).T)
            dist = EuclideanDistance(test, projection).distance(0, 0)

            if (dist < minDist):
                minDist = dist
                minClass = self._labels[sampleIdx]

        return minClass

    def getMean(self):
        """
        Return the mean vector
        """
        return self.pca.get_mean()

    def getEigenValues(self):
        """
        Return the eigenvalues vector
        """
        return self.pca.get_eigenvalues()
Exemplo n.º 2
0
class EigenFaces():
    def __init__(self, num_components):
        """
        Constructor
        """
        self._num_components = num_components;
        self._projections = []

    def train(self, images, labels):
        """
        Train eigenfaces
        """
        print "Train...",
        #copy labels
        self._labels = labels;

        #transform the numpe vector to shogun structure
        features = RealFeatures(images)
        #PCA
        self.pca = PCA()
        #set dimension
        self.pca.set_target_dim(self._num_components);
        #compute PCA
        self.pca.init(features)

        for sampleIdx in range(features.get_num_vectors()):
            v = features.get_feature_vector(sampleIdx);
            p = self.pca.apply_to_feature_vector(v);
            self._projections.insert(sampleIdx, p);

        print "ok!"

    def predict(self, image):
        """
        Predict the face
        """
        #image as row
        imageAsRow = np.asarray(image.reshape(image.shape[0]*image.shape[1],1),
                                np.float64);
        #project inthe subspace
        p = self.pca.apply_to_feature_vector(RealFeatures(imageAsRow).get_feature_vector(0));

        #min value to find the face
        minDist =1e100;
        #class
        minClass = -1;
        #search which face is the best match
        for sampleIdx in range(len(self._projections)):
            test = RealFeatures(np.asmatrix(p,np.float64).T)
            projection = RealFeatures(np.asmatrix(self._projections[sampleIdx],
                                        np.float64).T)
            dist = EuclideanDistance( test, projection).distance(0,0)

            if(dist < minDist ):
                minDist = dist;
                minClass = self._labels[sampleIdx];

        return minClass

    def getMean(self):
        """
        Return the mean vector
        """
        return self.pca.get_mean()

    def getEigenValues(self):
        """
        Return the eigenvalues vector
        """
        return self.pca.get_eigenvalues();
Exemplo n.º 3
0
        pca = PCA()
        #set dimension
        pca.set_target_dim(i)
        #compute PCA
        pca.init(RealFeatures(images))

        pca.apply_to_feature_vector(
            RealFeatures(imageAsRow).get_feature_vector(0))

        #reconstruct
        projection = pca.apply_to_feature_vector(
            RealFeatures(imageAsRow).get_feature_vector(0))

        reconstruction = np.asmatrix( np.asarray(projection, np.float64))* \
                         np.asmatrix( pca.get_transformation_matrix()).T
        reconstruction = reconstruction + pca.get_mean()

        #normlize image
        reconstruction_normalize = np.zeros((IMAGE_HEIGHT, IMAGE_WIDHT, 1),
                                            np.uint8)
        reconstruction_normalize = cv2.normalize(reconstruction,
                                                 reconstruction_normalize, 0,
                                                 255, cv2.NORM_MINMAX,
                                                 cv2.CV_8UC1)
        reconstruction_normalize = reconstruction_normalize.reshape(
            IMAGE_HEIGHT, IMAGE_WIDHT)
        #show reconstruction
        cv2.imshow("reconstruction" + str(i), reconstruction_normalize)
    cv2.waitKey(0)
Exemplo n.º 4
0
    #Reconstruct 10 eigen vectors to 300, step 15
    for i in reconstructions:

        print "Reconstruct with " + str(i) + " eigenvectors"

        pca = PCA()
        #set dimension
        pca.set_target_dim(i);
        #compute PCA
        pca.init(RealFeatures(images))

        pca.apply_to_feature_vector(RealFeatures(imageAsRow)
                                    .get_feature_vector(0));

        #reconstruct
        projection = pca.apply_to_feature_vector(RealFeatures(imageAsRow)
                                                .get_feature_vector(0));

        reconstruction = np.asmatrix( np.asarray(projection, np.float64))* \
                         np.asmatrix( pca.get_transformation_matrix()).T
        reconstruction = reconstruction + pca.get_mean()

        #prepare the data to visualize in one window
        show_images_titles.append( str(i) + " eigenvectors" );
        show_images.append(reconstruction.reshape(IMAGE_HEIGHT, IMAGE_WIDHT))

    plot_gallery(show_images, show_images_titles, IMAGE_HEIGHT,
                 IMAGE_WIDHT, 4, 4);
    pl.show()