Question

I am a beginner in python and I am trying to apply Principal Component Analysis (PCA) to a set of images. I want to put the images in a matrix to be able to perform PCA. I am still at the beginning but I am having errors.

import numpy as np
import Image
import os

#insert images to matrix
dirname = 'C:\Users\Karim\Downloads\shp_marcel_train\Marcel-Train\A'
X = [np.asarray(Image.open(os.path.join(dirname, fn))) for fn in os.listdir(dirname)]

#get dimensions
num_data,dim = X.shape

It gives AttributeError: 'list' object has no attribute 'shape'

anyone can help?

a link to a detailed tutorial would be appreciated

Was it helpful?

Solution 2

When you do

[np.asarray(Image.open(os.path.join(dirname, fn))) for fn in os.listdir(dirname)]

It would return an array, which is stored in local variable X

You are trying to extract variables from a list, and hence the error.

The individual elements within X have the shape attributes. Hence, you need to do something like this (modify it according to the datastructure shape is defined as )

dim0 = X[0].shape and so on

OTHER TIPS

As you've defined it, X is a list:

X = [... for fn in ...]

Python lists do not have a shape attribute. Inside this list are NumPy arrays which do have a shape attribute. So you could say

num_data = len(X)
shape_of_first_array = X[0].shape

(You didn't explain what dim is supposed to represent...)


Here is an example of face recognition done with PCA and a State Vector Machine. Here is another related example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top