-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalysis.py
352 lines (308 loc) · 11.4 KB
/
analysis.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
from interpolation import interpolate_data
from myIO import (
inputAlignedVideos,
inputAnalysis,
inputCoin,
inputDebug,
inputDataset,
inputSynth,
inputExtractedData,
inputInterpolatedData,
inputInterpolatedMode,
inputModelTraining,
)
from utils import (
loadDataFile,
getChoosenCoinVideosPaths,
getChoosenSynthPaths,
getRtiPaths,
writeDataFile,
generateGaussianMatrix,
)
from pca_model import train_pca_model
from neural_model import train_neural_model
from analyze_data import analyze_data
from extract_data import extractCoinDataFromVideos, extractSynthDataFromAssets, extractRtiDataFromAssets
import os
import torch
from constants import constants
import ffmpeg
# Generate aligned videos
def generateAlignedVideo(not_aligned_video_path, video_path, delay=0):
print("\t— Generating aligned video: {}".format(video_path))
ffmpeg.input(not_aligned_video_path, itsoffset=delay).filter(
"fps", fps=constants["ALIGNED_VIDEO_FPS"]
).output(video_path).run()
def saveDatapointsToFile(path, data):
# Get Datapoints
datapoints = []
keys = list(data[0][0].keys())
light_directions_x = [i.split("|")[0] for i in keys]
light_directions_y = [i.split("|")[1] for i in keys]
for i in range(len(light_directions_x)):
datapoints.append((light_directions_x[i], light_directions_y[i]))
# Save to file
writeDataFile(path, datapoints)
# def saveDatapointsToFile(path, data):
# # Get Datapoints
# datapoints = []
# keys = list(data[0][0].keys())
# light_directions_x = [i.split("|")[0] for i in keys]
# light_directions_y = [i.split("|")[1] for i in keys]
# for i in range(len(light_directions_x)):
# datapoints.append((int(light_directions_x[i]), int(light_directions_y[i])))
# # Save to file
# writeDataFile(path, datapoints)
def coinSubMain(interpolation_mode):
coin = inputCoin()
debug_mode = inputDebug()
(
not_aligned_static_video_path,
not_aligned_moving_video_path,
moving_camera_delay,
static_video_path,
moving_video_path,
extracted_data_file_path,
test_data_file_path,
interpolated_data_file_path,
model_path,
pca_data_file_path,
datapoints_file_path,
test_datapoints_file_path,
) = getChoosenCoinVideosPaths(coin, interpolation_mode)
if (not os.path.exists(not_aligned_static_video_path)) or (
not os.path.exists(not_aligned_moving_video_path)
):
raise (Exception("Video assets not founded!"))
if (not os.path.exists(constants["CALIBRATION_INTRINSICS_CAMERA_STATIC_PATH"])) or (
not os.path.exists(constants["CALIBRATION_INTRINSICS_CAMERA_MOVING_PATH"])
):
raise (Exception("You need to run the calibration before the analysis!"))
print(
"*** Analysis *** \n\tStatic_Video: '{}' \n\tMoving_Video: '{}'".format(
not_aligned_static_video_path, not_aligned_moving_video_path
)
)
extracted_data = None
test_data = None
# Ask to generate aligned videos (if they already exists)
if inputAlignedVideos(static_video_path, moving_video_path):
generateAlignedVideo(not_aligned_static_video_path, static_video_path)
generateAlignedVideo(
not_aligned_moving_video_path, moving_video_path, moving_camera_delay
)
# Ask to extract data (if it already exists)
if inputExtractedData(extracted_data_file_path):
# [for each x, y : {"lightDirs_x|lightDirs_y": pixelIntensities}]
extracted_data, test_data = extractCoinDataFromVideos(
static_video_path, moving_video_path, debug_mode
)
writeDataFile(extracted_data_file_path, extracted_data)
writeDataFile(test_data_file_path, test_data)
else:
extracted_data = loadDataFile(extracted_data_file_path)
test_data = loadDataFile(test_data_file_path)
return (
extracted_data,
test_data,
interpolated_data_file_path,
model_path,
pca_data_file_path,
datapoints_file_path,
test_datapoints_file_path,
)
def synthSubMain(interpolation_mode):
synth = inputSynth()
(
data_folder_path,
data_light_directions_file_path,
test_folder_path,
test_light_directions_file_path,
extracted_data_file_path,
test_data_file_path,
interpolated_data_file_path,
model_path,
pca_data_file_path,
datapoints_file_path,
test_datapoints_file_path,
) = getChoosenSynthPaths(synth, interpolation_mode)
if (not os.path.exists(data_folder_path)) or (
not os.path.exists(data_light_directions_file_path)
):
raise (Exception("Synth assets not founded!"))
print("*** Analysis *** \n\tSynthData folder: '{}'".format(data_folder_path))
extracted_data = None
test_data = None
# Ask to extract data (if it already exists)
if inputExtractedData(extracted_data_file_path):
# [for each x, y : {"lightDirs_x|lightDirs_y": pixelIntensities}]
extracted_data, test_data = extractSynthDataFromAssets(
data_folder_path,
data_light_directions_file_path,
test_folder_path,
test_light_directions_file_path,
)
writeDataFile(extracted_data_file_path, extracted_data)
writeDataFile(test_data_file_path, test_data)
else:
extracted_data = loadDataFile(extracted_data_file_path)
test_data = loadDataFile(test_data_file_path)
return (
extracted_data,
test_data,
interpolated_data_file_path,
model_path,
pca_data_file_path,
datapoints_file_path,
test_datapoints_file_path,
)
def rtiSubMain(interpolation_mode):
(
data_folder_path,
data_light_directions_file_path,
test_folder_path,
test_light_directions_file_path,
extracted_data_file_path,
test_data_file_path,
interpolated_data_file_path,
model_path,
pca_data_file_path,
datapoints_file_path,
test_datapoints_file_path,
) = getRtiPaths(interpolation_mode)
if (not os.path.exists(data_folder_path)) or (
not os.path.exists(data_light_directions_file_path)
):
raise (Exception("Rti assets not founded!"))
print("*** Analysis *** \n\tRti-dataset folder: '{}'".format(data_folder_path))
extracted_data = None
test_data = None
# Ask to extract data (if it already exists)
if inputExtractedData(extracted_data_file_path):
# [for each x, y : {"lightDirs_x|lightDirs_y": pixelIntensities}]
extracted_data, test_data = extractRtiDataFromAssets(
data_folder_path,
data_light_directions_file_path,
test_folder_path,
test_light_directions_file_path,
)
writeDataFile(extracted_data_file_path, extracted_data)
writeDataFile(test_data_file_path, test_data)
else:
extracted_data = loadDataFile(extracted_data_file_path)
test_data = loadDataFile(test_data_file_path)
return (
extracted_data,
test_data,
interpolated_data_file_path,
model_path,
pca_data_file_path,
datapoints_file_path,
test_datapoints_file_path,
)
def main():
dataset = inputDataset()
interpolation_mode = inputInterpolatedMode()
if dataset == 1:
(
extracted_data,
test_data,
interpolated_data_file_path,
model_path,
pca_data_file_path,
datapoints_file_path,
test_datapoints_file_path,
) = coinSubMain(interpolation_mode)
elif dataset == 2:
(
extracted_data,
test_data,
interpolated_data_file_path,
model_path,
pca_data_file_path,
datapoints_file_path,
test_datapoints_file_path,
) = synthSubMain(interpolation_mode)
else:
(
extracted_data,
test_data,
interpolated_data_file_path,
model_path,
pca_data_file_path,
datapoints_file_path,
test_datapoints_file_path,
) = rtiSubMain(interpolation_mode)
# Save lightdirection datapoints (interactive relighting debug)
saveDatapointsToFile(datapoints_file_path, extracted_data)
# Save test lightdirection datapoints (interactive relighting debug)
saveDatapointsToFile(test_datapoints_file_path, test_data)
interpolated_data = None
# Train model if necessary
if (interpolation_mode == 3 or interpolation_mode == 4) and inputModelTraining(
model_path
):
# if not os.path.exists(constants["GAUSSIAN_MATRIX_FILE_PATH"]):
gaussian_matrix = generateGaussianMatrix(
0, torch.tensor(constants["PCA_SIGMA"]), constants["PCA_H"]
)
writeDataFile(constants["GAUSSIAN_MATRIX_FILE_PATH"], gaussian_matrix)
# else:
# gaussian_matrix = loadDataFile(constants["GAUSSIAN_MATRIX_FILE_PATH"])
train_pca_model(
model_path,
extracted_data,
gaussian_matrix,
pca_data_file_path,
)
if (interpolation_mode == 5 or interpolation_mode == 6) and inputModelTraining(
model_path
):
# if not os.path.exists(
# constants["GAUSSIAN_MATRIX_FILE_PATH_XY"]
# ) or not os.path.exists(constants["GAUSSIAN_MATRIX_FILE_PATH_UV"]):
# gaussian_matrix_xy = generateGaussianMatrix(
# 0, torch.tensor(constants["NEURAL_SIGMA_XY"]), constants["NEURAL_H"]
# )
# gaussian_matrix_uv = generateGaussianMatrix(
# 0, torch.tensor(constants["NEURAL_SIGMA_UV"]), constants["NEURAL_H"]
# )
# writeDataFile(constants["GAUSSIAN_MATRIX_FILE_PATH_XY"], gaussian_matrix_xy)
# writeDataFile(constants["GAUSSIAN_MATRIX_FILE_PATH_UV"], gaussian_matrix_uv)
# else:
# gaussian_matrix_xy = loadDataFile(constants["GAUSSIAN_MATRIX_FILE_PATH_XY"])
# gaussian_matrix_uv = loadDataFile(constants["GAUSSIAN_MATRIX_FILE_PATH_UV"])
gaussian_matrix_xy = generateGaussianMatrix(
0, torch.tensor(constants["NEURAL_SIGMA_XY"]), constants["NEURAL_H"]
)
gaussian_matrix_uv = generateGaussianMatrix(
0, torch.tensor(constants["NEURAL_SIGMA_UV"]), constants["NEURAL_H"]
)
writeDataFile(constants["GAUSSIAN_MATRIX_FILE_PATH_XY"], gaussian_matrix_xy)
writeDataFile(constants["GAUSSIAN_MATRIX_FILE_PATH_UV"], gaussian_matrix_uv)
train_neural_model(
model_path,
extracted_data,
gaussian_matrix_xy,
gaussian_matrix_uv,
)
# Interpolate data from extracted if necessary
if interpolation_mode != 4 and interpolation_mode != 6 and inputInterpolatedData(interpolated_data_file_path):
interpolated_data = interpolate_data(
extracted_data,
interpolation_mode,
model_path,
pca_data_file_path,
)
writeDataFile(interpolated_data_file_path, interpolated_data)
print("All Done! Now you can use the interactive relighting.")
if inputAnalysis():
analyze_data(
extracted_data,
test_data,
interpolation_mode,
pca_data_file_path,
model_path,
)
if __name__ == "__main__":
main()