我正在尝试将通过(VIA) JSON文件转换为Labelme文件,但唯一的问题是Labelme中的imageData属性。没有imageData,我无法将JSON文件上传到Labelme工具中。有没有人知道如何获得imageData或任何对解决这个问题有用的东西。
发布于 2019-12-08 10:37:38
你只是不够幸运,在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我对他们的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发布于 2020-07-06 09:30:33
首先,您应该安装labelme,然后尝试以下操作:
data = labelme.LabelFile.load_image_file(img_path)
image_data = base64.b64encode(data).decode('utf-8')输出与手动JSON文件相同。
发布于 2019-10-03 09:44:34
它称为base64类型的图像,您可以通过以下代码将图像转换为base64数据:
import base64
encoded = base64.b64encode(open(img_path, "rb").read())
print(encoded)https://stackoverflow.com/questions/57004792
复制相似问题