-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdata_read.py
289 lines (218 loc) · 10.3 KB
/
data_read.py
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import os
from PIL import Image
import matplotlib.pyplot as plt
import glob
import numpy as np
import random
def get_paths_and_transform():
root_d = os.path.join('./depth_selection/KITTI/Sparse_Lidar')
root_rgb = os.path.join('./depth_selection/KITTI/RGB')
glob_sparse_lidar = "train/*_sync/proj_depth/velodyne_raw/image_0[2,3]/*.png"
glob_sparse_lidar = os.path.join(root_d,glob_sparse_lidar)
all_lidar_path_with_new=glob.glob(glob_sparse_lidar)
all_lidar_path_without_new=[i for i in all_lidar_path_with_new if not (('left' in i) or('right' in i)) ]
paths_sparse_lidar = sorted(all_lidar_path_without_new)
def get_rgb_paths(p):
ps = p.split('/')
pnew = '/'.join([root_rgb]+ps[-6:-4]+ps[-2:-1]+['data']+ps[-1:])
return pnew
glob_rgb = [get_rgb_paths(i) for i in paths_sparse_lidar]
return paths_sparse_lidar,glob_rgb
def img_path_to_lidar(img_path):
#img_path:'./Dataset/KITTI/RGB/train/2011_09_26_drive_0051_sync/image_02/data/0000000432.png'
path_list=img_path.split('/')
return path_list[0]+'/'+path_list[1]+'/'+path_list[2]+'/'+'Sparse_Lidar/'+path_list[4]+'/'+path_list[5]+'/proj_depth/velodyne_raw/'+path_list[6]+'/'+path_list[8]
def img_path_to_ground_truth(img_path):
#img_path:'./Dataset/KITTI/RGB/train/2011_09_26_drive_0051_sync/image_02/data/0000000432.png'
path_list=img_path.split('/')
return path_list[0]+'/'+path_list[1]+'/'+path_list[2]+'/'+'ground_truth/'+path_list[4]+'/'+path_list[5]+'/proj_depth/groundtruth/'+path_list[6]+'/'+path_list[8]
def rgb_read(filename):
assert os.path.exists(filename), "file not found: {}".format(filename)
img_file = Image.open(filename)
# rgb_png = np.array(img_file, dtype=float) / 255.0 # scale pixels to the range [0,1]
rgb_png = np.array(img_file, dtype='uint8') # in the range [0,255]
img_file.close()
return rgb_png
def depth_new_read(filename):
depth=io.imread(filename)
depth=depth/255.0*100
return depth
def depth_read(filename):
# loads depth map D from png file
# and returns it as a numpy array,
# for details see readme.txt
assert os.path.exists(filename), "file not found: {}".format(filename)
img_file = Image.open(filename)
depth_png = np.array(img_file, dtype=int)
img_file.close()
# make sure we have a proper 16bit depth map here.. not 8bit!
assert np.max(depth_png) > 255, \
"np.max(depth_png)={}, path={}".format(np.max(depth_png),filename)
depth = depth_png.astype(np.float) / 256.
# depth[depth_png == 0] = -1.
depth = np.array(Image.fromarray(depth).resize((1216,352), Image.NEAREST))
depth = np.expand_dims(depth,-1)
return depth
lidar_path,img_path=get_paths_and_transform()
total_img=len(img_path)
total_lidar=len(lidar_path)
class Data_load():
def __init__(self):
lidar_path,img_path=get_paths_and_transform()
self.lidar_path=lidar_path
self.img_path=img_path
self.num_sample=[i for i in range(len(self.img_path))]
np.random.shuffle(self.num_sample)
self.index=0
self.total_sample=len(self.img_path)
def read_batch(self,batch_size=4):
i=0
img_batch=[]
lidar_batch=[]
gt_batch=[]
while (i<(batch_size)):
i=i+1
#img=rgb_read(img_path[index])
depth=depth_read(self.lidar_path[ self.num_sample[self.index]])
gt_path=img_path_to_ground_truth(self.img_path[self.num_sample[self.index]])
ground_truth=depth_read(gt_path)
lidar_batch.append(depth)
#img_batch.append(img)
gt_batch.append(ground_truth)
self.index=self.index+1
if self.index+batch_size>self.total_sample:
return [0],[1]
else:
return np.asarray(lidar_batch),np.asarray(gt_batch)#np.asarray(img_batch),
def read_one_val(index,line_number=64):
ground_truth_path='./depth_selection/val_selection_cropped/groundtruth_depth'
if line_number==64:
velodyne_raw_path='./depth_selection/val_selection_cropped/velodyne_raw'
if line_number==32:
velodyne_raw_path='./depth_selection/val_selection_cropped/velodyne_raw_32'
if line_number==16:
velodyne_raw_path='./depth_selection/val_selection_cropped/velodyne_raw_16'
intrinsics_path='./depth_selection/val_selection_cropped/intrinsics'
image_path='./depth_selection/val_selection_cropped/image'
instance_path='./depth_selection/val_selection_cropped/instance'
ground_truth=os.listdir('./depth_selection/val_selection_cropped/groundtruth_depth')
image=os.listdir('./depth_selection/val_selection_cropped/image')
velodyne_raw=os.listdir('./depth_selection/val_selection_cropped/velodyne_raw')
intrinsics=os.listdir('./depth_selection/val_selection_cropped/intrinsics')
i=image[index]
img_one=[]
lidar_one=[]
intrinsics_matrix=[]
ground_thuth_one=[]
img_file = Image.open(image_path+'/'+i)
# rgb_png = np.array(img_file, dtype=float) / 255.0 # scale pixels to the range [0,1]
rgb_png = np.array(img_file, dtype='uint8') # in the range [0,255]
img_file.close()
img=rgb_png
img_file = Image.open(velodyne_raw_path+ '/'+i[:27]+'velodyne_raw'+i[32:])
depth_png = np.array(img_file, dtype=int)
img_file.close()
depth = depth_png.astype(np.float32) / 256.
# depth[depth_png == 0] = -1.
depth = np.expand_dims(depth,-1)
'''
temp_i=i
ii=temp_i.replace("image","predicted")
img_file = Image.open(instance_path+ '/'+ii)
instance_png = np.array(img_file, dtype=int)
img_file.close()
instance_png = np.expand_dims(instance_png,-1)
'''
img_file = Image.open(ground_truth_path+ '/'+i[:27]+'groundtruth_depth'+i[32:])
ground_truth = np.array(img_file, dtype=int)
img_file.close()
ground_truth = ground_truth.astype(np.float) / 256.
F = open(intrinsics_path+'/'+i[:len(i)-4]+'.txt','r')
intrinsics_matrix_per=F.readline().split(' ')
intrinsics_matrix_per=[float(n) for n in intrinsics_matrix_per if not(n=='\n')]
F.close()
img_one.append(img)
lidar_one.append(depth[:,:,0])
ground_thuth_one.append(ground_truth)
intrinsics_matrix.append(intrinsics_matrix_per)
return np.asarray(img_one),np.asarray(lidar_one),np.asarray(ground_thuth_one), np.asarray(intrinsics_matrix)#, np.asarray(instance_png)
def read_one_test(index):
ground_truth_path='./depth_selection/test_depth_completion_anonymous/groundtruth_depth'
velodyne_raw_path='./depth_selection/test_depth_completion_anonymous/velodyne_raw'
intrinsics_path='./depth_selection/test_depth_completion_anonymous/intrinsics'
image_path='./depth_selection/test_depth_completion_anonymous/image'
image=os.listdir('./depth_selection/test_depth_completion_anonymous/image')
velodyne_raw=os.listdir('./depth_selection/test_depth_completion_anonymous/velodyne_raw')
intrinsics=os.listdir('./depth_selection/test_depth_completion_anonymous/intrinsics')
i=image[index]
img_one=[]
lidar_one=[]
intrinsics_matrix=[]
ground_thuth_one=[]
img_file = Image.open(image_path+'/'+i)
# rgb_png = np.array(img_file, dtype=float) / 255.0 # scale pixels to the range [0,1]
rgb_png = np.array(img_file, dtype='uint8') # in the range [0,255]
img_file.close()
img=rgb_png
img_file = Image.open(velodyne_raw_path+ '/'+i[:27])
depth_png = np.array(img_file, dtype=int)
img_file.close()
depth = depth_png.astype(np.float) / 256.
# depth[depth_png == 0] = -1.
depth = np.expand_dims(depth,-1)
F = open(intrinsics_path+'/'+i[:len(i)-4]+'.txt','r')
intrinsics_matrix_per=F.readline().split(' ')
intrinsics_matrix_per=[float(n) for n in intrinsics_matrix_per if not(n=='\n')]
F.close()
intrinsics_matrix.append(intrinsics_matrix_per)
img_one.append(img)
lidar_one.append(depth[:,:,0])
return np.asarray(img_one),np.asarray(lidar_one),image[index], np.asarray(intrinsics_matrix)
class KITTI_demo_loader():
def __init__(self):
self.lidar_sequence=[]
self.rgb_sequence=[]
self.demo_KITTI()
self.index=0
def demo_KITTI(self):
RGB_image_path=glob.glob('./depth_selection/KITTI/RGB/train/2011_09_26_drive_0039_sync/image_02/data/*.png')
Lidar_image_path=glob.glob('./depth_selection/KITTI/Sparse_Lidar/train/2011_09_26_drive_0039_sync/proj_depth/*/image_02/*.png')
for i in Lidar_image_path:
temp=i.split('/')[-1]
for j in RGB_image_path:
if j.split('/')[-1]==temp:
self.lidar_sequence.append(i)
self.rgb_sequence.append(j)
self.rgb_sequence.sort()
self.lidar_sequence.sort()
def rgb_read(self,filename):
assert os.path.exists(filename), "file not found: {}".format(filename)
img_file = Image.open(filename)
# rgb_png = np.array(img_file, dtype=float) / 255.0 # scale pixels to the range [0,1]
rgb_png = np.array(img_file, dtype='uint8') # in the range [0,255]
rgb_png =np.array(Image.fromarray(rgb_png).resize((1216,352), Image.NEAREST))
img_file.close()
return rgb_png
def depth_read(self,filename):
# loads depth map D from png file
# and returns it as a numpy array,
# for details see readme.txt
assert os.path.exists(filename), "file not found: {}".format(filename)
img_file = Image.open(filename)
depth_png = np.array(img_file, dtype=int)
img_file.close()
# make sure we have a proper 16bit depth map here.. not 8bit!
assert np.max(depth_png) > 255, \
"np.max(depth_png)={}, path={}".format(np.max(depth_png),filename)
depth = depth_png.astype(np.float) / 256.
# depth[depth_png == 0] = -1.
depth = np.array(Image.fromarray(depth).resize((1216,352), Image.NEAREST))
depth = np.expand_dims(depth,-1)
return depth
def read_one_image(self):
lidar_ind_path=self.lidar_sequence[self.index]
rgb_ind_path=self.rgb_sequence[self.index]
name_ind_path=lidar_ind_path.split('/')
self.index=self.index+1
gt_path=img_path_to_ground_truth(rgb_ind_path)
return self.rgb_read(rgb_ind_path),self.depth_read(lidar_ind_path),self.depth_read(gt_path)