OpenDroneMap-ODM/scripts/opensfm.py

98 wiersze
4.1 KiB
Python
Czysty Zwykły widok Historia

2015-11-26 12:15:02 +00:00
import ecto
2015-11-20 10:00:43 +00:00
from opendm import log
2015-11-26 12:15:02 +00:00
from opendm import io
2015-11-20 10:00:43 +00:00
from opendm import system
from opendm import context
2015-11-26 12:15:02 +00:00
class ODMOpenSfMCell(ecto.Cell):
def declare_params(self, params):
params.declare("use_exif_size", "The application arguments.", False)
params.declare("feature_process_size", "The application arguments.", False)
params.declare("feature_min_frames", "The application arguments.", 0)
params.declare("processes", "The application arguments.", 0)
params.declare("matching_gps_neighbors", "The application arguments.", 0)
def declare_io(self, params, inputs, outputs):
2015-11-27 10:00:43 +00:00
inputs.declare("args", "The application arguments.", {})
2015-11-27 16:51:21 +00:00
inputs.declare("photos", "Clusters output. list of ODMPhoto's", [])
2015-11-27 10:00:43 +00:00
outputs.declare("reconstructions", "Clusters output. list of reconstructions", [])
outputs.declare("reconstruction_path", "The directory to the images to load.", "")
2015-11-26 12:15:02 +00:00
def process(self, inputs, outputs):
log.ODM_INFO('Running OMD OpenSfm Cell')
# get inputs
2015-11-27 10:00:43 +00:00
args = self.inputs.args
2015-11-26 12:15:02 +00:00
photos = self.inputs.photos
2015-11-27 16:51:21 +00:00
project_path = io.absolute_path_file(args['project_path'])
2015-11-26 12:15:02 +00:00
2015-11-27 16:51:21 +00:00
if not photos:
log.ODM_ERROR('Not enough photos in photos array to start OpenSfm')
return ecto.QUIT
# create working directories
2015-11-27 16:51:21 +00:00
opensfm_path = io.join_paths(project_path, 'opensfm')
pmvs_path = io.join_paths(project_path, 'pmvs')
2015-11-27 16:51:21 +00:00
system.mkdir_p(opensfm_path)
system.mkdir_p(pmvs_path)
2015-11-26 12:15:02 +00:00
### check if reconstruction was done before
2015-11-27 16:51:21 +00:00
reconstruction_file = io.join_paths(opensfm_path, 'reconstruction.json')
2015-11-26 12:15:02 +00:00
if not io.file_exists(reconstruction_file):
# create file list
list_path = io.join_paths(opensfm_path, 'image_list.txt')
with open(list_path, 'w') as fout:
for photo in photos:
fout.write('%s\n' % photo.path_file)
# create config file for OpenSfM
config = [
"use_exif_size: %s" % ('no' if not self.params.use_exif_size else 'yes'),
"feature_process_size: %s" % self.params.feature_process_size,
"feature_min_frames: %s" % self.params.feature_min_frames,
"processes: %s" % self.params.processes,
"matching_gps_neighbors: %s" % self.params.matching_gps_neighbors
]
# write config file
config_filename = io.join_paths(project_path, 'config.yaml')
with open(config_filename, 'w') as fout:
fout.write("\n".join(config))
# run OpenSfM reconstruction
system.run('PYTHONPATH=%s %s/bin/run_all %s' %
(context.pyopencv_path, context.opensfm_path, opensfm_path))
else:
2015-11-27 16:51:21 +00:00
log.ODM_WARNING('Found a valid reconstruction file in: %s' %
(reconstruction_file))
2015-11-16 14:52:47 +00:00
### check if reconstruction was exported to bundler before
bundler_file = io.join_paths(opensfm_path, 'bundle_r000.out')
2015-11-16 14:52:47 +00:00
if not io.file_exists(bundler_file):
# convert back to bundler's format
system.run('PYTHONPATH=%s %s/bin/export_bundler %s' %
(context.pyopencv_path, context.opensfm_path, opensfm_path))
else:
log.ODM_WARNING('Found a valid bundle file in: %s' %
(reconstruction_file))
2015-11-16 14:52:47 +00:00
### check if reconstruction was exported to pmvs before
pmvs_file = io.join_paths(pmvs_path, 'recon0/pmvs_options.txt')
2015-11-16 14:52:47 +00:00
if not io.file_exists(pmvs_file):
# run PMVS converter
system.run('PYTHONPATH=%s %s/bin/export_pmvs %s --output %s' %
(context.pyopencv_path, context.opensfm_path, opensfm_path, pmvs_path))
2015-11-26 12:15:02 +00:00
else:
log.ODM_WARNING('Found a valid PMVS file in: %s' % pmvs_file)
2015-11-16 14:52:47 +00:00
# append biggest reconstruction path to output
self.outputs.reconstruction_path = io.join_paths(pmvs_path, 'recon0')
2015-11-16 14:52:47 +00:00
log.ODM_INFO('Running OMD OpenSfm Cell - Finished')
return ecto.OK if args['end_with'] != 'opensfm' else ecto.QUIT