Advertisement
Guest User

HDF5 attempt

a guest
Nov 19th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import os
  4. import cv2
  5. from tqdm import tqdm
  6.  
  7. DATADIR = "E:\Kash project files\Manveer\DataCollection\Scattered\Testing"
  8.  
  9. CATEGORIES = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
  10.  
  11. IMG_SIZE = 100
  12.  
  13. training_data = []
  14.  
  15. def create_training_data():
  16. for category in CATEGORIES:
  17.  
  18. path = os.path.join(DATADIR,category)
  19. class_num = CATEGORIES.index(category)
  20.  
  21. for img in tqdm(os.listdir(path)):
  22. try:
  23. img_array = cv2.imread(os.path.join(path,img) ,cv2.IMREAD_GRAYSCALE) # convert to array
  24. new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE)) # resize to normalize data size
  25. training_data.append([new_array, class_num]) # add this to our training_data
  26. except Exception as e: # in the interest in keeping the output clean...
  27. pass
  28. #except OSError as e:
  29. # print("OSErrroBad img most likely", e, os.path.join(path,img))
  30. #except Exception as e:
  31. # print("general exception", e, os.path.join(path,img))
  32.  
  33. create_training_data()
  34.  
  35. print(len(training_data))
  36.  
  37. import random
  38.  
  39. random.shuffle(training_data)
  40.  
  41. for sample in training_data[:10]:
  42. print(sample[1])
  43.  
  44. X = []
  45. y = []
  46.  
  47. for features,label in training_data:
  48. X.append(features)
  49. y.append(label)
  50.  
  51. print(X[0].reshape(-1, IMG_SIZE, IMG_SIZE, 1))
  52.  
  53. X = np.array(X).reshape(-1, IMG_SIZE, IMG_SIZE, 1)
  54.  
  55.  
  56. import h5py
  57.  
  58. # import pickle
  59.  
  60. # pickle_out = open("x_test_plastic40.pickle","wb")
  61. # pickle.dump(X, pickle_out)
  62. # pickle_out.close()
  63.  
  64. # pickle_out = open("y_test_plastic40.pickle","wb")
  65. # pickle.dump(y, pickle_out)
  66. # pickle_out.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement