Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

make_dataset.py 5.1 KB

You have to be logged in to leave a comment. Sign In
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
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #######################################################################################
  4. # The MIT License
  5. # Copyright (c) 2014 Hannes Schulz, University of Bonn <schulz@ais.uni-bonn.de>
  6. # Copyright (c) 2013 Benedikt Waldvogel, University of Bonn <mail@bwaldvogel.de>
  7. # Copyright (c) 2008-2009 Sebastian Nowozin <nowozin@gmail.com>
  8. # Permission is hereby granted, free of charge, to any person obtaining a copy
  9. # of this software and associated documentation files (the "Software"), to deal
  10. # in the Software without restriction, including without limitation the rights
  11. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. # copies of the Software, and to permit persons to whom the Software is
  13. # furnished to do so, subject to the following conditions:
  14. #
  15. # The above copyright notice and this permission notice shall be included in all
  16. # copies or substantial portions of the Software.
  17. #
  18. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. # SOFTWARE.
  25. #######################################################################################
  26. #
  27. # See https://github.com/deeplearningais/curfil/wiki/Training-and-Prediction-with-the-NYU-Depth-v2-Dataset
  28. """Helper script to convert the NYU Depth v2 dataset Matlab file into a set of PNG and JPEG images.
  29. Receives 3 Files from argparse:
  30. <h5_file> - Contains the original images, depths maps, and scene types
  31. <train_test_split> - contains two numpy arrays with the index of the
  32. images based on the split to train and test sets.
  33. <out_folder> - Name of the folder to save the original and depth images.
  34. Every image in the DB will have it's twine B&W image that indicates the depth
  35. in the image. the images will be read, converted by the convert_image function
  36. and finally saved to path based on train test split and Scene types.
  37. """
  38. from __future__ import print_function
  39. import h5py
  40. import numpy as np
  41. import os
  42. import scipy.io
  43. import sys
  44. import cv2
  45. from tqdm import tqdm
  46. def convert_image(index, depth_map, img, output_folder):
  47. """Processes data images and depth maps
  48. :param index: int, image index
  49. :param depth_map: numpy array, image depth - 2D array.
  50. :param img: numpy array, the original RGB image - 3D array.
  51. :param output_folder: path to save the image in.
  52. Receives an image with it's relevant depth map.
  53. Normalizes the depth map, and adds a 7 px boundary to the original image.
  54. Saves both image and depth map to the appropriate processed data folder.
  55. """
  56. # Normalize the depth image
  57. # normalized_depth = cv2.normalize(depth_map, None, 0, 255, cv2.NORM_MINMAX)
  58. img_depth = depth_map * 25.0
  59. cv2.imwrite("%s/%05d_depth.png" % (output_folder, index), img_depth)
  60. # Adding black frame to original image
  61. img = img[:, :, ::-1] # Flipping the image from RGB to BGR for opencv
  62. image_black_boundary = np.zeros(img.shape, dtype=np.uint8)
  63. image_black_boundary[7:image_black_boundary.shape[0] - 6, 7:image_black_boundary.shape[1] - 6, :] = \
  64. img[7:img.shape[0] - 6, 7:img.shape[1] - 6, :]
  65. cv2.imwrite("%s/%05d.jpg" % (output_folder, index), image_black_boundary)
  66. if __name__ == "__main__":
  67. # Check if got all needed input for argparse
  68. if len(sys.argv) != 4:
  69. print("usage: %s <h5_file> <train_test_split> <out_folder>" % sys.argv[0], file=sys.stderr)
  70. sys.exit(0)
  71. # load arguments to variables
  72. h5_file = h5py.File(sys.argv[1], "r")
  73. train_test = scipy.io.loadmat(sys.argv[2]) # h5py is not able to open that file. but scipy is
  74. out_folder = sys.argv[3]
  75. # Extract images *indexes* for train and test data sets
  76. test_images = set([int(x) for x in train_test["testNdxs"]])
  77. train_images = set([int(x) for x in train_test["trainNdxs"]])
  78. print("%d training images" % len(train_images))
  79. print("%d test images" % len(test_images))
  80. # Grayscale
  81. depth = h5_file['depths']
  82. print("Reading", sys.argv[1])
  83. images = h5_file['images'] # (num_channels, height, width)
  84. # Extract all sceneTypes per image - "office", "classroom", etc.
  85. scenes = [u''.join(chr(c[0]) for c in h5_file[obj_ref]) for obj_ref in h5_file['sceneTypes'][0]]
  86. for i, image in tqdm(enumerate(images), desc="Processing images", total=len(images)):
  87. idx = int(i) + 1
  88. if idx in train_images:
  89. train_test = "train"
  90. else:
  91. assert idx in test_images, "index %d neither found in training set nor in test set" % idx
  92. train_test = "test"
  93. # Create path to save image in
  94. folder = "%s/%s/%s" % (out_folder, train_test, scenes[i])
  95. if not os.path.exists(folder):
  96. os.makedirs(folder)
  97. convert_image(i, depth[i, :, :].T, image.T, folder)
  98. print("Finished")
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...