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 3.8 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
  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. # Helper script to convert the NYU Depth v2 dataset Matlab file into a set of
  28. # PNG and JPEG images.
  29. #
  30. # See https://github.com/deeplearningais/curfil/wiki/Training-and-Prediction-with-the-NYU-Depth-v2-Dataset
  31. from __future__ import print_function
  32. import h5py
  33. import numpy as np
  34. import os
  35. import scipy.io
  36. import sys
  37. import cv2
  38. def convert_image(i, scene, depth, image, folder):
  39. img_depth = depth * 1000.0
  40. img_depth_uint16 = img_depth.astype(np.uint16)
  41. normalized_depth = np.zeros(img_depth_uint16.shape)
  42. normalized_depth = cv2.normalize(img_depth_uint16, normalized_depth, 0, 255, cv2.NORM_MINMAX)
  43. cv2.imwrite("%s/%05d_depth.png" % (folder, i), normalized_depth)
  44. image = image[:, :, ::-1]
  45. image_black_boundary = np.zeros((480, 640, 3), dtype=np.uint8)
  46. image_black_boundary[7:474, 7:632, :] = image[7:474, 7:632, :]
  47. cv2.imwrite("%s/%05d.jpg" % (folder, i), image_black_boundary)
  48. if __name__ == "__main__":
  49. if len(sys.argv) < 4:
  50. print("usage: %s <h5_file> <train_test_split> <out_folder>" % sys.argv[0], file=sys.stderr)
  51. sys.exit(0)
  52. h5_file = h5py.File(sys.argv[1], "r")
  53. # h5py is not able to open that file. but scipy is
  54. train_test = scipy.io.loadmat(sys.argv[2])
  55. out_folder = sys.argv[3]
  56. test_images = set([int(x) for x in train_test["testNdxs"]])
  57. train_images = set([int(x) for x in train_test["trainNdxs"]])
  58. print("%d training images" % len(train_images))
  59. print("%d test images" % len(test_images))
  60. depth = h5_file['depths']
  61. print("reading", sys.argv[1])
  62. images = h5_file['images']
  63. scenes = [u''.join(chr(c) for c in h5_file[obj_ref]) for obj_ref in h5_file['sceneTypes'][0]]
  64. print("processing images")
  65. for i, image in enumerate(images):
  66. print("image", i + 1, "/", len(images))
  67. idx = int(i) + 1
  68. if idx in train_images:
  69. train_test = "train"
  70. else:
  71. assert idx in test_images, "index %d neither found in training set nor in test set" % idx
  72. train_test = "test"
  73. folder = "%s/%s/%s" % (out_folder, train_test, scenes[i])
  74. if not os.path.exists(folder):
  75. os.makedirs(folder)
  76. convert_image(i, scenes[i], depth[i, :, :].T, image.T, folder)
  77. print("Finished")
Tip!

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

Comments

Loading...