2016-03-01 11:01:52 +00:00
|
|
|
"""Cell to run odm_slam."""
|
|
|
|
|
2016-02-04 10:27:45 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
from opendm import log
|
2016-02-04 13:17:45 +00:00
|
|
|
from opendm import io
|
2016-02-04 10:27:45 +00:00
|
|
|
from opendm import system
|
|
|
|
from opendm import context
|
2019-04-22 19:14:39 +00:00
|
|
|
from opendm import types
|
2016-02-04 10:27:45 +00:00
|
|
|
|
2019-04-22 19:14:39 +00:00
|
|
|
class ODMSlamStage(types.ODM_Stage):
|
2016-03-01 11:01:52 +00:00
|
|
|
"""Run odm_slam on a video and export to opensfm format."""
|
|
|
|
|
2019-04-22 19:14:39 +00:00
|
|
|
def process(self, args, outputs):
|
|
|
|
tree = outputs['tree']
|
2016-05-18 13:49:11 +00:00
|
|
|
video = os.path.join(tree.root_path, args.video)
|
|
|
|
slam_config = os.path.join(tree.root_path, args.slam_config)
|
2016-02-04 10:27:45 +00:00
|
|
|
|
|
|
|
if not video:
|
|
|
|
log.ODM_ERROR('No video provided')
|
2019-04-22 19:14:39 +00:00
|
|
|
exit(1)
|
2016-02-04 10:27:45 +00:00
|
|
|
|
|
|
|
# create working directories
|
|
|
|
system.mkdir_p(tree.opensfm)
|
|
|
|
|
2016-03-01 11:01:52 +00:00
|
|
|
vocabulary = os.path.join(context.orb_slam2_path,
|
|
|
|
'Vocabulary/ORBvoc.txt')
|
2016-02-04 13:17:45 +00:00
|
|
|
orb_slam_cmd = os.path.join(context.odm_modules_path, 'odm_slam')
|
2016-02-04 13:35:15 +00:00
|
|
|
trajectory = os.path.join(tree.opensfm, 'KeyFrameTrajectory.txt')
|
2016-02-05 00:02:55 +00:00
|
|
|
map_points = os.path.join(tree.opensfm, 'MapPoints.txt')
|
2016-02-04 13:35:15 +00:00
|
|
|
|
|
|
|
# check if slam was run before
|
2019-04-22 19:14:39 +00:00
|
|
|
if not io.file_exists(trajectory) or self.rerun():
|
2016-02-04 13:35:15 +00:00
|
|
|
# run slam binary
|
|
|
|
system.run(' '.join([
|
|
|
|
'cd {} &&'.format(tree.opensfm),
|
|
|
|
orb_slam_cmd,
|
|
|
|
vocabulary,
|
|
|
|
slam_config,
|
|
|
|
video,
|
|
|
|
]))
|
|
|
|
else:
|
|
|
|
log.ODM_WARNING('Found a valid slam trajectory in: {}'.format(
|
|
|
|
trajectory))
|
|
|
|
|
|
|
|
# check if trajectory was exported to opensfm before
|
2019-04-22 19:14:39 +00:00
|
|
|
if not io.file_exists(tree.opensfm_reconstruction) or self.rerun():
|
2016-02-04 13:35:15 +00:00
|
|
|
# convert slam to opensfm
|
|
|
|
system.run(' '.join([
|
|
|
|
'cd {} &&'.format(tree.opensfm),
|
2016-03-01 11:01:52 +00:00
|
|
|
'PYTHONPATH={}:{}'.format(context.pyopencv_path,
|
|
|
|
context.opensfm_path),
|
2016-02-04 13:35:15 +00:00
|
|
|
'python',
|
2016-03-01 11:01:52 +00:00
|
|
|
os.path.join(context.odm_modules_src_path,
|
|
|
|
'odm_slam/src/orb_slam_to_opensfm.py'),
|
2016-02-04 13:35:15 +00:00
|
|
|
video,
|
|
|
|
trajectory,
|
2016-02-05 00:02:55 +00:00
|
|
|
map_points,
|
2016-02-04 13:35:15 +00:00
|
|
|
slam_config,
|
|
|
|
]))
|
2016-03-03 11:58:25 +00:00
|
|
|
# link opensfm images to resized images
|
|
|
|
os.symlink(tree.opensfm + '/images', tree.dataset_resize)
|
2016-02-04 13:35:15 +00:00
|
|
|
else:
|
2016-03-01 11:01:52 +00:00
|
|
|
log.ODM_WARNING('Found a valid OpenSfM file in: {}'.format(
|
2016-02-04 13:35:15 +00:00
|
|
|
tree.opensfm_reconstruction))
|
2016-02-04 13:17:45 +00:00
|
|
|
|
2016-02-04 13:35:15 +00:00
|
|
|
# check if reconstruction was exported to bundler before
|
2019-04-22 19:14:39 +00:00
|
|
|
if not io.file_exists(tree.opensfm_bundle_list) or self.rerun():
|
2016-02-04 13:17:45 +00:00
|
|
|
# convert back to bundler's format
|
2016-03-01 11:01:52 +00:00
|
|
|
system.run(
|
|
|
|
'PYTHONPATH={} {}/bin/export_bundler {}'.format(
|
|
|
|
context.pyopencv_path, context.opensfm_path, tree.opensfm))
|
2016-02-04 13:17:45 +00:00
|
|
|
else:
|
2016-03-01 11:01:52 +00:00
|
|
|
log.ODM_WARNING(
|
|
|
|
'Found a valid Bundler file in: {}'.format(
|
|
|
|
tree.opensfm_reconstruction))
|
2016-02-04 13:17:45 +00:00
|
|
|
|