如何在没有 Caffe 的情况下在 python 中从 LMDB 加载图像?

Nig*_*y13 5 python numpy image lmdb

我想从我创建的 LMDB 数据库加载我的图像和标签数据。我为相应的图像标签对分配一个唯一键并将它们添加到 LMDB(例如 image-000000001、label-000000001)。在保存图像时,我将图像的 numpy-array 使用image.tostring(). 现在在加载 LMDB 时,我发现我可以通过传递我生成的键来非常简单地获取标签,但是图像数据以编码方式显示。做一个numpy.fromstring(lmdb_cursor.get('image-000000001'))不起作用。

在这里看到- 第二个答案,特别是@Ghilas BELHADJ,必须使用 Caffe-datum 对象来首先加载数据,然后使用datum.data. 但是我没有这样的结构,其中使用“数据”和“标签”标签来组织图像和标签。如何从python中的这种LMDB以numpy图像的形式正确读取数据?

在 Lua 中,这可以通过以下方式实现,

    local imgBin -- this is the object returned from cursor:get(image-id)
    local imageByteLen = string.len(imgBin)
    local imageBytes = torch.ByteTensor(imageByteLen):fill(0)
    imageBytes:storage():string(imgBin)
    local img = Image.decompress(imageBytes, 3, 'byte')
    img = Image.rgb2y(img)
    img = Image.scale(img, imgW, imgH)
Run Code Online (Sandbox Code Playgroud)

我不知道如何在 Python 中做到这一点。

小智 1

import lmdb
import cv2
import numpy as np
with lmdb.open(lmdb_dir,readonly=True).begin(write=False) as txn:
    for idx,(key,val) in enumerate(txn.cursor()):
        img = cv2.imdecode(np.fromstring(val,dtype=np.uint8),1)
Run Code Online (Sandbox Code Playgroud)