3/09/2022

save labelme json data

Save json and image for labelme


..

def make_labelme_json(list_ltrb, img, label, img_full_fn):

h, w, _ = img.shape
shape_dict =[]

for v in list_ltrb:
x1, y1, x2, y2 = v
adict={"label": label, "points":[[x1,y1],[x2,y2]], "group_id": None, "shape_type":"rectangle", "flags":{}}
shape_dict.append(adict)

image_string = cv2.imencode('.jpg', img)[1]
image_string = base64.b64encode(image_string).decode()
#make string image dict
wdict={"version":"4.5.6", "flags": {}, "shapes":shape_dict, "imagePath": img_full_fn, "imageData":image_string}

return wdict


def save_json_img(wdict, labelme_path, fn_only, img):
#save json
str_type = json.dumps(wdict) #dic to json
prefix_fn = "{}{}".format(labelme_path, fn_only)
f= open("{}.json".format(prefix_fn),"w+")
f.write(str_type)
f.close

#save image
cv2.imwrite("{}.jpg".format(prefix_fn), img)

wdict = make_labelme_json(list_ltrb, img,'lp', './'+fn )
save_json_img(wdict, labelme_path, fn_only, img)

..