Just as the title states, I am trying to convert two of my ndarrays to a dictionary. However, none of the solutions on other stack overflow pages seem to work(see Create dictionary from two numpy arrays). Here is my code
import os
from PIL import Image, ImageOps
import numpy as np
from pathlib import Path
input_dir = Path("/Users/games/Desktop/ML/ImageBank")
directories = os.listdir(input_dir)
print(os.getcwd() + "elll")
#Directory containing images you wish to convert
directories = os.listdir(input_dir)
print(directories)
index = 0
index2 = 0
index_test = 0
index2_test = 0
size = (64,64)
for folder in directories[0:10]:
#Ignoring .DS_Store dir
if folder == '.DS_Store':
pass
else:
print (folder)
folder2 = os.listdir(str(input_dir) + '/' + folder)
index += 1
len_images = len(folder2)
len_images80 = len_images * 80 #Getting index of image on the 80% mark
len_images20 = len_images * 20 #Getting index of image on the 20% mark
#Iterating through first 80% of images in folder for train data
for image in folder2[0:int(len_images80)]:
print(image + "hello")
if image == ".DS_Store":
pass
else:
index2 += 1
im = Image.open(str(input_dir)+"/"+folder+"/"+image) #Opening image
im = ImageOps.fit(im,size,Image.ANTIALIAS)
im = (np.array(im)) #Converting to numpy array
try:
r = im[:,:,0] #Slicing to get R data
g = im[:,:,1] #Slicing to get G data
b = im[:,:,2] #Slicing to get B data
if index2 != 1:
new_array = np.array([[r] + [g] + [b]], np.uint8) #Creating array with shape (3, 100, 100)
out = np.append(out, new_array, 0) #Adding new image to array shape of (x, 3, 100, 100) where x is image number
elif index2 == 1:
out = np.array([[r] + [g] + [b]], np.uint8) #Creating array with shape (3, 100, 100)
if index == 1 and index2 == 1:
index_array = np.array([[index]])
else:
new_index_array = np.array([[index]], np.int8)
index_array = np.append(index_array, new_index_array, 0)
except Exception as e:
print (e)
print ("Removing image" + image)
print("dang")
os.remove(str(input_dir)+"/"+folder+"/"+image)
#Iterating throught last 20% of image in folder for test data
for image in folder2[len_images-int(len_images20):len_images]:
if image == ".DS_Store":
pass
else:
index2_test += 1
im = Image.open(str(input_dir)+"/"+folder+"/"+image) #Opening image
im = ImageOps.fit(im,size,Image.ANTIALIAS)
im = (np.array(im)) #Converting to numpy array
try:
r = im[:,:,0] #Slicing to get R data
g = im[:,:,1] #Slicing to get G data
b = im[:,:,2] #Slicing to get B data
if index2_test != 1:
new_array_test = np.array([[r] + [g] + [b]], np.uint8) #Creating array with shape (3, 100, 100)
out_test = np.append(out_test, new_array_test, 0) #Adding new image to array shape of (x, 3, 100, 100) where x is image number
elif index2_test == 1:
out_test = np.array([[r] + [g] + [b]], np.uint8) #Creating array with shape (3, 100, 100)
if index == 1 and index2_test == 1:
index_array_test = np.array([[index]])
else:
new_index_array_test = np.array([[index]], np.int8)
index_array_test = np.append(index_array_test, new_index_array_test, 0)
except Exception as e:
print (e)
print("what")
print ("Removing image" + image)
os.remove(str(input_dir)+"/"+folder+"/"+image)
print(index_array)
print(index_array_test)
print(type(out))
print(type(index_array))
# print(out)
dict = {}
for A, B in zip(index_array,out):
dict[A] = B
print(dict)
np.save(os.path.join('/Users/games/Desktop/ML/Data','X_train.npy'), out) #Saving train image arrays
np.save(os.path.join('/Users/games/Desktop/ML/Data','Y_train.npy'), index_array) #Saving train labels
np.save(os.path.join('/Users/games/Desktop/ML/Data','X_test.npy'), out_test) #Saving test image arrays
np.save(os.path.join('/Users/games/Desktop/ML/Data','Y_test.npy'), index_array_test) #Saving test labels
print("hello")
I end up getting the error
Traceback (most recent call last): File "/Users/games/Desktop/ML/ImageConverter.py", line 130, in <module>
dict[A] = B TypeError: unhashable type: 'numpy.ndarray'
Any help would be greatly appreciated