-
Notifications
You must be signed in to change notification settings - Fork 353
/
demo.py
417 lines (328 loc) · 14.1 KB
/
demo.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import os
import sys
import glob
import time
import math
import argparse
import numpy as np
# Computer Vision
import cv2
from scipy import ndimage
from skimage.transform import resize
# Visualization
import matplotlib.pyplot as plt
plasma = plt.get_cmap('plasma')
# UI and OpenGL
from PySide2 import QtCore, QtGui, QtWidgets, QtOpenGL
from OpenGL import GL, GLU
from OpenGL.arrays import vbo
from OpenGL.GL import shaders
import glm
# Argument Parser
parser = argparse.ArgumentParser(description='High Quality Monocular Depth Estimation via Transfer Learning')
parser.add_argument('--model', default='nyu.h5', type=str, help='Trained Keras model file.')
args = parser.parse_args()
# Image shapes
height_rgb, width_rgb = 480, 640
height_depth, width_depth = height_rgb // 2, width_rgb // 2
rgb_width = width_rgb
rgb_height = height_rgb
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
global graph,model
graph = tf.compat.v1.get_default_graph()
def load_model():
# Kerasa / TensorFlow
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '5'
from keras.models import load_model
from layers import BilinearUpSampling2D
# Custom object needed for inference and training
custom_objects = {'BilinearUpSampling2D': BilinearUpSampling2D, 'depth_loss_function': None}
# Load model into GPU / CPU
return load_model(args.model, custom_objects=custom_objects, compile=False)
# Function timing
ticTime = time.time()
def tic(): global ticTime; ticTime = time.time()
def toc(): print('{0} seconds.'.format(time.time() - ticTime))
# Conversion from Numpy to QImage and back
def np_to_qimage(a):
im = a.copy()
return QtGui.QImage(im.data, im.shape[1], im.shape[0], im.strides[0], QtGui.QImage.Format_RGB888).copy()
def qimage_to_np(img):
img = img.convertToFormat(QtGui.QImage.Format.Format_ARGB32)
return np.array(img.constBits()).reshape(img.height(), img.width(), 4)
# Compute edge magnitudes
def edges(d):
dx = ndimage.sobel(d, 0) # horizontal derivative
dy = ndimage.sobel(d, 1) # vertical derivative
return np.abs(dx) + np.abs(dy)
# Main window
class Window(QtWidgets.QWidget):
updateInput = QtCore.Signal()
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.model = None
self.capture = None
self.glWidget = GLWidget()
mainLayout = QtWidgets.QVBoxLayout()
# Input / output views
viewsLayout = QtWidgets.QGridLayout()
self.inputViewer = QtWidgets.QLabel("[Click to start]")
self.inputViewer.setPixmap(QtGui.QPixmap(rgb_width,rgb_height))
self.outputViewer = QtWidgets.QLabel("[Click to start]")
self.outputViewer.setPixmap(QtGui.QPixmap(rgb_width//2,rgb_height//2))
imgsFrame = QtWidgets.QFrame()
inputsLayout = QtWidgets.QVBoxLayout()
imgsFrame.setLayout(inputsLayout)
inputsLayout.addWidget(self.inputViewer)
inputsLayout.addWidget(self.outputViewer)
viewsLayout.addWidget(imgsFrame,0,0)
viewsLayout.addWidget(self.glWidget,0,1)
viewsLayout.setColumnStretch(1, 10)
mainLayout.addLayout(viewsLayout)
# Load depth estimation model
toolsLayout = QtWidgets.QHBoxLayout()
self.button = QtWidgets.QPushButton("Load model...")
self.button.clicked.connect(self.loadModel)
toolsLayout.addWidget(self.button)
self.button5 = QtWidgets.QPushButton("Load image")
self.button5.clicked.connect(self.loadImageFile)
toolsLayout.addWidget(self.button5)
self.button2 = QtWidgets.QPushButton("Webcam")
self.button2.clicked.connect(self.loadCamera)
toolsLayout.addWidget(self.button2)
self.button3 = QtWidgets.QPushButton("Video")
self.button3.clicked.connect(self.loadVideoFile)
toolsLayout.addWidget(self.button3)
self.button4 = QtWidgets.QPushButton("Pause")
self.button4.clicked.connect(self.loadImage)
toolsLayout.addWidget(self.button4)
self.button6 = QtWidgets.QPushButton("Refresh")
self.button6.clicked.connect(self.updateCloud)
toolsLayout.addWidget(self.button6)
mainLayout.addLayout(toolsLayout)
self.setLayout(mainLayout)
self.setWindowTitle(self.tr("RGBD Viewer"))
# Signals
self.updateInput.connect(self.update_input)
# Default example
img = (self.glWidget.rgb * 255).astype('uint8')
self.inputViewer.setPixmap(QtGui.QPixmap.fromImage(np_to_qimage(img)))
coloredDepth = (plasma(self.glWidget.depth[:,:,0])[:,:,:3] * 255).astype('uint8')
self.outputViewer.setPixmap(QtGui.QPixmap.fromImage(np_to_qimage(coloredDepth)))
def loadModel(self):
QtGui.QGuiApplication.setOverrideCursor(QtCore.Qt.WaitCursor)
tic()
self.model = load_model()
print('Model loaded.')
toc()
self.updateCloud()
QtGui.QGuiApplication.restoreOverrideCursor()
def loadCamera(self):
self.capture = cv2.VideoCapture(0)
self.updateInput.emit()
def loadVideoFile(self):
self.capture = cv2.VideoCapture('video.mp4')
self.updateInput.emit()
def loadImage(self):
self.capture = None
img = (self.glWidget.rgb * 255).astype('uint8')
self.inputViewer.setPixmap(QtGui.QPixmap.fromImage(np_to_qimage(img)))
self.updateCloud()
def loadImageFile(self):
self.capture = None
filename = QtWidgets.QFileDialog.getOpenFileName(None, 'Select image', '', self.tr('Image files (*.jpg *.png)'))[0]
img = QtGui.QImage(filename).scaledToHeight(rgb_height)
xstart = 0
if img.width() > rgb_width: xstart = (img.width() - rgb_width) // 2
img = img.copy(xstart, 0, xstart+rgb_width, rgb_height)
self.inputViewer.setPixmap(QtGui.QPixmap.fromImage(img))
self.updateCloud()
def update_input(self):
# Don't update anymore if no capture device is set
if self.capture == None: return
# Capture a frame
ret, frame = self.capture.read()
# Loop video playback if current stream is video file
if not ret:
self.capture.set(cv2.CAP_PROP_POS_FRAMES, 0)
ret, frame = self.capture.read()
# Prepare image and show in UI
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image = np_to_qimage(frame)
self.inputViewer.setPixmap(QtGui.QPixmap.fromImage(image))
# Update the point cloud
self.updateCloud()
def updateCloud(self):
rgb8 = qimage_to_np(self.inputViewer.pixmap().toImage())
self.glWidget.rgb = resize((rgb8[:,:,:3]/255)[:,:,::-1], (rgb_height, rgb_width), order=1, anti_aliasing=True)
if self.model:
with graph.as_default():
depth = (1000 / self.model.predict( np.expand_dims(self.glWidget.rgb, axis=0) )) / 1000
coloredDepth = (plasma(depth[0,:,:,0])[:,:,:3] * 255).astype('uint8')
self.outputViewer.setPixmap(QtGui.QPixmap.fromImage(np_to_qimage(coloredDepth)))
self.glWidget.depth = depth[0,:,:,0]
else:
self.glWidget.depth = 0.5 + np.zeros((rgb_height//2, rgb_width//2, 1))
self.glWidget.updateRGBD()
self.glWidget.updateGL()
# Update to next frame if we are live
QtCore.QTimer.singleShot(10, self.updateInput)
class GLWidget(QtOpenGL.QGLWidget):
def __init__(self, parent=None):
QtOpenGL.QGLWidget.__init__(self, parent)
self.object = 0
self.xRot = 5040
self.yRot = 40
self.zRot = 0
self.zoomLevel = 9
self.lastPos = QtCore.QPoint()
self.trolltechGreen = QtGui.QColor.fromCmykF(0.40, 0.0, 1.0, 0.0)
self.trolltechPurple = QtGui.QColor.fromCmykF(0.39, 0.39, 0.0, 0.0)
# Precompute for world coordinates
self.xx, self.yy = self.worldCoords(width=rgb_width//2, height=rgb_height//2)
# Load test frame from disk
self.rgb = np.load('demo_rgb.npy')
self.depth = resize(np.load('demo_depth.npy'), (240,320))
self.col_vbo = None
self.pos_vbo = None
self.updateRGBD()
def xRotation(self):
return self.xRot
def yRotation(self):
return self.yRot
def zRotation(self):
return self.zRot
def minimumSizeHint(self):
return QtCore.QSize(640, 480)
def sizeHint(self):
return QtCore.QSize(640, 480)
def setXRotation(self, angle):
if angle != self.xRot:
self.xRot = angle
self.emit(QtCore.SIGNAL("xRotationChanged(int)"), angle)
self.updateGL()
def setYRotation(self, angle):
if angle != self.yRot:
self.yRot = angle
self.emit(QtCore.SIGNAL("yRotationChanged(int)"), angle)
self.updateGL()
def setZRotation(self, angle):
if angle != self.zRot:
self.zRot = angle
self.emit(QtCore.SIGNAL("zRotationChanged(int)"), angle)
self.updateGL()
def resizeGL(self, width, height):
GL.glViewport(0, 0, width, height)
def mousePressEvent(self, event):
self.lastPos = QtCore.QPoint(event.pos())
def mouseMoveEvent(self, event):
dx = -(event.x() - self.lastPos.x())
dy = (event.y() - self.lastPos.y())
if event.buttons() & QtCore.Qt.LeftButton:
self.setXRotation(self.xRot + dy)
self.setYRotation(self.yRot + dx)
elif event.buttons() & QtCore.Qt.RightButton:
self.setXRotation(self.xRot + dy)
self.setZRotation(self.zRot + dx)
self.lastPos = QtCore.QPoint(event.pos())
def wheelEvent(self, event):
numDegrees = event.delta() / 8
numSteps = numDegrees / 15
self.zoomLevel = self.zoomLevel + numSteps
event.accept()
self.updateGL()
def initializeGL(self):
self.qglClearColor(self.trolltechPurple.darker())
GL.glShadeModel(GL.GL_FLAT)
GL.glEnable(GL.GL_DEPTH_TEST)
GL.glEnable(GL.GL_CULL_FACE)
VERTEX_SHADER = shaders.compileShader("""#version 330
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 color;
uniform mat4 mvp; out vec4 frag_color;
void main() {gl_Position = mvp * vec4(position, 1.0);frag_color = vec4(color, 1.0);}""", GL.GL_VERTEX_SHADER)
FRAGMENT_SHADER = shaders.compileShader("""#version 330
in vec4 frag_color; out vec4 out_color;
void main() {out_color = frag_color;}""", GL.GL_FRAGMENT_SHADER)
self.shaderProgram = shaders.compileProgram(VERTEX_SHADER, FRAGMENT_SHADER)
self.UNIFORM_LOCATIONS = {
'position': GL.glGetAttribLocation( self.shaderProgram, 'position' ),
'color': GL.glGetAttribLocation( self.shaderProgram, 'color' ),
'mvp': GL.glGetUniformLocation( self.shaderProgram, 'mvp' ),
}
shaders.glUseProgram(self.shaderProgram)
def paintGL(self):
GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
self.drawObject()
def worldCoords(self, width, height):
hfov_degrees, vfov_degrees = 57, 43
hFov = math.radians(hfov_degrees)
vFov = math.radians(vfov_degrees)
cx, cy = width/2, height/2
fx = width/(2*math.tan(hFov/2))
fy = height/(2*math.tan(vFov/2))
xx, yy = np.tile(range(width), height), np.repeat(range(height), width)
xx = (xx-cx)/fx
yy = (yy-cy)/fy
return xx, yy
def posFromDepth(self, depth):
length = depth.shape[0] * depth.shape[1]
depth[edges(depth) > 0.3] = 1e6 # Hide depth edges
z = depth.reshape(length)
return np.dstack((self.xx*z, self.yy*z, z)).reshape((length, 3))
def createPointCloudVBOfromRGBD(self):
# Create position and color VBOs
self.pos_vbo = vbo.VBO(data=self.pos, usage=GL.GL_DYNAMIC_DRAW, target=GL.GL_ARRAY_BUFFER)
self.col_vbo = vbo.VBO(data=self.col, usage=GL.GL_DYNAMIC_DRAW, target=GL.GL_ARRAY_BUFFER)
def updateRGBD(self):
# RGBD dimensions
width, height = self.depth.shape[1], self.depth.shape[0]
# Reshape
points = self.posFromDepth(self.depth.copy())
colors = resize(self.rgb, (height, width)).reshape((height * width, 3))
# Flatten and convert to float32
self.pos = points.astype('float32')
self.col = colors.reshape(height * width, 3).astype('float32')
# Move center of scene
self.pos = self.pos + glm.vec3(0, -0.06, -0.3)
# Create VBOs
if not self.col_vbo:
self.createPointCloudVBOfromRGBD()
def drawObject(self):
# Update camera
model, view, proj = glm.mat4(1), glm.mat4(1), glm.perspective(45, self.width() / self.height(), 0.01, 100)
center, up, eye = glm.vec3(0,-0.075,0), glm.vec3(0,-1,0), glm.vec3(0,0,-0.4 * (self.zoomLevel/10))
view = glm.lookAt(eye, center, up)
model = glm.rotate(model, self.xRot / 160.0, glm.vec3(1,0,0))
model = glm.rotate(model, self.yRot / 160.0, glm.vec3(0,1,0))
model = glm.rotate(model, self.zRot / 160.0, glm.vec3(0,0,1))
mvp = proj * view * model
GL.glUniformMatrix4fv(self.UNIFORM_LOCATIONS['mvp'], 1, False, glm.value_ptr(mvp))
# Update data
self.pos_vbo.set_array(self.pos)
self.col_vbo.set_array(self.col)
# Point size
GL.glPointSize(4)
# Position
self.pos_vbo.bind()
GL.glEnableVertexAttribArray(0)
GL.glVertexAttribPointer(0, 3, GL.GL_FLOAT, GL.GL_FALSE, 0, None)
# Color
self.col_vbo.bind()
GL.glEnableVertexAttribArray(1)
GL.glVertexAttribPointer(1, 3, GL.GL_FLOAT, GL.GL_FALSE, 0, None)
# Draw
GL.glDrawArrays(GL.GL_POINTS, 0, self.pos.shape[0])
# Center debug
if False:
self.qglColor(QtGui.QColor(255,0,0))
GL.glPointSize(20)
GL.glBegin(GL.GL_POINTS)
GL.glVertex3d(0,0,0)
GL.glEnd()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.show()
res = app.exec_()