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
}
转换代码:
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'] = None
final_json['frames'] = None
#解析from_json
load_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=-1
for cls in classes_locations:
i=i+1
print(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 = 1
for dir_json in dir_jsons:
print("第" + str(i) + "/" + str(len(dir_jsons)) + "张 ===>" + dir_json)
i += 1
from_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)