-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_coco.py
More file actions
154 lines (132 loc) · 4.65 KB
/
load_coco.py
File metadata and controls
154 lines (132 loc) · 4.65 KB
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
148
149
150
151
152
153
154
import base64
import json
import os
import random
from io import BytesIO
from math import trunc
import numpy as np
import requests
from PIL import Image as PILimage
from PIL import ImageDraw as PILimageDraw
"""
Script to load COCO-2017 dataset from scratch
"""
class COcoDataset:
def __init__(self, image_dir, annotation_path):
self.image_dir = image_dir
self.annotation_path = annotation_path
self.color = color = [
"blue",
"purple",
"red",
"green",
"orange",
"salmon",
"pink",
"gold",
"orchid",
"slateblue",
"limegreen",
"seagreen",
"darkgreen",
"olive",
"teal",
"aquamarine",
"steelblue",
"powderblue",
"dodgerblue",
"navy",
"magenta",
"sienna",
"maroon",
]
json_file = open(self.annotation_path)
self.coco = json.load(json_file)
json_file.close()
self.process_info()
self.process_licenses()
self.process_categories()
self.process_image()
self.process_segmentations()
def process_info(self):
self.info = self.coco["info"]
def process_licenses(self):
self.licenses = self.coco["licenses"]
def process_categories(self):
self.categories = {}
self.super_categories = {}
for category in self.coco["categories"]:
cat_id = category["id"]
super_category = category["supercategory"]
if cat_id not in self.categories:
self.categories[cat_id] = category # add category to the category dict
else:
print("ERROR: skipping duplicate category id : {}".format(category))
if super_category not in self.super_categories:
self.super_categories[super_category] = {cat_id}
else:
self.super_categories[super_category] |= {cat_id}
def process_image(self):
self.images = {}
for image in self.coco["images"]:
image_id = image["id"]
if image_id not in self.images:
self.images[image_id] = image
else:
print("ERROR: skipping duplicate image id : {}".format(image))
def process_segmentations(self):
self.segmentations = {}
for segmentation in self.coco["annotations"]:
image_id = segmentation["image_id"]
if image_id not in self.segmentations:
self.segmentations[image_id] = []
self.segmentations[image_id].append(segmentation)
def display_info(self):
print("Dataste Info")
print("============")
for key, item in self.info.items():
print("{}: {}".format(key, item))
requirements = [
["description", str],
["url", str],
["version", str],
["year", int],
["contributor", str],
["date_created", str],
]
for req, req_type in requirements:
if req not in self.info:
print("ERROR: {} is missing".format(req))
elif type(self.info[req]) != req_type:
print("ERROR: {} should be type {}".format(req, req_type))
print()
def display_licenses(self):
print("Licenses Info")
print("============")
for license in self.licenses:
for key, item in license.items():
print("{}: {}".format(key, item))
requirements = [["id", int], ["url", str], ["name", str]]
for req, req_type in requirements:
if req not in license:
print("ERROR: {} is missing".format(req))
elif type(license[req]) != req_type:
print("ERROR: {} should be type {}".format(req, req_type))
print()
def display_categories(self):
print("Category info")
print("=============")
for cat_key, id_val in self.super_categories.items():
print("Super Category: {}".format(cat_key))
for cat_id in id_val:
print(" {} {}".format(cat_id, self.categories[cat_id]["name"]))
print("")
if __name__ == "__main__":
image_root = "C:/Users/rashi/Desktop/yolact/data/coco/images"
annot_file = (
"C:/Users/rashi/Desktop/yolact/data/coco/annotations/instances_train2017.json"
)
cocodataset = COcoDataset(image_dir=image_root, annotation_path=annot_file)
cocodataset.display_info()
cocodataset.display_licenses()
cocodataset.display_categories()