Upload folder using huggingface_hub
Browse files- .gitignore +3 -0
- LICENSE.txt +13 -0
- README.md +20 -0
- hoho/__init__.py +27 -0
- hoho/color_mappings.py +206 -0
- hoho/hoho.py +338 -0
- hoho/read_write_colmap.py +489 -0
- hoho/vis.py +171 -0
- hoho/viz3d.py +302 -0
- hoho/wed.py +107 -0
- requirements.txt +10 -0
- setup.py +16 -0
.gitignore
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.DS_Store
|
| 2 |
+
__pycache__
|
| 3 |
+
hoho.egg-info/
|
LICENSE.txt
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Copyright 2024 Jack Langerman & Dmytro Mishkin
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.
|
README.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
---
|
| 4 |
+
# HoHo Tools
|
| 5 |
+
|
| 6 |
+
Tools and utilities for the [S23DR competition](https://huggingface.co/spaces/usm3d/S23DR) and [HoHo Dataset](https://huggingface.co/datasets/usm3d/usm-training-data)
|
| 7 |
+
|
| 8 |
+
## Installation
|
| 9 |
+
```bash
|
| 10 |
+
# pip install over ssh
|
| 11 |
+
pip install git+ssh://git@hf.co/usm3d/tools.git
|
| 12 |
+
|
| 13 |
+
# pip install over http
|
| 14 |
+
pip install git+http://hf.co/usm3d/tools.git
|
| 15 |
+
|
| 16 |
+
# editable
|
| 17 |
+
git clone http://hf.co/usm3d/tools
|
| 18 |
+
cd tools
|
| 19 |
+
pip install -e .
|
| 20 |
+
```
|
hoho/__init__.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .hoho import *
|
| 2 |
+
from . import vis
|
| 3 |
+
from . import read_write_colmap
|
| 4 |
+
from .wed import compute_WED
|
| 5 |
+
|
| 6 |
+
import importlib
|
| 7 |
+
import sys
|
| 8 |
+
|
| 9 |
+
class LazyLoadModule:
|
| 10 |
+
def __init__(self, module_name):
|
| 11 |
+
self.module_name = module_name
|
| 12 |
+
self.module = None
|
| 13 |
+
|
| 14 |
+
def __getattribute__(self, attr):
|
| 15 |
+
if attr == 'module_name' or attr == 'module':
|
| 16 |
+
return super().__getattribute__(attr)
|
| 17 |
+
|
| 18 |
+
if self.module is None:
|
| 19 |
+
self.module = importlib.import_module(f'hoho.{self.module_name}')
|
| 20 |
+
sys.modules[self.module_name] = self.module
|
| 21 |
+
|
| 22 |
+
return getattr(self.module, attr)
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
import viz3d
|
| 26 |
+
except ImportError:
|
| 27 |
+
viz3d = LazyLoadModule('viz3d')
|
hoho/color_mappings.py
ADDED
|
@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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
gestalt_color_mapping = {
|
| 4 |
+
"unclassified": (215, 62, 138),
|
| 5 |
+
"apex": (235, 88, 48),
|
| 6 |
+
"eave_end_point": (248, 130, 228),
|
| 7 |
+
"flashing_end_point": (71, 11, 161),
|
| 8 |
+
"ridge": (214, 251, 248),
|
| 9 |
+
"rake": (13, 94, 47),
|
| 10 |
+
"eave": (54, 243, 63),
|
| 11 |
+
"post": (187, 123, 236),
|
| 12 |
+
"ground_line": (136, 206, 14),
|
| 13 |
+
"flashing": (162, 162, 32),
|
| 14 |
+
"step_flashing": (169, 255, 219),
|
| 15 |
+
"hip": (8, 89, 52),
|
| 16 |
+
"valley": (85, 27, 65),
|
| 17 |
+
"roof": (215, 232, 179),
|
| 18 |
+
"door": (110, 52, 23),
|
| 19 |
+
"garage": (50, 233, 171),
|
| 20 |
+
"window": (230, 249, 40),
|
| 21 |
+
"shutter": (122, 4, 233),
|
| 22 |
+
"fascia": (95, 230, 240),
|
| 23 |
+
"soffit": (2, 102, 197),
|
| 24 |
+
"horizontal_siding": (131, 88, 59),
|
| 25 |
+
"vertical_siding": (110, 187, 198),
|
| 26 |
+
"brick": (171, 252, 7),
|
| 27 |
+
"concrete": (32, 47, 246),
|
| 28 |
+
"other_wall": (112, 61, 240),
|
| 29 |
+
"trim": (151, 206, 58),
|
| 30 |
+
"unknown": (127, 127, 127),
|
| 31 |
+
"transition_line": (0,0,0),
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
ade20k_color_mapping = {
|
| 35 |
+
'wall': (120, 120, 120),
|
| 36 |
+
'building;edifice': (180, 120, 120),
|
| 37 |
+
'sky': (6, 230, 230),
|
| 38 |
+
'floor;flooring': (80, 50, 50),
|
| 39 |
+
'tree': (4, 200, 3),
|
| 40 |
+
'ceiling': (120, 120, 80),
|
| 41 |
+
'road;route': (140, 140, 140),
|
| 42 |
+
'bed': (204, 5, 255),
|
| 43 |
+
'windowpane;window': (230, 230, 230),
|
| 44 |
+
'grass': (4, 250, 7),
|
| 45 |
+
'cabinet': (224, 5, 255),
|
| 46 |
+
'sidewalk;pavement': (235, 255, 7),
|
| 47 |
+
'person;individual;someone;somebody;mortal;soul': (150, 5, 61),
|
| 48 |
+
'earth;ground': (120, 120, 70),
|
| 49 |
+
'door;double;door': (8, 255, 51),
|
| 50 |
+
'table': (255, 6, 82),
|
| 51 |
+
'mountain;mount': (143, 255, 140),
|
| 52 |
+
'plant;flora;plant;life': (204, 255, 4),
|
| 53 |
+
'curtain;drape;drapery;mantle;pall': (255, 51, 7),
|
| 54 |
+
'chair': (204, 70, 3),
|
| 55 |
+
'car;auto;automobile;machine;motorcar': (0, 102, 200),
|
| 56 |
+
'water': (61, 230, 250),
|
| 57 |
+
'painting;picture': (255, 6, 51),
|
| 58 |
+
'sofa;couch;lounge': (11, 102, 255),
|
| 59 |
+
'shelf': (255, 7, 71),
|
| 60 |
+
'house': (255, 9, 224),
|
| 61 |
+
'sea': (9, 7, 230),
|
| 62 |
+
'mirror': (220, 220, 220),
|
| 63 |
+
'rug;carpet;carpeting': (255, 9, 92),
|
| 64 |
+
'field': (112, 9, 255),
|
| 65 |
+
'armchair': (8, 255, 214),
|
| 66 |
+
'seat': (7, 255, 224),
|
| 67 |
+
'fence;fencing': (255, 184, 6),
|
| 68 |
+
'desk': (10, 255, 71),
|
| 69 |
+
'rock;stone': (255, 41, 10),
|
| 70 |
+
'wardrobe;closet;press': (7, 255, 255),
|
| 71 |
+
'lamp': (224, 255, 8),
|
| 72 |
+
'bathtub;bathing;tub;bath;tub': (102, 8, 255),
|
| 73 |
+
'railing;rail': (255, 61, 6),
|
| 74 |
+
'cushion': (255, 194, 7),
|
| 75 |
+
'base;pedestal;stand': (255, 122, 8),
|
| 76 |
+
'box': (0, 255, 20),
|
| 77 |
+
'column;pillar': (255, 8, 41),
|
| 78 |
+
'signboard;sign': (255, 5, 153),
|
| 79 |
+
'chest;of;drawers;chest;bureau;dresser': (6, 51, 255),
|
| 80 |
+
'counter': (235, 12, 255),
|
| 81 |
+
'sand': (160, 150, 20),
|
| 82 |
+
'sink': (0, 163, 255),
|
| 83 |
+
'skyscraper': (140, 140, 140),
|
| 84 |
+
'fireplace;hearth;open;fireplace': (250, 10, 15),
|
| 85 |
+
'refrigerator;icebox': (20, 255, 0),
|
| 86 |
+
'grandstand;covered;stand': (31, 255, 0),
|
| 87 |
+
'path': (255, 31, 0),
|
| 88 |
+
'stairs;steps': (255, 224, 0),
|
| 89 |
+
'runway': (153, 255, 0),
|
| 90 |
+
'case;display;case;showcase;vitrine': (0, 0, 255),
|
| 91 |
+
'pool;table;billiard;table;snooker;table': (255, 71, 0),
|
| 92 |
+
'pillow': (0, 235, 255),
|
| 93 |
+
'screen;door;screen': (0, 173, 255),
|
| 94 |
+
'stairway;staircase': (31, 0, 255),
|
| 95 |
+
'river': (11, 200, 200),
|
| 96 |
+
'bridge;span': (255 ,82, 0),
|
| 97 |
+
'bookcase': (0, 255, 245),
|
| 98 |
+
'blind;screen': (0, 61, 255),
|
| 99 |
+
'coffee;table;cocktail;table': (0, 255, 112),
|
| 100 |
+
'toilet;can;commode;crapper;pot;potty;stool;throne': (0, 255, 133),
|
| 101 |
+
'flower': (255, 0, 0),
|
| 102 |
+
'book': (255, 163, 0),
|
| 103 |
+
'hill': (255, 102, 0),
|
| 104 |
+
'bench': (194, 255, 0),
|
| 105 |
+
'countertop': (0, 143, 255),
|
| 106 |
+
'stove;kitchen;stove;range;kitchen;range;cooking;stove': (51, 255, 0),
|
| 107 |
+
'palm;palm;tree': (0, 82, 255),
|
| 108 |
+
'kitchen;island': (0, 255, 41),
|
| 109 |
+
'computer;computing;machine;computing;device;data;processor;electronic;computer;information;processing;system': (0, 255, 173),
|
| 110 |
+
'swivel;chair': (10, 0, 255),
|
| 111 |
+
'boat': (173, 255, 0),
|
| 112 |
+
'bar': (0, 255, 153),
|
| 113 |
+
'arcade;machine': (255, 92, 0),
|
| 114 |
+
'hovel;hut;hutch;shack;shanty': (255, 0, 255),
|
| 115 |
+
'bus;autobus;coach;charabanc;double-decker;jitney;motorbus;motorcoach;omnibus;passenger;vehicle': (255, 0, 245),
|
| 116 |
+
'towel': (255, 0, 102),
|
| 117 |
+
'light;light;source': (255, 173, 0),
|
| 118 |
+
'truck;motortruck': (255, 0, 20),
|
| 119 |
+
'tower': (255, 184, 184),
|
| 120 |
+
'chandelier;pendant;pendent': (0, 31, 255),
|
| 121 |
+
'awning;sunshade;sunblind': (0, 255, 61),
|
| 122 |
+
'streetlight;street;lamp': (0, 71, 255),
|
| 123 |
+
'booth;cubicle;stall;kiosk': (255, 0, 204),
|
| 124 |
+
'television;television;receiver;television;set;tv;tv;set;idiot;box;boob;tube;telly;goggle;box': (0, 255, 194),
|
| 125 |
+
'airplane;aeroplane;plane': (0, 255, 82),
|
| 126 |
+
'dirt;track': (0, 10, 255),
|
| 127 |
+
'apparel;wearing;apparel;dress;clothes': (0, 112, 255),
|
| 128 |
+
'pole': (51, 0, 255),
|
| 129 |
+
'land;ground;soil': (0, 194, 255),
|
| 130 |
+
'bannister;banister;balustrade;balusters;handrail': (0, 122, 255),
|
| 131 |
+
'escalator;moving;staircase;moving;stairway': (0, 255, 163),
|
| 132 |
+
'ottoman;pouf;pouffe;puff;hassock': (255, 153, 0),
|
| 133 |
+
'bottle': (0, 255, 10),
|
| 134 |
+
'buffet;counter;sideboard': (255, 112, 0),
|
| 135 |
+
'poster;posting;placard;notice;bill;card': (143, 255, 0),
|
| 136 |
+
'stage': (82, 0, 255),
|
| 137 |
+
'van': (163, 255, 0),
|
| 138 |
+
'ship': (255, 235, 0),
|
| 139 |
+
'fountain': (8, 184, 170),
|
| 140 |
+
'conveyer;belt;conveyor;belt;conveyer;conveyor;transporter': (133, 0, 255),
|
| 141 |
+
'canopy': (0, 255, 92),
|
| 142 |
+
'washer;automatic;washer;washing;machine': (184, 0, 255),
|
| 143 |
+
'plaything;toy': (255, 0, 31),
|
| 144 |
+
'swimming;pool;swimming;bath;natatorium': (0, 184, 255),
|
| 145 |
+
'stool': (0, 214, 255),
|
| 146 |
+
'barrel;cask': (255, 0, 112),
|
| 147 |
+
'basket;handbasket': (92, 255, 0),
|
| 148 |
+
'waterfall;falls': (0, 224, 255),
|
| 149 |
+
'tent;collapsible;shelter': (112, 224, 255),
|
| 150 |
+
'bag': (70, 184, 160),
|
| 151 |
+
'minibike;motorbike': (163, 0, 255),
|
| 152 |
+
'cradle': (153, 0, 255),
|
| 153 |
+
'oven': (71, 255, 0),
|
| 154 |
+
'ball': (255, 0, 163),
|
| 155 |
+
'food;solid;food': (255, 204, 0),
|
| 156 |
+
'step;stair': (255, 0, 143),
|
| 157 |
+
'tank;storage;tank': (0, 255, 235),
|
| 158 |
+
'trade;name;brand;name;brand;marque': (133, 255, 0),
|
| 159 |
+
'microwave;microwave;oven': (255, 0, 235),
|
| 160 |
+
'pot;flowerpot': (245, 0, 255),
|
| 161 |
+
'animal;animate;being;beast;brute;creature;fauna': (255, 0, 122),
|
| 162 |
+
'bicycle;bike;wheel;cycle': (255, 245, 0),
|
| 163 |
+
'lake': (10, 190, 212),
|
| 164 |
+
'dishwasher;dish;washer;dishwashing;machine': (214, 255, 0),
|
| 165 |
+
'screen;silver;screen;projection;screen': (0, 204, 255),
|
| 166 |
+
'blanket;cover': (20, 0, 255),
|
| 167 |
+
'sculpture': (255, 255, 0),
|
| 168 |
+
'hood;exhaust;hood': (0, 153, 255),
|
| 169 |
+
'sconce': (0, 41, 255),
|
| 170 |
+
'vase': (0, 255, 204),
|
| 171 |
+
'traffic;light;traffic;signal;stoplight': (41, 0, 255),
|
| 172 |
+
'tray': (41, 255, 0),
|
| 173 |
+
'ashcan;trash;can;garbage;can;wastebin;ash;bin;ash-bin;ashbin;dustbin;trash;barrel;trash;bin': (173, 0, 255),
|
| 174 |
+
'fan': (0, 245, 255),
|
| 175 |
+
'pier;wharf;wharfage;dock': (71, 0, 255),
|
| 176 |
+
'crt;screen': (122, 0, 255),
|
| 177 |
+
'plate': (0, 255, 184),
|
| 178 |
+
'monitor;monitoring;device': (0, 92, 255),
|
| 179 |
+
'bulletin;board;notice;board': (184, 255, 0),
|
| 180 |
+
'shower': (0, 133, 255),
|
| 181 |
+
'radiator': (255, 214, 0),
|
| 182 |
+
'glass;drinking;glass': (25, 194, 194),
|
| 183 |
+
'clock': (102, 255, 0),
|
| 184 |
+
'flag': (92, 0, 255),
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
# edge_colors = np.asarray([(214, 251, 248),
|
| 189 |
+
# (13, 94, 47),
|
| 190 |
+
# (54, 243, 63),
|
| 191 |
+
# (187, 123, 236),
|
| 192 |
+
# (162, 162, 32),
|
| 193 |
+
# (169, 255, 219),
|
| 194 |
+
# (8, 89, 52),
|
| 195 |
+
# (85, 27, 65),
|
| 196 |
+
# (0, 0, 0)]
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
# edge_colors = np.array([[ 54, 243, 63],
|
| 200 |
+
# [214, 251, 248],
|
| 201 |
+
# [169, 255, 219],
|
| 202 |
+
# [ 13, 94, 47],
|
| 203 |
+
# [162, 162, 32],
|
| 204 |
+
# [187, 123, 236],
|
| 205 |
+
# [ 85, 27, 65],
|
| 206 |
+
# [ 0, 0, 0]])
|
hoho/hoho.py
ADDED
|
@@ -0,0 +1,338 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 9 |
+
from PIL import Image
|
| 10 |
+
import io
|
| 11 |
+
import webdataset as wds
|
| 12 |
+
import numpy as np
|
| 13 |
+
import importlib
|
| 14 |
+
import subprocess
|
| 15 |
+
|
| 16 |
+
from PIL import ImageFile
|
| 17 |
+
|
| 18 |
+
from huggingface_hub.utils._headers import build_hf_headers # note: using _headers
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
| 22 |
+
|
| 23 |
+
LOCAL_DATADIR = None
|
| 24 |
+
|
| 25 |
+
def setup(local_dir='./data/usm-training-data/data'):
|
| 26 |
+
|
| 27 |
+
# If we are in the test environment, we need to link the data directory to the correct location
|
| 28 |
+
tmp_datadir = Path('/tmp/data/data')
|
| 29 |
+
local_test_datadir = Path('./data/usm-test-data-x/data')
|
| 30 |
+
local_val_datadir = Path(local_dir)
|
| 31 |
+
|
| 32 |
+
os.system('pwd')
|
| 33 |
+
os.system('ls -lahtr .')
|
| 34 |
+
|
| 35 |
+
if tmp_datadir.exists() and not local_test_datadir.exists():
|
| 36 |
+
global LOCAL_DATADIR
|
| 37 |
+
LOCAL_DATADIR = local_test_datadir
|
| 38 |
+
# shutil.move(datadir, './usm-test-data-x/data')
|
| 39 |
+
print(f"Linking {tmp_datadir} to {LOCAL_DATADIR} (we are in the test environment)")
|
| 40 |
+
LOCAL_DATADIR.parent.mkdir(parents=True, exist_ok=True)
|
| 41 |
+
LOCAL_DATADIR.symlink_to(tmp_datadir)
|
| 42 |
+
else:
|
| 43 |
+
LOCAL_DATADIR = local_val_datadir
|
| 44 |
+
print(f"Using {LOCAL_DATADIR} as the data directory (we are running locally)")
|
| 45 |
+
|
| 46 |
+
if not LOCAL_DATADIR.exists():
|
| 47 |
+
warnings.warn(f"Data directory {LOCAL_DATADIR} does not exist: creating it...")
|
| 48 |
+
LOCAL_DATADIR.mkdir(parents=True)
|
| 49 |
+
|
| 50 |
+
return LOCAL_DATADIR
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def download_package(package_name, path_to_save='packages'):
|
| 54 |
+
"""
|
| 55 |
+
Downloads a package using pip and saves it to a specified directory.
|
| 56 |
+
|
| 57 |
+
Parameters:
|
| 58 |
+
package_name (str): The name of the package to download.
|
| 59 |
+
path_to_save (str): The path to the directory where the package will be saved.
|
| 60 |
+
"""
|
| 61 |
+
try:
|
| 62 |
+
# pip download webdataset -d packages/webdataset --platform manylinux1_x86_64 --python-version 38 --only-binary=:all:
|
| 63 |
+
subprocess.check_call([subprocess.sys.executable, "-m", "pip", "download", package_name,
|
| 64 |
+
"-d", str(Path(path_to_save)/package_name), # Download the package to the specified directory
|
| 65 |
+
"--platform", "manylinux1_x86_64", # Specify the platform
|
| 66 |
+
"--python-version", "38", # Specify the Python version
|
| 67 |
+
"--only-binary=:all:"]) # Download only binary packages
|
| 68 |
+
print(f'Package "{package_name}" downloaded successfully')
|
| 69 |
+
except subprocess.CalledProcessError as e:
|
| 70 |
+
print(f'Failed to downloaded package "{package_name}". Error: {e}')
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def install_package_from_local_file(package_name, folder='packages'):
|
| 74 |
+
"""
|
| 75 |
+
Installs a package from a local .whl file or a directory containing .whl files using pip.
|
| 76 |
+
|
| 77 |
+
Parameters:
|
| 78 |
+
path_to_file_or_directory (str): The path to the .whl file or the directory containing .whl files.
|
| 79 |
+
"""
|
| 80 |
+
try:
|
| 81 |
+
pth = str(Path(folder) / package_name)
|
| 82 |
+
subprocess.check_call([subprocess.sys.executable, "-m", "pip", "install",
|
| 83 |
+
"--no-index", # Do not use package index
|
| 84 |
+
"--find-links", pth, # Look for packages in the specified directory or at the file
|
| 85 |
+
package_name]) # Specify the package to install
|
| 86 |
+
print(f"Package installed successfully from {pth}")
|
| 87 |
+
except subprocess.CalledProcessError as e:
|
| 88 |
+
print(f"Failed to install package from {pth}. Error: {e}")
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def importt(module_name, as_name=None):
|
| 92 |
+
"""
|
| 93 |
+
Imports a module and returns it.
|
| 94 |
+
|
| 95 |
+
Parameters:
|
| 96 |
+
module_name (str): The name of the module to import.
|
| 97 |
+
as_name (str): The name to use for the imported module. If None, the original module name will be used.
|
| 98 |
+
|
| 99 |
+
Returns:
|
| 100 |
+
The imported module.
|
| 101 |
+
"""
|
| 102 |
+
for _ in range(2):
|
| 103 |
+
try:
|
| 104 |
+
if as_name is None:
|
| 105 |
+
print(f'imported {module_name}')
|
| 106 |
+
return importlib.import_module(module_name)
|
| 107 |
+
else:
|
| 108 |
+
print(f'imported {module_name} as {as_name}')
|
| 109 |
+
return importlib.import_module(module_name, as_name)
|
| 110 |
+
except ModuleNotFoundError as e:
|
| 111 |
+
install_package_from_local_file(module_name)
|
| 112 |
+
print(f"Failed to import module {module_name}. Error: {e}")
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def prepare_submission():
|
| 116 |
+
# Download packages from requirements.txt
|
| 117 |
+
if Path('requirements.txt').exists():
|
| 118 |
+
print('downloading packages from requirements.txt')
|
| 119 |
+
Path('packages').mkdir(exist_ok=True)
|
| 120 |
+
with open('requirements.txt') as f:
|
| 121 |
+
packages = f.readlines()
|
| 122 |
+
for p in packages:
|
| 123 |
+
download_package(p.strip())
|
| 124 |
+
|
| 125 |
+
print('all packages downloaded. Don\'t foget to include the packages in the submission by adding them with git lfs.')
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
def Rt_to_eye_target(im, K, R, t):
|
| 129 |
+
height = im.height
|
| 130 |
+
focal_length = K[0,0]
|
| 131 |
+
fov = 2.0 * np.arctan2((0.5 * height), focal_length) / (np.pi / 180.0)
|
| 132 |
+
|
| 133 |
+
x_axis, y_axis, z_axis = R
|
| 134 |
+
|
| 135 |
+
eye = -(R.T @ t).squeeze()
|
| 136 |
+
z_axis = z_axis.squeeze()
|
| 137 |
+
target = eye + z_axis
|
| 138 |
+
up = -y_axis
|
| 139 |
+
|
| 140 |
+
return eye, target, up, fov
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
########## general utilities ##########
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
@contextlib.contextmanager
|
| 147 |
+
def working_directory(path):
|
| 148 |
+
"""Changes working directory and returns to previous on exit."""
|
| 149 |
+
prev_cwd = Path.cwd()
|
| 150 |
+
os.chdir(path)
|
| 151 |
+
try:
|
| 152 |
+
yield
|
| 153 |
+
finally:
|
| 154 |
+
os.chdir(prev_cwd)
|
| 155 |
+
|
| 156 |
+
@contextlib.contextmanager
|
| 157 |
+
def temp_working_directory():
|
| 158 |
+
with tempfile.TemporaryDirectory(dir='.') as D:
|
| 159 |
+
with working_directory(D):
|
| 160 |
+
yield
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
############# Dataset #############
|
| 164 |
+
def proc(row, split='train'):
|
| 165 |
+
out = {}
|
| 166 |
+
out['__key__'] = None
|
| 167 |
+
out['__imagekey__'] = []
|
| 168 |
+
for k, v in row.items():
|
| 169 |
+
key_parts = k.split('.')
|
| 170 |
+
colname = key_parts[0]
|
| 171 |
+
if colname == 'ade20k':
|
| 172 |
+
out['__imagekey__'].append(key_parts[1])
|
| 173 |
+
if colname in {'ade20k', 'depthcm', 'gestalt'}:
|
| 174 |
+
if colname in out:
|
| 175 |
+
out[colname].append(v)
|
| 176 |
+
else:
|
| 177 |
+
out[colname] = [v]
|
| 178 |
+
elif colname in {'wireframe', 'mesh'}:
|
| 179 |
+
out.update({a: b for a,b in v.items()})
|
| 180 |
+
elif colname in 'kr':
|
| 181 |
+
out[colname.upper()] = v
|
| 182 |
+
else:
|
| 183 |
+
out[colname] = v
|
| 184 |
+
return Sample(out)
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
from . import read_write_colmap
|
| 188 |
+
def decode_colmap(s):
|
| 189 |
+
with temp_working_directory():
|
| 190 |
+
|
| 191 |
+
with open('points3D.bin', 'wb') as stream:
|
| 192 |
+
stream.write(s['points3d'])
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
with open('cameras.bin', 'wb') as stream:
|
| 196 |
+
stream.write(s['cameras'])
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
with open('images.bin', 'wb') as stream:
|
| 200 |
+
stream.write(s['images'])
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
cameras, images, points3D = read_write_colmap.read_model(
|
| 204 |
+
path='.', ext='.bin'
|
| 205 |
+
)
|
| 206 |
+
return cameras, images, points3D
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
def decode(row):
|
| 210 |
+
cameras, images, points3D = decode_colmap(row)
|
| 211 |
+
|
| 212 |
+
out = {}
|
| 213 |
+
|
| 214 |
+
for k, v in row.items():
|
| 215 |
+
# colname = k.split('.')[0]
|
| 216 |
+
if k in {'ade20k', 'depthcm', 'gestalt'}:
|
| 217 |
+
# print(k, len(v), type(v))
|
| 218 |
+
v = [Image.open(io.BytesIO(im)) for im in v]
|
| 219 |
+
if k in out:
|
| 220 |
+
out[k].extend(v)
|
| 221 |
+
else:
|
| 222 |
+
out[k] = v
|
| 223 |
+
elif k in {'wireframe', 'mesh'}:
|
| 224 |
+
# out.update({a: b.tolist() for a,b in v.items()})
|
| 225 |
+
v = dict(np.load(io.BytesIO(v)))
|
| 226 |
+
out.update({a: b for a,b in v.items()})
|
| 227 |
+
elif k in 'kr':
|
| 228 |
+
out[k.upper()] = v
|
| 229 |
+
elif k == 'cameras':
|
| 230 |
+
out[k] = cameras
|
| 231 |
+
elif k == 'images':
|
| 232 |
+
out[k] = images
|
| 233 |
+
elif k =='points3d':
|
| 234 |
+
out[k] = points3D
|
| 235 |
+
else:
|
| 236 |
+
out[k] = v
|
| 237 |
+
|
| 238 |
+
return Sample(out)
|
| 239 |
+
|
| 240 |
+
|
| 241 |
+
class Sample(Dict):
|
| 242 |
+
def __repr__(self):
|
| 243 |
+
return str({k: v.shape if hasattr(v, 'shape') else [type(v[0])] if isinstance(v, list) else type(v) for k,v in self.items()})
|
| 244 |
+
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
def get_params():
|
| 248 |
+
exmaple_param_dict = {
|
| 249 |
+
"competition_id": "usm3d/S23DR",
|
| 250 |
+
"competition_type": "script",
|
| 251 |
+
"metric": "custom",
|
| 252 |
+
"token": "hf_**********************************",
|
| 253 |
+
"team_id": "local-test-team_id",
|
| 254 |
+
"submission_id": "local-test-submission_id",
|
| 255 |
+
"submission_id_col": "__key__",
|
| 256 |
+
"submission_cols": [
|
| 257 |
+
"__key__",
|
| 258 |
+
"wf_edges",
|
| 259 |
+
"wf_vertices",
|
| 260 |
+
"edge_semantics"
|
| 261 |
+
],
|
| 262 |
+
"submission_rows": 180,
|
| 263 |
+
"output_path": ".",
|
| 264 |
+
"submission_repo": "<THE HF MODEL ID of THIS REPO",
|
| 265 |
+
"time_limit": 7200,
|
| 266 |
+
"dataset": "usm3d/usm-test-data-x",
|
| 267 |
+
"submission_filenames": [
|
| 268 |
+
"submission.parquet"
|
| 269 |
+
]
|
| 270 |
+
}
|
| 271 |
+
|
| 272 |
+
param_path = Path('params.json')
|
| 273 |
+
|
| 274 |
+
if not param_path.exists():
|
| 275 |
+
print('params.json not found (this means we probably aren\'t in the test env). Using example params.')
|
| 276 |
+
params = exmaple_param_dict
|
| 277 |
+
else:
|
| 278 |
+
print('found params.json (this means we are probably in the test env). Using params from file.')
|
| 279 |
+
with param_path.open() as f:
|
| 280 |
+
params = json.load(f)
|
| 281 |
+
print(params)
|
| 282 |
+
return params
|
| 283 |
+
|
| 284 |
+
|
| 285 |
+
|
| 286 |
+
|
| 287 |
+
|
| 288 |
+
SHARD_IDS = {'train': (0, 25), 'val': (25, 26), 'public': (26, 27), 'private': (27, 32)}
|
| 289 |
+
def get_dataset(decode='pil', proc=proc, split='train', dataset_type='webdataset', stream=True):
|
| 290 |
+
if LOCAL_DATADIR is None:
|
| 291 |
+
raise ValueError('LOCAL_DATADIR is not set. Please run setup() first.')
|
| 292 |
+
|
| 293 |
+
local_dir = Path(LOCAL_DATADIR)
|
| 294 |
+
if split != 'all':
|
| 295 |
+
local_dir = local_dir / split
|
| 296 |
+
|
| 297 |
+
paths = [str(p) for p in local_dir.rglob('*.tar.gz')]
|
| 298 |
+
msg = f'no tarfiles found in {local_dir}.'
|
| 299 |
+
if len(paths) == 0:
|
| 300 |
+
if stream:
|
| 301 |
+
if split=='all': split = 'train'
|
| 302 |
+
warnings.warn('streaming isn\'t using with \'all\': changing `split` to \'train\'')
|
| 303 |
+
warnings.warn(msg)
|
| 304 |
+
if split == 'val':
|
| 305 |
+
names = [f'data/val/inputs/hoho_v3_{i:03}-of-032.tar.gz' for i in range(*SHARD_IDS[split])]
|
| 306 |
+
elif split == 'train':
|
| 307 |
+
names = [f'data/train/hoho_v3_{i:03}-of-032.tar.gz' for i in range(*SHARD_IDS[split])]
|
| 308 |
+
|
| 309 |
+
auth = build_hf_headers()['authorization']
|
| 310 |
+
paths = [f"pipe:curl -L -s https://huggingface.co/datasets/usm3d/hoho-train-set/resolve/main/{name} -H 'Authorization: {auth}'" for name in names]
|
| 311 |
+
else:
|
| 312 |
+
raise FileNotFoundError(msg)
|
| 313 |
+
|
| 314 |
+
dataset = wds.WebDataset(paths)
|
| 315 |
+
|
| 316 |
+
if decode is not None:
|
| 317 |
+
dataset = dataset.decode(decode)
|
| 318 |
+
else:
|
| 319 |
+
dataset = dataset.decode()
|
| 320 |
+
|
| 321 |
+
dataset = dataset.map(proc)
|
| 322 |
+
|
| 323 |
+
if dataset_type == 'webdataset':
|
| 324 |
+
return dataset
|
| 325 |
+
|
| 326 |
+
if dataset_type == 'hf':
|
| 327 |
+
import datasets
|
| 328 |
+
from datasets import Features, Value, Sequence, Image, Array2D
|
| 329 |
+
|
| 330 |
+
if split == 'train':
|
| 331 |
+
return datasets.IterableDataset.from_generator(lambda: dataset.iterator())
|
| 332 |
+
elif split == 'val':
|
| 333 |
+
return datasets.IterableDataset.from_generator(lambda: dataset.iterator())
|
| 334 |
+
else:
|
| 335 |
+
raise NotImplementedError('only train and val are implemented as hf datasets')
|
| 336 |
+
|
| 337 |
+
|
| 338 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apel Hill.
|
| 4 |
+
# All rights reserved.
|
| 5 |
+
#
|
| 6 |
+
# Redistribution and use in source and binary forms, with or without
|
| 7 |
+
# modification, are permitted provided that the following conditions are met:
|
| 8 |
+
#
|
| 9 |
+
# * Redistributions of source code must retain the above copyright
|
| 10 |
+
# notice, this list of conditions and the following disclaimer.
|
| 11 |
+
#
|
| 12 |
+
# * Redistributions in binary form must reproduce the above copyright
|
| 13 |
+
# notice, this list of conditions and the following disclaimer in the
|
| 14 |
+
# documentation and/or other materials provided with the distribution.
|
| 15 |
+
#
|
| 16 |
+
# * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of
|
| 17 |
+
# its contributors may be used to endorse or promote products derived
|
| 18 |
+
# from this software without specific prior written permission.
|
| 19 |
+
#
|
| 20 |
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
| 21 |
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
| 22 |
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
| 23 |
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
|
| 24 |
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
| 25 |
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
| 26 |
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
| 27 |
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
| 28 |
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
| 29 |
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
| 30 |
+
# POSSIBILITY OF SUCH DAMAGE.
|
| 31 |
+
#
|
| 32 |
+
# Author: Johannes L. Schoenberger (jsch-at-demuc-dot-de)
|
| 33 |
+
|
| 34 |
+
import os
|
| 35 |
+
import collections
|
| 36 |
+
import numpy as np
|
| 37 |
+
import struct
|
| 38 |
+
import argparse
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
CameraModel = collections.namedtuple(
|
| 42 |
+
"CameraModel", ["model_id", "model_name", "num_params"])
|
| 43 |
+
Camera = collections.namedtuple(
|
| 44 |
+
"Camera", ["id", "model", "width", "height", "params"])
|
| 45 |
+
BaseImage = collections.namedtuple(
|
| 46 |
+
"Image", ["id", "qvec", "tvec", "camera_id", "name", "xys", "point3D_ids"])
|
| 47 |
+
Point3D = collections.namedtuple(
|
| 48 |
+
"Point3D", ["id", "xyz", "rgb", "error", "image_ids", "point2D_idxs"])
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
class Image(BaseImage):
|
| 52 |
+
def qvec2rotmat(self):
|
| 53 |
+
return qvec2rotmat(self.qvec)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
CAMERA_MODELS = {
|
| 57 |
+
CameraModel(model_id=0, model_name="SIMPLE_PINHOLE", num_params=3),
|
| 58 |
+
CameraModel(model_id=1, model_name="PINHOLE", num_params=4),
|
| 59 |
+
CameraModel(model_id=2, model_name="SIMPLE_RADIAL", num_params=4),
|
| 60 |
+
CameraModel(model_id=3, model_name="RADIAL", num_params=5),
|
| 61 |
+
CameraModel(model_id=4, model_name="OPENCV", num_params=8),
|
| 62 |
+
CameraModel(model_id=5, model_name="OPENCV_FISHEYE", num_params=8),
|
| 63 |
+
CameraModel(model_id=6, model_name="FULL_OPENCV", num_params=12),
|
| 64 |
+
CameraModel(model_id=7, model_name="FOV", num_params=5),
|
| 65 |
+
CameraModel(model_id=8, model_name="SIMPLE_RADIAL_FISHEYE", num_params=4),
|
| 66 |
+
CameraModel(model_id=9, model_name="RADIAL_FISHEYE", num_params=5),
|
| 67 |
+
CameraModel(model_id=10, model_name="THIN_PRISM_FISHEYE", num_params=12)
|
| 68 |
+
}
|
| 69 |
+
CAMERA_MODEL_IDS = dict([(camera_model.model_id, camera_model)
|
| 70 |
+
for camera_model in CAMERA_MODELS])
|
| 71 |
+
CAMERA_MODEL_NAMES = dict([(camera_model.model_name, camera_model)
|
| 72 |
+
for camera_model in CAMERA_MODELS])
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def read_next_bytes(fid, num_bytes, format_char_sequence, endian_character="<"):
|
| 76 |
+
"""Read and unpack the next bytes from a binary file.
|
| 77 |
+
:param fid:
|
| 78 |
+
:param num_bytes: Sum of combination of {2, 4, 8}, e.g. 2, 6, 16, 30, etc.
|
| 79 |
+
:param format_char_sequence: List of {c, e, f, d, h, H, i, I, l, L, q, Q}.
|
| 80 |
+
:param endian_character: Any of {@, =, <, >, !}
|
| 81 |
+
:return: Tuple of read and unpacked values.
|
| 82 |
+
"""
|
| 83 |
+
data = fid.read(num_bytes)
|
| 84 |
+
return struct.unpack(endian_character + format_char_sequence, data)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def write_next_bytes(fid, data, format_char_sequence, endian_character="<"):
|
| 88 |
+
"""pack and write to a binary file.
|
| 89 |
+
:param fid:
|
| 90 |
+
:param data: data to send, if multiple elements are sent at the same time,
|
| 91 |
+
they should be encapsuled either in a list or a tuple
|
| 92 |
+
:param format_char_sequence: List of {c, e, f, d, h, H, i, I, l, L, q, Q}.
|
| 93 |
+
should be the same length as the data list or tuple
|
| 94 |
+
:param endian_character: Any of {@, =, <, >, !}
|
| 95 |
+
"""
|
| 96 |
+
if isinstance(data, (list, tuple)):
|
| 97 |
+
bytes = struct.pack(endian_character + format_char_sequence, *data)
|
| 98 |
+
else:
|
| 99 |
+
bytes = struct.pack(endian_character + format_char_sequence, data)
|
| 100 |
+
fid.write(bytes)
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def read_cameras_text(path):
|
| 104 |
+
"""
|
| 105 |
+
see: src/base/reconstruction.cc
|
| 106 |
+
void Reconstruction::WriteCamerasText(const std::string& path)
|
| 107 |
+
void Reconstruction::ReadCamerasText(const std::string& path)
|
| 108 |
+
"""
|
| 109 |
+
cameras = {}
|
| 110 |
+
with open(path, "r") as fid:
|
| 111 |
+
while True:
|
| 112 |
+
line = fid.readline()
|
| 113 |
+
if not line:
|
| 114 |
+
break
|
| 115 |
+
line = line.strip()
|
| 116 |
+
if len(line) > 0 and line[0] != "#":
|
| 117 |
+
elems = line.split()
|
| 118 |
+
camera_id = int(elems[0])
|
| 119 |
+
model = elems[1]
|
| 120 |
+
width = int(elems[2])
|
| 121 |
+
height = int(elems[3])
|
| 122 |
+
params = np.array(tuple(map(float, elems[4:])))
|
| 123 |
+
cameras[camera_id] = Camera(id=camera_id, model=model,
|
| 124 |
+
width=width, height=height,
|
| 125 |
+
params=params)
|
| 126 |
+
return cameras
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
def read_cameras_binary(path_to_model_file=None, fid=None):
|
| 130 |
+
"""
|
| 131 |
+
see: src/base/reconstruction.cc
|
| 132 |
+
void Reconstruction::WriteCamerasBinary(const std::string& path)
|
| 133 |
+
void Reconstruction::ReadCamerasBinary(const std::string& path)
|
| 134 |
+
"""
|
| 135 |
+
cameras = {}
|
| 136 |
+
if fid is None:
|
| 137 |
+
fid = open(path_to_model_file, "rb")
|
| 138 |
+
num_cameras = read_next_bytes(fid, 8, "Q")[0]
|
| 139 |
+
for _ in range(num_cameras):
|
| 140 |
+
camera_properties = read_next_bytes(
|
| 141 |
+
fid, num_bytes=24, format_char_sequence="iiQQ")
|
| 142 |
+
camera_id = camera_properties[0]
|
| 143 |
+
model_id = camera_properties[1]
|
| 144 |
+
model_name = CAMERA_MODEL_IDS[camera_properties[1]].model_name
|
| 145 |
+
width = camera_properties[2]
|
| 146 |
+
height = camera_properties[3]
|
| 147 |
+
num_params = CAMERA_MODEL_IDS[model_id].num_params
|
| 148 |
+
params = read_next_bytes(fid, num_bytes=8*num_params,
|
| 149 |
+
format_char_sequence="d"*num_params)
|
| 150 |
+
cameras[camera_id] = Camera(id=camera_id,
|
| 151 |
+
model=model_name,
|
| 152 |
+
width=width,
|
| 153 |
+
height=height,
|
| 154 |
+
params=np.array(params))
|
| 155 |
+
assert len(cameras) == num_cameras
|
| 156 |
+
if path_to_model_file is not None:
|
| 157 |
+
fid.close()
|
| 158 |
+
return cameras
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
def write_cameras_text(cameras, path):
|
| 162 |
+
"""
|
| 163 |
+
see: src/base/reconstruction.cc
|
| 164 |
+
void Reconstruction::WriteCamerasText(const std::string& path)
|
| 165 |
+
void Reconstruction::ReadCamerasText(const std::string& path)
|
| 166 |
+
"""
|
| 167 |
+
HEADER = "# Camera list with one line of data per camera:\n" + \
|
| 168 |
+
"# CAMERA_ID, MODEL, WIDTH, HEIGHT, PARAMS[]\n" + \
|
| 169 |
+
"# Number of cameras: {}\n".format(len(cameras))
|
| 170 |
+
with open(path, "w") as fid:
|
| 171 |
+
fid.write(HEADER)
|
| 172 |
+
for _, cam in cameras.items():
|
| 173 |
+
to_write = [cam.id, cam.model, cam.width, cam.height, *cam.params]
|
| 174 |
+
line = " ".join([str(elem) for elem in to_write])
|
| 175 |
+
fid.write(line + "\n")
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
def write_cameras_binary(cameras, path_to_model_file):
|
| 179 |
+
"""
|
| 180 |
+
see: src/base/reconstruction.cc
|
| 181 |
+
void Reconstruction::WriteCamerasBinary(const std::string& path)
|
| 182 |
+
void Reconstruction::ReadCamerasBinary(const std::string& path)
|
| 183 |
+
"""
|
| 184 |
+
with open(path_to_model_file, "wb") as fid:
|
| 185 |
+
write_next_bytes(fid, len(cameras), "Q")
|
| 186 |
+
for _, cam in cameras.items():
|
| 187 |
+
model_id = CAMERA_MODEL_NAMES[cam.model].model_id
|
| 188 |
+
camera_properties = [cam.id,
|
| 189 |
+
model_id,
|
| 190 |
+
cam.width,
|
| 191 |
+
cam.height]
|
| 192 |
+
write_next_bytes(fid, camera_properties, "iiQQ")
|
| 193 |
+
for p in cam.params:
|
| 194 |
+
write_next_bytes(fid, float(p), "d")
|
| 195 |
+
return cameras
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
def read_images_text(path):
|
| 199 |
+
"""
|
| 200 |
+
see: src/base/reconstruction.cc
|
| 201 |
+
void Reconstruction::ReadImagesText(const std::string& path)
|
| 202 |
+
void Reconstruction::WriteImagesText(const std::string& path)
|
| 203 |
+
"""
|
| 204 |
+
images = {}
|
| 205 |
+
with open(path, "r") as fid:
|
| 206 |
+
while True:
|
| 207 |
+
line = fid.readline()
|
| 208 |
+
if not line:
|
| 209 |
+
break
|
| 210 |
+
line = line.strip()
|
| 211 |
+
if len(line) > 0 and line[0] != "#":
|
| 212 |
+
elems = line.split()
|
| 213 |
+
image_id = int(elems[0])
|
| 214 |
+
qvec = np.array(tuple(map(float, elems[1:5])))
|
| 215 |
+
tvec = np.array(tuple(map(float, elems[5:8])))
|
| 216 |
+
camera_id = int(elems[8])
|
| 217 |
+
image_name = elems[9]
|
| 218 |
+
elems = fid.readline().split()
|
| 219 |
+
xys = np.column_stack([tuple(map(float, elems[0::3])),
|
| 220 |
+
tuple(map(float, elems[1::3]))])
|
| 221 |
+
point3D_ids = np.array(tuple(map(int, elems[2::3])))
|
| 222 |
+
images[image_id] = Image(
|
| 223 |
+
id=image_id, qvec=qvec, tvec=tvec,
|
| 224 |
+
camera_id=camera_id, name=image_name,
|
| 225 |
+
xys=xys, point3D_ids=point3D_ids)
|
| 226 |
+
return images
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
def read_images_binary(path_to_model_file=None, fid=None):
|
| 230 |
+
"""
|
| 231 |
+
see: src/base/reconstruction.cc
|
| 232 |
+
void Reconstruction::ReadImagesBinary(const std::string& path)
|
| 233 |
+
void Reconstruction::WriteImagesBinary(const std::string& path)
|
| 234 |
+
"""
|
| 235 |
+
images = {}
|
| 236 |
+
if fid is None:
|
| 237 |
+
fid = open(path_to_model_file, "rb")
|
| 238 |
+
num_reg_images = read_next_bytes(fid, 8, "Q")[0]
|
| 239 |
+
for _ in range(num_reg_images):
|
| 240 |
+
binary_image_properties = read_next_bytes(
|
| 241 |
+
fid, num_bytes=64, format_char_sequence="idddddddi")
|
| 242 |
+
image_id = binary_image_properties[0]
|
| 243 |
+
qvec = np.array(binary_image_properties[1:5])
|
| 244 |
+
tvec = np.array(binary_image_properties[5:8])
|
| 245 |
+
camera_id = binary_image_properties[8]
|
| 246 |
+
image_name = ""
|
| 247 |
+
current_char = read_next_bytes(fid, 1, "c")[0]
|
| 248 |
+
while current_char != b"\x00": # look for the ASCII 0 entry
|
| 249 |
+
image_name += current_char.decode("utf-8")
|
| 250 |
+
current_char = read_next_bytes(fid, 1, "c")[0]
|
| 251 |
+
num_points2D = read_next_bytes(fid, num_bytes=8,
|
| 252 |
+
format_char_sequence="Q")[0]
|
| 253 |
+
x_y_id_s = read_next_bytes(fid, num_bytes=24*num_points2D,
|
| 254 |
+
format_char_sequence="ddq"*num_points2D)
|
| 255 |
+
xys = np.column_stack([tuple(map(float, x_y_id_s[0::3])),
|
| 256 |
+
tuple(map(float, x_y_id_s[1::3]))])
|
| 257 |
+
point3D_ids = np.array(tuple(map(int, x_y_id_s[2::3])))
|
| 258 |
+
images[image_id] = Image(
|
| 259 |
+
id=image_id, qvec=qvec, tvec=tvec,
|
| 260 |
+
camera_id=camera_id, name=image_name,
|
| 261 |
+
xys=xys, point3D_ids=point3D_ids)
|
| 262 |
+
if path_to_model_file is not None:
|
| 263 |
+
fid.close()
|
| 264 |
+
return images
|
| 265 |
+
|
| 266 |
+
|
| 267 |
+
def write_images_text(images, path):
|
| 268 |
+
"""
|
| 269 |
+
see: src/base/reconstruction.cc
|
| 270 |
+
void Reconstruction::ReadImagesText(const std::string& path)
|
| 271 |
+
void Reconstruction::WriteImagesText(const std::string& path)
|
| 272 |
+
"""
|
| 273 |
+
if len(images) == 0:
|
| 274 |
+
mean_observations = 0
|
| 275 |
+
else:
|
| 276 |
+
mean_observations = sum((len(img.point3D_ids) for _, img in images.items()))/len(images)
|
| 277 |
+
HEADER = "# Image list with two lines of data per image:\n" + \
|
| 278 |
+
"# IMAGE_ID, QW, QX, QY, QZ, TX, TY, TZ, CAMERA_ID, NAME\n" + \
|
| 279 |
+
"# POINTS2D[] as (X, Y, POINT3D_ID)\n" + \
|
| 280 |
+
"# Number of images: {}, mean observations per image: {}\n".format(len(images), mean_observations)
|
| 281 |
+
|
| 282 |
+
with open(path, "w") as fid:
|
| 283 |
+
fid.write(HEADER)
|
| 284 |
+
for _, img in images.items():
|
| 285 |
+
image_header = [img.id, *img.qvec, *img.tvec, img.camera_id, img.name]
|
| 286 |
+
first_line = " ".join(map(str, image_header))
|
| 287 |
+
fid.write(first_line + "\n")
|
| 288 |
+
|
| 289 |
+
points_strings = []
|
| 290 |
+
for xy, point3D_id in zip(img.xys, img.point3D_ids):
|
| 291 |
+
points_strings.append(" ".join(map(str, [*xy, point3D_id])))
|
| 292 |
+
fid.write(" ".join(points_strings) + "\n")
|
| 293 |
+
|
| 294 |
+
|
| 295 |
+
def write_images_binary(images, path_to_model_file):
|
| 296 |
+
"""
|
| 297 |
+
see: src/base/reconstruction.cc
|
| 298 |
+
void Reconstruction::ReadImagesBinary(const std::string& path)
|
| 299 |
+
void Reconstruction::WriteImagesBinary(const std::string& path)
|
| 300 |
+
"""
|
| 301 |
+
with open(path_to_model_file, "wb") as fid:
|
| 302 |
+
write_next_bytes(fid, len(images), "Q")
|
| 303 |
+
for _, img in images.items():
|
| 304 |
+
write_next_bytes(fid, img.id, "i")
|
| 305 |
+
write_next_bytes(fid, img.qvec.tolist(), "dddd")
|
| 306 |
+
write_next_bytes(fid, img.tvec.tolist(), "ddd")
|
| 307 |
+
write_next_bytes(fid, img.camera_id, "i")
|
| 308 |
+
for char in img.name:
|
| 309 |
+
write_next_bytes(fid, char.encode("utf-8"), "c")
|
| 310 |
+
write_next_bytes(fid, b"\x00", "c")
|
| 311 |
+
write_next_bytes(fid, len(img.point3D_ids), "Q")
|
| 312 |
+
for xy, p3d_id in zip(img.xys, img.point3D_ids):
|
| 313 |
+
write_next_bytes(fid, [*xy, p3d_id], "ddq")
|
| 314 |
+
|
| 315 |
+
|
| 316 |
+
def read_points3D_text(path):
|
| 317 |
+
"""
|
| 318 |
+
see: src/base/reconstruction.cc
|
| 319 |
+
void Reconstruction::ReadPoints3DText(const std::string& path)
|
| 320 |
+
void Reconstruction::WritePoints3DText(const std::string& path)
|
| 321 |
+
"""
|
| 322 |
+
points3D = {}
|
| 323 |
+
with open(path, "r") as fid:
|
| 324 |
+
while True:
|
| 325 |
+
line = fid.readline()
|
| 326 |
+
if not line:
|
| 327 |
+
break
|
| 328 |
+
line = line.strip()
|
| 329 |
+
if len(line) > 0 and line[0] != "#":
|
| 330 |
+
elems = line.split()
|
| 331 |
+
point3D_id = int(elems[0])
|
| 332 |
+
xyz = np.array(tuple(map(float, elems[1:4])))
|
| 333 |
+
rgb = np.array(tuple(map(int, elems[4:7])))
|
| 334 |
+
error = float(elems[7])
|
| 335 |
+
image_ids = np.array(tuple(map(int, elems[8::2])))
|
| 336 |
+
point2D_idxs = np.array(tuple(map(int, elems[9::2])))
|
| 337 |
+
points3D[point3D_id] = Point3D(id=point3D_id, xyz=xyz, rgb=rgb,
|
| 338 |
+
error=error, image_ids=image_ids,
|
| 339 |
+
point2D_idxs=point2D_idxs)
|
| 340 |
+
return points3D
|
| 341 |
+
|
| 342 |
+
|
| 343 |
+
def read_points3D_binary(path_to_model_file=None, fid=None):
|
| 344 |
+
"""
|
| 345 |
+
see: src/base/reconstruction.cc
|
| 346 |
+
void Reconstruction::ReadPoints3DBinary(const std::string& path)
|
| 347 |
+
void Reconstruction::WritePoints3DBinary(const std::string& path)
|
| 348 |
+
"""
|
| 349 |
+
points3D = {}
|
| 350 |
+
if fid is None:
|
| 351 |
+
fid = open(path_to_model_file, "rb")
|
| 352 |
+
num_points = read_next_bytes(fid, 8, "Q")[0]
|
| 353 |
+
for _ in range(num_points):
|
| 354 |
+
binary_point_line_properties = read_next_bytes(
|
| 355 |
+
fid, num_bytes=43, format_char_sequence="QdddBBBd")
|
| 356 |
+
point3D_id = binary_point_line_properties[0]
|
| 357 |
+
xyz = np.array(binary_point_line_properties[1:4])
|
| 358 |
+
rgb = np.array(binary_point_line_properties[4:7])
|
| 359 |
+
error = np.array(binary_point_line_properties[7])
|
| 360 |
+
track_length = read_next_bytes(
|
| 361 |
+
fid, num_bytes=8, format_char_sequence="Q")[0]
|
| 362 |
+
track_elems = read_next_bytes(
|
| 363 |
+
fid, num_bytes=8*track_length,
|
| 364 |
+
format_char_sequence="ii"*track_length)
|
| 365 |
+
image_ids = np.array(tuple(map(int, track_elems[0::2])))
|
| 366 |
+
point2D_idxs = np.array(tuple(map(int, track_elems[1::2])))
|
| 367 |
+
points3D[point3D_id] = Point3D(
|
| 368 |
+
id=point3D_id, xyz=xyz, rgb=rgb,
|
| 369 |
+
error=error, image_ids=image_ids,
|
| 370 |
+
point2D_idxs=point2D_idxs)
|
| 371 |
+
if path_to_model_file is not None:
|
| 372 |
+
fid.close()
|
| 373 |
+
return points3D
|
| 374 |
+
|
| 375 |
+
|
| 376 |
+
def write_points3D_text(points3D, path):
|
| 377 |
+
"""
|
| 378 |
+
see: src/base/reconstruction.cc
|
| 379 |
+
void Reconstruction::ReadPoints3DText(const std::string& path)
|
| 380 |
+
void Reconstruction::WritePoints3DText(const std::string& path)
|
| 381 |
+
"""
|
| 382 |
+
if len(points3D) == 0:
|
| 383 |
+
mean_track_length = 0
|
| 384 |
+
else:
|
| 385 |
+
mean_track_length = sum((len(pt.image_ids) for _, pt in points3D.items()))/len(points3D)
|
| 386 |
+
HEADER = "# 3D point list with one line of data per point:\n" + \
|
| 387 |
+
"# POINT3D_ID, X, Y, Z, R, G, B, ERROR, TRACK[] as (IMAGE_ID, POINT2D_IDX)\n" + \
|
| 388 |
+
"# Number of points: {}, mean track length: {}\n".format(len(points3D), mean_track_length)
|
| 389 |
+
|
| 390 |
+
with open(path, "w") as fid:
|
| 391 |
+
fid.write(HEADER)
|
| 392 |
+
for _, pt in points3D.items():
|
| 393 |
+
point_header = [pt.id, *pt.xyz, *pt.rgb, pt.error]
|
| 394 |
+
fid.write(" ".join(map(str, point_header)) + " ")
|
| 395 |
+
track_strings = []
|
| 396 |
+
for image_id, point2D in zip(pt.image_ids, pt.point2D_idxs):
|
| 397 |
+
track_strings.append(" ".join(map(str, [image_id, point2D])))
|
| 398 |
+
fid.write(" ".join(track_strings) + "\n")
|
| 399 |
+
|
| 400 |
+
|
| 401 |
+
def write_points3D_binary(points3D, path_to_model_file):
|
| 402 |
+
"""
|
| 403 |
+
see: src/base/reconstruction.cc
|
| 404 |
+
void Reconstruction::ReadPoints3DBinary(const std::string& path)
|
| 405 |
+
void Reconstruction::WritePoints3DBinary(const std::string& path)
|
| 406 |
+
"""
|
| 407 |
+
with open(path_to_model_file, "wb") as fid:
|
| 408 |
+
write_next_bytes(fid, len(points3D), "Q")
|
| 409 |
+
for _, pt in points3D.items():
|
| 410 |
+
write_next_bytes(fid, pt.id, "Q")
|
| 411 |
+
write_next_bytes(fid, pt.xyz.tolist(), "ddd")
|
| 412 |
+
write_next_bytes(fid, pt.rgb.tolist(), "BBB")
|
| 413 |
+
write_next_bytes(fid, pt.error, "d")
|
| 414 |
+
track_length = pt.image_ids.shape[0]
|
| 415 |
+
write_next_bytes(fid, track_length, "Q")
|
| 416 |
+
for image_id, point2D_id in zip(pt.image_ids, pt.point2D_idxs):
|
| 417 |
+
write_next_bytes(fid, [image_id, point2D_id], "ii")
|
| 418 |
+
|
| 419 |
+
|
| 420 |
+
def detect_model_format(path, ext):
|
| 421 |
+
if os.path.isfile(os.path.join(path, "cameras" + ext)) and \
|
| 422 |
+
os.path.isfile(os.path.join(path, "images" + ext)) and \
|
| 423 |
+
os.path.isfile(os.path.join(path, "points3D" + ext)):
|
| 424 |
+
print("Detected model format: '" + ext + "'")
|
| 425 |
+
return True
|
| 426 |
+
|
| 427 |
+
return False
|
| 428 |
+
|
| 429 |
+
|
| 430 |
+
def read_model(path, ext=""):
|
| 431 |
+
# try to detect the extension automatically
|
| 432 |
+
if ext == "":
|
| 433 |
+
if detect_model_format(path, ".bin"):
|
| 434 |
+
ext = ".bin"
|
| 435 |
+
elif detect_model_format(path, ".txt"):
|
| 436 |
+
ext = ".txt"
|
| 437 |
+
else:
|
| 438 |
+
print("Provide model format: '.bin' or '.txt'")
|
| 439 |
+
return
|
| 440 |
+
|
| 441 |
+
if ext == ".txt":
|
| 442 |
+
cameras = read_cameras_text(os.path.join(path, "cameras" + ext))
|
| 443 |
+
images = read_images_text(os.path.join(path, "images" + ext))
|
| 444 |
+
points3D = read_points3D_text(os.path.join(path, "points3D") + ext)
|
| 445 |
+
else:
|
| 446 |
+
cameras = read_cameras_binary(os.path.join(path, "cameras" + ext))
|
| 447 |
+
images = read_images_binary(os.path.join(path, "images" + ext))
|
| 448 |
+
points3D = read_points3D_binary(os.path.join(path, "points3D") + ext)
|
| 449 |
+
return cameras, images, points3D
|
| 450 |
+
|
| 451 |
+
|
| 452 |
+
def write_model(cameras, images, points3D, path, ext=".bin"):
|
| 453 |
+
if ext == ".txt":
|
| 454 |
+
write_cameras_text(cameras, os.path.join(path, "cameras" + ext))
|
| 455 |
+
write_images_text(images, os.path.join(path, "images" + ext))
|
| 456 |
+
write_points3D_text(points3D, os.path.join(path, "points3D") + ext)
|
| 457 |
+
else:
|
| 458 |
+
write_cameras_binary(cameras, os.path.join(path, "cameras" + ext))
|
| 459 |
+
write_images_binary(images, os.path.join(path, "images" + ext))
|
| 460 |
+
write_points3D_binary(points3D, os.path.join(path, "points3D") + ext)
|
| 461 |
+
return cameras, images, points3D
|
| 462 |
+
|
| 463 |
+
|
| 464 |
+
def qvec2rotmat(qvec):
|
| 465 |
+
return np.array([
|
| 466 |
+
[1 - 2 * qvec[2]**2 - 2 * qvec[3]**2,
|
| 467 |
+
2 * qvec[1] * qvec[2] - 2 * qvec[0] * qvec[3],
|
| 468 |
+
2 * qvec[3] * qvec[1] + 2 * qvec[0] * qvec[2]],
|
| 469 |
+
[2 * qvec[1] * qvec[2] + 2 * qvec[0] * qvec[3],
|
| 470 |
+
1 - 2 * qvec[1]**2 - 2 * qvec[3]**2,
|
| 471 |
+
2 * qvec[2] * qvec[3] - 2 * qvec[0] * qvec[1]],
|
| 472 |
+
[2 * qvec[3] * qvec[1] - 2 * qvec[0] * qvec[2],
|
| 473 |
+
2 * qvec[2] * qvec[3] + 2 * qvec[0] * qvec[1],
|
| 474 |
+
1 - 2 * qvec[1]**2 - 2 * qvec[2]**2]])
|
| 475 |
+
|
| 476 |
+
|
| 477 |
+
def rotmat2qvec(R):
|
| 478 |
+
Rxx, Ryx, Rzx, Rxy, Ryy, Rzy, Rxz, Ryz, Rzz = R.flat
|
| 479 |
+
K = np.array([
|
| 480 |
+
[Rxx - Ryy - Rzz, 0, 0, 0],
|
| 481 |
+
[Ryx + Rxy, Ryy - Rxx - Rzz, 0, 0],
|
| 482 |
+
[Rzx + Rxz, Rzy + Ryz, Rzz - Rxx - Ryy, 0],
|
| 483 |
+
[Ryz - Rzy, Rzx - Rxz, Rxy - Ryx, Rxx + Ryy + Rzz]]) / 3.0
|
| 484 |
+
eigvals, eigvecs = np.linalg.eigh(K)
|
| 485 |
+
qvec = eigvecs[[3, 0, 1, 2], np.argmax(eigvals)]
|
| 486 |
+
if qvec[0] < 0:
|
| 487 |
+
qvec *= -1
|
| 488 |
+
return qvec
|
| 489 |
+
|
hoho/vis.py
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import trimesh
|
| 2 |
+
import numpy as np
|
| 3 |
+
from copy import deepcopy
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
from . import color_mappings
|
| 7 |
+
|
| 8 |
+
def line(p1, p2, c=(255,0,0), resolution=10, radius=0.05):
|
| 9 |
+
'''draws a 3d cylinder along the line (p1, p2)'''
|
| 10 |
+
# check colors
|
| 11 |
+
if len(c) == 1:
|
| 12 |
+
c = [c[0]]*4
|
| 13 |
+
elif len(c) == 3:
|
| 14 |
+
c = [*c, 255]
|
| 15 |
+
elif len(c) != 4:
|
| 16 |
+
raise ValueError(f'{c} is not a valid color (must have 1,3, or 4 elements).')
|
| 17 |
+
|
| 18 |
+
# compute length and direction of segment
|
| 19 |
+
p1, p2 = np.asarray(p1), np.asarray(p2)
|
| 20 |
+
l = np.linalg.norm(p2-p1)
|
| 21 |
+
|
| 22 |
+
direction = (p2 - p1) / l
|
| 23 |
+
|
| 24 |
+
# point z along direction of segment
|
| 25 |
+
T = np.eye(4)
|
| 26 |
+
T[:3, 2] = direction
|
| 27 |
+
T[:3, 3] = (p1+p2)/2
|
| 28 |
+
|
| 29 |
+
#reorthogonalize basis
|
| 30 |
+
b0, b1 = T[:3, 0], T[:3, 1]
|
| 31 |
+
if np.abs(np.dot(b0, direction)) < np.abs(np.dot(b1, direction)):
|
| 32 |
+
T[:3, 1] = -np.cross(b0, direction)
|
| 33 |
+
else:
|
| 34 |
+
T[:3, 0] = np.cross(b1, direction)
|
| 35 |
+
|
| 36 |
+
# generate and transform mesh
|
| 37 |
+
mesh = trimesh.primitives.Cylinder(radius=radius, height=l, transform=T)
|
| 38 |
+
|
| 39 |
+
# apply uniform color
|
| 40 |
+
mesh.visual.vertex_colors = np.ones_like(mesh.visual.vertex_colors)*c
|
| 41 |
+
|
| 42 |
+
return mesh
|
| 43 |
+
|
| 44 |
+
def show_wf(row, radius=10, show_vertices=False, vertex_color=(255,0,0, 255)):
|
| 45 |
+
EDGE_CLASSES = ['eave',
|
| 46 |
+
'ridge',
|
| 47 |
+
'step_flashing',
|
| 48 |
+
'rake',
|
| 49 |
+
'flashing',
|
| 50 |
+
'post',
|
| 51 |
+
'valley',
|
| 52 |
+
'hip',
|
| 53 |
+
'transition_line']
|
| 54 |
+
out_meshes = []
|
| 55 |
+
if show_vertices:
|
| 56 |
+
out_meshes.extend([trimesh.primitives.Sphere(radius=radius+5, center = center, color=vertex_color) for center in row['wf_vertices']])
|
| 57 |
+
for m in out_meshes:
|
| 58 |
+
m.visual.vertex_colors = np.ones_like(m.visual.vertex_colors)*vertex_color
|
| 59 |
+
if 'edge_semantics' not in row:
|
| 60 |
+
print ("Warning: edge semantics is not here, skipping")
|
| 61 |
+
out_meshes.extend([line(a,b, radius=radius, c=(214, 251, 248)) for a,b in np.stack([*row['wf_vertices']])[np.stack(row['wf_edges'])]])
|
| 62 |
+
elif len(np.stack(row['wf_edges'])) == len(row['edge_semantics']):
|
| 63 |
+
out_meshes.extend([line(a,b, radius=radius, c=color_mappings.gestalt_color_mapping[EDGE_CLASSES[cls_id]]) for (a,b), cls_id in zip(np.stack([*row['wf_vertices']])[np.stack(row['wf_edges'])], row['edge_semantics'])])
|
| 64 |
+
else:
|
| 65 |
+
print ("Warning: edge semantics has different length compared to edges, skipping semantics")
|
| 66 |
+
out_meshes.extend([line(a,b, radius=radius, c=(214, 251, 248)) for a,b in np.stack([*row['wf_vertices']])[np.stack(row['wf_edges'])]])
|
| 67 |
+
return out_meshes
|
| 68 |
+
# return [line(a,b, radius=radius, c=color_mappings.edge_colors[cls_id]) for (a,b), cls_id in zip(np.stack([*row['wf_vertices']])[np.stack(row['wf_edges'])], row['edge_semantics'])]
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def show_grid(edges, meshes=None, row_length=5):
|
| 72 |
+
'''
|
| 73 |
+
edges: list of list of meshes
|
| 74 |
+
meshes: optional corresponding list of meshes
|
| 75 |
+
row_length: number of meshes per row
|
| 76 |
+
|
| 77 |
+
returns trimesh.Scene()
|
| 78 |
+
'''
|
| 79 |
+
|
| 80 |
+
T = np.eye(4)
|
| 81 |
+
out = []
|
| 82 |
+
edges = [sum(e[1:], e[0]) for e in edges]
|
| 83 |
+
row_height = 1.1 * max((e.extents for e in edges), key=lambda e: e[1])[1]
|
| 84 |
+
col_width = 1.1 * max((e.extents for e in edges), key=lambda e: e[0])[0]
|
| 85 |
+
# print(row_height, col_width)
|
| 86 |
+
|
| 87 |
+
if meshes is None:
|
| 88 |
+
meshes = [None]*len(edges)
|
| 89 |
+
|
| 90 |
+
for i, (gt, mesh) in enumerate(zip(edges, meshes), start=0):
|
| 91 |
+
mesh = deepcopy(mesh)
|
| 92 |
+
gt = deepcopy(gt)
|
| 93 |
+
|
| 94 |
+
if i%row_length != 0:
|
| 95 |
+
T[0, 3] += col_width
|
| 96 |
+
|
| 97 |
+
else:
|
| 98 |
+
T[0, 3] = 0
|
| 99 |
+
T[1, 3] += row_height
|
| 100 |
+
|
| 101 |
+
# print(T[0,3]/col_width, T[2,3]/row_height)
|
| 102 |
+
|
| 103 |
+
if mesh is not None:
|
| 104 |
+
mesh.apply_transform(T)
|
| 105 |
+
out.append(mesh)
|
| 106 |
+
|
| 107 |
+
gt.apply_transform(T)
|
| 108 |
+
out.append(gt)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
out.extend([mesh, gt])
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
return trimesh.Scene(out)
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
def visualize_order_images(row_order):
|
| 120 |
+
return create_image_grid(row_order['ade20k'] + row_order['gestalt'] + [visualize_depth(dm) for dm in row_order['depthcm']], num_per_row=len(row_order['ade20k']))
|
| 121 |
+
|
| 122 |
+
def create_image_grid(images, target_length=312, num_per_row=2):
|
| 123 |
+
# Calculate the target size for the first image
|
| 124 |
+
first_img = images[0]
|
| 125 |
+
aspect_ratio = first_img.width / first_img.height
|
| 126 |
+
new_width = int((target_length ** 2 * aspect_ratio) ** 0.5)
|
| 127 |
+
new_height = int((target_length ** 2 / aspect_ratio) ** 0.5)
|
| 128 |
+
|
| 129 |
+
# Resize the first image
|
| 130 |
+
resized_images = [img.resize((new_width, new_height), Image.Resampling.LANCZOS) for img in images]
|
| 131 |
+
|
| 132 |
+
# Calculate the grid size
|
| 133 |
+
num_rows = (len(resized_images) + num_per_row - 1) // num_per_row
|
| 134 |
+
grid_width = new_width * num_per_row
|
| 135 |
+
grid_height = new_height * num_rows
|
| 136 |
+
|
| 137 |
+
# Create a new image for the grid
|
| 138 |
+
grid_img = Image.new('RGB', (grid_width, grid_height))
|
| 139 |
+
|
| 140 |
+
# Paste the images into the grid
|
| 141 |
+
for i, img in enumerate(resized_images):
|
| 142 |
+
x_offset = (i % num_per_row) * new_width
|
| 143 |
+
y_offset = (i // num_per_row) * new_height
|
| 144 |
+
grid_img.paste(img, (x_offset, y_offset))
|
| 145 |
+
|
| 146 |
+
return grid_img
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
import matplotlib.pyplot as plt
|
| 150 |
+
|
| 151 |
+
def visualize_depth(depth, min_depth=None, max_depth=None, cmap='rainbow'):
|
| 152 |
+
depth = np.array(depth)
|
| 153 |
+
|
| 154 |
+
if min_depth is None:
|
| 155 |
+
min_depth = np.min(depth)
|
| 156 |
+
if max_depth is None:
|
| 157 |
+
max_depth = np.max(depth)
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
# Normalize the depth to be between 0 and 1
|
| 161 |
+
depth = (depth - min_depth) / (max_depth - min_depth)
|
| 162 |
+
depth = np.clip(depth, 0, 1)
|
| 163 |
+
|
| 164 |
+
# Use the matplotlib colormap to convert the depth to an RGB image
|
| 165 |
+
cmap = plt.get_cmap(cmap)
|
| 166 |
+
depth_image = (cmap(depth) * 255).astype(np.uint8)
|
| 167 |
+
|
| 168 |
+
# Convert the depth image to a PIL image
|
| 169 |
+
depth_image = Image.fromarray(depth_image)
|
| 170 |
+
|
| 171 |
+
return depth_image
|
hoho/viz3d.py
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
"""
|
| 3 |
+
Copyright [2022] [Paul-Edouard Sarlin and Philipp Lindenberger]
|
| 4 |
+
|
| 5 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
you may not use this file except in compliance with the License.
|
| 7 |
+
You may obtain a copy of the License at
|
| 8 |
+
|
| 9 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
|
| 11 |
+
Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 14 |
+
See the License for the specific language governing permissions and
|
| 15 |
+
limitations under the License.
|
| 16 |
+
|
| 17 |
+
3D visualization based on plotly.
|
| 18 |
+
Works for a small number of points and cameras, might be slow otherwise.
|
| 19 |
+
|
| 20 |
+
1) Initialize a figure with `init_figure`
|
| 21 |
+
2) Add 3D points, camera frustums, or both as a pycolmap.Reconstruction
|
| 22 |
+
|
| 23 |
+
Written by Paul-Edouard Sarlin and Philipp Lindenberger.
|
| 24 |
+
"""
|
| 25 |
+
# Slightly modified by Dmytro Mishkin
|
| 26 |
+
|
| 27 |
+
from typing import Optional
|
| 28 |
+
import numpy as np
|
| 29 |
+
import pycolmap
|
| 30 |
+
import plotly.graph_objects as go
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
### Some helper functions for geometry
|
| 34 |
+
def qvec2rotmat(qvec):
|
| 35 |
+
return np.array([
|
| 36 |
+
[1 - 2 * qvec[2]**2 - 2 * qvec[3]**2,
|
| 37 |
+
2 * qvec[1] * qvec[2] - 2 * qvec[0] * qvec[3],
|
| 38 |
+
2 * qvec[3] * qvec[1] + 2 * qvec[0] * qvec[2]],
|
| 39 |
+
[2 * qvec[1] * qvec[2] + 2 * qvec[0] * qvec[3],
|
| 40 |
+
1 - 2 * qvec[1]**2 - 2 * qvec[3]**2,
|
| 41 |
+
2 * qvec[2] * qvec[3] - 2 * qvec[0] * qvec[1]],
|
| 42 |
+
[2 * qvec[3] * qvec[1] - 2 * qvec[0] * qvec[2],
|
| 43 |
+
2 * qvec[2] * qvec[3] + 2 * qvec[0] * qvec[1],
|
| 44 |
+
1 - 2 * qvec[1]**2 - 2 * qvec[2]**2]])
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def to_homogeneous(points):
|
| 48 |
+
pad = np.ones((points.shape[:-1]+(1,)), dtype=points.dtype)
|
| 49 |
+
return np.concatenate([points, pad], axis=-1)
|
| 50 |
+
|
| 51 |
+
def t_to_proj_center(qvec, tvec):
|
| 52 |
+
Rr = qvec2rotmat(qvec)
|
| 53 |
+
tt = (-Rr.T) @ tvec
|
| 54 |
+
return tt
|
| 55 |
+
|
| 56 |
+
def calib(params):
|
| 57 |
+
out = np.eye(3)
|
| 58 |
+
if len(params) == 3:
|
| 59 |
+
out[0,0] = params[0]
|
| 60 |
+
out[1,1] = params[0]
|
| 61 |
+
out[0,2] = params[1]
|
| 62 |
+
out[1,2] = params[2]
|
| 63 |
+
else:
|
| 64 |
+
out[0,0] = params[0]
|
| 65 |
+
out[1,1] = params[1]
|
| 66 |
+
out[0,2] = params[2]
|
| 67 |
+
out[1,2] = params[3]
|
| 68 |
+
return out
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
### Plotting functions
|
| 72 |
+
|
| 73 |
+
def init_figure(height: int = 800) -> go.Figure:
|
| 74 |
+
"""Initialize a 3D figure."""
|
| 75 |
+
fig = go.Figure()
|
| 76 |
+
axes = dict(
|
| 77 |
+
visible=False,
|
| 78 |
+
showbackground=False,
|
| 79 |
+
showgrid=False,
|
| 80 |
+
showline=False,
|
| 81 |
+
showticklabels=True,
|
| 82 |
+
autorange=True,
|
| 83 |
+
)
|
| 84 |
+
fig.update_layout(
|
| 85 |
+
template="plotly_dark",
|
| 86 |
+
height=height,
|
| 87 |
+
scene_camera=dict(
|
| 88 |
+
eye=dict(x=0., y=-.1, z=-2),
|
| 89 |
+
up=dict(x=0, y=-1., z=0),
|
| 90 |
+
projection=dict(type="orthographic")),
|
| 91 |
+
scene=dict(
|
| 92 |
+
xaxis=axes,
|
| 93 |
+
yaxis=axes,
|
| 94 |
+
zaxis=axes,
|
| 95 |
+
aspectmode='data',
|
| 96 |
+
dragmode='orbit',
|
| 97 |
+
),
|
| 98 |
+
margin=dict(l=0, r=0, b=0, t=0, pad=0),
|
| 99 |
+
legend=dict(
|
| 100 |
+
orientation="h",
|
| 101 |
+
yanchor="top",
|
| 102 |
+
y=0.99,
|
| 103 |
+
xanchor="left",
|
| 104 |
+
x=0.1
|
| 105 |
+
),
|
| 106 |
+
)
|
| 107 |
+
return fig
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def plot_lines_3d(
|
| 111 |
+
fig: go.Figure,
|
| 112 |
+
pts: np.ndarray,
|
| 113 |
+
color: str = 'rgba(255, 255, 255, 1)',
|
| 114 |
+
ps: int = 2,
|
| 115 |
+
colorscale: Optional[str] = None,
|
| 116 |
+
name: Optional[str] = None):
|
| 117 |
+
"""Plot a set of 3D points."""
|
| 118 |
+
x = pts[..., 0]
|
| 119 |
+
y = pts[..., 1]
|
| 120 |
+
z = pts[..., 2]
|
| 121 |
+
traces = [go.Scatter3d(x=x1, y=y1, z=z1,
|
| 122 |
+
mode='lines',
|
| 123 |
+
line=dict(color=color, width=2)) for x1, y1, z1 in zip(x,y,z)]
|
| 124 |
+
for t in traces:
|
| 125 |
+
fig.add_trace(t)
|
| 126 |
+
fig.update_traces(showlegend=False)
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
def plot_points(
|
| 130 |
+
fig: go.Figure,
|
| 131 |
+
pts: np.ndarray,
|
| 132 |
+
color: str = 'rgba(255, 0, 0, 1)',
|
| 133 |
+
ps: int = 2,
|
| 134 |
+
colorscale: Optional[str] = None,
|
| 135 |
+
name: Optional[str] = None):
|
| 136 |
+
"""Plot a set of 3D points."""
|
| 137 |
+
x, y, z = pts.T
|
| 138 |
+
tr = go.Scatter3d(
|
| 139 |
+
x=x, y=y, z=z, mode='markers', name=name, legendgroup=name,
|
| 140 |
+
marker=dict(
|
| 141 |
+
size=ps, color=color, line_width=0.0, colorscale=colorscale))
|
| 142 |
+
fig.add_trace(tr)
|
| 143 |
+
|
| 144 |
+
def plot_camera(
|
| 145 |
+
fig: go.Figure,
|
| 146 |
+
R: np.ndarray,
|
| 147 |
+
t: np.ndarray,
|
| 148 |
+
K: np.ndarray,
|
| 149 |
+
color: str = 'rgb(0, 0, 255)',
|
| 150 |
+
name: Optional[str] = None,
|
| 151 |
+
legendgroup: Optional[str] = None,
|
| 152 |
+
size: float = 1.0):
|
| 153 |
+
"""Plot a camera frustum from pose and intrinsic matrix."""
|
| 154 |
+
W, H = K[0, 2]*2, K[1, 2]*2
|
| 155 |
+
corners = np.array([[0, 0], [W, 0], [W, H], [0, H], [0, 0]])
|
| 156 |
+
if size is not None:
|
| 157 |
+
image_extent = max(size * W / 1024.0, size * H / 1024.0)
|
| 158 |
+
world_extent = max(W, H) / (K[0, 0] + K[1, 1]) / 0.5
|
| 159 |
+
scale = 0.5 * image_extent / world_extent
|
| 160 |
+
else:
|
| 161 |
+
scale = 1.0
|
| 162 |
+
corners = to_homogeneous(corners) @ np.linalg.inv(K).T
|
| 163 |
+
corners = (corners / 2 * scale) @ R.T + t
|
| 164 |
+
|
| 165 |
+
x, y, z = corners.T
|
| 166 |
+
rect = go.Scatter3d(
|
| 167 |
+
x=x, y=y, z=z, line=dict(color=color), legendgroup=legendgroup,
|
| 168 |
+
name=name, marker=dict(size=0.0001), showlegend=False)
|
| 169 |
+
fig.add_trace(rect)
|
| 170 |
+
|
| 171 |
+
x, y, z = np.concatenate(([t], corners)).T
|
| 172 |
+
i = [0, 0, 0, 0]
|
| 173 |
+
j = [1, 2, 3, 4]
|
| 174 |
+
k = [2, 3, 4, 1]
|
| 175 |
+
|
| 176 |
+
pyramid = go.Mesh3d(
|
| 177 |
+
x=x, y=y, z=z, color=color, i=i, j=j, k=k,
|
| 178 |
+
legendgroup=legendgroup, name=name, showlegend=False)
|
| 179 |
+
fig.add_trace(pyramid)
|
| 180 |
+
triangles = np.vstack((i, j, k)).T
|
| 181 |
+
vertices = np.concatenate(([t], corners))
|
| 182 |
+
tri_points = np.array([
|
| 183 |
+
vertices[i] for i in triangles.reshape(-1)
|
| 184 |
+
])
|
| 185 |
+
|
| 186 |
+
x, y, z = tri_points.T
|
| 187 |
+
pyramid = go.Scatter3d(
|
| 188 |
+
x=x, y=y, z=z, mode='lines', legendgroup=legendgroup,
|
| 189 |
+
name=name, line=dict(color=color, width=1), showlegend=False)
|
| 190 |
+
fig.add_trace(pyramid)
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
def plot_camera_colmap(
|
| 194 |
+
fig: go.Figure,
|
| 195 |
+
image: pycolmap.Image,
|
| 196 |
+
camera: pycolmap.Camera,
|
| 197 |
+
name: Optional[str] = None,
|
| 198 |
+
**kwargs):
|
| 199 |
+
"""Plot a camera frustum from PyCOLMAP objects"""
|
| 200 |
+
intr = calib(camera.params)
|
| 201 |
+
if intr[0][0] > 10000:
|
| 202 |
+
print("Bad camera")
|
| 203 |
+
return
|
| 204 |
+
plot_camera(
|
| 205 |
+
fig,
|
| 206 |
+
qvec2rotmat(image.qvec).T,
|
| 207 |
+
t_to_proj_center(image.qvec, image.tvec),
|
| 208 |
+
intr,#calibration_matrix(),
|
| 209 |
+
name=name or str(image.id),
|
| 210 |
+
**kwargs)
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
def plot_cameras(
|
| 214 |
+
fig: go.Figure,
|
| 215 |
+
reconstruction,#: pycolmap.Reconstruction,
|
| 216 |
+
**kwargs):
|
| 217 |
+
"""Plot a camera as a cone with camera frustum."""
|
| 218 |
+
for image_id, image in reconstruction["images"].items():
|
| 219 |
+
plot_camera_colmap(
|
| 220 |
+
fig, image, reconstruction["cameras"][image.camera_id], **kwargs)
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
def plot_reconstruction(
|
| 224 |
+
fig: go.Figure,
|
| 225 |
+
rec,
|
| 226 |
+
color: str = 'rgb(0, 0, 255)',
|
| 227 |
+
name: Optional[str] = None,
|
| 228 |
+
points: bool = True,
|
| 229 |
+
cameras: bool = True,
|
| 230 |
+
cs: float = 1.0,
|
| 231 |
+
single_color_points=False,
|
| 232 |
+
camera_color='rgba(0, 255, 0, 0.5)'):
|
| 233 |
+
# rec is result of loading reconstruction from "read_write_colmap.py"
|
| 234 |
+
# Filter outliers
|
| 235 |
+
xyzs = []
|
| 236 |
+
rgbs = []
|
| 237 |
+
for k, p3D in rec['points'].items():
|
| 238 |
+
xyzs.append(p3D.xyz)
|
| 239 |
+
rgbs.append(p3D.rgb)
|
| 240 |
+
|
| 241 |
+
if points:
|
| 242 |
+
plot_points(fig, np.array(xyzs), color=color if single_color_points else np.array(rgbs), ps=1, name=name)
|
| 243 |
+
if cameras:
|
| 244 |
+
plot_cameras(fig, rec, color=camera_color, legendgroup=name, size=cs)
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
def plot_pointcloud(
|
| 248 |
+
fig: go.Figure,
|
| 249 |
+
pts: np.ndarray,
|
| 250 |
+
colors: np.ndarray,
|
| 251 |
+
ps: int = 2,
|
| 252 |
+
name: Optional[str] = None):
|
| 253 |
+
"""Plot a set of 3D points."""
|
| 254 |
+
plot_points(fig, np.array(pts), color=colors, ps=ps, name=name)
|
| 255 |
+
|
| 256 |
+
|
| 257 |
+
def plot_triangle_mesh(
|
| 258 |
+
fig: go.Figure,
|
| 259 |
+
vert: np.ndarray,
|
| 260 |
+
colors: np.ndarray,
|
| 261 |
+
triangles: np.ndarray,
|
| 262 |
+
name: Optional[str] = None):
|
| 263 |
+
"""Plot a triangle mesh."""
|
| 264 |
+
tr = go.Mesh3d(
|
| 265 |
+
x=vert[:,0],
|
| 266 |
+
y=vert[:,1],
|
| 267 |
+
z=vert[:,2],
|
| 268 |
+
vertexcolor = np.clip(255*colors, 0, 255),
|
| 269 |
+
# i, j and k give the vertices of triangles
|
| 270 |
+
# here we represent the 4 triangles of the tetrahedron surface
|
| 271 |
+
i=triangles[:,0],
|
| 272 |
+
j=triangles[:,1],
|
| 273 |
+
k=triangles[:,2],
|
| 274 |
+
name=name,
|
| 275 |
+
showscale=False
|
| 276 |
+
)
|
| 277 |
+
fig.add_trace(tr)
|
| 278 |
+
|
| 279 |
+
def plot_estimate_and_gt(pred_vertices, pred_connections, gt_vertices=None, gt_connections=None):
|
| 280 |
+
fig3d = init_figure()
|
| 281 |
+
c1 = (30, 20, 255)
|
| 282 |
+
img_color = [c1 for _ in range(len(pred_vertices))]
|
| 283 |
+
plot_points(fig3d, pred_vertices, color = img_color, ps = 10)
|
| 284 |
+
lines = []
|
| 285 |
+
for c in pred_connections:
|
| 286 |
+
v1 = pred_vertices[c[0]]
|
| 287 |
+
v2 = pred_vertices[c[1]]
|
| 288 |
+
lines.append(np.stack([v1, v2], axis=0))
|
| 289 |
+
plot_lines_3d(fig3d, np.array(lines), img_color, ps=4)
|
| 290 |
+
if gt_vertices is not None:
|
| 291 |
+
c2 = (30, 255, 20)
|
| 292 |
+
img_color2 = [c2 for _ in range(len(gt_vertices))]
|
| 293 |
+
plot_points(fig3d, gt_vertices, color = img_color2, ps = 10)
|
| 294 |
+
if gt_connections is not None:
|
| 295 |
+
gt_lines = []
|
| 296 |
+
for c in gt_connections:
|
| 297 |
+
v1 = gt_vertices[c[0]]
|
| 298 |
+
v2 = gt_vertices[c[1]]
|
| 299 |
+
gt_lines.append(np.stack([v1, v2], axis=0))
|
| 300 |
+
plot_lines_3d(fig3d, np.array(gt_lines), img_color2, ps=4)
|
| 301 |
+
fig3d.show()
|
| 302 |
+
return fig3d
|
hoho/wed.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from scipy.spatial.distance import cdist
|
| 2 |
+
from scipy.optimize import linear_sum_assignment
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def preregister_mean_std(verts_to_transform, target_verts, single_scale=True):
|
| 7 |
+
mu_target = target_verts.mean(axis=0)
|
| 8 |
+
mu_in = verts_to_transform.mean(axis=0)
|
| 9 |
+
std_target = np.std(target_verts, axis=0)
|
| 10 |
+
std_in = np.std(verts_to_transform, axis=0)
|
| 11 |
+
|
| 12 |
+
if np.any(std_in == 0):
|
| 13 |
+
std_in[std_in == 0] = 1
|
| 14 |
+
if np.any(std_target == 0):
|
| 15 |
+
std_target[std_target == 0] = 1
|
| 16 |
+
if np.any(np.isnan(std_in)):
|
| 17 |
+
std_in[np.isnan(std_in)] = 1
|
| 18 |
+
if np.any(np.isnan(std_target)):
|
| 19 |
+
std_target[np.isnan(std_target)] = 1
|
| 20 |
+
|
| 21 |
+
if single_scale:
|
| 22 |
+
std_target = np.linalg.norm(std_target)
|
| 23 |
+
std_in = np.linalg.norm(std_in)
|
| 24 |
+
|
| 25 |
+
transformed_verts = (verts_to_transform - mu_in) / std_in
|
| 26 |
+
transformed_verts = transformed_verts * std_target + mu_target
|
| 27 |
+
|
| 28 |
+
return transformed_verts
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def update_cv(cv, gt_vertices):
|
| 32 |
+
if cv < 0:
|
| 33 |
+
diameter = cdist(gt_vertices, gt_vertices).max()
|
| 34 |
+
# Cost of adding or deleting a vertex is set to -cv times the diameter of the ground truth wireframe
|
| 35 |
+
cv = -cv * diameter
|
| 36 |
+
return cv
|
| 37 |
+
|
| 38 |
+
def compute_WED(pd_vertices, pd_edges, gt_vertices, gt_edges, cv_ins=-1/2, cv_del=-1/4, ce=1.0, normalized=True, preregister=True, single_scale=True):
|
| 39 |
+
'''The function computes the Wireframe Edge Distance (WED) between two graphs.
|
| 40 |
+
pd_vertices: list of predicted vertices
|
| 41 |
+
pd_edges: list of predicted edges
|
| 42 |
+
gt_vertices: list of ground truth vertices
|
| 43 |
+
gt_edges: list of ground truth edges
|
| 44 |
+
cv_ins: vertex insertion cost: if positive, the cost in centimeters of inserting vertex, if negative, multiplies diameter to compute cost (default is -1/2)
|
| 45 |
+
cv_del: vertex deletion cost: if positive, the cost in centimeters of deleting a vertex, if negative, multiplies diameter to compute cost (default is -1/4)
|
| 46 |
+
ce: edge cost (multiplier of the edge length for edge deletion and insertion, default is 1.0)
|
| 47 |
+
normalized: if True, the WED is normalized by the total length of the ground truth edges
|
| 48 |
+
preregister: if True, the predicted vertices have their mean and scale matched to the ground truth vertices
|
| 49 |
+
'''
|
| 50 |
+
|
| 51 |
+
pd_vertices = np.array(pd_vertices)
|
| 52 |
+
gt_vertices = np.array(gt_vertices)
|
| 53 |
+
pd_edges = np.array(pd_edges)
|
| 54 |
+
gt_edges = np.array(gt_edges)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
cv_del = update_cv(cv_del, gt_vertices)
|
| 58 |
+
cv_ins = update_cv(cv_ins, gt_vertices)
|
| 59 |
+
|
| 60 |
+
# Step 0: Prenormalize / preregister
|
| 61 |
+
if preregister:
|
| 62 |
+
pd_vertices = preregister_mean_std(pd_vertices, gt_vertices, single_scale=single_scale)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
# Step 1: Bipartite Matching
|
| 66 |
+
distances = cdist(pd_vertices, gt_vertices, metric='euclidean')
|
| 67 |
+
row_ind, col_ind = linear_sum_assignment(distances)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
# Step 2: Vertex Translation
|
| 71 |
+
translation_costs = np.sum(distances[row_ind, col_ind])
|
| 72 |
+
|
| 73 |
+
# Step 3: Vertex Deletion
|
| 74 |
+
unmatched_pd_indices = set(range(len(pd_vertices))) - set(row_ind)
|
| 75 |
+
deletion_costs = cv_del * len(unmatched_pd_indices)
|
| 76 |
+
|
| 77 |
+
# Step 4: Vertex Insertion
|
| 78 |
+
unmatched_gt_indices = set(range(len(gt_vertices))) - set(col_ind)
|
| 79 |
+
insertion_costs = cv_ins * len(unmatched_gt_indices)
|
| 80 |
+
|
| 81 |
+
# Step 5: Edge Deletion and Insertion
|
| 82 |
+
updated_pd_edges = [(col_ind[np.where(row_ind == edge[0])[0][0]], col_ind[np.where(row_ind == edge[1])[0][0]]) for edge in pd_edges if len(edge)==2 and edge[0] in row_ind and edge[1] in row_ind]
|
| 83 |
+
pd_edges_set = set(map(tuple, [set(edge) for edge in updated_pd_edges]))
|
| 84 |
+
gt_edges_set = set(map(tuple, [set(edge) for edge in gt_edges]))
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
# Delete edges not in ground truth
|
| 88 |
+
edges_to_delete = pd_edges_set - gt_edges_set
|
| 89 |
+
|
| 90 |
+
vert_tf = [np.where(col_ind == v)[0][0] if v in col_ind else 0 for v in range(len(gt_vertices))]
|
| 91 |
+
deletion_edge_costs = ce * sum(np.linalg.norm(pd_vertices[vert_tf[edge[0]]] - pd_vertices[vert_tf[edge[1]]]) for edge in edges_to_delete if len(edge) == 2)
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
# Insert missing edges from ground truth
|
| 95 |
+
edges_to_insert = gt_edges_set - pd_edges_set
|
| 96 |
+
insertion_edge_costs = ce * sum(np.linalg.norm(gt_vertices[edge[0]] - gt_vertices[edge[1]]) for edge in edges_to_insert if len(edge) == 2)
|
| 97 |
+
|
| 98 |
+
# Step 6: Calculation of WED
|
| 99 |
+
WED = translation_costs + deletion_costs + insertion_costs + deletion_edge_costs + insertion_edge_costs
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
if normalized:
|
| 103 |
+
total_length_of_gt_edges = np.linalg.norm((gt_vertices[gt_edges[:, 0]] - gt_vertices[gt_edges[:, 1]]), axis=1).sum()
|
| 104 |
+
WED = WED / total_length_of_gt_edges
|
| 105 |
+
|
| 106 |
+
# print ("Total length", total_length_of_gt_edges)
|
| 107 |
+
return WED
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
datasets
|
| 2 |
+
ipywidgets
|
| 3 |
+
matplotlib
|
| 4 |
+
numpy
|
| 5 |
+
pillow
|
| 6 |
+
plotly
|
| 7 |
+
pycolmap
|
| 8 |
+
scipy
|
| 9 |
+
trimesh
|
| 10 |
+
webdataset
|
setup.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from setuptools import setup, find_packages
|
| 2 |
+
import glob
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
with open('requirements.txt') as f:
|
| 6 |
+
required = f.read().splitlines()
|
| 7 |
+
|
| 8 |
+
setup(name='hoho',
|
| 9 |
+
version='0.0.4',
|
| 10 |
+
description='Tools and utilites for the HoHo Dataset and S23DR Competition',
|
| 11 |
+
url='usm3d.github.io',
|
| 12 |
+
author='Jack Langerman, Dmytro Mishkin, S23DR Orgainizing Team',
|
| 13 |
+
author_email='hoho@jackml.com',
|
| 14 |
+
install_requires=required,
|
| 15 |
+
packages=find_packages(),
|
| 16 |
+
include_package_data=True)
|