forked from polmorenoc/opendr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo_fit_cube.py
177 lines (136 loc) · 5.38 KB
/
demo_fit_cube.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
__author__ = 'pol'
import matplotlib.pyplot as plt
import glfw
import generative_models
from utils import *
import OpenGL.GL as GL
from utils import *
plt.ion()
from OpenGL import contextdata
import sys
#__GL_THREADED_OPTIMIZATIONS
#Main script options:r
glModes = ['glfw','mesa']
glMode = glModes[0]
np.random.seed(1)
width, height = (128, 128)
numPixels = width*height
shapeIm = [width, height,3]
win = -1
clip_start = 0.01
clip_end = 10
frustum = {'near': clip_start, 'far': clip_end, 'width': width, 'height': height}
if glMode == 'glfw':
#Initialize base GLFW context for the Demo and to share context among all renderers.
glfw.init()
glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
glfw.window_hint(glfw.DEPTH_BITS,32)
glfw.window_hint(glfw.VISIBLE, GL.GL_FALSE)
win = glfw.create_window(width, height, "Demo", None, None)
glfw.make_context_current(win)
else:
from OpenGL.raw.osmesa._types import *
from OpenGL.raw.osmesa import mesa
winShared = None
gtCamElevation = np.pi/3
gtCamHeight = 0.4 #meters
chLightAzimuthGT = ch.Ch([0])
chLightElevationGT = ch.Ch([np.pi/3])
chLightIntensityGT = ch.Ch([1])
chGlobalConstantGT = ch.Ch([0.5])
chCamElGT = ch.Ch([gtCamElevation])
chCamHeightGT = ch.Ch([gtCamHeight])
focalLenght = 35 ##milimeters
chCamFocalLengthGT = ch.Ch([35/1000])
#Move camera backwards to match the elevation desired as it looks at origin:
# bottomElev = np.pi/2 - (gtCamElevation + np.arctan(17.5 / focalLenght ))
# ZshiftGT = ch.Ch(-gtCamHeight * np.tan(bottomElev)) #Move camera backwards to match the elevation desired as it looks at origin.
# ZshiftGT = ch.Ch([-0.2])
# Baackground cube - add to renderer by default.
verticesCube, facesCube, normalsCube, vColorsCube, texturesListCube, haveTexturesCube = getCubeData()
uvCube = np.zeros([verticesCube.shape[0],2])
chCubePosition = ch.Ch([0, 0, 0])
chCubeScale = ch.Ch([10.0])
chCubeAzimuth = ch.Ch([0])
chCubeVCColors = ch.Ch(np.ones_like(vColorsCube) * 1) #white cube
v_transf, vn_transf = transformObject([verticesCube], [normalsCube], chCubeScale, chCubeAzimuth, chCubePosition)
v_scene = [v_transf]
f_list_scene = [[[facesCube]]]
vc_scene = [[chCubeVCColors]]
vn_scene = [vn_transf]
uv_scene = [[uvCube]]
haveTextures_list_scene = [haveTexturesCube]
textures_list_scene = [texturesListCube]
#Example object 1: forgroudn cube
verticesCube, facesCube, normalsCube, vColorsCube, texturesListCube, haveTexturesCube = getCubeData()
uvCube = np.zeros([verticesCube.shape[0],2])
chCubeVCColors = ch.Ch(np.ones_like(vColorsCube) * 0.5) #Gray cube
chPositionGT = ch.Ch([0, 0, 0.])
# chPositionGT = ch.Ch([-0.23, 0.36, 0.])
chScaleGT = ch.Ch([0.1, 0.1, 0.1])
chColorGT = ch.Ch([1.0, 1.0, 1.0])
chAzimuthGT = ch.Ch([0.1])
objectParamsGT = {'chPosition':chPositionGT, 'chScale':chScaleGT, 'chColor':chColorGT, 'chAzimuth':chAzimuthGT}
v_transf, vn_transf = transformObject([verticesCube], [normalsCube], chScaleGT, chAzimuthGT, chPositionGT)
vc_illuminated = computeGlobalAndDirectionalLighting(vn_transf, [chCubeVCColors], chLightAzimuthGT, chLightElevationGT, chLightIntensityGT, chGlobalConstantGT)
v_scene += [v_transf]
f_list_scene += [[[facesCube]]]
vc_scene += [vc_illuminated]
vn_scene += [vn_transf]
uv_scene += [[uvCube]]
haveTextures_list_scene += [haveTexturesCube]
textures_list_scene += [texturesListCube]
#COnfigure lighting
lightParamsGT = {'chLightAzimuth': chLightAzimuthGT, 'chLightElevation': chLightElevationGT, 'chLightIntensity': chLightIntensityGT, 'chGlobalConstant':chGlobalConstantGT}
c0 = width/2 #principal point
c1 = height/2 #principal point
a1 = 3.657 #Aspect ratio / mm to pixels
a2 = 3.657 #Aspect ratio / mm to pixels
cameraParamsGT = {'chCamEl': chCamElGT, 'chCamHeight':chCamHeightGT, 'chCamFocalLength':chCamFocalLengthGT, 'a':np.array([a1,a2]), 'width': width, 'height':height, 'c':np.array([c0, c1])}
#Create renderer object
renderer = createRenderer(glMode, cameraParamsGT, v_scene, vc_scene, f_list_scene, vn_scene, uv_scene, haveTextures_list_scene,
textures_list_scene, frustum, None)
# Initialize renderer
renderer.overdraw = True
renderer.nsamples = 8
renderer.msaa = True #Without anti-aliasing optimization often does not work.
renderer.initGL()
renderer.initGLTexture()
renderer.debug = False
winShared = renderer.win
plt.figure()
plt.title('GT object')
plt.imshow(renderer.r)
rendererGT = ch.Ch(renderer.r.copy()) #Fix the GT position
#Vary cube scale:
chScaleGT[0] = 0.05
chScaleGT[1] = 0.05
plt.figure()
plt.title('Init object')
renderer.r
plt.imshow(renderer.r)
variances = ch.Ch([0.1])**2
negLikModel = -ch.sum(generative_models.LogGaussianModel(renderer=renderer, groundtruth=rendererGT, variances=variances, useMask=True)) / numPixels
plt.title('GT object')
global iter
iter = 0
def cb(_):
pass
global method
methods = ['dogleg', 'minimize', 'BFGS', 'L-BFGS-B', 'Nelder-Mead'] #Nelder-mead is the finite difference simplex method
method = 3
options = {'disp': True, 'maxiter': 1000}
ch.minimize({'raw': negLikModel}, bounds=None, method=methods[method], x0=chScaleGT, callback=cb, options=options)
plt.figure()
plt.title('Fitted object')
renderer.r
plt.imshow(renderer.r)
plt.show(0.1)
#Clean up.
renderer.makeCurrentContext()
renderer.clear()
contextdata.cleanupContext(contextdata.getContext())
# glfw.destroy_window(renderer.win)
del renderer