#https://numpy.org/doc/stable/user/absolute_beginners.html #https://docs.python.org/3/tutorial/introduction.html#lists from sklearn.datasets import fetch_olivetti_faces data, targets = fetch_olivetti_faces(return_X_y=True) from sklearn.tree import DecisionTreeClassifier from sklearn.tree import export_text from sklearn import tree from sklearn import preprocessing from sklearn import utils from matplotlib import image from matplotlib import pyplot # load image as pixel array image = image.imread('face-64.png') from PIL import Image import numpy as np img = Image.open('face-64.png').convert('L') # summarize shape of the pixel array imgarr = np.array(img) print(imgarr.shape) imgarrf = imgarr #normalize #generate list of numbers 0 to 255 , classification requires integers classes_list = [] for i in range(0, 256): classes_list.append(i) print(classes_list) tophalflady = imgarrf.flatten()[0: 2048] #4096 / 2 print(len(data)) #400 rows print(len(data[0])) #4096 columns = 64 x 64 = 4096 n_pixels = data.shape[1] # (400,4096) [1] = 4096 print(n_pixels) #should be 4096 flattened #slice the list data and target using index : meens start # Upper half of the faces from 0 numpy arrays [rom start to middle] X_train = data[:, : (n_pixels + 1) // 2] X_train = np.rint(X_train * 255) #round to int # Lower half of the faces [for all from end to middle] from end to #classifier expects integer values y_target = data[:, n_pixels // 2 :] y_target = np.rint(y_target * 255) #as int #lab_enc = preprocessing.LabelEncoder() #encoded = lab_enc.fit_transform(y_target) clf = tree.DecisionTreeClassifier(random_state=0, max_depth=24) clf = clf.fit(X_train, y_target) arrpredict= clf.predict([ tophalflady ]) # load and display an image with Matplotlib print(tophalflady.shape)#2048,1 tophalflady = np.expand_dims(tophalflady, axis=0) print(tophalflady.shape)#2048,1 print(arrpredict.shape) #1,2048 linear flatten array, reshape to 2d print('yes') #arrpredict = numpy.transpose(arrpredict) print(arrpredict.shape) #1,2048 linear flatten array, reshape to 2d print(tophalflady.shape)#2048,1 #merge array top and predicted topbottom = np.concatenate((tophalflady, arrpredict), axis=0) # display the array of pixels as an image #The image is quantized to 256 grey levels and stored as unsigned 8-bit integers; # #the loader will convert these to floating point values on the interval [0, 1], # which are easier to work with for many algorithms. from numpy import asarray #pyplot.imshow(oneimg.reshape( (64,64))) #reshape to 64 by 64 pyplot.imshow( topbottom.reshape((64,64)) ) #reshape to 32 rows and 64 columns pyplot.show()