Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. import numpy as np
  2. import cPickle
  3. import cv2
  4.  
  5. def extractImagesAndLabels(path, file):
  6. f = open(path+file, 'rb')
  7. dict = cPickle.load(f)
  8. images = dict['data']
  9. images = np.reshape(images, (10000, 3, 32, 32))
  10. labels = dict['labels']
  11. return images, labels
  12.  
  13. def extractCategories(path, file):
  14. f = open(path+file, 'rb')
  15. dict = cPickle.load(f)
  16. return dict['label_names']
  17.  
  18. def saveCifarImage(array, path, file):
  19. # array is 3x32x32. cv2 needs 32x32x3
  20. array = array.transpose(1,2,0)
  21. # array is RGB. cv2 needs BGR
  22. array = cv2.cvtColor(array, cv2.COLOR_RGB2BGR)
  23. # save to PNG file
  24. return cv2.imwrite(path+file+".png", array)
  25.  
  26. imgarray, lblarray = extractImagesAndLabels("cifar-10-batches-py/", "data_batch_1")
  27. print imgarray.shape
  28. print len(lblarray)
  29.  
  30. categories = extractCategories("cifar-10-batches-py/", "batches.meta")
  31. print categories
  32.  
  33. for i in range(0,10):
  34. saveCifarImage(imgarray[i], "./", "toto"+(str)(i))
  35. print categories[lblarray[i]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement