1
- """bootstrap .py
1
+ """prepare_egohands .py
2
2
3
- This script downloads the 'egohands' dataset and convert the annotations
3
+ This script downloads the 'egohands' dataset and convert its annotations
4
4
into bounding boxes in KITTI format.
5
5
6
- Output of this script is data in KITTI format :
6
+ Output of this script:
7
7
8
+ ./egohands_data.zip
9
+ ./egohands
10
+ ├── (egohands dataset unzipped)
11
+ └── ......
8
12
./egohands_kitti_formatted
9
- ├── CARDS_COURTYARD_B_T_frame_0011.jpg
10
- ├── CARDS_COURTYARD_B_T_frame_0011.txt
11
- ├── ......
12
- ├── PUZZLE_OFFICE_T_S_frame_2697.jpg
13
- └── PUZZLE_OFFICE_T_S_frame_2697.txt
13
+ ├── images
14
+ │ ├── CARDS_COURTYARD_B_T_frame_0011.jpg
15
+ │ ├── ......
16
+ │ └── PUZZLE_OFFICE_T_S_frame_2697.jpg
17
+ └── labels
18
+ ├── CARDS_COURTYARD_B_T_frame_0011.txt
19
+ ├── ......
20
+ └── PUZZLE_OFFICE_T_S_frame_2697.txt
14
21
"""
15
22
16
23
22
29
from zipfile import ZipFile
23
30
from shutil import rmtree , copyfile
24
31
25
- import cv2
32
+ import numpy as np
26
33
from scipy .io import loadmat
34
+ import cv2
27
35
28
36
29
37
EGOHANDS_DATASET_URL = \
30
38
'http://vision.soic.indiana.edu/egohands_files/egohands_data.zip'
31
39
EGOHANDS_DIR = './egohands'
32
40
EGOHANDS_DATA_DIR = './egohands/_LABELLED_SAMPLES'
33
41
CONVERTED_DIR = './egohands_kitti_formatted'
42
+ CONVERTED_IMG_DIR = './egohands_kitti_formatted/images'
43
+ CONVERTED_LBL_DIR = './egohands_kitti_formatted/labels'
34
44
35
45
VISUALIZE = False # visualize each image (for debugging)
36
46
@@ -83,10 +93,10 @@ def polygon_to_box(polygon):
83
93
if len (polygon ) < 3 : # a polygon has at least 3 vertices
84
94
return None
85
95
86
- x_min = min (polygon [:, 0 ])
87
- y_min = min (polygon [:, 1 ])
88
- x_max = max (polygon [:, 0 ])
89
- y_max = max (polygon [:, 1 ])
96
+ x_min = np . min (polygon [:, 0 ])
97
+ y_min = np . min (polygon [:, 1 ])
98
+ x_max = np . max (polygon [:, 0 ])
99
+ y_max = np . max (polygon [:, 1 ])
90
100
91
101
x_min = int (math .floor (x_min ))
92
102
y_min = int (math .floor (y_min ))
@@ -159,19 +169,19 @@ def convert_one_folder(folder):
159
169
src_jpg = frame + '.jpg'
160
170
dst_jpg = folder + '_' + src_jpg
161
171
copyfile (os .path .join (folder_path , src_jpg ),
162
- os .path .join (CONVERTED_DIR , dst_jpg ))
172
+ os .path .join (CONVERTED_IMG_DIR , dst_jpg ))
163
173
# generate txt (the KITTI annotation corresponding to the jpg)
164
174
dst_txt = folder + '_' + frame + '.txt'
165
175
boxes = []
166
- with open (os .path .join (CONVERTED_DIR , dst_txt ), 'w' ) as f :
176
+ with open (os .path .join (CONVERTED_LBL_DIR , dst_txt ), 'w' ) as f :
167
177
for polygon in polygons [i ]:
168
178
box = polygon_to_box (polygon )
169
179
if box :
170
180
boxes .append (box )
171
181
f .write (box_to_line (box ) + '\n ' )
172
182
173
183
if VISUALIZE :
174
- img = cv2 .imread (os .path .join (CONVERTED_DIR , dst_jpg ))
184
+ img = cv2 .imread (os .path .join (CONVERTED_IMG_DIR , dst_jpg ))
175
185
for box in boxes :
176
186
cv2 .rectangle (img , (box [0 ], box [1 ]), (box [2 ], box [3 ]),
177
187
(0 , 224 , 0 ), 2 )
@@ -190,6 +200,9 @@ def egohands_to_kitti():
190
200
3. convert the original annotations ('polygon.mat') into
191
201
bounding boxes and write a KITTI txt file for each image.
192
202
"""
203
+ rmtree (CONVERTED_DIR , ignore_errors = True )
204
+ os .makedirs (CONVERTED_IMG_DIR )
205
+ os .makedirs (CONVERTED_LBL_DIR )
193
206
for folder in os .listdir (EGOHANDS_DATA_DIR ):
194
207
convert_one_folder (folder )
195
208
@@ -209,8 +222,6 @@ def main():
209
222
zf .extractall (EGOHANDS_DIR )
210
223
211
224
logging .info ('Copying jpg files and converting annotations...' )
212
- rmtree (CONVERTED_DIR , ignore_errors = True )
213
- os .makedirs (CONVERTED_DIR )
214
225
egohands_to_kitti ()
215
226
216
227
logging .info ('All done.' )
0 commit comments