034. convert image to vector,increas quantity of data by processing original data by some techniques like rotating
from PIL import Image import os,glob import numpy as np import random,math # You define folder for image files folder_containing_image_file_string="./image/" # You define categories_list for image data categories_list=["normal","beni","negi","cheese"] # Number of class is same length of categories_list number_of_classes=len(categories_list) # You will use image sized 50*50 image_size_integer=50 X_feature_train_data_list=[] Y_feature_train_data_list=[] def add_sample(category,fname,is_train): opened_image_file=Image.open(fname) converted_image_file_to_rgb=opened_image_file.convert("RGB") resized_image_file=converted_image_file_to_rgb.resize((image_size_integer,image_size_integer)) image_file_in_nparray=np.asarray(resized_image_file) X_feature_train_data_list.append(image_file_in_nparray) Y_feature_train_data_list.append(category) if not is_train: return # You add image data which is created by manipulating angle of image # starting, last, 5 is steps # So, it will process by -20,-15,-10,... for one_angle in range(-20,20,5): # First, you rotate image by -20, # and assign rotated image into rotated_image rotated_image=opened_image_file.rotate(one_angle) # You store rotated image into nparray rotated_image_in_nparray=np.asarray(rotated_image) # You append data into X_feature_train_data_list X_feature_train_data_list.append(rotated_image_in_nparray) # You append label data Y_feature_train_data_list.append(category) # rotated_image.save("gyudon-"+str(one_angle)+".PNG") flipped_image=rotated_image.transpose(Image.FLIP_LEFT_RIGHT) flipped_image_in_nparray=np.asarray(flipped_image) X_feature_train_data_list.append(flipped_image_in_nparray) Y_feature_train_data_list.append(category) def make_sample(files,is_train): global X_feature_train_data_list,Y_feature_train_data_list X_feature_train_data_list=[]; Y_feature_train_data_list=[] for category,fname in files: add_sample(category,fname,is_train) return np.array(X_feature_train_data_list),np.array(Y_feature_train_data_list) # You collect files in each folder allfiles_list=[] for index,category in enumerate(categories_list): image_folder_string=folder_containing_image_file_string+"/"+category loaded_files=glob.glob(image_folder_string+"/*.PG") for one_file in loaded_files: allfiles_list.append((index,one_file)) # You suffle files, # and then separate train data and test data random.shuffle(allfiles_list) borderline_for_deviding_data_number=math.floor(len(allfiles_list)*0.6) train_data=allfiles_list[0:borderline_for_deviding_data_number] test_data=allfiles_list[borderline_for_deviding_data_number:] x_train,y_train=make_sample(train_data,True) x_test,y_test=make_sample(test_data,False) xy_data=(x_train,x_test,y_train,y_test) np.save("./image/gyudon2.npy",xy_data) print("ok,",len(y_train))