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();