Python Forum

Full Version: ValueError: setting an array element with a sequence
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am new to this python coding, I tried to modified other author's code for object classification. However, I encounter this error, ValueError: setting an array element with a sequence (error at immatrix). when I tried to convert RGB images to create matrix. It can convert 4 species to matrix,is it this coding is not suitable to flatten large amount of images? Thank you.
path1 = 'C:/Users/Z/Documents/Python Scripts/Input'      
path2 = 'C:/Users/Z/Documents/Python Scripts/Input_resized'   

listing = os.listdir(path1) 
#num_samples=size(listing)

for file in listing:
    im = Image.open(path1 + '\\' + file)
    img_rows, img_cols = 224, 224  
    img = im.resize((img_rows,img_cols),3)  
#    gray = img.convert('L')  
    img.save(path2 +'\\' +  file, "JPEG")

imlist = os.listdir(path2)

im1 = array(Image.open(path2 + '/'+ imlist[0])) 
m,n = im1.shape[0:2] 
imnbr = len(imlist) 
num_samples = len(imlist)
print (imnbr)


immatrix = array([array(Image.open(path2+ '/' + im2)).flatten()
              for im2 in imlist],'f')
label=np.ones((num_samples,),dtype = int)
label[0:1315]=0
label[1315:1977]=1
label[1977:2689]=2
label[2689:3861]=3
label[3861:5427]=4
label[5427:6446]=5
label[6446:6966]=6
label[6966:7985]=7
label[7985:8997]=8
label[8997:9311]=9
label[9311:10622]=10
label[10622:11477]=11
label[11477:12990]=12
label[12990:14405]=13
label[14405:15660]=14
label[15660:15948]=15
label[15948:17042]=16
label[17042:18060]=17
label[18060:19193]=18
label[19193:20360]=19
label[20360:21970]=20


data,Label = shuffle(immatrix,label, random_state=2)
train_data = [data,Label]
Please, post the full traceback you get, in error tags
Error:
runfile('C:/Users/Z/gray scale.py', wdir='C:/Users/Z') Using TensorFlow backend. C:\Users\Z\Anaconda3\envs\deeplearning\lib\site-packages\sklearn\cross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20. "This module will be removed in 0.20.", DeprecationWarning) 21970 Traceback (most recent call last): File "<ipython-input-1-2c7840cbbd80>", line 1, in <module> runfile('C:/Users/Z/gray scale.py', wdir='C:/Users/Z') File "C:\Users\Z\Anaconda3\envs\deeplearning\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile execfile(filename, namespace) File "C:\Users\Z\Anaconda3\envs\deeplearning\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "C:/Users/Z/gray scale.py", line 46, in <module> for im2 in imlist],'f') ValueError: setting an array element with a sequence.