2015-11-26 12:15:02 +00:00
|
|
|
import os
|
|
|
|
import ecto
|
|
|
|
|
2016-09-30 09:42:10 +00:00
|
|
|
from functools import partial
|
|
|
|
from multiprocessing import Pool
|
2015-11-26 12:15:02 +00:00
|
|
|
from opendm import context
|
|
|
|
from opendm import io
|
|
|
|
from opendm import types
|
|
|
|
from opendm import log
|
2016-12-09 14:51:25 +00:00
|
|
|
from opendm import system
|
|
|
|
from shutil import copyfile
|
2015-11-26 12:15:02 +00:00
|
|
|
|
2016-02-26 18:50:12 +00:00
|
|
|
|
2016-09-30 09:42:10 +00:00
|
|
|
def make_odm_photo(force_focal, force_ccd, path_file):
|
|
|
|
return types.ODM_Photo(path_file,
|
|
|
|
force_focal,
|
|
|
|
force_ccd)
|
|
|
|
|
|
|
|
|
2015-11-27 10:00:43 +00:00
|
|
|
class ODMLoadDatasetCell(ecto.Cell):
|
|
|
|
|
|
|
|
def declare_params(self, params):
|
2015-12-10 12:35:52 +00:00
|
|
|
params.declare("force_focal", 'Override the focal length information for the '
|
2016-02-26 18:50:12 +00:00
|
|
|
'images', None)
|
2016-10-18 15:37:37 +00:00
|
|
|
params.declare("force_ccd", 'Override the ccd width information for the '
|
2016-02-26 18:50:12 +00:00
|
|
|
'images', None)
|
2015-11-26 12:15:02 +00:00
|
|
|
|
|
|
|
def declare_io(self, params, inputs, outputs):
|
2015-12-10 11:01:41 +00:00
|
|
|
inputs.declare("tree", "Struct with paths", [])
|
2016-10-18 15:37:37 +00:00
|
|
|
outputs.declare("photos", "list of ODMPhotos", [])
|
2015-11-26 12:15:02 +00:00
|
|
|
|
|
|
|
def process(self, inputs, outputs):
|
2016-12-09 14:51:25 +00:00
|
|
|
# check if the extension is supported
|
2015-11-26 12:15:02 +00:00
|
|
|
def supported_extension(file_name):
|
|
|
|
(pathfn, ext) = os.path.splitext(file_name)
|
|
|
|
return ext.lower() in context.supported_extensions
|
|
|
|
|
2016-12-09 14:51:25 +00:00
|
|
|
# Get supported images from dir
|
|
|
|
def get_images(in_dir):
|
|
|
|
# filter images for its extension type
|
|
|
|
log.ODM_DEBUG(in_dir)
|
|
|
|
return [f for f in io.get_files_list(in_dir) if supported_extension(f)]
|
|
|
|
|
2015-11-26 12:15:02 +00:00
|
|
|
log.ODM_INFO('Running ODM Load Dataset Cell')
|
2015-11-27 16:50:09 +00:00
|
|
|
|
2015-12-10 11:01:41 +00:00
|
|
|
# get inputs
|
|
|
|
tree = self.inputs.tree
|
|
|
|
|
2016-12-09 14:51:25 +00:00
|
|
|
# get images directory
|
|
|
|
input_dir = tree.input_images
|
|
|
|
images_dir = tree.dataset_raw
|
|
|
|
resize_dir = tree.dataset_resize
|
2015-12-02 11:16:30 +00:00
|
|
|
|
2016-12-09 14:51:25 +00:00
|
|
|
# Check first if a project already exists. This is a mediocre way to check, by checking the resize dir
|
|
|
|
if io.dir_exists(resize_dir):
|
|
|
|
log.ODM_DEBUG("resize dir: %s" % resize_dir)
|
|
|
|
images_dir = resize_dir
|
|
|
|
# if first time running, create project directory and copy images over to project/images
|
|
|
|
else:
|
2015-12-02 14:23:48 +00:00
|
|
|
if not io.dir_exists(images_dir):
|
2016-12-09 14:51:25 +00:00
|
|
|
log.ODM_INFO("Project directory %s doesn't exist. Creating it now. " % images_dir)
|
|
|
|
system.mkdir_p(images_dir)
|
|
|
|
copied = [copyfile(io.join_paths(input_dir, f), io.join_paths(images_dir, f)) for f in get_images(input_dir)]
|
2015-11-26 12:15:02 +00:00
|
|
|
|
|
|
|
log.ODM_DEBUG('Loading dataset from: %s' % images_dir)
|
|
|
|
|
2016-12-09 14:51:25 +00:00
|
|
|
files = get_images(images_dir)
|
2015-11-26 12:15:02 +00:00
|
|
|
|
2015-11-27 16:50:09 +00:00
|
|
|
if files:
|
2015-11-26 12:15:02 +00:00
|
|
|
# create ODMPhoto list
|
2016-09-30 09:42:10 +00:00
|
|
|
path_files = [io.join_paths(images_dir, f) for f in files]
|
|
|
|
photos = Pool().map(
|
|
|
|
partial(make_odm_photo, self.params.force_focal, self.params.force_ccd),
|
|
|
|
path_files
|
|
|
|
)
|
2015-11-27 10:00:43 +00:00
|
|
|
|
2015-11-27 16:50:09 +00:00
|
|
|
log.ODM_INFO('Found %s usable images' % len(photos))
|
|
|
|
else:
|
|
|
|
log.ODM_ERROR('Not enough supported images in %s' % images_dir)
|
|
|
|
return ecto.QUIT
|
2015-11-26 12:15:02 +00:00
|
|
|
|
|
|
|
# append photos to cell output
|
|
|
|
outputs.photos = photos
|
|
|
|
|
|
|
|
log.ODM_INFO('Running ODM Load Dataset Cell - Finished')
|
2016-02-26 18:50:12 +00:00
|
|
|
return ecto.OK
|