简体   繁体   English

ValueError:尝试存储图像HoG功能时,使用序列设置数组元素

[英]ValueError: setting an array element with a sequence while trying to store image HoG features

I'm getting a ValueError while assigning a converted array to a new variable. 将转换后的数组分配给新变量时出现ValueError。 I want the variable img_hogs to consist of the HoG features of each image. 我希望变量img_hogs包含每个图像的HoG功能。 So it's a 2D array, where the each row represents the HoG features of one image. 因此,这是一个2D数组,其中每一行代表一个图像的HoG特征。

tmp_hogs = [] 

for filen in out: # get hog features for each training image
        filepath = "C:\\path\\cropped_cars\\"
        readfile = filepath + filen
        curr_img = color.rgb2gray(io.imread(readfile))
        imgs.append(curr_img)
        fd, hog_image = hog(curr_img, orientations=8, pixels_per_cell=(16, 16),
                 cells_per_block=(1, 1), visualise=True, normalise=True)
        tmp_hogs.append(fd)
        i+=1

img_hogs = np.array(tmp_hogs, dtype=float)

The error comes on the last line above, saying: 错误出现在上面的最后一行,说:

Traceback (most recent call last):
  File "C:\path\gbc_classifier_test_2.py", line 71, in <module>
    img_hogs = np.array(tmp_hogs, dtype=float) 
ValueError: setting an array element with a sequence.

What's the problem here? 这是什么问题 I seem to have used this without issue before. 我以前似乎没有用过。 I haven't initialized img_hogs to anything. 我还没有将img_hogs初始化为任何东西。

Edit 编辑

When I print out tmp_hogs, I get arrays, this is one: 当我打印出tmp_hogs时,得到数组,这是一个:

array([  9.43988506e-02,   1.16434393e-01,   1.66949456e-01,
         1.67258585e-01,   1.55137816e-01,   1.18421903e-01,
         7.17979933e-02,   1.07738525e-01,   5.04513644e-02,
         .. # and it goes on for about 270 lines...
         1.02937285e-01,   3.79174496e-02,         2.82693731e-02])

and then this: 然后这个:

, array([ 0.17943262,  0.17266624,  0.18836   , ...,  0.03710205,
        0.01623901,  0.00623639]), array([ 0.12019744,  0.18129971,  0.17268482, ...,  0.03478858,
        0.01075355,  0.0086803 ]), array([ 0.13604284,  0.11251585,  0.13396777, ...,  0.07344526,
        0.01842701,  0.00995614]), array([ 0.09036218,  0.15552837,  0.14932259, ...,  0.09498851,
        0.08212626,  0.01198428]), array([ 0.03504824,  0.07993138,  0.08090841, ...,  0.0207428 ,
        0.00867545,  0.00285672]), array([ 0.05378212,  0.01994047,  0.03666057, ...,  0.09112556,
        0.02215141,  0.01434096]), array([ 0.14161055,  0.14635719,  0.1749262 , ...,  0.03148477,
        0.02349815,  0.02309164]), array([ 0.11262369,  0.10024635,  0.14424181, ...,  0.08426981,
        0.0486145 ,  0.026758  ]), array([ 0.03906336,  0.03750173,  0.13635017, ...,  0.13814893,
        0.07869883,  0.05370808]), array([ 0.04162636,  0.053963  ,  0.10114257, ...,  0.10504406,
        0.03798734,  0.01648739]), array([ 0.02912047,  0.05139863,  0.13072171, ...,  0.14025979,
        0.03743591,  0.01188805]), array([ 0.06857062,  0.09075094,  0.20136449, ...,  0.09126319,
        0.05310212,  0.05257662]), array([ 0.02797054,  0.01915037,  0.09330322, ...,  0.078432  ,
        0.02443184,  0.0265646 ]), array([ 0.1754544 ,  0.10865344,  0.23155207, ...,  0.10221248,
        0.07416729,  0.02623395]), array([ 0.0574084 ,  0.05975376,  0.17089832, ...,  0.11830615,
        0.05896331,  0.02676903])]

and so on. 等等。

Oops - the answer was simple. 糟糕-答案很简单。 I had to resize all the images first before taking out the HoG features, so that the arrays could all be the same size. 在删除HoG功能之前,我必须先重新调整所有图像的大小,以便数组的大小都可以相同。

So before appending each image to the imgs list, I added this line: 因此,在将每个图像添加到imgs列表之前,我添加了以下行:

curr_img = resize(curr_img, (50,100))

That solved the problem. 那解决了问题。

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

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