什么是来自 labelme 工具的 JSON 文件中的 imageData?

Jat*_*pta 5 data-annotations

我正在尝试将VIA (VGG Image Annotator) JSON 文件转换Labelme JSON 文件,但唯一的问题是 Labelme 中的 imageData 属性。如果没有 imageData,我无法将我的 JSON 文件上传到 Labelme 工具。有没有人知道如何获取 imageData 或任何对解决这个问题有用的东西。

Mik*_* M 6

您只是不够幸运,无法在 Google 中找到此内容:)。这些函数可以在LabelMe 的来源中找到:

def img_b64_to_arr(img_b64):
    f = io.BytesIO()
    f.write(base64.b64decode(img_b64))
    img_arr = np.array(PIL.Image.open(f))
    return img_arr

def img_arr_to_b64(img_arr):
    img_pil = PIL.Image.fromarray(img_arr)
    f = io.BytesIO()
    img_pil.save(f, format='PNG')
    img_bin = f.getvalue()
    if hasattr(base64, 'encodebytes'):
        img_b64 = base64.encodebytes(img_bin)
    else:
        img_b64 = base64.encodestring(img_bin)
    return img_b64
Run Code Online (Sandbox Code Playgroud)

我对他们的 if hasattr(base64, 'encodebytes'):... 有问题,它生成多余的 \n 和 b' ',所以我将第二个重写为

import codecs

def encodeImageForJson(image):
    img_pil = PIL.Image.fromarray(image, mode='RGB')
    f = io.BytesIO()
    img_pil.save(f, format='PNG')
    data = f.getvalue()
    encData = codecs.encode(data, 'base64').decode()
    encData = encData.replace('\n', '')
    return encData
Run Code Online (Sandbox Code Playgroud)


小智 5

首先,您应该安装labelme,然后尝试以下操作:

data = labelme.LabelFile.load_image_file(img_path)
image_data = base64.b64encode(data).decode('utf-8')
Run Code Online (Sandbox Code Playgroud)

输出与手动的 JSON 文件相同。


Jim*_*hen 2

它被称为图像的base64类型,您可以通过以下代码将图像转换为base64数据:

import base64
encoded = base64.b64encode(open(img_path, "rb").read())
print(encoded)
Run Code Online (Sandbox Code Playgroud)