Skip to content

Commit f31167b

Browse files
committed
Create images/ and labels/ subdirectories for converted KITTI formatted data, and also suffle images before splitting train/val sets
1 parent 943828c commit f31167b

File tree

2 files changed

+43
-23
lines changed

2 files changed

+43
-23
lines changed

create_kitti_tf_record.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717
# ========================================================================
1818

1919

20-
r"""Convert KITTI detection dataset to TFRecord for object_detection.
20+
"""Convert KITTI detection dataset to TFRecord for object detection.
2121
2222
Example usage:
23+
2324
python3 create_kitti_tf_record.py \
2425
--data_dir=egohands_kitti_formatted \
25-
--output_path=egohands
26+
--output_path=data/egohands \
27+
--classes_to_use=hand \
28+
--label_map_path=data/egohands_label_map.pbtxt \
29+
--validation_set_size=500
2630
"""
2731

2832

@@ -34,6 +38,7 @@
3438
import hashlib
3539
import io
3640
import os
41+
import random
3742

3843
import numpy as np
3944
import PIL.Image as pil
@@ -72,7 +77,9 @@ def convert_kitti_to_tfrecords(data_dir, output_path, classes_to_use,
7277
"""Convert the KITTI detection dataset to TFRecords.
7378
7479
Args:
75-
data_dir: The full path containing all jpg and txt files.
80+
data_dir: The full path containing KITTI formatted data, with all
81+
jpg files located in <data_dir>/images/ and all txt files in
82+
<data_dir>/labels/.
7683
output_path: The path to which TFRecord files will be written.
7784
The TFRecord with the training set will be located at:
7885
<output_path>_train.tfrecord
@@ -92,8 +99,8 @@ def convert_kitti_to_tfrecords(data_dir, output_path, classes_to_use,
9299
train_count = 0
93100
val_count = 0
94101

95-
annotation_dir = data_dir
96-
image_dir = data_dir
102+
annotation_dir = os.path.join(data_dir, 'labels')
103+
image_dir = os.path.join(data_dir, 'images')
97104

98105
train_writer = tf.python_io.TFRecordWriter('%s_train.tfrecord'%
99106
output_path)
@@ -102,6 +109,8 @@ def convert_kitti_to_tfrecords(data_dir, output_path, classes_to_use,
102109

103110
images = sorted(tf.gfile.ListDirectory(image_dir))
104111
images = [f for f in images if f.endswith('jpg')] # only keep jpg files
112+
assert len(images) > 0
113+
random.shuffle(images)
105114
for idx, img_name in enumerate(images):
106115
img_num = img_name.split('.')[0]
107116
is_validation_img = idx < validation_set_size

bootstrap.py prepare_egohands.py

+29-18
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1-
"""bootstrap.py
1+
"""prepare_egohands.py
22
3-
This script downloads the 'egohands' dataset and convert the annotations
3+
This script downloads the 'egohands' dataset and convert its annotations
44
into bounding boxes in KITTI format.
55
6-
Output of this script is data in KITTI format:
6+
Output of this script:
77
8+
./egohands_data.zip
9+
./egohands
10+
├── (egohands dataset unzipped)
11+
└── ......
812
./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
1421
"""
1522

1623

@@ -22,15 +29,18 @@
2229
from zipfile import ZipFile
2330
from shutil import rmtree, copyfile
2431

25-
import cv2
32+
import numpy as np
2633
from scipy.io import loadmat
34+
import cv2
2735

2836

2937
EGOHANDS_DATASET_URL = \
3038
'http://vision.soic.indiana.edu/egohands_files/egohands_data.zip'
3139
EGOHANDS_DIR = './egohands'
3240
EGOHANDS_DATA_DIR = './egohands/_LABELLED_SAMPLES'
3341
CONVERTED_DIR = './egohands_kitti_formatted'
42+
CONVERTED_IMG_DIR = './egohands_kitti_formatted/images'
43+
CONVERTED_LBL_DIR = './egohands_kitti_formatted/labels'
3444

3545
VISUALIZE = False # visualize each image (for debugging)
3646

@@ -83,10 +93,10 @@ def polygon_to_box(polygon):
8393
if len(polygon) < 3: # a polygon has at least 3 vertices
8494
return None
8595

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])
90100

91101
x_min = int(math.floor(x_min))
92102
y_min = int(math.floor(y_min))
@@ -159,19 +169,19 @@ def convert_one_folder(folder):
159169
src_jpg = frame + '.jpg'
160170
dst_jpg = folder + '_' + src_jpg
161171
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))
163173
# generate txt (the KITTI annotation corresponding to the jpg)
164174
dst_txt = folder + '_' + frame + '.txt'
165175
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:
167177
for polygon in polygons[i]:
168178
box = polygon_to_box(polygon)
169179
if box:
170180
boxes.append(box)
171181
f.write(box_to_line(box) + '\n')
172182

173183
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))
175185
for box in boxes:
176186
cv2.rectangle(img, (box[0], box[1]), (box[2], box[3]),
177187
(0, 224, 0), 2)
@@ -190,6 +200,9 @@ def egohands_to_kitti():
190200
3. convert the original annotations ('polygon.mat') into
191201
bounding boxes and write a KITTI txt file for each image.
192202
"""
203+
rmtree(CONVERTED_DIR, ignore_errors=True)
204+
os.makedirs(CONVERTED_IMG_DIR)
205+
os.makedirs(CONVERTED_LBL_DIR)
193206
for folder in os.listdir(EGOHANDS_DATA_DIR):
194207
convert_one_folder(folder)
195208

@@ -209,8 +222,6 @@ def main():
209222
zf.extractall(EGOHANDS_DIR)
210223

211224
logging.info('Copying jpg files and converting annotations...')
212-
rmtree(CONVERTED_DIR, ignore_errors=True)
213-
os.makedirs(CONVERTED_DIR)
214225
egohands_to_kitti()
215226

216227
logging.info('All done.')

0 commit comments

Comments
 (0)