Skip to content

Commit

Permalink
Hot fix for issue with creating the config files for OmniPhotos (prep…
Browse files Browse the repository at this point in the history
…rocessing) (#11)

* fixes issue with generating input images

* fixed issue for if find_stable_cirle is False

Co-authored-by: Reuben Lindroos <rjl67@bath.ac.uk>
  • Loading branch information
reubenlindroos and reubenlindroos committed Oct 25, 2021
1 parent 5c3c288 commit 9ce5672
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 30 deletions.
62 changes: 41 additions & 21 deletions Python/preprocessing/abs_preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import PIL
import yaml
import numpy as np

import ffmpeg
from abs_ui import AbsUI
Expand Down Expand Up @@ -45,12 +46,12 @@ def __init__(self, args):
os.path.abspath(inspect.getfile(inspect.currentframe()))) # current directory

# the image folder
#self.original_images_dir = self.root_dir / "original_images"
# self.original_images_dir = self.root_dir / "original_images"
# the trajectory reconstruction input images (rotated, selected)
self.traj_input_images_dir = self.root_dir / "trajectory_images"
# OmniPhotos ready images from traj_input_images_dir folder
self.op_images_dir = self.root_dir / "Input"
self.original_image_list = [] # storing all original image filenames
self.original_image_list = [] # storing all original image filenames

self.input_path = self.root_dir / self.config["preprocessing.input_path"]
# check input data type
Expand Down Expand Up @@ -86,15 +87,17 @@ def __init__(self, args):
if self.image_start_idx > self.image_end_idx:
self.show_info("image_start_id is larger than image_end_idx", "error")

# create the images list
self.original_filename_expression = self.config["preprocessing.original_filename_expression"]

self.trajectory_images_list = [] # used to storage the processed original image file name
self.op_image_list = [] # used to storage the mp ready image file name
# create the images list and keep copy of desired indices for generation of
# config files for later
self.set_trajectory_images_list(self.original_filename_expression,
self.config["preprocessing.frame_fixed_number"], self.image_start_idx,
self.image_end_idx)

for idx in range(self.image_start_idx, self.image_end_idx + 1):
self.trajectory_images_list.append(self.original_filename_expression % idx)
self.op_image_list.append(self.original_filename_expression % idx)

# NOTE: op_image_list gets overwritten by op_preprocessor if preprocessing.find_stable_cirle is True
self.op_image_list = self.trajectory_images_list

self.image_list = self.op_image_list

Expand Down Expand Up @@ -122,11 +125,29 @@ def __init__(self, args):
self.colmap_essential_file_list = ["points3D.txt", "images.txt", "cameras.txt", "full_model_ba_points3D.ply"]

# default settting
self.ffmpeg_thread_number = 3 # multi-thread thread number configuration
self.ffmpeg_thread_number = 3 # multi-thread thread number configuration
self.cache_folder_name = None

self.check_config()

def set_trajectory_images_list(self, filename_expression,
frame_fixed_number,
image_start_idx,
image_end_idx):
# generate the list of desired frame indices
if frame_fixed_number <= 0:
vframes_size = 1 # NOTE : should be 1
frame_idx_list = list(range(image_start_idx, image_end_idx + 1, vframes_size))
else:
frame_idx_list = \
np.linspace(start=image_start_idx, \
stop=image_end_idx + 1, num=frame_fixed_number)
frame_idx_list = list(frame_idx_list.astype(int))
self.desired_frame_indices = frame_idx_list
# remove the unwanted images
self.trajectory_images_list = [filename_expression % frame for frame in frame_idx_list] # files to keep
self.trajectory_images_list = [os.path.basename(f) for f in self.trajectory_images_list] # filenames to keep

def load_config(self, args):
"""
load configuration from *.yaml file and CLI
Expand Down Expand Up @@ -170,7 +191,7 @@ def load_config(self, args):
elif term_key.find('directory') != -1:
term_value = pathlib.Path(term_value)
if not term_value.exists() or not term_value.is_dir():
msg = 'Directory {} : {} is not exist'.\
msg = 'Directory {} : {} is not exist'. \
format(term_key, str(term_value))

if msg != "":
Expand All @@ -181,14 +202,14 @@ def check_config(self):
Check the setting of variables, and set the default value.
"""
# check the config, if not set, use the default value
setting_list = {"preprocessing.ffmpeg_thread_number": self.ffmpeg_thread_number, "preprocessing.cache_folder_name" : self.cache_folder_name}
setting_list = {"preprocessing.ffmpeg_thread_number": self.ffmpeg_thread_number,
"preprocessing.cache_folder_name": self.cache_folder_name}
for key in setting_list:
try:
item = self.config[key]
except KeyError:
self.show_info("{} not set, use default setting {}".format(key, setting_list[key]))


