objectImageIdentifierAI/other/imagesToDataset/main.py
other/imagesToDataset/main.pyRaw
1from os import walk
2import matplotlib.pyplot as plt
3import numpy as np
4from PIL import Image, ImageOps
5import pandas as pd
6
7#pixels, pixels of the output resizing images
8size = 100, 100
9def imgFileToData(path):
10 image = Image.open(path)
11 #resize the image
12 thumb = ImageOps.fit(image, size, Image.ANTIALIAS)
13 image_data = np.asarray(thumb)
14 #.flatten()
15
16 #check if the image had been resized to 100x100. 3pixels * 100width + 100 height = 30000
17 if len(image_data)!=100:
18 print("possible future ERROR!")
19 print("len: " + str(len(image_data)))
20 print("please, delete: " + path)
21 return np.array(list(image_data))
22
23def getDirectoryFiles(path, imgClass):
24 images = []
25 for (dirpath, dirnames, filenames) in walk(path):
26 for filename in filenames:
27 #print(filename)
28 image_data = imgFileToData(path + "/" + filename)
29 images.append([image_data, imgClass])
30 print(path + "/" + filename)
31 return images
32
33
34objects = getDirectoryFiles("object", 1)
35noobjects = getDirectoryFiles("noobject", 0)
36
37dataset = np.concatenate((objects, noobjects), axis=0)
38#print(dataset[0])
39
40np.save('dataset.npy', dataset)
41'''
42print(dataset)
43np.savetxt('dataset.csv', dataset, delimiter=",", fmt='%d')
44
45pd.set_option('display.max_colwidth', -1)
46df = pd.DataFrame(dataset)
47print(df.head())
48print("aaa")
49print(df[0][0])
50print("aaa")
51pd.set_option('display.max_colwidth', -1)
52pd.set_option('display.max_columns', None)
53df.to_csv("dataset.csv", encoding='utf-8', index=False, header=False)
54'''