Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. #Enter the path of your image data folder
  2. image_data_folder_path = "/data/animals/"
  3.  
  4. # initialize the data and labels as an empty list
  5. #we will reshape the image data and append it in the list-data
  6. #we will encode the image labels and append it in the list-labels
  7. data = []
  8. labels = []
  9.  
  10. # grab the image paths and randomly shuffle them
  11. imagePaths = sorted(list(paths.list_images(image_data_folder_path)))
  12.  
  13. #total number images
  14. total_number_of_images = len(imagePaths)
  15. print("\n")
  16. print("Total number of images----->",total_number_of_images)
  17.  
  18. #randomly shuffle all the image file name
  19. random.shuffle(imagePaths)
  20.  
  21. # loop over the shuffled input images
  22. for imagePath in imagePaths:
  23.  
  24. #Read the image into a numpy array using opencv
  25. #all the read images are of different shapes
  26. image = cv2.imread(imagePath)
  27.  
  28. #resize the image to be 32x32 pixels (ignoring aspect ratio)
  29. #After reshape size of all the images will become 32x32x3
  30. #Total number of pixels in every image = 32x32x3=3072
  31. image = cv2.resize(image, (32, 32))
  32.  
  33. #flatten converts every 3D image (32x32x3) into 1D numpy array of shape (3072,)
  34. #(3072,) is the shape of the flatten image
  35. #(3072,) shape means 3072 columns and 1 row
  36. image_flatten = image.flatten()
  37.  
  38. #Append each image data 1D array to the data list
  39. data.append(image_flatten)
  40.  
  41. # extract the class label from the image path and update the
  42. label = imagePath.split(os.path.sep)[-2]
  43.  
  44. #Append each image label to the labels list
  45. labels.append(label)
  46. # scale the raw pixel intensities to the range [0, 1]
  47. #convert the data and label list to numpy array
  48. data = np.array(data, dtype="float") / 255.0
  49. labels = np.array(labels)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement