-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjson2xml.py
57 lines (45 loc) · 1.64 KB
/
json2xml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import json
import os
import numpy as np
from mltools.src.log.logger import logger
from mltools.src.utils.img2xml.multi_object_process import img2xml_multiobj
def j2x_conversion(jsonPath: str):
"""this function is used to convert jsons(labelme) to xmls(labelImg)"""
with open(jsonPath, "r", encoding="utf-8") as f:
jsonObj = json.load(f)
if isinstance(jsonObj, str):
jsonObj = json.loads(jsonObj)
tmpPath = jsonPath.replace(".json", ".xml")
aimPath = tmpPath
folder = os.path.abspath(jsonPath)
filename = jsonObj["imagePath"]
path = tmpPath
width = jsonObj["imageWidth"]
height = jsonObj["imageHeight"]
objs = []
shapes = jsonObj["shapes"]
# print(type(shapes))
for shape in shapes:
label = shape["label"]
points = shape["points"]
# print(type(points))
tmp = np.array(points)
# print(tmp)
obj = dict()
obj["name"] = label
obj["difficult"] = 0
bndbox = dict()
bndbox["xmin"] = (
np.min(tmp[:, 0]) if np.min(tmp[:, 0]) > 0 else 1
) # https://github.com/AlexeyAB/darknet the bounding box cannot be 0
bndbox["xmax"] = np.max(tmp[:, 0])
bndbox["ymin"] = np.min(tmp[:, 1]) if np.min(tmp[:, 1]) > 0 else 1
bndbox["ymax"] = np.max(tmp[:, 1])
obj["bndbox"] = bndbox
if bndbox["ymax"] - bndbox["ymin"] < 10 or bndbox["xmax"] - bndbox["xmin"] < 10:
pass
else:
objs.append(obj)
img2xml_multiobj(tmpPath, aimPath, folder, filename, path, width, height, objs)
# print(objs)
logger.info("Done! Check {}".format(tmpPath))