"examples/research_projects/pplm/requirements.txt" did not exist on "827c5194940d48ac0d5aca44567fc6432794ec70"
coco.py.bk 3.02 KB
Newer Older
Sugon_ldc's avatar
Sugon_ldc committed
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
cocoDict = {
    "info": info,
    "images": [image],
    "annotations": [annotation],
    "categories": [
        {
            "id": int,
            "name": str,
            "supercategory": str,
        }
    ],
    "licenses": [license],
}
license = {
    "id": int,
    "name": str,
    "url": str,
}
image = {
    "id": int,
    "width": int,
    "height": int,
    "file_name": str,
    "license": int,
    "flickr_url": str,
    "coco_url": str,
    "date_captured": datetime,
}
annotation = {
    "id": int,
    "image_id": int,
    "category_id": int,
    "segmentation": [polygon],
    "area": float,
    "bbox": [x, y, width, height],
}
info = {
    "year": int,
    "version": str,
    "description": str,
    "contributor": str,
    "url": str,
    "date_created": datetime,
}
import datetime


class CoCoAnn:
    def __init__(self, cocoFile=None):
        self.dict = {
            "info": {},
            "images": [],
            "annotations": [],
            "categories": [],
            "licenses": [],
        }
        self.annId = 0

    def setInfo(
        self,
        year: int = "",
        version="",
        description="",
        contributor="",
        url="",
        date_created="",
    ):
        # if not year:
        #     now = datetime.now()
        #     year = now.strftime("%Y")
        # # TODO: datetime
        # if not date_created:
        #     pass
        self.dict["info"] = {
            "year": year,
            "version": version,
            "description": description,
            "contributor": contributor,
            "url": url,
            "date_created": date_created,
        }

    def setCategories(self, categories):
        self.dict["categories"] = categories

    def addCategory(self, id, name, supercategory=""):
        cat = {
            "id": int,
            "name": str,
            "supercategory": str,
        }
        self.dict["categories"].append(cat)

    def setLicenses(self, licenses):
        self.licenses = licenses

    def addLicense(self, id, name, url):
        license = {
            "id": int,
            "name": str,
            "url": str,
        }
        self.dict["licenses"].append(license)

    def addImage(
        self,
        id,
        width,
        height,
        file_name,
        license="",
        flickr_url="",
        coco_url="",
        date_captured="",
    ):
        image = {
            "id": id,
            "width": width,
            "height": height,
            "file_name": file_name,
            "license": license,
            "flickr_url": flickr_url,
            "coco_url": coco_url,
            "date_captured": date_captured,
        }
        self.dict["images"].append(image)

    def addAnnotation(
        self,
        image_id,
        category_id,
        segmentation,
        bbox,
        area,
        id,
    ):
        {
            "id": int,
            "image_id": int,
            "category_id": int,
            "segmentation": [polygon],
            "area": float,
            "bbox": [x, y, width, height],
        }