我正在尝试将VIA(VGG图像注释器)JSON文件转换为Labelme JSON文件,但唯一的问题是Labelme中的imageData属性。如果没有 imageData,我无法将 JSON 文件上传到 Labelme 工具。有谁知道如何获取 imageData 或任何对解决此问题有用的东西。
你只是不够幸运,在谷歌中找到了这个:)。这些功能可以在 , 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
我对他们的 if hasattr(base64, 'encodebytes'):... 有问题,它会产生多余的 和 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
首先,您应该安装
labelme
,然后尝试以下操作:
data = labelme.LabelFile.load_image_file(img_path)
image_data = base64.b64encode(data).decode('utf-8')
输出与手动JSON文件相同。
称为图像的base64类型,可以通过以下代码将图像转换为base64数据:
import base64
encoded = base64.b64encode(open(img_path, "rb").read())
print(encoded)
对我有用的是: 将图像数据设置为“空” 另外不要忘记在 imagePath 中传递路径。