-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaving_json_converter.py
126 lines (118 loc) · 5.42 KB
/
saving_json_converter.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import json
import sys
def createFile(annoList):
sys.stdout.write(json.dumps(annoList))
def convert(input_file):
viaData = json.loads(input_file)
annoList = []
id = 0
viaData = json.loads(viaData[0])
for key in viaData:
if key == "_via_img_metadata":
for ele in viaData[key]:
for region in viaData[key][ele]["regions"]:
annoDict = {}
id = id + 1
annoDict["@context"] = "http://www.w3.org/ns/anno.jsonld"
annoDict["id"] = str(id)
annoDict["type"] = "Annotation"
annoDict["body"] = []
annoDict["target"] = {"selector": []}
annoDict["target"]["source"] = viaData[key][ele]["filename"]
annoDict["body"].append(
{
"type": "TextualBody",
"value": region["region_attributes"]["type"]
}
)
# check for ellipse
if region["shape_attributes"]["name"] == "ellipse":
rx = region["shape_attributes"]["rx"]
ry = region["shape_attributes"]["ry"]
cx = region["shape_attributes"]["cx"]
cy = region["shape_attributes"]["cy"]
ellipsePath = f'<svg><ellipse cx="{cx}" cy="{cy}" rx="{rx}" ry="{ry}"></ellipse></svg>'
annoDict["target"]["selector"].append(
{
"type": "SvgSelector",
"value": ellipsePath,
}
)
# check for circle
elif (
region["shape_attributes"]["name"] == "circle"
):
r = region["shape_attributes"]["r"]
cx = region["shape_attributes"]["cx"]
cy = region["shape_attributes"]["cy"]
circlePath = (
f'<svg><circle cx="{cx}" cy="{cy}" r="{r}"></circle></svg>'
)
annoDict["target"]["selector"].append(
{
"type": "SvgSelector",
"value": circlePath,
}
)
# check for square/rect
elif (
region["shape_attributes"]["name"] == "rect"
):
pixels = (
str(region["shape_attributes"]["x"])
+ ","
+ str(region["shape_attributes"]["y"])
+ ","
+ str(region["shape_attributes"]["width"])
+ ","
+ str(region["shape_attributes"]["height"])
)
annoDict["target"]["selector"].append(
{
"type": "SvgSelector",
"conformsTo": "http://www.w3.org/TR/media-frags/",
"value": "xywh=pixel:"+pixels,
}
)
elif (
region["shape_attributes"]["name"] == "point"
):
r = 4
cx = region["shape_attributes"]["cx"]
cy = region["shape_attributes"]["cy"]
pointPath = (
f'<svg><circle cx="{cx}" cy="{cy}" r="{r}"></circle></svg>'
)
annoDict["target"]["selector"].append(
{
"type": "SvgSelector",
"value": pointPath,
}
)
# check for polygon
else:
pointsList = []
totalLen = len(region["shape_attributes"]["all_points_x"]) + len(region["shape_attributes"]["all_points_y"])
i = 0
j = 0
k = 0
while i < totalLen:
if i%2 == 0:
pointsList.append(str(region["shape_attributes"]['all_points_x'][j]))
j = j + 1
else:
pointsList.append(str(region["shape_attributes"]['all_points_y'][k]))
k = k + 1
i = i + 1
svgPoints = ",".join(pointsList)
annoDict["target"]["selector"].append(
{
"type": "SvgSelector",
"value": f"<svg><polygon points='{svgPoints}'></polygon></svg>",
}
)
annoList.append(annoDict)
createFile(annoList)
if __name__ == '__main__':
input = sys.stdin.readlines()
convert(json.dumps(input))