-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #624 from borglab/mbrick-loader
Runner + Loader for the mobile brick dataset
- Loading branch information
Showing
21 changed files
with
326 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
"""Simple loader class that reads from the MobileBrick dataset. | ||
Reference to MobileBrick: https://code.active.vision/MobileBrick/, Kejie Li et al. | ||
Authors: Akshay Krishnan | ||
""" | ||
|
||
import os | ||
from pathlib import Path | ||
from typing import List, Optional | ||
|
||
import numpy as np | ||
|
||
from gtsam import Cal3Bundler, Pose3, Rot3 | ||
|
||
import gtsfm.utils.io as io_utils | ||
import gtsfm.utils.logger as logger_utils | ||
from gtsfm.common.image import Image | ||
from gtsfm.loader.loader_base import LoaderBase | ||
|
||
logger = logger_utils.get_logger() | ||
|
||
|
||
class MobilebrickLoader(LoaderBase): | ||
"""Loader class that reads from the MobileBrick dataset.""" | ||
|
||
def __init__( | ||
self, | ||
data_dir: str, | ||
use_gt_intrinsics: bool = False, | ||
max_frame_lookahead: int = 5, | ||
max_resolution: int = 1024, | ||
input_worker: Optional[str] = None, | ||
) -> None: | ||
""" """ | ||
super().__init__(max_resolution=max_resolution, input_worker=input_worker) | ||
|
||
self._use_gt_intrinsics = use_gt_intrinsics | ||
self._max_frame_lookahead = max_frame_lookahead | ||
self._image_dir = os.path.join(data_dir, "image") | ||
self._num_images = len(os.listdir(self._image_dir)) | ||
|
||
# Cache image paths | ||
self._image_paths = [] | ||
for i in range(self._num_images): | ||
image_path = os.path.join(self._image_dir, f"{i:06d}.jpg") | ||
self._image_paths.append(image_path) | ||
|
||
# Load GT intrinsics | ||
if self._use_gt_intrinsics: | ||
intrinsics_dir = os.path.join(data_dir, "intrinsic") | ||
self._intrinsics = [] | ||
for i in range(self._num_images): | ||
intrinsics_file = os.path.join(intrinsics_dir, f"{i:06d}.txt") | ||
K = np.loadtxt(intrinsics_file) | ||
self._intrinsics.append(Cal3Bundler((K[0, 0] + K[1, 1]) / 2, 0, 0, K[0, 2], K[1, 2])) | ||
else: | ||
# TODO(akshay): It should be possible to cache approx intrinsics here. | ||
self._intrinsics = None | ||
|
||
# Load GT poses | ||
self._poses_dir = os.path.join(data_dir, "pose") | ||
self._wTi = [] | ||
for i in range(self._num_images): | ||
pose_file = os.path.join(self._poses_dir, f"{i:06d}.txt") | ||
wTi_mat = np.loadtxt(pose_file) | ||
wTi = Pose3(Rot3(wTi_mat[:3, :3]), wTi_mat[:3, 3]) | ||
self._wTi.append(wTi) | ||
|
||
def image_filenames(self) -> List[Path]: | ||
"""Return the file names corresponding to each image index.""" | ||
return [Path(fpath) for fpath in sorted(os.listdir(self._image_dir))] | ||
|
||
def __len__(self) -> int: | ||
"""The number of images in the dataset. | ||
Returns: | ||
The number of images. | ||
""" | ||
return self._num_images | ||
|
||
def get_image_full_res(self, index: int) -> Image: | ||
"""Get the image at the given index, at full resolution. | ||
Args: | ||
index: The index to fetch. | ||
Returns: | ||
The image at the query index. | ||
Raises: | ||
IndexError: If an out-of-bounds image index is requested. | ||
""" | ||
if index < 0 or index >= len(self): | ||
raise IndexError(f"Image index {index} is invalid") | ||
|
||
# Read in image. | ||
img = io_utils.load_image(self._image_paths[index]) | ||
return Image(value_array=img.value_array, exif_data=img.exif_data, file_name=img.file_name) | ||
|
||
def get_camera_intrinsics_full_res(self, index: int) -> Cal3Bundler: | ||
"""Get the camera intrinsics at the given index, valid for a full-resolution image. | ||
Args: | ||
index: The index to fetch. | ||
Returns: | ||
Ground truth intrinsics for the given camera. | ||
Raises: | ||
IndexError: If an out-of-bounds image index is requested. | ||
""" | ||
if index < 0 or index >= len(self): | ||
raise IndexError(f"Image index {index} is invalid") | ||
|
||
if self._intrinsics: | ||
return self._intrinsics[index] | ||
else: | ||
# 0.8 is better than the default factor of 1.2 for this dataset, but it has not been fully tuned. | ||
return io_utils.load_image(self._image_paths[index]).get_intrinsics_from_exif( | ||
default_focal_length_factor=0.8 | ||
) | ||
|
||
def get_camera_pose(self, index: int) -> Optional[Pose3]: | ||
"""Get the camera pose (in world coordinates) at the given index. | ||
Args: | ||
index: The index to fetch. | ||
Returns: | ||
Ground truth pose for the given camera. | ||
Raises: | ||
IndexError: If an out-of-bounds image index is requested. | ||
""" | ||
if index < 0 or index >= len(self): | ||
raise IndexError(f"Image index {index} is invalid") | ||
|
||
wTi = self._wTi[index] | ||
return wTi | ||
|
||
def is_valid_pair(self, idx1: int, idx2: int) -> bool: | ||
"""Checks if (idx1, idx2) is a valid pair. idx1 < idx2 is required. | ||
Args: | ||
idx1: First index of the pair. | ||
idx2: Second index of the pair. | ||
Returns: | ||
Validation result. | ||
""" | ||
return super().is_valid_pair(idx1, idx2) and abs(idx1 - idx2) <= self._max_frame_lookahead |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
"""Runner for datasets loaded from the MobileBrick loader. | ||
Authors: Akshay Krishnan | ||
""" | ||
import argparse | ||
|
||
import gtsfm.utils.logger as logger_utils | ||
from gtsfm.loader.loader_base import LoaderBase | ||
from gtsfm.loader.mobilebrick_loader import MobilebrickLoader | ||
from gtsfm.runner.gtsfm_runner_base import GtsfmRunnerBase | ||
|
||
logger = logger_utils.get_logger() | ||
|
||
|
||
class GtsfmRunnerMobilebrickLoader(GtsfmRunnerBase): | ||
"""Runner for the Mobilebrick dataset.""" | ||
|
||
def __init__(self): | ||
super(GtsfmRunnerMobilebrickLoader, self).__init__(tag="Run GTSFM on dataset from MobileBrick.") | ||
|
||
def construct_argparser(self) -> argparse.ArgumentParser: | ||
parser = super(GtsfmRunnerMobilebrickLoader, self).construct_argparser() | ||
parser.add_argument("--data_dir", type=str, default="", help="") | ||
parser.add_argument("--use_gt_intrinsics", type=bool, default=False, help="") | ||
return parser | ||
|
||
def construct_loader(self) -> LoaderBase: | ||
loader = MobilebrickLoader( | ||
data_dir=self.parsed_args.data_dir, | ||
use_gt_intrinsics=self.parsed_args.use_gt_intrinsics, | ||
max_frame_lookahead=self.parsed_args.max_frame_lookahead, | ||
max_resolution=self.parsed_args.max_resolution, | ||
) | ||
return loader | ||
|
||
|
||
if __name__ == "__main__": | ||
runner = GtsfmRunnerMobilebrickLoader() | ||
runner.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
1459.52795410 0.00000000 962.14691162 | ||
0.00000000 1459.52795410 724.53588867 | ||
0.00000000 0.00000000 1.00000000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
1460.12475586 0.00000000 962.19854736 | ||
0.00000000 1460.12475586 724.49261475 | ||
0.00000000 0.00000000 1.00000000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
1460.12475586 0.00000000 962.19372559 | ||
0.00000000 1460.12475586 724.62994385 | ||
0.00000000 0.00000000 1.00000000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
1459.05395508 0.00000000 962.14215088 | ||
0.00000000 1459.05395508 724.56298828 | ||
0.00000000 0.00000000 1.00000000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
1458.52233887 0.00000000 962.09661865 | ||
0.00000000 1458.52233887 724.60595703 | ||
0.00000000 0.00000000 1.00000000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
-0.46448371 -0.85866576 0.21667491 -0.22408816 | ||
-0.54823434 0.08665860 -0.83182293 0.32383028 | ||
0.69548112 -0.50515682 -0.51100159 0.34980530 | ||
0.00000000 0.00000000 0.00000000 1.00000000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
-0.46954235 -0.85014164 0.23830472 -0.21554375 | ||
-0.55491459 0.07422182 -0.82858974 0.32398531 | ||
0.68673122 -0.52129674 -0.50660628 0.35298249 | ||
0.00000000 0.00000000 0.00000000 1.00000000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
-0.46406239 -0.85505915 0.23134370 -0.20851623 | ||
-0.55035943 0.07367838 -0.83167058 0.33216473 | ||
0.69408256 -0.51326925 -0.50478125 0.36323601 | ||
0.00000000 0.00000000 0.00000000 1.00000000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
-0.45643684 -0.86206996 0.22022893 -0.20537572 | ||
-0.53969383 0.07145945 -0.83882308 0.34330064 | ||
0.70738673 -0.50172597 -0.49787053 0.36881423 | ||
0.00000000 0.00000000 0.00000000 1.00000000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
-0.43685880 -0.87210071 0.22044215 -0.19587429 | ||
-0.53288990 0.05347520 -0.84449321 0.34679961 | ||
0.72469491 -0.48639569 -0.48809478 0.36172125 | ||
0.00000000 0.00000000 0.00000000 1.00000000 |
Oops, something went wrong.