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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
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] |