Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[r36928]: trunk / contrib / brl / bseg / boxm2 / pyscripts / app / tailwindDataProcessor.py Maximize Restore History

Download this file

tailwindDataProcessor.py    531 lines (394 with data), 15.4 kB

  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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
import os
import sys
from PIL import Image
import getopt
import time
import re
import subprocess, shlex
import boxm2_batch
from xml.dom.minidom import parse, parseString
from boxm2_scene_adaptor import *
from vil_adaptor import *
from vpgl_adaptor import *
import random
import shutil
dryrun = False
def log(msg):
timeStr = time.strftime("%m/%d/%y %H:%M:%S", time.localtime())
if dryrun:
print "[%s][DRYRUN] %s" % (timeStr, msg)
else:
print "[%s] %s" % (timeStr, msg)
def getAllFilesInSubDirs(root, makeRelative=True):
fileList = []
for path, subdirs, files in os.walk(root):
for fileName in files:
if makeRelative:
fileList.append(os.path.relpath(os.path.join(path, fileName), root))
else:
fileList.append(os.path.join(path,fileName))
return fileList
def copyFile(src, dst):
if (not os.path.exists(os.path.dirname(dst))):
if not dryrun:
os.makedirs(os.path.dirname(dst))
if not dryrun:
shutil.copy2(src, dst)
log("\tCopied %s to %s" % (src, dst))
return 1
def convertcamera(srcFileName, dstFileName):
filehandle = open(srcFileName);
K = [];
Rbae = [];
Cbae = [];
for line in filehandle:
p = str.split(line);
for i in range(0,len(p)):
K.append(float(p[i]));
if(len(K) == 9):
break;
for line in filehandle:
p = str.split(line);
for i in range(0,len(p)):
Rbae.append(float(p[i]));
if(len(Rbae) == 9):
break;
for line in filehandle:
p = str.split(line);
for i in range(0,len(p)):
Cbae.append(float(p[i]));
if(len(Cbae) == 3):
break;
filehandle.close();
filehandleout = open(dstFileName,'w');
for i in range(0,3):
print>>filehandleout, K[i*3+0],K[i*3+1],K[i*3+2];
print>>filehandleout
for i in range(0,3):
print>>filehandleout, Rbae[i],Rbae[i+3],Rbae[i+6];
print>>filehandleout
T=[0,0,0];
for i in range(0,3):
T[i]= Rbae[i]*Cbae[0]+Rbae[i+3]*Cbae[1]+Rbae[i+6]*Cbae[2];
print>>filehandleout, -T[0], -T[1], -T[2]
filehandleout.close();
def removeFile(fileName):
if not dryrun:
os.remove( fileName )
log("\tRemoved %s." % (fileName))
return 1
class TailwindPipelineRunner():
def __init__(self, config):
self.srcJPGDimensions = None
self.config = config
def runPipeline(self):
steps = [
self.movePNGs, # Step 1. Find the PNGs in the data directory, convert them to JPGs and write them out to a new folder.
self.runVisualSFM, # Step 2. Run VisualSFM to generate a NVM file.
self.createScene, # Step 3. Create the scene using the .nvm file produced above.
self.updateScene, # Step 4. Update the scene
self.exportScene # Step 5. Export the scene
]
for stepIndex in xrange(self.config.resumeAtStep, len(steps)):
retVal = steps[stepIndex](stepIndex+1)
if 0 != retVal:
log("Processing cannot continue.")
return retVal
return 0
def runPipeline2(self):
steps = [
self.moveData, # Step 1. Find the PNGs in the data directory, convert them to JPGs and write them out to a new folder.
self.createSceneFromBox, # Step 2. Create the scene using the .nvm file produced above.
self.updateScene, # Step 3. Update the scene
self.exportScene # Step 4. Export the scene
]
for stepIndex in xrange(self.config.resumeAtStep, len(steps)):
retVal = steps[stepIndex](stepIndex+1)
print stepIndex, retVal;
if 0 != retVal:
log("Processing cannot continue.")
return retVal
return 0
def callSystem(self, cmd, args, force=False):
fullCommand = ' '.join([cmd, args])
log("Running system command: %s" % (fullCommand))
try:
if dryrun and not force:
retVal = 0
else:
retVal = subprocess.call(shlex.split(fullCommand))
except OSError, err:
log("Unhandled OSError exception %s" % err)
return retVal
def movePNGs(self, stepNum):
srcDataFiles = getAllFilesInSubDirs(os.path.relpath( self.config.SRCImageDirName), makeRelative=False)
pngRegex = re.compile('.+\.png', re.IGNORECASE)
# Create a directory called "jpeg" to store all the newly converted PNG files.
if (not os.path.isdir(self.config.JPEGDirName)):
if not dryrun:
os.makedirs(self.config.JPEGDirName)
for srcFileName in srcDataFiles:
if re.search(pngRegex, srcFileName) is None:
continue
# This file is a PNG. Let's load the image
img = Image.open(srcFileName)
if None == self.srcJPGDimensions:
self.srcJPGDimensions = img.size
fileName = os.path.basename(srcFileName)
baseFileName, extension = os.path.splitext(fileName)
newName = os.path.join(self.config.JPEGDirName, baseFileName + ".jpg")
img.save(newName, "JPEG")
log("Converted and moved %s to %s." % (srcFileName, newName))
def moveData(self, stepNum):
pngRegex = re.compile('.+\.png', re.IGNORECASE)
camRegex = re.compile('.+\.txt', re.IGNORECASE)
inputfile = self.config.inputListFile ;
filehandle = open(inputfile,'r');
# Create a directory called "jpeg" to store all the newly converted PNG files.
if (not os.path.isdir(self.config.IMGDirName)):
if not dryrun:
os.makedirs(self.config.IMGDirName)
for srcFileName in filehandle:
srcFileName = srcFileName.rstrip()
if re.search(pngRegex, srcFileName) is not None :
fileName = os.path.basename(srcFileName)
newName = os.path.join(self.config.IMGDirName, fileName)
copyFile(srcFileName,newName)
log("moved %s to %s." % (srcFileName, newName))
if re.search(camRegex, srcFileName) is not None :
fileName = os.path.basename(srcFileName)
newName = os.path.join(self.config.CAMDirName, fileName)
copyFile(srcFileName,newName)
convertcamera(srcFileName,newName);
log("moved %s to %s." % (srcFileName, newName))
return 0;
def createSceneFromBox(self, stepNum):
filehandle = open(self.config.bbFileName,'r');
pts=[];
for line in filehandle:
p = str.split(line);
for i in range(0,len(p)):
pts.append(float(p[i]));
print pts;
success = 0;
if(len(pts) == 6):
success = get_scene_from_box_cams(self.config.CAMDirName,pts[0],pts[1],pts[2],pts[3]-pts[0],pts[4]-pts[1],pts[5]-pts[2],".")
if success:
return 0
else:
return None;
def getSourceImageFileDimension(self):
srcDataFiles = getAllFilesInSubDirs(os.path.relpath( self.config.SRCImageDirName), makeRelative=False)
if 0 == len(srcDataFiles):
log("Unable to find any source image files %s" % os.path.relpath( self.config.SRCImageDirName))
return None
pngRegex = re.compile('.+\.png', re.IGNORECASE)
for srcFileName in srcDataFiles:
if re.search(pngRegex, srcFileName) is None:
continue
img = Image.open(srcFileName)
return img.size
return None
def runVisualSFM(self, stepNum):
if self.srcJPGDimensions == None:
self.srcJPGDimensions = self.getSourceImageFileDimension()
if None == self.srcJPGDimensions:
log("Step %d - Unable to determine source image size dimensions for images here: %s." % (stepNum, os.path.relpath( self.config.SRCImageDirName)))
return -stepNum
commandLineArgs = "sfm+k=%d,%d,%d,%d %s %s" % ( self.config.focalLength, self.srcJPGDimensions[0]/2,
self.config.focalLength, self.srcJPGDimensions[1]/2,
os.path.abspath(self.config.JPEGDirName),
self.config.NVMFileName
)
retVal = self.callSystem("/home/vsi/projects/vsfm/vsfm/bin/VisualSFM", commandLineArgs)
if 0 != retVal:
log("Step %d - VisualSFM failed with return code %d!" % (stepNum, retVal))
log("Processing will not proceed.")
return -stepNum
return 0
def exportScene(self, stepNum):
# Extract the cell size from a file called ./model/uscene.xml
retVal, finestCellsize = self.extractCellSize(stepNum)
if False == retVal:
log("Step %d - extractCellSize failed." % (stepNum))
return -stepNum
commandLineArgs = "-scene ./model/uscene.xml -dir %s -depth 1 -ni 1024 -nj 768 -num_in 5 -init_incline 45 -end_incline 5 -gsd %s " % (self.config.finalExportDirName, finestCellsize)
retVal = self.callSystem("boxm2_export_scene", commandLineArgs, force=True)
if 0 != retVal:
log("Step %d - boxm2_export_scene failed with return code %d!" % (stepNum, retVal))
return -stepNum
return 0
def updateScene(self, stepNum):
# Should initialize a GPU
scenePath = os.path.abspath(self.config.uSceneFileName)
scene = boxm2_scene_adaptor (scenePath, "gpu0")
# Get list of imgs and cams
imgsDir = os.path.abspath(os.path.join( self.config.NVMOutputDirName, "imgs" ))
camsDir = os.path.abspath(os.path.join( self.config.NVMOutputDirName, "cams"))
if False == os.path.isdir(imgsDir):
log("The folder %s does not exist." % (imgsDir))
return -stepNum
if False == os.path.isdir(camsDir):
log("The folder %s does not exist." % (camsDir))
return -stepNum
imgFiles = getAllFilesInSubDirs(imgsDir)
camFiles = getAllFilesInSubDirs(camsDir)
if len(imgFiles) != len(camFiles):
log("The number of files in %s is %d." % (imgsDir, len(imgFiles)))
log("The number of fiels in %s is %d." % (camsDir, len(camFiles)))
log("These numbers should match and they do not.")
return -stepNum
if len(imgFiles) == 0:
log("No image files were found in %s." % (imgsDir))
return -stepNum
imgFiles.sort()
camFiles.sort()
# Make two passes over the image set
for p in xrange(0, self.config.updateScenePassCount):
frames = range(0, len(imgFiles), 1)
if self.config.randomizeUpdateOrder:
random.shuffle(frames)
for idx, i in enumerate(frames):
pcam = load_perspective_camera(os.path.join(camsDir, camFiles[i]))
img, ni, nj = load_image(os.path.join(imgsDir, imgFiles[i]))
scene.update(pcam, img,1,"","gpu0");
if 0 == (idx % 15): #No idea what is special about the magic number 15
scene.refine()
scene.write_cache()
return 0
def extractCellSize(self, stepNum):
if False == os.path.isfile(self.config.uSceneFileName):
log("Step %d - Cell size extraction failed because the uScene file can't be found at %s." % (stepNum, self.config.uSceneFileName))
return False, None
dom = parse(self.config.uSceneFileName)
xmlTag = dom.getElementsByTagName("block")
if 0 == len(xmlTag):
log("Step %d - No \"block\" elements could be found in the XML document %s." % (stepNum, self.config.uSceneFileName))
return False, None
if False == xmlTag[0].hasAttribute("dim_x"):
log("Step %d - The XML document %s Has a \"block\" element has no \"dim_x\" attribute." % (stepNum, self.config.uSceneFileName))
return False, None
dimXString = xmlTag[0].attributes["dim_x"].value
try:
dimXValue = float(dimXString) * 0.125
except ValueError:
log("Step %d - The parsed value of dimX %s appears to be invalid." % (stepNum, dimXValue))
return False, None
log("Finest cell size of %f found." % dimXValue)
return True, dimXValue
def createScene(self, stepNum):
bundleFile = os.path.abspath(self.config.NVMFileName)
imgDir = os.path.abspath(self.config.SRCImageDirName)
outDir = os.path.abspath(self.config.NVMOutputDirName)
if False == os.path.isfile(bundleFile):
log("Step %d - SceneCreation cannot begin because the NVM file %s is missing." % (stepNum, bundleFile))
log("Processing will not proceed.")
return -stepNum
if False == os.path.isdir(imgDir):
log("Step %d - SceneCreation cannot beging because the image directory %s cannot be found." % (stepNum, imgDir))
log("Processing will not proceed.")
return -stepNum
log("Creating scene with bundleFile %s." % (bundleFile))
log("Output will be written to %s." % (outDir))
boxm2_batch.not_verbose()
boxm2_batch.register_processes()
boxm2_batch.register_datatypes()
#class used for python/c++ pointers in database
class dbvalue:
def __init__(self, index, type):
self.id = index # unsigned integer
self.type = type # string
#run process
boxm2_batch.init_process("boxm2BundleToSceneProcess")
boxm2_batch.set_input_string(0, bundleFile)
boxm2_batch.set_input_string(1, imgDir)
boxm2_batch.set_input_string(2, self.config.NVMAppModel)
boxm2_batch.set_input_string(3, self.config.NVMNobsModel)
boxm2_batch.set_input_int(4, 8)
boxm2_batch.set_input_string(5, outDir)
boxm2_batch.run_process()
(scene_id, scene_type) = boxm2_batch.commit_output(0)
uscene = dbvalue(scene_id, scene_type)
(scene_id, scene_type) = boxm2_batch.commit_output(1)
rscene = dbvalue(scene_id, scene_type)
return 0
class PipelineConfig():
def __init__(self):
self.JPEGDirName = 'jpeg'
self.CAMDirName = 'nvm_out/cams'
self.IMGDirName = 'nvm_out/imgs'
self.inputListFile = None
self.bbFileName = None
self.modelDir = 'model'
self.SRCImageDirName = 'imgs'
self.NVMFileName = './output.nvm'
self.NVMOutputDirName = './nvm_out'
self.NVMAppModel = "boxm2_mog3_grey"
self.NVMNobsModel = "boxm2_num_obs"
self.uSceneFileName = './model/uscene.xml'
self.finalExportDirName = './export'
self.randomizeUpdateOrder = False
self.updateScenePassCount = 1
self.resumeAtStep = 0
def setFocalLength(self, fl):
try:
self.focalLength = int(fl)
except ValueError:
log("The value %s is not an acceptable focalLength." % fl)
def setUpdatePassCount(self, cnt):
try:
self.updateScenePassCount = int(cnt)
except ValueError:
log("The value %s is not an acceptable update pass count." % cnt)
def setResumeAtStep(self, step):
try:
self.resumeAtStep = int(step)
except ValueError:
log("The value %s is not an acceptable step to resume at." % step)
def isConfigComplete(self):
isOK = True
if self.inputListFile == None or self.bbFileName == None:
log("The input list file is missing ")
isOK = False
if self.resumeAtStep < 0 or self.resumeAtStep > 3:
log("The \"step\" value of %d is not valid. Valid values are in the range of 0-3 (inclusive)." % self.resumeAtStep)
isOK = False
if self.updateScenePassCount < 1:
log("The \"updateScenePassCount\" value of %d is not valid. Valid values are 1 or greater." % self.updateScenePassCount)
isOK = False
return isOK
if __name__ == '__main__':
config = PipelineConfig()
try:
opts, args = getopt.getopt(sys.argv[1:], "d:b:i:e:rs:u:", ["dryrun","bbfile=", "inputlist=", "exportDir=", "randomize", "step=", "updatePassCount="])
print opts
except getopt.GetoptError, err:
print str(err) + "\n"
sys.exit(1)
for o, a in opts:
o = str.lower(o)
if o in ("-d", "--dryrun"):
dryrun = True
elif o in ("-i", "--inputlist"):
config.inputListFile = a
elif o in ("-b", "--bbfile"):
config.bbFileName = a
elif o in ("-e", "--exportdir"):
config.finalExportDirName = a
elif o in ("-r", "--randomize"):
config.randomizeUpdateOrder = True
elif o in ("-u", "--updatePassCount"):
config.setUpdatePassCount(a)
elif o in ("-s", "--step"):
print a;
config.setResumeAtStep(a)
if False == config.isConfigComplete():
log("%s cannot be run until the above errors are fixed." % (sys.argv[0]))
sys.exit(-100)
pipeline = TailwindPipelineRunner(config)
retVal = pipeline.runPipeline2()
if 0 != retVal:
log("Pipeline processing has failed with return code %s." % retVal)
else:
log("Pipeline processing has completed succesfully.")
sys.exit(retVal)