objectImageIdentifierAI/imagesToDataset/main.py
imagesToDataset/main.pyRaw
1from os import walk
2import matplotlib.pyplot as plt
3import numpy as np
4from PIL import Image, ImageOps
5
6#pixels, pixels of the output resizing images
7size = 100, 100
8def imgFileToData(path):
9 image = Image.open(path)
10 #resize the image
11 thumb = ImageOps.fit(image, size, Image.ANTIALIAS)
12 image_data = np.asarray(thumb).flatten()
13 '''
14 plt.plot(111)
15 plt.imshow(thumb)
16 plt.show()
17 '''
18 if len(image_data)!=30000:
19 print "possible future ERROR!"
20 print "len: " + str(len(image_data))
21 print "please, delete: " + path
22 return image_data
23
24def getDirectoryFiles(path, imgClass):
25 images = []
26 for (dirpath, dirnames, filenames) in walk(path):
27 for filename in filenames:
28 #print filename
29 image_data = imgFileToData(path + "/" + filename)
30 images.append([image_data, imgClass])
31 return images
32
33
34def asdf():
35 for index, (image, prediction) in enumerate(images_and_predictions[:4]):
36 plt.subplot(2, 4, index + 5)
37 plt.axis('off')
38 plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')
39 plt.title('Prediction: %i' % prediction)
40
41
42objects = getDirectoryFiles("object", 1)
43noobjects = getDirectoryFiles("noobject", 0)
44
45dataset = np.concatenate((objects, noobjects), axis=0)
46
47np.save('dataset.npy', dataset)