【问题标题】:How to save a encoded image using Pickle如何使用 Pickle 保存编码图像
【发布时间】:2019-02-08 04:52:01
【问题描述】:

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

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")

【问题讨论】:

  • 我很困惑。我看不到问题。并且标题谈到了 Pickle,但没有与 Pickle 相关的代码。问题是您要保存不是数据库的数据库。请在您的问题下单击edit 并澄清事情。谢谢。
  • 好的,我重做了,希望这能阐明我想要做什么。

标签: python-3.x image-processing pickle opencv3.0


【解决方案1】:

想通了。

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

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

【讨论】:

    猜你喜欢
    • 2018-02-14
    • 2012-06-28
    • 2015-04-23
    • 2011-12-03
    • 2019-07-19
    • 1970-01-01
    • 2014-09-22
    • 1970-01-01
    • 2021-03-17
    相关资源
    最近更新 更多