简体   繁体   English

Scipy.cluster.vq.kmeans“列表没有属性形状”

[英]Scipy.cluster.vq.kmeans “list has no attribute shape”

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. 我基本上试图创建一个练习码本,它使用由Python中的kmeans算法聚类的图像的SIFT特征。 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. 我假设这是Scipy库的vq脚本中的错误。 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. 但是,我还有其他朋友正在研究这个问题,我使用与scipy库完全相同的代码,但我仍然遇到这个问题。 I've also tried to completely uninstall Python reinstalling everything. 我也尝试完全卸载Python重新安装一切。 I'm running the thing on Windows 7 btw. 我正在运行Windows 7上的东西顺便说一句。 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. 那里的glob函数需要我从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 希望我能在这里得到一些指导

The issue looks to be that kmeans is expecting an array, and you're feeding it a list. 问题似乎是kmeans期待一个数组,你正在给它一个列表。 Try changing the last line of your codebook_construction() function to: 尝试将codebook_construction()函数的最后一行更改为:

return scipy.array(codebook)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM