-
Notifications
You must be signed in to change notification settings - Fork 0
/
coco_pipeline.py
101 lines (97 loc) · 3.97 KB
/
coco_pipeline.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
import json
import sys
def createFile(annoList):
sys.stdout.write(json.dumps(annoList))
def convert(input):
cocoData = json.loads(input)
annoList = []
id = 0
cocoData = json.loads(cocoData[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"] = str(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 item in cocoData["images"]:
if item["id"] == ele["image_id"]:
annoDict["target"]["source"] = item["file_name"]
# 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 polygon
else:
pointsList = []
totalLen = len(ele["segmentation"][0])
i = 0
while i < totalLen:
if i == 0:
pointsList.append(str(ele["segmentation"][0][i]))
i = i + 1
continue
try:
points = (
str(ele["segmentation"][0][i])
+ " "
+ str(ele["segmentation"][0][i + 1])
)
pointsList.append(points)
i = i + 2
except IndexError:
break
pointsList.append(str(ele["segmentation"][0][i]))
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))