You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.5 KiB

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