其他分享
首页 > 其他分享> > labelme生成的json文件修改

labelme生成的json文件修改

作者:互联网

labelme生成的json文件需要修改“imagePath”以及“imageData”

需要注意的是,利用base64生成imageData之后,字符串开头多了'b 以及末尾多了',所以需要额外删除。

import os
import json
import base64

def get_json_data(img_data_folder, json_old_path, img_name):
    with open(json_new_path, 'rb') as f:
        cur_json_dict = json.load(open(json_old_path))
        # modify imagePath
        cur_json_dict["imagePath"] = str(img_name)
        # modify imageData
        img_path = os.path.join(img_data_folder, cur_json_dict["imagePath"])
        cur_json_dict["imageData"] = str(base64.b64encode(open(img_path, "rb").read()))
        # delete 'b and '
        cur_json_dict["imageData"] = cur_json_dict["imageData"][2:-1]
        new_dict = cur_json_dict
    f.close()
    return  new_dict

def write_json_data(json_new_path, dict):
    with open(json_new_path, 'w') as r:
        json.dump(dict, r)
    r.close()
    return

之后依次执行

dict = get_json_data(img_data_folder, json_old_path, img_name)
write_json_data(json_new_path, dict)

这样生成的json文件可以直接用labelme打开。

标签:cur,img,生成,json,dict,path,labelme,data
来源: https://blog.csdn.net/qq_38308028/article/details/120347519