Example #1
0
    def get_pca_features(self):
        # Get the list of images
        img_list = []
        y = []
        with open(IMAGE_PATH + TRAIN_LIST, 'r') as f:
                while True:
                    filename = f.readline()
                    # Zero length indicates EOF
                    if len(filename) == 0:
                        break
                    filename = filename.rstrip()
                    target = filename.split('.')
                    y.append(target[0])

                    # get filename without return
                    img_list.append(IMAGE_PATH + 'train/' + filename)

        X = None
        # X = np.empty((len(img_list),))
        # Read each image for PCA
        for row, img_name in zip(range(len(img_list)), img_list):
            img = cv2.imread(img_name, cv2.IMREAD_GRAYSCALE)
            cv2.imshow('image', img)

            # Calculate the lbp histograms
            features = lbp.circular_lbp(img, 2, 8)
            cv2.imshow('LBP', np.int8(features))

            # separate the image into grids and get histograms
            sizex, sizey = features.shape

            # Set the size
            if row is 0:
                blocksx = sizex/FaceRec.__block_size[0] + 1
                blocksy = sizey/FaceRec.__block_size[1] + 1
                X = np.empty((len(img_list), blocksx * blocksy * 256))

            hists = []
            for i in range(0, sizex, FaceRec.__block_size[0]):
                for j in range(0, sizey, FaceRec.__block_size[1]):
                    hists.append(np.bincount(
                        features[i:i+FaceRec.__block_size[0],
                                 j:j+FaceRec.__block_size[1]].ravel(),
                        minlength=256))
            X[row, :] = (np.array(hists).ravel())

            print X.shape

        # Train the Classifier
        self.pca.fit(X)

        # Storing the data
        self.features = np.empty((len(X), 3))
        for i in range(len(X)):
            self.features[i, :] = self.pca.transform(X[i, :])
        self.targets = y
        print len(self.features)
Example #2
0
 def test(self, img):
     features = lbp.circular_lbp(img, 2, 8)
     hists = []
     sizex, sizey = features.shape
     X = []
     for i in range(0, sizex, FaceRec.__block_size[0]):
             for j in range(0, sizey, FaceRec.__block_size[1]):
                 hists.append(np.bincount(
                     features[i:i+FaceRec.__block_size[0],
                              j:j+FaceRec.__block_size[1]].ravel(),
                     minlength=256))
     X.append(np.array(hists).ravel())
     X = self.pca.transform(np.array(X))
     prediction = self.clf.predict(X)
     return prediction