-
Notifications
You must be signed in to change notification settings - Fork 57
/
coco_voc.py
102 lines (83 loc) · 3.05 KB
/
coco_voc.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
# --------------------------------------------------------
# Written by Saurabh Gupta
# --------------------------------------------------------
import math, sys, json, os, h5py
import numpy as np
from pycoco.coco import COCO
class coco_voc():
def __init__(self, image_set, devkit_path=None, image_path=None):
self._name = 'coco' + '_' + image_set
self._image_set = image_set
self._devkit_path = self._get_default_path() \
if devkit_path is None else devkit_path
self._data_path = os.path.join(self._devkit_path)
self._image_path = os.path.join(self._data_path, 'images') \
if image_path is None else image_path;
self._image_ext = '.jpg'
image_index_str, self._image_index = self._load_image_set_index()
# Load the annotation file
self._coco_caption_data = COCO(os.path.join(self._devkit_path, 'annotations', 'captions_trainval2014.json'));
assert os.path.exists(self._devkit_path), \
'COCO path does not exist: {}'.format(self._devkit_path)
assert os.path.exists(self._data_path), \
'COCO does not exist: {}'.format(self._data_path)
def get_file_name(self, index):
"""
Returns the file name with the folder in the beginning.
"""
return os.path.join('{:02d}'.format(int(math.floor(int(index)/1e4))), '{}'.format(index))
def image_path_at(self, i):
"""
Return the absolute path to image i in the image sequence.
"""
return self.image_path_from_index(self._image_index[i])
def image_path_from_index(self, index):
"""
Construct an image path from the image's "index" identifier.
"""
pre_fix = '%02d' % int(math.floor(int(index)/1e4))
image_path = os.path.join(self._image_path, self.get_file_name(index) + self._image_ext)
# assert os.path.exists(image_path), 'Path does not exist: {}'.format(image_path)
return image_path
def _load_image_set_index(self):
"""
Load the indexes listed in this dataset's image set file.
"""
# Example path to image set file:
image_set_file = os.path.join(self._data_path, 'splits',
self._image_set + '.ids')
assert os.path.exists(image_set_file), \
'Path does not exist: {}'.format(image_set_file)
with open(image_set_file) as f:
image_index = [x.strip() for x in f.readlines()]
imlist = [int(x) for x in image_index]
return image_index, imlist #[:100]
def _get_default_path(self):
"""
Return the default path where COCO is expected to be installed.
"""
return os.path.join('..', 'data');
@property
def name(self):
return self._name
@property
def coco_caption_data(self):
return self._coco_caption_data
@property
def num_classes(self):
return len(self._classes)
@property
def classes(self):
return self._classes
@property
def class_to_ind(self):
return self._class_to_ind
@property
def image_index(self):
return self._image_index
@property
def num_images(self):
return len(self.image_index)
if __name__ == '__main__':
d = coco_voc('train')
from IPython import embed; embed()