0

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

wwii
  • 23,232
  • 7
  • 37
  • 77
Dylan Ong
  • 786
  • 2
  • 6
  • 14
  • That's a lot of code. Any chance of providing a [mre] - emphasis on minimal - just enough to illustrate the problem, even if it is a *toy/fake* example? What do you want the dictionary keys to be? What are `(index_array,out)` - part of a [mre] is a minimal example of the **relavant** data. – wwii Jul 25 '20 at 00:40
  • 2
    Basically an array can't be used as the key for a `dict`. List's canpt either. Numbers, strings and tuples are usual keys, – hpaulj Jul 25 '20 at 00:44
  • @wwii that's a good idea. However, my watered down versions work just fine, so I was wondering if the issue was somewhere else in my code – Dylan Ong Jul 25 '20 at 00:55
  • We do not have access to `directories` - I'm wondering how we would be able to reproduce your problem. You haven't indicated what you expect the dictionary keys to be or why they might be something other than you expected. – wwii Jul 25 '20 at 15:44
  • wwii Hello. Sorry for the inconvenience. @hpaulj solved my issue. – Dylan Ong Jul 25 '20 at 17:19

0 Answers0