jacklangerman
commited on
Commit
•
19b8852
1
Parent(s):
dd5adff
add files
Browse files- LICENSE.txt +13 -0
- README.md +15 -0
- hoho/__init__.py +23 -0
- hoho/color_mappings.py +182 -0
- hoho/hoho.py +0 -3
- hoho/read_write_colmap.py +489 -0
- hoho/viz3d.py +302 -0
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,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# HoHo Tools
|
2 |
+
|
3 |
+
|
4 |
+
```bash
|
5 |
+
# pip install over ssh
|
6 |
+
pip install git+ssh://[email protected]/test-org-usm3d/tools.git
|
7 |
+
|
8 |
+
# pip install over http
|
9 |
+
pip install git+http://hf.co/test-org-usm3d/tools.git
|
10 |
+
|
11 |
+
# editable
|
12 |
+
git clone http://hf.co/test-org-usm3d/tools
|
13 |
+
cd tools
|
14 |
+
pip install -e .
|
15 |
+
```
|
hoho/__init__.py
CHANGED
@@ -1,2 +1,25 @@
|
|
1 |
from .hoho import *
|
2 |
from . import vis
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from .hoho import *
|
2 |
from . import vis
|
3 |
+
from . import read_write_colmap
|
4 |
+
|
5 |
+
import importlib
|
6 |
+
import sys
|
7 |
+
class LazyLoadModule:
|
8 |
+
def __init__(self, module_name):
|
9 |
+
self.module_name = module_name
|
10 |
+
self.module = None
|
11 |
+
|
12 |
+
def __getattribute__(self, attr):
|
13 |
+
if attr == 'module_name' or attr == 'module':
|
14 |
+
return super().__getattribute__(attr)
|
15 |
+
|
16 |
+
if self.module is None:
|
17 |
+
self.module = importlib.import_module(f'hoho.{self.module_name}')
|
18 |
+
sys.modules[self.module_name] = self.module
|
19 |
+
|
20 |
+
return getattr(self.module, attr)
|
21 |
+
|
22 |
+
print('hi')
|
23 |
+
vis = LazyLoadModule('vis')
|
24 |
+
viz3d = LazyLoadModule('viz3d')
|
25 |
+
print(viz3d)
|
hoho/color_mappings.py
ADDED
@@ -0,0 +1,182 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gestalt_color_mapping = {
|
2 |
+
"unclassified": (215, 62, 138),
|
3 |
+
"apex": (235, 88, 48),
|
4 |
+
"eave_end_point": (248, 130, 228),
|
5 |
+
"flashing_end_point": (71, 11, 161),
|
6 |
+
"ridge": (214, 251, 248),
|
7 |
+
"rake": (13, 94, 47),
|
8 |
+
"eave": (54, 243, 63),
|
9 |
+
"post": (187, 123, 236),
|
10 |
+
"ground_line": (136, 206, 14),
|
11 |
+
"flashing": (162, 162, 32),
|
12 |
+
"step_flashing": (169, 255, 219),
|
13 |
+
"hip": (8, 89, 52),
|
14 |
+
"valley": (85, 27, 65),
|
15 |
+
"roof": (215, 232, 179),
|
16 |
+
"door": (110, 52, 23),
|
17 |
+
"garage": (50, 233, 171),
|
18 |
+
"window": (230, 249, 40),
|
19 |
+
"shutter": (122, 4, 233),
|
20 |
+
"fascia": (95, 230, 240),
|
21 |
+
"soffit": (2, 102, 197),
|
22 |
+
"horizontal_siding": (131, 88, 59),
|
23 |
+
"vertical_siding": (110, 187, 198),
|
24 |
+
"brick": (171, 252, 7),
|
25 |
+
"concrete": (32, 47, 246),
|
26 |
+
"other_wall": (112, 61, 240),
|
27 |
+
"trim": (151, 206, 58),
|
28 |
+
"unknown": (127, 127, 127),
|
29 |
+
}
|
30 |
+
|
31 |
+
ade20k_color_mapping = {
|
32 |
+
'wall': (120, 120, 120),
|
33 |
+
'building;edifice': (180, 120, 120),
|
34 |
+
'sky': (6, 230, 230),
|
35 |
+
'floor;flooring': (80, 50, 50),
|
36 |
+
'tree': (4, 200, 3),
|
37 |
+
'ceiling': (120, 120, 80),
|
38 |
+
'road;route': (140, 140, 140),
|
39 |
+
'bed': (204, 5, 255),
|
40 |
+
'windowpane;window': (230, 230, 230),
|
41 |
+
'grass': (4, 250, 7),
|
42 |
+
'cabinet': (224, 5, 255),
|
43 |
+
'sidewalk;pavement': (235, 255, 7),
|
44 |
+
'person;individual;someone;somebody;mortal;soul': (150, 5, 61),
|
45 |
+
'earth;ground': (120, 120, 70),
|
46 |
+
'door;double;door': (8, 255, 51),
|
47 |
+
'table': (255, 6, 82),
|
48 |
+
'mountain;mount': (143, 255, 140),
|
49 |
+
'plant;flora;plant;life': (204, 255, 4),
|
50 |
+
'curtain;drape;drapery;mantle;pall': (255, 51, 7),
|
51 |
+
'chair': (204, 70, 3),
|
52 |
+
'car;auto;automobile;machine;motorcar': (0, 102, 200),
|
53 |
+
'water': (61, 230, 250),
|
54 |
+
'painting;picture': (255, 6, 51),
|
55 |
+
'sofa;couch;lounge': (11, 102, 255),
|
56 |
+
'shelf': (255, 7, 71),
|
57 |
+
'house': (255, 9, 224),
|
58 |
+
'sea': (9, 7, 230),
|
59 |
+
'mirror': (220, 220, 220),
|
60 |
+
'rug;carpet;carpeting': (255, 9, 92),
|
61 |
+
'field': (112, 9, 255),
|
62 |
+
'armchair': (8, 255, 214),
|
63 |
+
'seat': (7, 255, 224),
|
64 |
+
'fence;fencing': (255, 184, 6),
|
65 |
+
'desk': (10, 255, 71),
|
66 |
+
'rock;stone': (255, 41, 10),
|
67 |
+
'wardrobe;closet;press': (7, 255, 255),
|
68 |
+
'lamp': (224, 255, 8),
|
69 |
+
'bathtub;bathing;tub;bath;tub': (102, 8, 255),
|
70 |
+
'railing;rail': (255, 61, 6),
|
71 |
+
'cushion': (255, 194, 7),
|
72 |
+
'base;pedestal;stand': (255, 122, 8),
|
73 |
+
'box': (0, 255, 20),
|
74 |
+
'column;pillar': (255, 8, 41),
|
75 |
+
'signboard;sign': (255, 5, 153),
|
76 |
+
'chest;of;drawers;chest;bureau;dresser': (6, 51, 255),
|
77 |
+
'counter': (235, 12, 255),
|
78 |
+
'sand': (160, 150, 20),
|
79 |
+
'sink': (0, 163, 255),
|
80 |
+
'skyscraper': (140, 140, 140),
|
81 |
+
'fireplace;hearth;open;fireplace': (250, 10, 15),
|
82 |
+
'refrigerator;icebox': (20, 255, 0),
|
83 |
+
'grandstand;covered;stand': (31, 255, 0),
|
84 |
+
'path': (255, 31, 0),
|
85 |
+
'stairs;steps': (255, 224, 0),
|
86 |
+
'runway': (153, 255, 0),
|
87 |
+
'case;display;case;showcase;vitrine': (0, 0, 255),
|
88 |
+
'pool;table;billiard;table;snooker;table': (255, 71, 0),
|
89 |
+
'pillow': (0, 235, 255),
|
90 |
+
'screen;door;screen': (0, 173, 255),
|
91 |
+
'stairway;staircase': (31, 0, 255),
|
92 |
+
'river': (11, 200, 200),
|
93 |
+
'bridge;span': (255 ,82, 0),
|
94 |
+
'bookcase': (0, 255, 245),
|
95 |
+
'blind;screen': (0, 61, 255),
|
96 |
+
'coffee;table;cocktail;table': (0, 255, 112),
|
97 |
+
'toilet;can;commode;crapper;pot;potty;stool;throne': (0, 255, 133),
|
98 |
+
'flower': (255, 0, 0),
|
99 |
+
'book': (255, 163, 0),
|
100 |
+
'hill': (255, 102, 0),
|
101 |
+
'bench': (194, 255, 0),
|
102 |
+
'countertop': (0, 143, 255),
|
103 |
+
'stove;kitchen;stove;range;kitchen;range;cooking;stove': (51, 255, 0),
|
104 |
+
'palm;palm;tree': (0, 82, 255),
|
105 |
+
'kitchen;island': (0, 255, 41),
|
106 |
+
'computer;computing;machine;computing;device;data;processor;electronic;computer;information;processing;system': (0, 255, 173),
|
107 |
+
'swivel;chair': (10, 0, 255),
|
108 |
+
'boat': (173, 255, 0),
|
109 |
+
'bar': (0, 255, 153),
|
110 |
+
'arcade;machine': (255, 92, 0),
|
111 |
+
'hovel;hut;hutch;shack;shanty': (255, 0, 255),
|
112 |
+
'bus;autobus;coach;charabanc;double-decker;jitney;motorbus;motorcoach;omnibus;passenger;vehicle': (255, 0, 245),
|
113 |
+
'towel': (255, 0, 102),
|
114 |
+
'light;light;source': (255, 173, 0),
|
115 |
+
'truck;motortruck': (255, 0, 20),
|
116 |
+
'tower': (255, 184, 184),
|
117 |
+
'chandelier;pendant;pendent': (0, 31, 255),
|
118 |
+
'awning;sunshade;sunblind': (0, 255, 61),
|
119 |
+
'streetlight;street;lamp': (0, 71, 255),
|
120 |
+
'booth;cubicle;stall;kiosk': (255, 0, 204),
|
121 |
+
'television;television;receiver;television;set;tv;tv;set;idiot;box;boob;tube;telly;goggle;box': (0, 255, 194),
|
122 |
+
'airplane;aeroplane;plane': (0, 255, 82),
|
123 |
+
'dirt;track': (0, 10, 255),
|
124 |
+
'apparel;wearing;apparel;dress;clothes': (0, 112, 255),
|
125 |
+
'pole': (51, 0, 255),
|
126 |
+
'land;ground;soil': (0, 194, 255),
|
127 |
+
'bannister;banister;balustrade;balusters;handrail': (0, 122, 255),
|
128 |
+
'escalator;moving;staircase;moving;stairway': (0, 255, 163),
|
129 |
+
'ottoman;pouf;pouffe;puff;hassock': (255, 153, 0),
|
130 |
+
'bottle': (0, 255, 10),
|
131 |
+
'buffet;counter;sideboard': (255, 112, 0),
|
132 |
+
'poster;posting;placard;notice;bill;card': (143, 255, 0),
|
133 |
+
'stage': (82, 0, 255),
|
134 |
+
'van': (163, 255, 0),
|
135 |
+
'ship': (255, 235, 0),
|
136 |
+
'fountain': (8, 184, 170),
|
137 |
+
'conveyer;belt;conveyor;belt;conveyer;conveyor;transporter': (133, 0, 255),
|
138 |
+
'canopy': (0, 255, 92),
|
139 |
+
'washer;automatic;washer;washing;machine': (184, 0, 255),
|
140 |
+
'plaything;toy': (255, 0, 31),
|
141 |
+
'swimming;pool;swimming;bath;natatorium': (0, 184, 255),
|
142 |
+
'stool': (0, 214, 255),
|
143 |
+
'barrel;cask': (255, 0, 112),
|
144 |
+
'basket;handbasket': (92, 255, 0),
|
145 |
+
'waterfall;falls': (0, 224, 255),
|
146 |
+
'tent;collapsible;shelter': (112, 224, 255),
|
147 |
+
'bag': (70, 184, 160),
|
148 |
+
'minibike;motorbike': (163, 0, 255),
|
149 |
+
'cradle': (153, 0, 255),
|
150 |
+
'oven': (71, 255, 0),
|
151 |
+
'ball': (255, 0, 163),
|
152 |
+
'food;solid;food': (255, 204, 0),
|
153 |
+
'step;stair': (255, 0, 143),
|
154 |
+
'tank;storage;tank': (0, 255, 235),
|
155 |
+
'trade;name;brand;name;brand;marque': (133, 255, 0),
|
156 |
+
'microwave;microwave;oven': (255, 0, 235),
|
157 |
+
'pot;flowerpot': (245, 0, 255),
|
158 |
+
'animal;animate;being;beast;brute;creature;fauna': (255, 0, 122),
|
159 |
+
'bicycle;bike;wheel;cycle': (255, 245, 0),
|
160 |
+
'lake': (10, 190, 212),
|
161 |
+
'dishwasher;dish;washer;dishwashing;machine': (214, 255, 0),
|
162 |
+
'screen;silver;screen;projection;screen': (0, 204, 255),
|
163 |
+
'blanket;cover': (20, 0, 255),
|
164 |
+
'sculpture': (255, 255, 0),
|
165 |
+
'hood;exhaust;hood': (0, 153, 255),
|
166 |
+
'sconce': (0, 41, 255),
|
167 |
+
'vase': (0, 255, 204),
|
168 |
+
'traffic;light;traffic;signal;stoplight': (41, 0, 255),
|
169 |
+
'tray': (41, 255, 0),
|
170 |
+
'ashcan;trash;can;garbage;can;wastebin;ash;bin;ash-bin;ashbin;dustbin;trash;barrel;trash;bin': (173, 0, 255),
|
171 |
+
'fan': (0, 245, 255),
|
172 |
+
'pier;wharf;wharfage;dock': (71, 0, 255),
|
173 |
+
'crt;screen': (122, 0, 255),
|
174 |
+
'plate': (0, 255, 184),
|
175 |
+
'monitor;monitoring;device': (0, 92, 255),
|
176 |
+
'bulletin;board;notice;board': (184, 255, 0),
|
177 |
+
'shower': (0, 133, 255),
|
178 |
+
'radiator': (255, 214, 0),
|
179 |
+
'glass;drinking;glass': (25, 194, 194),
|
180 |
+
'clock': (102, 255, 0),
|
181 |
+
'flag': (92, 0, 255),
|
182 |
+
}
|
hoho/hoho.py
CHANGED
@@ -156,9 +156,6 @@ def temp_working_directory():
|
|
156 |
|
157 |
############# Dataset #############
|
158 |
def proc(row, split='train'):
|
159 |
-
# column_names_train = ['ade20k', 'depthcm', 'gestalt', 'colmap', 'KRt', 'mesh', 'wireframe']
|
160 |
-
# column_names_test = ['ade20k', 'depthcm', 'gestalt', 'colmap', 'KRt', 'wireframe']
|
161 |
-
# cols = column_names_train if split == 'train' else column_names_test
|
162 |
out = {}
|
163 |
for k, v in row.items():
|
164 |
colname = k.split('.')[0]
|
|
|
156 |
|
157 |
############# Dataset #############
|
158 |
def proc(row, split='train'):
|
|
|
|
|
|
|
159 |
out = {}
|
160 |
for k, v in row.items():
|
161 |
colname = k.split('.')[0]
|
hoho/read_write_colmap.py
ADDED
@@ -0,0 +1,489 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Modified to read from bytes-like object by Dmytro Mishkin.
|
2 |
+
# The original license is below:
|
3 |
+
# Copyright (c) 2018, ETH Zurich and UNC Chapel 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/viz3d.py
ADDED
@@ -0,0 +1,302 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|