Jul-15-2018, 06:40 AM
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]