Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is imageData in JSON file which comes from labelme tool?

I am trying to convert VIA (VGG Image Annotator) JSON file to Labelme JSON file, but the only problem is imageData attribute in Labelme. I am not able to upload my JSON file into Labelme tool without imageData. Does Anyone has idea how to get imageData or anything which is useful to solve this problem.

like image 741
Jatin Gupta Avatar asked Jul 12 '19 10:07

Jatin Gupta


People also ask

What is LabelMe format?

LabelMe is an actively developed open source graphical image annotation tool inspired by the app of the same name released in 2012 by MIT CSAIL. ‍ It is capable of annotating images for object detection, segmentation, and classification (along with polygon, circle, line, and point annotations).

What is LabelMe annotation tool?

Labelme is an open source annotation tool based on http://labelme.csail.mit.edu. It was written in Python to support manual image polygonal annotation for object detection, classification, and segmentation. Labelme lets you create various shapes, including polygons, circles, rectangles, lines, line strips, and points.


3 Answers

You were just not lucky enough to find this in Google :) . The functions can be found in , LabelMe's sources:

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

I had problems with theirs if hasattr(base64, 'encodebytes'):..., it generates superfluous \n and b' ', so I rewrote the second one as

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
like image 129
Mikhail M Avatar answered Sep 21 '22 01:09

Mikhail M


First, you shoud install labelme, then try the following:

data = labelme.LabelFile.load_image_file(img_path)
image_data = base64.b64encode(data).decode('utf-8')

Output is the same as JSON file by manual.

like image 29
yaodi Avatar answered Sep 24 '22 01:09

yaodi


It is called base64 type of an image, you can turn image into base64 data by the following code:

import base64
encoded = base64.b64encode(open(img_path, "rb").read())
print(encoded)
like image 27
Jim Chen Avatar answered Sep 21 '22 01:09

Jim Chen