如何使用 Pickle 保存编码图像

How to save a encoded image using Pickle

我在这里做的是对图像进行编码,然后将其添加到数据库变量中包含原始图像路径的列表中,如下所示

database.append[path, encoding]

然后我想将这个数据库变量保存到 pickle 中以供其他程序使用。我将如何去做,因为我还没有正确保存文件。 任何帮助,将不胜感激。 这是我用来生成要保存的变量的方法

def embedDatabase(imagePath, model, metadata):
#Get the metadata


#Perform embedding
# calculated by feeding the aligned and scaled images into the pre-trained network.

'''
#Go through the database and get the embedding for each image
'''
database = []
embedded = np.zeros((metadata.shape[0], 128))
print("Embedding")
for i, m in enumerate(metadata):
    img = imgUtil.loadImage(m.image_path())
    _,img = imgUtil.alignImage(img)
     # scale RGB values to interval [0,1]
    if img is not None:
        img = (img / 255.).astype(np.float32)
        #Get the embedding vectors for the image
        embedded[i] = model.predict(np.expand_dims(img, axis=0))[0]
        database.append([m.image_path(), embedded[i]])

#return the array of embedded images from the database    
return embedded, database

这是加载图片的方法

def loadImage(path):
img = cv2.imread(path, 1)
if img is not None:  
    # OpenCV loads images with color channels
    # in BGR order. So we need to reverse them
    return img[...,::-1]
else:
    pass
    print("There is no Image avaliable")

想通了。

with open("database.pickle", "wb") as f:
     pickle.dump(database, f, pickle.HIGHEST_PROTOCOL)

出于某种原因我需要 pickle.HIGHEST_PROTOCOL 东西