简体   繁体   中英

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. I want the variable img_hogs to consist of the HoG features of each image. So it's a 2D array, where the each row represents the HoG features of one image.

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.

Edit

When I print out tmp_hogs, I get arrays, this is one:

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.

So before appending each image to the imgs list, I added this line:

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

That solved the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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