如何在Python中不使用Caffe加载LMDB图像?

5
我希望从我创建的LMDB数据库中加载图像和标签数据。我为相应的图像-标签对分配一个唯一的键,并将它们添加到LMDB中(例如,image-000000001,label-000000001)。在保存图像时,我使用image.tostring()将图像的numpy数组转换为字符串。现在在加载LMDB时,我发现可以通过传递我生成的键轻松获取标签,但是图像数据以编码方式显示。使用numpy.fromstring(lmdb_cursor.get('image-000000001'))不起作用。
我看到这里 - 具体来说是由@Ghilas BELHADJ提供的第二个答案,必须使用Caffe-datum对象先加载数据,然后使用datum.data获取图像。但是我没有这样的结构,其中使用“data”和“label”标记组织图像和标签。如何正确地从此类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)

我不知道如何在Python中做这个。

1个回答

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)

img = cv2.imdecode(np.frombuffer(val,dtype=np.uint8),1)只是一个小更新,请使用 np.frombuffer(),因为如果输入是 Unicode,则字符串不起作用。 - Vinay Verma

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接