-
Notifications
You must be signed in to change notification settings - Fork 0
/
cocotool.py
218 lines (160 loc) · 7.83 KB
/
cocotool.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import numpy as np
from matplotlib import image as Image
import matplotlib.pyplot as plt
import json
class COCO:
def __init__(self, rootPath):
self.rootPath = rootPath
self.annotations = {
'train': {
'captions': None,
'instances': None,
'person_keypoints': None
},
'val': {
'captions': None,
'instances': None,
'person_keypoints': None
}
}
self.categories = None
self.superCategories = None
def loadAnnotationFile(self, typeData = "train", typeAnnotation="instances"):
assert typeData in ["train", "val"], "typeData has to be either 'train' or 'val'"
assert typeAnnotation in ["captions", "instances", "person_keypoints"], 'typeAnnotation has to be either "captions", "instances" or "person_keypoints"'
fileName = f"{typeAnnotation}_{typeData}2017.json"
if self.annotations[typeData][typeAnnotation] == None:
print(f"Loading {fileName}")
with open(self.rootPath + "/annotations/" + fileName, 'r') as f:
data = json.load(f)
self.annotations[typeData][typeAnnotation] = data
print(f"{fileName} loaded")
return data
else:
#print(f"{fileName} already loaded")
return self.annotations[typeData][typeAnnotation]
def loadInstancesData(self):
# Load train and test (=val) instances data
trainData = self.instancesToData(typeData = "train")
testData = self.instancesToData(typeData = "val")
return (trainData, testData)
def instancesToData(self, typeData="train"):
"""
Generator. Convert the coco instances dataset
"""
assert typeData in ['train', 'val'], "typeData has to be either 'train' or 'val'"
annotations = self.loadAnnotationFile(typeData = typeData, typeAnnotation = "instances")
# Usefull to retrieve image or class by id
images = {i['id']: i for i in annotations['images']}
classes = {i['id']: i for i in annotations['categories']}
for annotation in annotations["annotations"]:
result = annotation.copy()
idCategory = annotation['category_id']
result['super_category'] = classes[idCategory]['supercategory']
result['category_name'] = classes[idCategory]['name']
imageData = images[annotation['image_id']]
image_path = self.rootPath + f"/images/{typeData}2017/" + imageData['file_name']
im = Image.imread(image_path)
result['image'] = np.asarray(im)
# Maybe change the result in a form usefull for a model. Like create a target key.
yield result
def getImageByFileName(self, fileName = '391895.jpg'):
# fileName always have the format 000000000016.jpg
fileName = (fileName.split('.')[0] + ".jpg").zfill(16)
# Try train data
trainAnnotations = self.loadAnnotationFile(typeData = "train", typeAnnotation = "instances")
for image in trainAnnotations['images']:
if image['file_name'] == fileName:
return self.getImageById(image['id'], "train")
# Try test data
trainAnnotations = self.loadAnnotationFile(typeData = "val", typeAnnotation = "instances")
for image in trainAnnotations['images']:
if image['file_name'] == fileName:
return self.getImageById(image['id'], "val")
print("Warning: Image not found")
return None
def getImageById(self, id = 391895, typeData = 'train'):
annotations = self.loadAnnotationFile(typeData = typeData, typeAnnotation = "instances")
imageData = None
for image in annotations['images']:
if image['id'] == id:
imageData = image.copy()
break
if imageData == None:
raise ValueError(f'id not found in {typeData} dataset')
# Fill imageData with every existing instances
imageData['instances'] = []
for instance in annotations['annotations']:
if instance['image_id'] == id:
imageData['instances'].append(instance.copy())
# Add category to each element on the picture
for category in annotations['categories']:
if imageData['instances'][-1]['category_id'] == category['id']:
imageData['instances'][-1]['category'] = category
captions = self.loadAnnotationFile(typeData=typeData, typeAnnotation='captions')
for caption in captions['annotations']:
if caption['image_id'] == imageData['id']:
imageData['caption'] = caption
# Add image to imageData
im = Image.imread(self.rootPath + f"/images/{typeData}2017/" + imageData['file_name'])
imageData['image'] = im
return imageData
def showImageInstancesSegmentation(self, imageData, index = None, minArea = 2000, ax = plt):
if index != None:
self.showSegmentation(imageData['instances'][index])
else:
for instance in imageData['instances']:
if instance['area'] > minArea:
self.showSegmentation(instance, ax = ax)
ax.imshow(imageData['image'])
ax.legend()
#plt.title(imageData['caption']['caption'])
def showSegmentation(self, instance, ax = plt):
if instance['iscrowd'] == 0:
for segmentation in instance['segmentation']:
x = segmentation[::2]
y = segmentation[1::2]
x.append(x[0])
y.append(y[0])
if ("category" in instance):
ax.plot(x,y, label = instance['category']['name'])
else:
ax.plot(x,y)
else:
print("Need to handle crowd")
def showImageInstancesBbox(self, imageData, index = None, minArea = 2000, ax = plt):
if index != None:
instance = imageData['instances'][index]
self.showBbox(instance)
else:
for instance in imageData['instances']:
if instance['area'] > minArea:
self.showBbox(instance, ax = ax)
ax.imshow(imageData['image'])
ax.legend()
#ax.title(imageData['caption']['caption'])
def showBbox(self, instance, ax = plt):
# Print the box around the object
origin = (instance['bbox'][0], instance['bbox'][1])
width = instance['bbox'][2]
height = instance['bbox'][3]
xR = [origin[0], origin[0], origin[0] + width, origin[0] + width, origin[0]]
yR = [origin[1], origin[1] + height, origin[1] + height, origin[1], origin[1]]
if ("category" in instance):
ax.plot(xR,yR, label = instance['category']['name'])
else:
ax.plot(xR,yR)
def getCategories(self):
if self.categories != None:
return self.categories
annotations = self.loadAnnotationFile(typeData = 'train', typeAnnotation = "instances")
categories = [category['name'] for category in annotations['categories']]
self.categories = categories
return categories
def getSuperCategories(self):
if self.superCategories != None:
return self.categories
annotations = self.loadAnnotationFile(typeData = 'train', typeAnnotation = "instances")
superCategories = list(set([category['supercategory'] for category in annotations['categories']]))
self.superCategories = superCategories
return superCategories