OpenDroneMap-ODM/scripts/resize.py

91 wiersze
3.4 KiB
Python
Czysty Zwykły widok Historia

2015-11-26 12:15:02 +00:00
import ecto
2015-11-19 12:03:22 +00:00
import cv2
import pyexiv2
2015-11-20 10:00:43 +00:00
from opendm import log
from opendm import system
2015-11-26 12:15:02 +00:00
from opendm import io
2015-11-27 16:50:45 +00:00
from opendm import types
2015-11-26 12:15:02 +00:00
2015-11-27 16:50:45 +00:00
class ODMResizeCell(ecto.Cell):
2015-11-26 12:15:02 +00:00
def declare_params(self, params):
2015-12-10 12:35:52 +00:00
params.declare("resize_to", "resizes images by the largest side", 2400)
2015-11-26 12:15:02 +00:00
def declare_io(self, params, inputs, outputs):
inputs.declare("tree", "Struct with paths", [])
2015-11-27 16:50:45 +00:00
inputs.declare("args", "The application arguments.", [])
2015-11-26 12:15:02 +00:00
inputs.declare("photos", "Clusters inputs. list of ODMPhoto's", [])
outputs.declare("photos", "Clusters output. list of ODMPhoto's", [])
def process(self, inputs, outputs):
2015-11-27 16:50:45 +00:00
log.ODM_INFO('Running ODM Resize Cell')
# get inputs
args = self.inputs.args
tree = self.inputs.tree
2015-11-27 16:50:45 +00:00
photos = self.inputs.photos
2015-12-01 16:23:32 +00:00
if not photos:
log.ODM_ERROR('Not enough photos in photos to resize')
return ecto.QUIT
2015-11-27 16:50:45 +00:00
2015-12-01 16:23:32 +00:00
# create working directory
system.mkdir_p(tree.dataset_resize)
2015-11-27 16:50:45 +00:00
log.ODM_DEBUG('Resizing dataset to: %s' % tree.dataset_resize)
2015-11-26 12:15:02 +00:00
# check if we rerun cell or not
rerun_cell = args['rerun'] is not None \
and args['rerun'] == 'resize'
2015-12-01 16:23:32 +00:00
# loop over photos
for photo in photos:
2015-12-10 12:35:52 +00:00
# define image paths
path_file = photo.path_file
new_path_file = io.join_paths(tree.dataset_resize, photo.filename)
2015-12-10 12:35:52 +00:00
# set raw image path in case we want to rerun cell
if io.file_exists(new_path_file) and rerun_cell:
path_file = io.join_paths(tree.dataset_raw, photo.filename)
2015-11-27 16:50:45 +00:00
if not io.file_exists(new_path_file) or rerun_cell:
2015-12-01 16:23:32 +00:00
# open and resize image with opencv
2015-12-10 12:35:52 +00:00
img = cv2.imread(path_file)
2015-12-01 16:23:32 +00:00
# compute new size
2015-12-10 12:35:52 +00:00
max_side = max(img.shape[0], img.shape[1])
ratio = float(self.params.resize_to) / float(max_side)
2015-12-01 16:23:32 +00:00
img_r = cv2.resize(img, None, fx=ratio, fy=ratio)
# write image with opencv
cv2.imwrite(new_path_file, img_r)
# read metadata with pyexiv2
2015-12-10 12:35:52 +00:00
old_meta = pyexiv2.ImageMetadata(path_file)
2015-12-01 16:23:32 +00:00
new_meta = pyexiv2.ImageMetadata(new_path_file)
old_meta.read()
new_meta.read()
# copy metadata
old_meta.copy(new_meta)
2015-12-02 11:16:30 +00:00
# update metadata size
new_meta['Exif.Photo.PixelXDimension'].value = img_r.shape[0]
new_meta['Exif.Photo.PixelYDimension'].value = img_r.shape[1]
2015-12-01 16:23:32 +00:00
new_meta.write()
# update photos array with new values
photo.path_file = new_path_file
photo.width = img_r.shape[0]
photo.height = img_r.shape[1]
photo.update_focal()
2015-11-27 16:50:45 +00:00
2015-12-01 16:23:32 +00:00
# log message
2015-12-02 11:16:30 +00:00
log.ODM_DEBUG('Resized %s | dimensions: %s' % \
(photo.filename, img_r.shape))
2015-12-01 16:23:32 +00:00
else:
2015-12-02 11:16:30 +00:00
# log message
log.ODM_WARNING('Already resized %s | dimensions: %s x %s' % \
(photo.filename, photo.width, photo.height))
2015-11-27 16:50:45 +00:00
log.ODM_INFO('Resized %s images' % len(photos))
# append photos to cell output
self.outputs.photos = photos
2015-11-26 12:15:02 +00:00
2015-11-27 16:50:45 +00:00
log.ODM_INFO('Running ODM Resize Cell - Finished')
return ecto.OK if args['end_with'] != 'resize' else ecto.QUIT