Scipy.cluster.vq.kmeans "list has no attribute shape"
Question: So this is a really weird problem I've been getting. I'm basically trying to
create a practice codebook which uses SIFT features of images that are
clustered by the kmeans algorithm in Python. However whenever I run the code I
get the following error

    
    
    Traceback (most recent call last):
      File "C:\Users\Administrator\Desktop\Python\assignment2\SIFT_Dectection.py", line 34, in    <module>
        codebook, dis = cluster.vq.kmeans(codebook_construction(files[:20]),3)
      File "C:\Python27\lib\site-packages\scipy\cluster\vq.py", line 513, in kmeans
        No = obs.shape[0]
    AttributeError: 'list' object has no attribute 'shape'
    

I assume that this is an error within the vq script for the Scipy library.
However, I have other friends who are working on this as well and I am using
the exact same code as them with the scipy library but I'm still getting this
problem. I've also tried to completely uninstall Python reinstalling
everything. I'm running the thing on Windows 7 btw. The code I'm using looks
something like this:

    
    
    import cv2
    import glob
    from scipy import cluster
    
    files = glob.glob('101_ObjectCategories/*/*.jpg')
    
    def codebook_construction(im):
    
        codebook = []
    
        for image in im:
            img = cv2.imread(image)
            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            sift = cv2.SIFT()
            kp, desc = sift.detectAndCompute(gray, None)
    
            if codebook == []:            
                codebook = desc
    
            else:
                codebook = np.vstack((codebook, desc))
    
        return codebook
    
    codebook, dis = cluster.vq.kmeans(codebook_construction(files[:20]),3)
    

The glob function there calls for a library of images I've downloaded from
Caltech. I've searched high and low for an answer but it seems that no one has
been having similar problems. Hopefully I can get some guidance here


Answer: The issue looks to be that `kmeans` is expecting an array, and you're feeding
it a list. Try changing the last line of your `codebook_construction()`
function to:

    
    
    return scipy.array(codebook)
    

