savtadepth / src /code /make_dataset.py
Dean
remove secondary requirements (i.e. not things that are explicitly installed by the user), fix normalization problem, and use tqdm for image processing progress bar
068408a
raw
history blame
No virus
3.78 kB
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#######################################################################################
# The MIT License
# Copyright (c) 2014 Hannes Schulz, University of Bonn <schulz@ais.uni-bonn.de>
# Copyright (c) 2013 Benedikt Waldvogel, University of Bonn <mail@bwaldvogel.de>
# Copyright (c) 2008-2009 Sebastian Nowozin <nowozin@gmail.com>
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#######################################################################################
#
# Helper script to convert the NYU Depth v2 dataset Matlab file into a set of
# PNG and JPEG images.
#
# See https://github.com/deeplearningais/curfil/wiki/Training-and-Prediction-with-the-NYU-Depth-v2-Dataset
from __future__ import print_function
import h5py
import numpy as np
import os
import scipy.io
import sys
import cv2
from tqdm import tqdm
def convert_image(i, scene, depth, image, folder):
# depth is given in meters (Kinect has a range of around .5m and 4.5m but can sense also at 8m)
normalized_depth = cv2.normalize(depth, None, 0, 255, cv2.NORM_MINMAX)
cv2.imwrite("%s/%05d_depth.png" % (folder, i), normalized_depth)
image = image[:, :, ::-1]
image_black_boundary = np.zeros((480, 640, 3), dtype=np.uint8)
image_black_boundary[7:474, 7:632, :] = image[7:474, 7:632, :]
cv2.imwrite("%s/%05d.jpg" % (folder, i), image_black_boundary)
if __name__ == "__main__":
if len(sys.argv) < 4:
print("usage: %s <h5_file> <train_test_split> <out_folder>" % sys.argv[0], file=sys.stderr)
sys.exit(0)
h5_file = h5py.File(sys.argv[1], "r")
# h5py is not able to open that file. but scipy is
train_test = scipy.io.loadmat(sys.argv[2])
out_folder = sys.argv[3]
test_images = set([int(x) for x in train_test["testNdxs"]])
train_images = set([int(x) for x in train_test["trainNdxs"]])
print("%d training images" % len(train_images))
print("%d test images" % len(test_images))
depth = h5_file['depths']
print("reading", sys.argv[1])
images = h5_file['images']
scenes = [u''.join(chr(c[0]) for c in h5_file[obj_ref]) for obj_ref in h5_file['sceneTypes'][0]]
for i, image in tqdm(enumerate(images), desc="processing images", total=len(images)):
idx = int(i) + 1
if idx in train_images:
train_test = "train"
else:
assert idx in test_images, "index %d neither found in training set nor in test set" % idx
train_test = "test"
folder = "%s/%s/%s" % (out_folder, train_test, scenes[i])
if not os.path.exists(folder):
os.makedirs(folder)
convert_image(i, scenes[i], depth[i, :, :].T, image.T, folder)
print("Finished")