Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Enter the path of your image data folder
- image_data_folder_path = "/data/animals/"
- # initialize the data and labels as an empty list
- #we will reshape the image data and append it in the list-data
- #we will encode the image labels and append it in the list-labels
- data = []
- labels = []
- # grab the image paths and randomly shuffle them
- imagePaths = sorted(list(paths.list_images(image_data_folder_path)))
- #total number images
- total_number_of_images = len(imagePaths)
- print("\n")
- print("Total number of images----->",total_number_of_images)
- #randomly shuffle all the image file name
- random.shuffle(imagePaths)
- # loop over the shuffled input images
- for imagePath in imagePaths:
- #Read the image into a numpy array using opencv
- #all the read images are of different shapes
- image = cv2.imread(imagePath)
- #resize the image to be 32x32 pixels (ignoring aspect ratio)
- #After reshape size of all the images will become 32x32x3
- #Total number of pixels in every image = 32x32x3=3072
- image = cv2.resize(image, (32, 32))
- #flatten converts every 3D image (32x32x3) into 1D numpy array of shape (3072,)
- #(3072,) is the shape of the flatten image
- #(3072,) shape means 3072 columns and 1 row
- image_flatten = image.flatten()
- #Append each image data 1D array to the data list
- data.append(image_flatten)
- # extract the class label from the image path and update the
- label = imagePath.split(os.path.sep)[-2]
- #Append each image label to the labels list
- labels.append(label)
- # scale the raw pixel intensities to the range [0, 1]
- #convert the data and label list to numpy array
- data = np.array(data, dtype="float") / 255.0
- labels = np.array(labels)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement