如何解决不可散列类型的问题:“ numpy.ndarray”

时间:2019-12-13 06:01:53

标签: python python-3.x numpy typeerror numpy-ndarray

我正在尝试这样做:

for myimage in Images:
    image = cv2.imread(myimage)  
    processed_image = Image_Preprocessing(image) 

    name = os.path.basename(myimage) # Get image name to save it as a Class
    class_name = str(name) 
    Image_count= Image_count+1

    # To divide the image to mxm patches
    for r in range(0,processed_image.shape[0],patch_size):
        for c in range(0,processed_image.shape[1],patch_size):
             value= processed_image[r:r+patch_size, c:c+patch_size]


             #Search for similar patch 
             #if the value already exists in dict only update the classes list
             #else add the value in dict and update the classes list 
             if any((value == patch).all() for patch in Patches_dic.keys()):
                if (class_name in Patches_dic[value] ):  
                    continue
                else:
                    Patches_dic[value].append(class_name) 
             else:
                 list_Of_Classes.append(class_name)
                 Patches_dic.update({value : list_Of_Classes })   #Key: image patch  Value: List of Classes

但是,我遇到了这个错误

  

Patches_dic.update({tuple(value):list_Of_Classes})

     

TypeError:不可哈希类型:'numpy.ndarray'

我不知道自己在做什么以及如何解决这个问题:(

1 个答案:

答案 0 :(得分:0)

如@hpaulj所述。字典键不能是列表类型的对象,例如ndarrayslist

根据提供的代码,我猜测value存储着ndarray格式的一部分图像。因此,如果您想将其保留为键,请尝试对其进行散列,然后将图像的散列作为键。请参阅this以了解如何正确地将图像表示为键。

相关问题