-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoco_to_w3c.py
147 lines (140 loc) · 6.15 KB
/
coco_to_w3c.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import json
import os.path
def createFile(annoList,input_file):
with open(f'{input_file}_to_w3c.w3c.json', 'w', encoding='utf-8') as f:
json.dump(annoList, f, ensure_ascii=False, indent=4)
if(os.path.exists(f'{input_file}_to_w3c.w3c.json')):
print("File converted and saved in current working directory!")
def convert(input_file):
f = open(input_file+".json")
cocoData = json.load(f)
annoList = []
id = 0
for key in cocoData:
if key == "annotations":
for ele in cocoData[key]:
id = id + 1
annoDict = {}
annoDict["@context"] = "http://www.w3.org/ns/anno.jsonld"
annoDict["id"] = id
annoDict["type"] = "Annotation"
annoDict["body"] = []
annoDict["body"].append(
{
"type": "TextualBody",
"value": next(
iter(
item["name"]
for item in cocoData["categories"]
if item["id"] == ele["category_id"]
),
"",
),
}
)
annoDict["target"] = {"selector": []}
for links in cocoData["images"]:
annoDict["target"]["source"] = links['file_name']
break
# check for ellipse
if len(ele["segmentation"][0]) == 144 and ele["bbox"][2] != ele["bbox"][3]:
rx = str(ele["bbox"][2] / 2)
ry = str(ele["bbox"][3] / 2)
cx = ele["segmentation"][0][36]
cy = ele["segmentation"][0][1]
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 (
len(ele["segmentation"][0]) == 144 and ele["bbox"][2] == ele["bbox"][3]
):
r = str(ele["bbox"][2] / 2)
cx = ele["segmentation"][0][36]
cy = ele["segmentation"][0][1]
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 (
len(ele["segmentation"][0]) == 8
and ele["segmentation"][0][0] == ele["bbox"][0]
and ele["segmentation"][0][1] == ele["bbox"][1]
):
pixels = (
str(ele["bbox"][0])
+ ","
+ str(ele["bbox"][1])
+ ","
+ str(ele["bbox"][2])
+ ","
+ str(ele["bbox"][3])
)
annoDict["target"]["selector"].append(
{
"type": "FragmentSelector",
"conformsTo": "http://www.w3.org/TR/media-frags/",
"value": "xywh=pixel:"+pixels,
}
)
# check for polygon
else:
pointsList = []
totalLen = len(ele["segmentation"][0])
n = len(ele["segmentation"][0]) / 2
i = 0
while i < totalLen:
if i == 0:
pointsList.append(str(ele["segmentation"][0][i]))
i = i + 1
continue
try:
if (
ele["segmentation"][0][i] in ele["segmentation"][0]
and ele["segmentation"][0][i + 1] in ele["segmentation"][0]
):
points = (
str(ele["segmentation"][0][i])
+ " "
+ str(ele["segmentation"][0][i + 1])
)
pointsList.append(points)
i = i + 2
except IndexError:
if len(pointsList) - 1 < n:
points = (
str(ele["segmentation"][0][i])
+ " "
+ str(ele["segmentation"][0][i - 1])
)
pointsList.append(points)
pointsList.append(str(ele["segmentation"][0][i]))
break
if totalLen % 2 != 0:
pointsList.append(str(ele["segmentation"][0][totalLen - 1]))
svgPoints = ",".join(pointsList)
annoDict["target"]["selector"].append(
{
"type": "SvgSelector",
"value": f"<svg><polygon points='{svgPoints}'></polygon></svg>",
}
)
annoList.append(annoDict)
createFile(annoList,input_file)
input_file = input("Enter the file name you want to convert(without extension): ")
if not input_file or ' ' in input_file:
print("Please enter the name without spaces!")
elif not os.path.exists(f'{input_file}.json'):
print("Please make sure that the file you want to convert is in the current working directory!")
else:
convert(input_file)