labelme的 json格式转化成自己的json格式
生活随笔
收集整理的這篇文章主要介紹了
labelme的 json格式转化成自己的json格式
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
labelme的json格式:
{"version": "4.5.6","flags": {},"shapes": [{"label": "1","points": [[1609.6923076923076,884.6153846153846],[1988.1538461538462,1172.3076923076924]],"group_id": null,"shape_type": "rectangle","flags": {}},{"label": "0","points": [[1352.7692307692307,3.0769230769230766],[1925.076923076923,578.4615384615385]],"group_id": null,"shape_type": "rectangle","flags": {}}],"imagePath": "..\\imgs\\0001.jpg","imageData": null,"imageHeight": 1492,"imageWidth": 2040 }自己的json格式:
{"name": "0001.jpg","frames": [{"timestamp": "10000","objects": [{"category": "1","id": 0,"attributes": {"occluded": false,"truncated": false,"trafficLightCorlor": "none"},"box2d": {"x1": 1609.6923076923076,"y1": 884.6153846153846,"x2": 1988.1538461538462,"y2": 1172.3076923076924}},{"category": "0","id": 1,"attributes": {"occluded": false,"truncated": false,"trafficLightCorlor": "none"},"box2d": {"x1": 1352.7692307692307,"y1": 3.0769230769230766,"x2": 1925.076923076923,"y2": 578.4615384615385}}]}],"imagePath": "..\\imgs\\0001.jpg","imageData": null,"imageHeight": 1492,"imageWidth": 2040 }轉(zhuǎn)換代碼:
import argparse import json import cv2 import numpy as np import os''' parser = argparse.ArgumentParser() parser.add_argument("square", help="display a square of a given number", type=int) args = parser.parse_args() print(args.square** 2) ''' def json_for_train(from_json, to_json):final_json = dict()final_json['name'] = Nonefinal_json['frames'] = None#解析from_jsonload_f = open(from_json)anno = json.load(load_f)version = anno["version"]flags = anno["flags"]imagePath = anno["imagePath"]imageData = anno["imageData"]imageHeight = anno["imageHeight"]imageWidth = anno["imageWidth"]shapes = []object1=[]fname=imagePath.split("\\")[-1] #獲得圖片的名字#print(fname)classes_locations = anno["shapes"]i=-1for cls in classes_locations:i=i+1print(cls)attr = {"occluded":False,"truncated": False,"trafficLightCorlor": "none"}print(cls.get("points"))box = {"x1": cls.get("points")[0][0],"y1": cls.get("points")[0][1],"x2": cls.get("points")[1][0],"y2": cls.get("points")[1][1]}#print(box)print("label:"+cls.get("label"))mid = {"category": cls.get("label"),"id": i,"attributes": attr,"box2d": box}object1.append(mid)print("******")print(object1)print("====")shape = {"timestamp": "10000","objects": object1,}shapes.append(shape)print("----")#print(shapes)result_json = {"name": fname,"frames": shapes,"imagePath": imagePath,"imageData": imageData,"imageHeight": imageHeight,"imageWidth": imageWidth}print(result_json)labelme_json = json.dumps(result_json, indent=4, ensure_ascii=False)with open(to_json, "w+") as fp:fp.write(labelme_json)if __name__ == '__main__':from_jsons = './annotation/'to_jsons = './annotation_new/'dir_jsons = os.listdir(from_jsons)print(dir_jsons)i = 1for dir_json in dir_jsons:print("第" + str(i) + "/" + str(len(dir_jsons)) + "張 ===>" + dir_json)i += 1from_name = os.path.join(from_jsons, dir_json)to_name = os.path.join(to_jsons, dir_json)try:json_for_train(from_name, to_name)except Exception as e:print(e)總結(jié)
以上是生活随笔為你收集整理的labelme的 json格式转化成自己的json格式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 案例:用户登录界面最全详解(JavaWe
- 下一篇: 简单验证码的原理