Vectorizing images in Python for Machine Learning image classification
I'm doing the Astrozoo competition on Kaggle right now. The goal is to create an algorithm to accurately classify galaxies into different types. The training set is a zip file consisting of over 50,000 images. Before you do anything, the first step would be to convert the images into a usable format for analysis, such as resizing each image to 100x100 and converting it into a vector of data points. Here is the code I've pieced together to do that.
Running this code will convert all jpeg files in a specified folder into vectors and dump it into a .txt file.
#take all files in one directory and move it into another directory import os import glob #allows you to look at file names import PIL #allows you to manipulate images (for resize) #main script variables orig_dir = '/home/luwei/Desktop/Dropbox/Kaggle/astrozoo/testing' #where the images are located crop_dimensions = (140, 140, 284, 284) #dimensions to crop each image by pixelsize = 5 #set size of pixels for the pixelate operation vectorfile = '/home/luwei/Desktop/Dropbox/Kaggle/astrozoo/imagevectors.txt' #directory of vector output file #change the directory to where the images are os.chdir(orig_dir) filenames = glob.glob("*.jpg") #store all the filenames into an array #resize every image in the folder to with open(vectorfile, 'wb') as outfile: for fileid in filenames: img = Image.open(fileid) #open the image img = img.crop(crop_dimensions) #crop each image img = img.resize((100,100)) #resize the image img = img.convert('L') #convert each image to grayscale ###pixelate the image### img = img.resize((img.size[0]/pixelsize,img.size[1]/pixelsize), Image.NEAREST) #reduce the size of the image by factor of 5 img = img.resize((img.size[0]*pixelsize, img.size[1]*pixelsize), Image.NEAREST) #increase the size of the image by a factor of 5 ######################### pixel = img.load() #store pixel data in the pixel variable imagevector = [] #create an empty imagevector for each image u = 0 #marker for the pixel for x in range(0, img.size[0], pixelsize): #for every x coordinate in the pixel data of the image for y in range(0, img.size[1], pixelsize): #for every y coordinate in the pixel data of the image pixelvalue = pixel[x,y] #value of that particular pixel coordinate imagevector.append((u, (x, y), round(pixelvalue/float(255),3) ) ) #store pixel number, coordinate, and grayscale value (converted from 0 to 255 to a number between 0 and 1)into an array u = u + 1 #add one to the pixel marker vectorfile = open('imagevectors.txt', 'r+') outfile.write(str(fileid) + " " + str(imagevector) + "\n") print fileid #img.save('resized' + file + '.jpg')
Comments
Post a Comment