def load_origin_data_info(self):
"""
get the base information of input data
Expand All @@ -197,16 +218,16 @@ def load_origin_data_info(self):
if self.input_type == 'video':
# get video information
probe = ffmpeg.probe(str(self.input_path))
video_stream = next((stream for stream in probe['streams']\
if stream['codec_type'] == 'video'), None)
video_stream = next((stream for stream in probe['streams'] \
if stream['codec_type'] == 'video'), None)
self.frame_width = int(video_stream['width'])
self.frame_height = int(video_stream['height'])
self.frame_fps = int(eval(video_stream['r_frame_rate']))
self.frame_number = int(video_stream['nb_frames'])
elif self.input_type == 'image':
if not self.input_path.is_dir():
msg = "The images input path {} is not a folder.".format(self.input_path)
raise RuntimeError(msg)
raise RuntimeError(msg)
self.get_image_file_list(self.input_path, self.original_image_list)
self.frame_width, self.frame_height = \
PIL.Image.open(str(self.input_path / self.original_image_list[0])).size
Expand All @@ -216,11 +237,10 @@ def load_origin_data_info(self):
self.show_info(msg, "error")

msg = 'Input data type is {}. {}: width is {}, height is {}, FPS is {}, Frame number is {}'
msg = msg.format(self.input_type, self.input_path, self.frame_width,\
self.frame_height, self.frame_fps, self.frame_number)
msg = msg.format(self.input_type, self.input_path, self.frame_width, \
self.frame_height, self.frame_fps, self.frame_number)
self.show_info(msg)


# def get_image_file_list(self, image_directory=None, image_file_list=None):
def get_image_file_list(self, image_directory, image_file_list):
"""
Expand All @@ -238,8 +258,8 @@ def get_image_file_list(self, image_directory, image_file_list):
file_list = []
for (_, _, files) in os.walk(image_directory):
for filename in files:
if filename.endswith('.png') or filename.endswith('.jpg')\
or filename.endswith('.jpeg'):
if filename.endswith('.png') or filename.endswith('.jpg') \
or filename.endswith('.jpeg'):
file_list.append(filename)
if len(file_list) == 0:
msg = "There do not have images in {}".format(image_directory)
Expand All @@ -266,7 +286,7 @@ def dir_make(self, directory):
self.show_info(msg, "error")
return
if not directory_path.exists():
msg = "Reconstruction output directory {} do not exist, and make a new output directory"\
msg = "Reconstruction output directory {} do not exist, and make a new output directory" \
.format(directory)
directory_path.mkdir()
self.show_info(msg)
Expand Down
34 changes: 25 additions & 9 deletions Python/preprocessing/op_preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,17 @@ def generating_config_files(self):
if self.find_stable_circle:
self.openvslam_select_stable_circle()
self.generating_cache_filename()
# 2) move the image from trajectory directory to ready folder

# create folder for downsampled input images
self.dir_make(self.op_images_dir)

expected_length = len(self.op_image_list)
actual_length = len(os.listdir(self.op_images_dir))

# 2) move the image from trajectory directory to the Input directory
# downsample the input image with the setting \
# "preprocessing.omniphotos.downsample_scalar"
expected_length = self.image_start_idx-self.image_end_idx
actual_length = len(os.listdir(self.op_images_dir))
if not self.op_images_dir.exists() or expected_length!=actual_length:
if expected_length!=actual_length:
self.show_info("Generate the input images for OmniPhotos to {}".format(self.op_images_dir))
self.omniphotos_generate_input_images()

Expand Down Expand Up @@ -303,17 +308,16 @@ def openvslam_select_stable_circle(self):

stable_circle = intervals.find_best_interval()["interval"]
self.image_start_idx, self.image_end_idx = stable_circle[0], stable_circle[1]
self.op_image_list = self.trajectory_images_list[self.image_start_idx:self.image_end_idx]

def omniphotos_generate_input_images(self):
"""
generate the images for OmniPhotos input.
copy & downsample the image from traj_input_images_dir to op_images_dir
"""
# create folder for downsample input images
self.dir_make(self.op_images_dir)

if self.op_input_frame_height == self.frame_height and self.op_input_frame_width == self.frame_width:
for idx, image_filename in enumerate(self.trajectory_images_list[self.image_start_idx:self.image_end_idx]):
for idx, image_filename in enumerate(self.op_image_list):
src_image_path = self.traj_input_images_dir / image_filename
dest_image_path = self.op_images_dir / image_filename
if idx % self.show_infor_interval == 0:
Expand Down Expand Up @@ -655,9 +659,21 @@ def openvslam_convert_traj_file(self,
yaml_traj_csv_file_handle_output = csv.writer(yaml_traj_file_handle_output, delimiter=' ',
quoting=csv.QUOTE_MINIMAL)
yaml_traj_csv_file_handle_output.writerow(output_csv_header)

if self.config["preprocessing.find_stable_circle"]:
# since image start_idx and end_idx will have been updated by circleselector by this point.
desired_frame_indices = self.desired_frame_indices[self.image_start_idx:self.image_end_idx]
else:
desired_frame_indices = self.desired_frame_indices

# need to establish what row in the csv each frame we have extracted corresponds to
# if frame_fixed_number != -1
desired_row_indices = []
for idx in desired_frame_indices:
desired_row_indices.append(idx - desired_frame_indices[0] + self.image_start_idx)
for enum,row in enumerate(yaml_traj_csv_file_handle):
if self.image_start_idx < enum < self.image_end_idx:
idx = int(ovslam_fps * float(row[0]) + 0.5)
if enum in desired_row_indices:
idx = enum - self.image_start_idx + desired_frame_indices[0]
row[0] = self.original_filename_expression % idx
yaml_traj_csv_file_handle_output.writerow([str(idx)] + row)

Expand Down

0 comments on commit 9ce5672

Please sign in to comment.