forked from deepdrive/deepdrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
261 lines (216 loc) · 8.12 KB
/
utils.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
import glob
import inspect
import os
import stat
import sys
import threading
import time
import zipfile
import tempfile
import h5py
import numpy as np
import requests
from clint.textui import progress
from subprocess import Popen, PIPE
import config as c
import logs
def normalize(a):
amax = a.max()
amin = a.min()
arange = amax - amin
a = (a - amin) / arange
return a
def preprocess_image(image):
start = time.time()
image = (image.astype(np.float32, copy=False)
** 0.45 # gamma correct
* 255.)
image = np.clip(image, a_min=0, a_max=255)\
.astype('uint8', copy=False)
end = time.time()
log.debug('preprocess_capture_image took %rms', (end - start) * 1000.)
return image
def preprocess_depth(depth):
depth = depth.astype('float64', copy=False)
# x = list(range(depth.size))
# y = depth.flatten()
# plt.scatter(x, y)
# plt.show()
depth = depth ** -(1 / 3.)
depth = normalize(depth)
return depth
def depth_heatmap(depth):
red = depth
green = 1.0 - np.abs(0.5 - depth) * 2.
blue = 1. - depth
ret = np.array([red, green, blue])
ret = np.transpose(ret, (1, 2, 0))
ret = (ret * 255).astype('uint8', copy=False)
return ret
def obj2dict(obj, exclude=None):
ret = {}
exclude = exclude or []
for name in dir(obj):
if not name.startswith('__') and name not in exclude:
value = getattr(obj, name)
if not inspect.ismethod(value):
value = getattr(obj, name)
ret[name] = value
return ret
def save_hdf5(out, filename):
if 'DEEPDRIVE_NO_THREAD_SAVE' in os.environ:
save_hdf5_thread(out, filename)
else:
thread = threading.Thread(target=save_hdf5_thread, args=(out, filename))
thread.start()
def save_hdf5_thread(out, filename):
os.makedirs(os.path.dirname(filename), exist_ok=True)
log.debug('Saving to %s', filename)
opts = dict(compression='lzf', fletcher32=True)
with h5py.File(filename, 'w') as f:
for i, frame in enumerate(out):
frame_grp = f.create_group('frame_%s' % str(i).zfill(10))
for j, camera in enumerate(frame['cameras']):
camera_grp = frame_grp.create_group('camera_%s' % str(j).zfill(5))
camera_grp.create_dataset('image', data=camera['image'], **opts)
camera_grp.create_dataset('depth', data=camera['depth'], **opts)
del camera['image_data']
del camera['depth_data']
del camera['image']
del camera['depth']
for k, v in camera.items():
camera_grp.attrs[k] = v
del frame['cameras']
for k, v in frame.items():
frame_grp.attrs[k] = v
log.info('Saved to %s', filename)
def read_hdf5(filename, save_png_dir=None):
ret = []
with h5py.File(filename, 'r') as file:
for i, frame_name in enumerate(file):
frame = file[frame_name]
out_frame = dict(frame.attrs)
out_cameras = []
for camera_name in frame:
camera = frame[camera_name]
out_camera = dict(camera.attrs)
out_camera['image'] = camera['image'].value
out_camera['depth'] = camera['depth'].value
out_cameras.append(out_camera)
if save_png_dir is not None:
save_camera(out_camera['image'], out_camera['depth'], save_dir=save_png_dir, name=str(i).zfill(10))
out_frame['cameras'] = out_cameras
ret.append(out_frame)
return ret
def save_camera(image, depth, save_dir, name):
from scipy.misc import imsave
imsave(os.path.join(save_dir, 'i_' + name + '.png'), image)
imsave(os.path.join(save_dir, 'z_' + name + '.png'), depth)
def show_camera(image, depth):
from scipy.misc import toimage
toimage(image).show()
toimage(depth).show()
input('Enter any key to continue')
def read_hdf5_manual(recording_dir):
save_png_dir = os.path.join(recording_dir, 'test_view')
os.makedirs(save_png_dir)
read_hdf5(os.path.join(recording_dir, '2017-11-22_0105_26AM', '0000000001.hdf5'), save_png_dir=save_png_dir)
def is_debugging():
for frame in inspect.stack():
if frame[1].endswith("pydevd.py"):
return True
return False
def download(url, directory, warn_existing=True, overwrite=False):
"""Useful for downloading a folder / zip file from dropbox/s3/cloudfront and unzipping it to path"""
if has_stuff(directory, warn_existing, overwrite):
return
else:
os.makedirs(directory, exist_ok=True)
log.info('Downloading %s to %s...', url, directory)
request = requests.get(url, stream=True)
filename = url.split('/')[-1]
if '?' in filename:
filename = filename[:filename.index('?')]
location = os.path.join(tempfile.gettempdir(), filename)
with open(location, 'wb') as f:
if request.status_code == 404:
raise RuntimeError('Download URL not accessible %s' % url)
total_length = int(request.headers.get('content-length'))
for chunk in progress.bar(request.iter_content(chunk_size=1024), expected_size=(total_length / 1024) + 1):
if chunk:
f.write(chunk)
f.flush()
log.info('done.')
zip_ref = zipfile.ZipFile(location, 'r')
log.info('Unzipping temp file %s to %s...', location, directory)
try:
zip_ref.extractall(directory)
print('done.')
except Exception:
print('You may want to close all programs that may have these files open or delete existing '
'folders this is trying to overwrite')
raise
finally:
zip_ref.close()
os.remove(location)
def dir_has_stuff(path):
return os.path.isdir(path) and os.listdir(path)
def file_has_stuff(path):
return os.path.isfile(path) and os.path.getsize(path) > 0
def has_stuff(path, warn_existing=False, overwrite=False):
if os.path.exists(path) and (dir_has_stuff(path) or file_has_stuff(path)):
if warn_existing:
print('%s exists, do you want to re-download and overwrite the existing files (y/n)?' % path, end=' ')
overwrite = input()
if 'n' in overwrite.lower():
print('USING EXISTING %s - Try rerunning and overwriting if you run into problems.' % path)
return True
elif not overwrite:
return True
return False
def ensure_executable(path):
if c.IS_UNIX:
st = os.stat(path)
os.chmod(path, st.st_mode | stat.S_IEXEC)
def get_sim_bin_path():
path = None
if c.REUSE_OPEN_SIM:
return None
elif c.IS_LINUX:
path = c.SIM_PATH + '/LinuxNoEditor/DeepDrive/Binaries/Linux/DeepDrive-Linux-Shipping'
elif c.IS_MAC:
raise NotImplementedError('Support for OSX not yet implemented, see FAQs')
elif c.IS_WINDOWS:
paths = glob.glob(os.path.join(c.SIM_PATH, 'WindowsNoEditor', 'DeepDrive', 'Binaries') + '/Win64/*.exe')
if not paths:
path = None
else:
path = paths[0]
if path and not os.path.exists(path):
path = None
return path
def run_command(cmd, cwd=None, env=None, throw=True, verbose=False, print_errors=True):
def say(*args):
if verbose:
print(*args)
say(cmd)
if not isinstance(cmd, list):
cmd = cmd.split()
process = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=cwd, env=env)
result, err = process.communicate()
if not isinstance(result, str):
result = ''.join(map(chr, result))
result = result.strip()
say(result)
if process.returncode != 0:
if not isinstance(err, str):
err = ''.join(map(chr, err))
err_msg = ' '.join(cmd) + ' finished with error ' + err.strip()
if throw:
raise RuntimeError(err_msg)
elif print_errors:
print(err_msg)
return result, process.returncode
log = logs.get_log(__name__)
if __name__ == '__main__':
download('https://d1y4edi1yk5yok.cloudfront.net/sim/asdf.zip', r'C:\Users\a\src\beta\deepdrive-agents-beta\asdf')