-
Notifications
You must be signed in to change notification settings - Fork 45
/
dataset_readers.py
1210 lines (908 loc) · 47.7 KB
/
dataset_readers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Copyright (C) 2023, Inria
# GRAPHDECO research group, https://team.inria.fr/graphdeco
# All rights reserved.
#
# This software is free for non-commercial, research and evaluation use
# under the terms of the LICENSE.md file.
#
# For inquiries contact [email protected]
#
import os
import sys
from PIL import Image
from typing import NamedTuple
from scene.colmap_loader import read_extrinsics_text, read_intrinsics_text, qvec2rotmat, \
read_extrinsics_binary, read_intrinsics_binary, read_points3D_binary, read_points3D_text
from utils.graphics_utils import getWorld2View2, focal2fov, fov2focal
import numpy as np
import json
from pathlib import Path
from plyfile import PlyData, PlyElement
from utils.sh_utils import SH2RGB
from utils.graphics_utils import BasicPointCloud
import glob
import natsort
from simple_knn._C import distCUDA2
import torch
class CameraInfo(NamedTuple):
uid: int
R: np.array
T: np.array
FovY: np.array
FovX: np.array
image: np.array
image_path: str
image_name: str
width: int
height: int
near: float
far: float
timestamp: float
pose: np.array
hpdirecitons: np.array
cxr: float
cyr: float
class SceneInfo(NamedTuple):
point_cloud: BasicPointCloud
train_cameras: list
test_cameras: list
nerf_normalization: dict
ply_path: str
def getNerfppNorm(cam_info):
def get_center_and_diag(cam_centers):
cam_centers = np.hstack(cam_centers)
avg_cam_center = np.mean(cam_centers, axis=1, keepdims=True)
center = avg_cam_center
dist = np.linalg.norm(cam_centers - center, axis=0, keepdims=True)
diagonal = np.max(dist)
return center.flatten(), diagonal
cam_centers = []
for cam in cam_info:
W2C = getWorld2View2(cam.R, cam.T)
C2W = np.linalg.inv(W2C)
cam_centers.append(C2W[:3, 3:4])
center, diagonal = get_center_and_diag(cam_centers)
radius = diagonal * 1.1
translate = -center
return {"translate": translate, "radius": radius}
def readColmapCameras(cam_extrinsics, cam_intrinsics, images_folder, near, far, startime=0, duration=50):
cam_infos = []
# pose in llff. pipeline by hypereel
originnumpy = os.path.join(os.path.dirname(os.path.dirname(images_folder)), "poses_bounds.npy")
with open(originnumpy, 'rb') as numpy_file:
poses_bounds = np.load(numpy_file)
poses = poses_bounds[:, :15].reshape(-1, 3, 5)
bounds = poses_bounds[:, -2:]
near = bounds.min() * 0.95
far = bounds.max() * 1.05
poses = poses_bounds[:, :15].reshape(-1, 3, 5) # 19, 3, 5
H, W, focal = poses[0, :, -1]
cx, cy = W / 2.0, H / 2.0
K = np.eye(3)
K[0, 0] = focal * W / W / 2.0
K[0, 2] = cx * W / W / 2.0
K[1, 1] = focal * H / H / 2.0
K[1, 2] = cy * H / H / 2.0
imageH = int (H//2) # note hard coded to half of the original image size
imageW = int (W//2)
totalcamname = []
for idx, key in enumerate(cam_extrinsics): # first is cam20_ so we strictly sort by camera name
extr = cam_extrinsics[key]
intr = cam_intrinsics[extr.camera_id]
totalcamname.append(extr.name)
sortedtotalcamelist = natsort.natsorted(totalcamname)
sortednamedict = {}
for i in range(len(sortedtotalcamelist)):
sortednamedict[sortedtotalcamelist[i]] = i # map each cam with a number
for idx, key in enumerate(cam_extrinsics): # first is cam20_ so we strictly sort by camera name
sys.stdout.write('\r')
# the exact output you're looking for:
sys.stdout.write("Reading camera {}/{}".format(idx+1, len(cam_extrinsics)))
sys.stdout.flush()
extr = cam_extrinsics[key]
intr = cam_intrinsics[extr.camera_id]
height = intr.height
width = intr.width
uid = intr.id
R = np.transpose(qvec2rotmat(extr.qvec))
T = np.array(extr.tvec)
if intr.model=="SIMPLE_PINHOLE":
focal_length_x = intr.params[0]
FovY = focal2fov(focal_length_x, height)
FovX = focal2fov(focal_length_x, width)
elif intr.model=="PINHOLE":
focal_length_x = intr.params[0]
focal_length_y = intr.params[1]
FovY = focal2fov(focal_length_y, height)
FovX = focal2fov(focal_length_x, width)
else:
assert False, "Colmap camera model not handled: only undistorted datasets (PINHOLE or SIMPLE_PINHOLE cameras) supported!"
for j in range(startime, startime+ int(duration)):
image_path = os.path.join(images_folder, os.path.basename(extr.name))
image_name = os.path.basename(image_path).split(".")[0]
image_path = image_path.replace("colmap_"+str(startime), "colmap_{}".format(j), 1)
assert os.path.exists(image_path), "Image {} does not exist!".format(image_path)
image = Image.open(image_path)
if j == startime:
# cam_info = CameraInfo(uid=uid, R=R, T=T, FovY=FovY, FovX=FovX, image=image, image_path=image_path, image_name=image_name, width=width, height=height, near=near, far=far, timestamp=(j-startime)/duration, pose=hpposes[sortednamedict[os.path.basename(extr.name)]], hpdirecitons=hpdirecitons,cxr=0.0, cyr=0.0)
cam_info = CameraInfo(uid=uid, R=R, T=T, FovY=FovY, FovX=FovX, image=image, image_path=image_path, image_name=image_name, width=width, height=height, near=near, far=far, timestamp=(j-startime)/duration, pose=1, hpdirecitons=1,cxr=0.0, cyr=0.0)
else:
cam_info = CameraInfo(uid=uid, R=R, T=T, FovY=FovY, FovX=FovX, image=image, image_path=image_path, image_name=image_name, width=width, height=height, near=near, far=far, timestamp=(j-startime)/duration, pose=None, hpdirecitons=None, cxr=0.0, cyr=0.0)
cam_infos.append(cam_info)
sys.stdout.write('\n')
return cam_infos
def readColmapCamerasTechnicolor(cam_extrinsics, cam_intrinsics, images_folder, near, far, startime=0, duration=50):
cam_infos = []
totalcamname = []
for idx, key in enumerate(cam_extrinsics): # first is cam20_ so we strictly sort by camera name
extr = cam_extrinsics[key]
intr = cam_intrinsics[extr.camera_id]
totalcamname.append(extr.name)
sortedtotalcamelist = natsort.natsorted(totalcamname)
sortednamedict = {}
for i in range(len(sortedtotalcamelist)):
sortednamedict[sortedtotalcamelist[i]] = i # map each cam with a number
for idx, key in enumerate(cam_extrinsics): # first is cam20_ so we strictly sort by camera name
sys.stdout.write('\r')
# the exact output you're looking for:
sys.stdout.write("Reading camera {}/{}".format(idx+1, len(cam_extrinsics)))
sys.stdout.flush()
extr = cam_extrinsics[key]
intr = cam_intrinsics[extr.camera_id]
height = intr.height
width = intr.width
uid = intr.id
R = np.transpose(qvec2rotmat(extr.qvec))
T = np.array(extr.tvec)
if intr.model=="SIMPLE_PINHOLE":
focal_length_x = intr.params[0]
FovY = focal2fov(focal_length_x, height)
FovX = focal2fov(focal_length_x, width)
elif intr.model=="PINHOLE":
focal_length_x = intr.params[0]
focal_length_y = intr.params[1]
FovY = focal2fov(focal_length_y, height)
FovX = focal2fov(focal_length_x, width)
else:
assert False, "Colmap camera model not handled: only undistorted datasets (PINHOLE or SIMPLE_PINHOLE cameras) supported!"
for j in range(startime, startime+ int(duration)):
image_path = os.path.join(images_folder, os.path.basename(extr.name))
image_name = os.path.basename(image_path).split(".")[0]
image_path = image_path.replace("colmap_"+str(startime), "colmap_{}".format(j), 1)
cxr = ((intr.params[2] )/ width - 0.5)
cyr = ((intr.params[3] ) / height - 0.5)
K = np.eye(3)
K[0, 0] = focal_length_x #* 0.5
K[0, 2] = intr.params[2] #* 0.5
K[1, 1] = focal_length_y #* 0.5
K[1, 2] = intr.params[3] #* 0.5
halfH = round(height / 2.0 )
halfW = round(width / 2.0 )
assert os.path.exists(image_path), "Image {} does not exist!".format(image_path)
image = Image.open(image_path)
if j == startime:
cam_info = CameraInfo(uid=uid, R=R, T=T, FovY=FovY, FovX=FovX, image=image, image_path=image_path, image_name=image_name, width=width, height=height, near=near, far=far, timestamp=(j-startime)/duration, pose=1, hpdirecitons=1, cxr=cxr, cyr=cyr)
else:
cam_info = CameraInfo(uid=uid, R=R, T=T, FovY=FovY, FovX=FovX, image=image, image_path=image_path, image_name=image_name, width=width, height=height, near=near, far=far, timestamp=(j-startime)/duration, pose=None, hpdirecitons=None, cxr=cxr, cyr=cyr)
cam_infos.append(cam_info)
sys.stdout.write('\n')
return cam_infos
def normalize(v):
return v / np.linalg.norm(v)
def readColmapCamerasMv(cam_extrinsics, cam_intrinsics, images_folder, near, far, startime=0, duration=50):
cam_infos = []
from utils.graphics_utils import getWorld2View2, getProjectionMatrix, getProjectionMatrixCV
for idx, key in enumerate(cam_extrinsics):
sys.stdout.write('\r')
sys.stdout.write("Reading camera {}/{}".format(idx+1, len(cam_extrinsics)))
sys.stdout.flush()
extr = cam_extrinsics[key]
intr = cam_intrinsics[extr.camera_id]
height = intr.height
width = intr.width
uid = intr.id
R = np.transpose(qvec2rotmat(extr.qvec))
T = np.array(extr.tvec)
world_view_transform = torch.tensor(getWorld2View2(R, T)).transpose(0, 1).cuda()
cxr = ((intr.params[2] )/ width - 0.5)
cyr = ((intr.params[3] ) / height - 0.5)
if extr.name == "cam00.png":
if intr.model=="SIMPLE_PINHOLE":
focal_length_x = intr.params[0]
FovY = focal2fov(focal_length_x, height)
FovX = focal2fov(focal_length_x, width)
elif intr.model=="PINHOLE":
focal_length_x = intr.params[0]
focal_length_y = intr.params[1]
FovY = focal2fov(focal_length_y, height)
FovX = focal2fov(focal_length_x, width)
else:
assert False, "Colmap camera model not handled: only undistorted datasets (PINHOLE or SIMPLE_PINHOLE cameras) supported!"
if cyr != 0.0 :
cxr = cxr
cyr = cyr
projection_matrix = getProjectionMatrixCV(znear=0.01, zfar=100.0, fovX=FovX, fovY=FovY, cx=cxr, cy=cyr).transpose(0,1).cuda()
else:
projection_matrix = getProjectionMatrix(znear=0.01, zfar=100.0, fovX=FovX, fovY=FovY).transpose(0,1).cuda()
camera_center = world_view_transform.inverse()[3, :3]
projectinverse = projection_matrix.T.inverse()
camera2wold = world_view_transform.T.inverse()
ndccamera = torch.Tensor((0, 0, 1, 1)).cuda()
ndccamera = ndccamera.unsqueeze(0) # 1, 4
projected = ndccamera @ projectinverse.T # 1, 4, @ 4,4
diretioninlocal = projected / projected[:,3:] #v
direction = diretioninlocal[:,:3] @ camera2wold[:3,:3].T
rays_d = torch.nn.functional.normalize(direction, p=2.0, dim=-1)
target = camera_center + rays_d * 30.0
break
radiace = 1.0
for i in range(240):
theta = i / 240.0 * 4 * np.pi
newcameracenter = camera_center + radiace * torch.Tensor((np.cos(theta), np.sin(theta), 2.0 + 2.0*np.sin(theta))).cuda()
newforward_vector = target - newcameracenter
newforward_vector = newforward_vector.cpu().numpy()
right_vector = R[:, 0] # First column
up_vector = R[:, 1] # Second column
forward_vector = R[:, 2] # Third column
newright = normalize(np.cross(up_vector, newforward_vector))
up = normalize(np.cross(newforward_vector, newright))
newR = np.eye(3)
newR[:, 0] = newright
newR[:, 1] = up
newR[:, 2] = normalize(newforward_vector)
C2W = np.zeros((4, 4))
C2W[:3, :3] = newR
C2W[:3, 3] = newcameracenter.cpu().numpy()
C2W[3, 3] = 1.0
rt = np.linalg.inv(C2W)
newt = rt[:3, 3]
image_name = "mv_" + str(i)
uid = i
time = (i)/240
cam_info = CameraInfo(uid=uid, R=newR, T=newt, FovY=FovY, FovX=FovX, image=None, image_path=None, image_name=image_name, width=width, height=height, near=near, far=far, timestamp=time, pose=1, hpdirecitons=0, cxr=0.0, cyr=0.0)
cam_infos.append(cam_info)
sys.stdout.write('\n')
return cam_infos
def readColmapCamerasImmersive(cam_extrinsics, cam_intrinsics, images_folder, near, far, startime=0, duration=50):
cam_infos = []
totalcamname = []
for idx, key in enumerate(cam_extrinsics): # first is cam20_ so we strictly sort by camera name
extr = cam_extrinsics[key]
intr = cam_intrinsics[extr.camera_id]
totalcamname.append(extr.name)
sortedtotalcamelist = natsort.natsorted(totalcamname)
sortednamedict = {}
for i in range(len(sortedtotalcamelist)):
sortednamedict[sortedtotalcamelist[i]] = i # map each cam with a number
for idx, key in enumerate(cam_extrinsics): # first is cam20_ so we strictly sort by camera name
sys.stdout.write('\r')
# the exact output you're looking for:
sys.stdout.write("Reading camera {}/{}".format(idx+1, len(cam_extrinsics)))
sys.stdout.flush()
extr = cam_extrinsics[key]
intr = cam_intrinsics[extr.camera_id]
height = intr.height
width = intr.width
uid = intr.id
R = np.transpose(qvec2rotmat(extr.qvec))
T = np.array(extr.tvec)
if intr.model=="SIMPLE_PINHOLE":
focal_length_x = intr.params[0]
FovY = focal2fov(focal_length_x, height)
FovX = focal2fov(focal_length_x, width)
elif intr.model=="PINHOLE":
focal_length_x = intr.params[0]
focal_length_y = intr.params[1]
FovY = focal2fov(focal_length_y, height)
FovX = focal2fov(focal_length_x, width)
else:
assert False, "Colmap camera model not handled: only undistorted datasets (PINHOLE or SIMPLE_PINHOLE cameras) supported!"
# if extr.name not in ["camera_0005.png", "camera_0001.png"]:
# continue
for j in range(startime, startime+ int(duration)):
# image_path = os.path.join(images_folder, os.path.basename(extr.name))
# image_name = os.path.basename(image_path).split(".")[0]
# image_path = image_path.replace("colmap_"+str(startime), "colmap_{}".format(j), 1)
parentfolder = os.path.dirname(images_folder)
parentfolder = os.path.dirname(parentfolder)
image_name = extr.name.split(".")[0]
rawvideofolder = os.path.join(parentfolder,os.path.basename(image_name))
image_path = os.path.join(rawvideofolder, str(j) + ".png")
#image_path.replace("colmap_"+str(startime), "colmap_{}".format(j), 1)
# K = np.eye(3)
# K[0, 0] = focal_length_x * 0.5
# K[0, 2] = intr.params[2] * 0.5
# K[1, 1] = focal_length_y * 0.5
# K[1, 2] = intr.params[3] * 0.5
cxr = ((intr.params[2] )/ width - 0.5)
cyr = ((intr.params[3] ) / height - 0.5)
K = np.eye(3)
K[0, 0] = focal_length_x #* 0.5
K[0, 2] = intr.params[2] #* 0.5
K[1, 1] = focal_length_y #* 0.5
K[1, 2] = intr.params[3] #* 0.5
if not os.path.exists(image_path):
image_path = image_path.replace("_S14","")
assert os.path.exists(image_path), "Image {} does not exist!".format(image_path)
image = Image.open(image_path)
if j == startime:
cam_info = CameraInfo(uid=uid, R=R, T=T, FovY=FovY, FovX=FovX, image=image, image_path=image_path, image_name=image_name, width=width, height=height, near=near, far=far, timestamp=(j-startime)/duration, pose=1, hpdirecitons=1, cxr=cxr, cyr=cyr)
else:
cam_info = CameraInfo(uid=uid, R=R, T=T, FovY=FovY, FovX=FovX, image=image, image_path=image_path, image_name=image_name, width=width, height=height, near=near, far=far, timestamp=(j-startime)/duration, pose=None, hpdirecitons=None, cxr=cxr, cyr=cyr)
cam_infos.append(cam_info)
sys.stdout.write('\n')
return cam_infos
def readColmapCamerasImmersiveTestonly(cam_extrinsics, cam_intrinsics, images_folder, near, far, startime=0, duration=50):
cam_infos = []
totalcamname = []
for idx, key in enumerate(cam_extrinsics): # first is cam20_ so we strictly sort by camera name
extr = cam_extrinsics[key]
intr = cam_intrinsics[extr.camera_id]
totalcamname.append(extr.name)
sortedtotalcamelist = natsort.natsorted(totalcamname)
sortednamedict = {}
for i in range(len(sortedtotalcamelist)):
sortednamedict[sortedtotalcamelist[i]] = i # map each cam with a number
for idx, key in enumerate(cam_extrinsics): # first is cam20_ so we strictly sort by camera name
sys.stdout.write('\r')
# the exact output you're looking for:
sys.stdout.write("Reading camera {}/{}".format(idx+1, len(cam_extrinsics)))
sys.stdout.flush()
extr = cam_extrinsics[key]
intr = cam_intrinsics[extr.camera_id]
height = intr.height
width = intr.width
uid = intr.id
R = np.transpose(qvec2rotmat(extr.qvec))
T = np.array(extr.tvec)
# T[1] = T[1] + 0.2 # y incrase by 1
# T[2] = T[2] + 0.65
# T[0] = T[0] + 0.65 # x by 0.65
if intr.model=="SIMPLE_PINHOLE":
focal_length_x = intr.params[0]
FovY = focal2fov(focal_length_x, height)
FovX = focal2fov(focal_length_x, width)
elif intr.model=="PINHOLE":
focal_length_x = intr.params[0]
focal_length_y = intr.params[1]
FovY = focal2fov(focal_length_y, height)
FovX = focal2fov(focal_length_x, width)
else:
assert False, "Colmap camera model not handled: only undistorted datasets (PINHOLE or SIMPLE_PINHOLE cameras) supported!"
for j in range(startime, startime+ int(duration)):
# image_path = os.path.join(images_folder, os.path.basename(extr.name))
# image_name = os.path.basename(image_path).split(".")[0]
# image_path = image_path.replace("colmap_"+str(startime), "colmap_{}".format(j), 1)
parentfolder = os.path.dirname(images_folder)
parentfolder = os.path.dirname(parentfolder)
image_name = extr.name.split(".")[0]
rawvideofolder = os.path.join(parentfolder,os.path.basename(image_name))
image_path = os.path.join(rawvideofolder, str(j) + ".png")
#image_path.replace("colmap_"+str(startime), "colmap_{}".format(j), 1)
# K = np.eye(3)
# K[0, 0] = focal_length_x * 0.5
# K[0, 2] = intr.params[2] * 0.5
# K[1, 1] = focal_length_y * 0.5
# K[1, 2] = intr.params[3] * 0.5
cxr = ((intr.params[2] )/ width - 0.5)
cyr = ((intr.params[3] ) / height - 0.5)
K = np.eye(3)
K[0, 0] = focal_length_x #* 0.5
K[0, 2] = intr.params[2] #* 0.5
K[1, 1] = focal_length_y #* 0.5
K[1, 2] = intr.params[3] #* 0.5
#halfH = round(height / 2.0 )
#halfW = round(width / 2.0 )
if not os.path.exists(image_path):
image_path = image_path.replace("_S14","")
assert os.path.exists(image_path), "Image {} does not exist!".format(image_path)
if image_name == "camera_0001":
image = Image.open(image_path)
else:
image = None
if j == startime:
cam_info = CameraInfo(uid=uid, R=R, T=T, FovY=FovY, FovX=FovX, image=image, image_path=image_path, image_name=image_name, width=width, height=height, near=near, far=far, timestamp=(j-startime)/duration, pose=1, hpdirecitons=1, cxr=cxr, cyr=cyr)
else:
cam_info = CameraInfo(uid=uid, R=R, T=T, FovY=FovY, FovX=FovX, image=image, image_path=image_path, image_name=image_name, width=width, height=height, near=near, far=far, timestamp=(j-startime)/duration, pose=None, hpdirecitons=None, cxr=cxr, cyr=cyr)
cam_infos.append(cam_info)
sys.stdout.write('\n')
return cam_infos
def fetchPly(path):
plydata = PlyData.read(path)
vertices = plydata['vertex']
positions = np.vstack([vertices['x'], vertices['y'], vertices['z']]).T
times = np.vstack([vertices['t']]).T
colors = np.vstack([vertices['red'], vertices['green'], vertices['blue']]).T / 255.0
normals = np.vstack([vertices['nx'], vertices['ny'], vertices['nz']]).T
return BasicPointCloud(points=positions, colors=colors, normals=normals, times=times)
def storePly(path, xyzt, rgb):
# Define the dtype for the structured array
dtype = [('x', 'f4'), ('y', 'f4'), ('z', 'f4'),('t','f4'),
('nx', 'f4'), ('ny', 'f4'), ('nz', 'f4'),
('red', 'u1'), ('green', 'u1'), ('blue', 'u1')]
xyz = xyzt[:, :3]
normals = np.zeros_like(xyz)
elements = np.empty(xyzt.shape[0], dtype=dtype)
attributes = np.concatenate((xyzt, normals, rgb), axis=1)
elements[:] = list(map(tuple, attributes))
# Create the PlyData object and write to file
vertex_element = PlyElement.describe(elements, 'vertex')
ply_data = PlyData([vertex_element])
ply_data.write(path)
def readColmapSceneInfoImmersive(path, images, eval, llffhold=8, multiview=False, duration=50, testonly=False ):
try:
cameras_extrinsic_file = os.path.join(path, "sparse/0", "images.bin")
cameras_intrinsic_file = os.path.join(path, "sparse/0", "cameras.bin")
cam_extrinsics = read_extrinsics_binary(cameras_extrinsic_file)
cam_intrinsics = read_intrinsics_binary(cameras_intrinsic_file)
except:
cameras_extrinsic_file = os.path.join(path, "sparse/0", "images.txt")
cameras_intrinsic_file = os.path.join(path, "sparse/0", "cameras.txt")
cam_extrinsics = read_extrinsics_text(cameras_extrinsic_file)
cam_intrinsics = read_intrinsics_text(cameras_intrinsic_file)
reading_dir = "images" if images == None else images
near = 0.01
far = 100
starttime = os.path.basename(path).split("_")[1] # colmap_0,
assert starttime.isdigit(), "Colmap folder name must be colmap_<startime>_<duration>!"
starttime = int(starttime)
# readColmapCamerasImmersiveTestonly
if testonly:
cam_infos_unsorted = readColmapCamerasImmersiveTestonly(cam_extrinsics=cam_extrinsics, cam_intrinsics=cam_intrinsics, images_folder=os.path.join(path, reading_dir), near=near, far=far, startime=starttime, duration=duration)
else:
cam_infos_unsorted = readColmapCamerasImmersive(cam_extrinsics=cam_extrinsics, cam_intrinsics=cam_intrinsics, images_folder=os.path.join(path, reading_dir), near=near, far=far, startime=starttime, duration=duration)
cam_infos = sorted(cam_infos_unsorted.copy(), key = lambda x : x.image_name)
if eval:
train_cam_infos = cam_infos[duration:] # + cam_infos[:duration] # for demo only
test_cam_infos = cam_infos[:duration]
uniquecheck = []
for cam_info in test_cam_infos:
if cam_info.image_name not in uniquecheck:
uniquecheck.append(cam_info.image_name)
assert len(uniquecheck) == 1
sanitycheck = []
for cam_info in train_cam_infos:
if cam_info.image_name not in sanitycheck:
sanitycheck.append(cam_info.image_name)
for testname in uniquecheck:
assert testname not in sanitycheck
else:
train_cam_infos = cam_infos # for demo without eval
test_cam_infos = cam_infos[:duration]
nerf_normalization = getNerfppNorm(train_cam_infos)
ply_path = os.path.join(path, "sparse/0/points3D.ply")
bin_path = os.path.join(path, "sparse/0/points3D.bin")
txt_path = os.path.join(path, "sparse/0/points3D.txt")
totalply_path = os.path.join(path, "sparse/0/points3D_total" + str(duration) + ".ply")
# if os.path.exists(ply_path):
# os.remove(ply_path)
# if os.path.exists(totalply_path):
# os.remove(totalply_path)
if not os.path.exists(totalply_path):
print("Converting point3d.bin to .ply, will happen only the first time you open the scene.")
totalxyz = []
totalrgb = []
totaltime = []
takeoffset = 0
for i in range(starttime, starttime + duration):
thisbin_path = os.path.join(path, "sparse/0/points3D.bin").replace("colmap_"+ str(starttime), "colmap_" + str(i), 1)
xyz, rgb, _ = read_points3D_binary(thisbin_path)
totalxyz.append(xyz)
totalrgb.append(rgb)
totaltime.append(np.ones((xyz.shape[0], 1)) * (i-starttime) / duration)
xyz = np.concatenate(totalxyz, axis=0)
rgb = np.concatenate(totalrgb, axis=0)
totaltime = np.concatenate(totaltime, axis=0)
assert xyz.shape[0] == rgb.shape[0]
xyzt =np.concatenate( (xyz, totaltime), axis=1)
storePly(totalply_path, xyzt, rgb)
try:
pcd = fetchPly(totalply_path)
except:
pcd = None
scene_info = SceneInfo(point_cloud=pcd,
train_cameras=train_cam_infos,
test_cameras=test_cam_infos,
nerf_normalization=nerf_normalization,
ply_path=totalply_path)
return scene_info
def readColmapSceneInfoMv(path, images, eval, llffhold=8, multiview=False, duration=50):
try:
cameras_extrinsic_file = os.path.join(path, "sparse/0", "images.bin")
cameras_intrinsic_file = os.path.join(path, "sparse/0", "cameras.bin")
cam_extrinsics = read_extrinsics_binary(cameras_extrinsic_file)
cam_intrinsics = read_intrinsics_binary(cameras_intrinsic_file)
except:
cameras_extrinsic_file = os.path.join(path, "sparse/0", "images.txt")
cameras_intrinsic_file = os.path.join(path, "sparse/0", "cameras.txt")
cam_extrinsics = read_extrinsics_text(cameras_extrinsic_file)
cam_intrinsics = read_intrinsics_text(cameras_intrinsic_file)
reading_dir = "images" if images == None else images
parentdir = os.path.dirname(path)
near = 0.01
far = 100
starttime = os.path.basename(path).split("_")[1] # colmap_0,
assert starttime.isdigit(), "Colmap folder name must be colmap_<startime>_<duration>!"
starttime = int(starttime)
cam_infos_unsorted = readColmapCamerasMv(cam_extrinsics=cam_extrinsics, cam_intrinsics=cam_intrinsics, images_folder=os.path.join(path, reading_dir), near=near, far=far, startime=starttime, duration=duration)
cam_infos = cam_infos_unsorted
# for cam in cam_infos:
# print(cam.image_name)
# for cam_info in cam_infos:
# print(cam_info.uid, cam_info.R, cam_info.T, cam_info.FovY, cam_info.image_name)
train_cam_infos = []
test_cam_infos = cam_infos
nerf_normalization = getNerfppNorm(test_cam_infos)
ply_path = os.path.join(path, "sparse/0/points3D.ply")
bin_path = os.path.join(path, "sparse/0/points3D.bin")
txt_path = os.path.join(path, "sparse/0/points3D.txt")
totalply_path = os.path.join(path, "sparse/0/points3D_total" + str(duration) + "_mv.ply")
# if os.path.exists(ply_path):
# os.remove(ply_path)
# if os.path.exists(totalply_path):
# os.remove(totalply_path)
# # if not os.path.exists(totalply_path):
# # print("Converting point3d.bin to .ply, will happen only the first time you open the scene.")
# # totalxyz = []
# # totalrgb = []
# # totaltime = []
# # for i in range(starttime, starttime + duration):
# # thisbin_path = os.path.join(path, "sparse/0/points3D.bin").replace("colmap_"+ str(starttime), "colmap_" + str(i), 1)
# # xyz, rgb, _ = read_points3D_binary(thisbin_path)
# # totalxyz.append(xyz)
# # totalrgb.append(rgb)
# # totaltime.append(np.ones((xyz.shape[0], 1)) * (i-starttime) / duration)
# # xyz = np.concatenate(totalxyz, axis=0)
# # rgb = np.concatenate(totalrgb, axis=0)
# # totaltime = np.concatenate(totaltime, axis=0)
# # assert xyz.shape[0] == rgb.shape[0]
# # xyzt =np.concatenate( (xyz, totaltime), axis=1)
# # storePly(totalply_path, xyzt, rgb)
# # try:
# # pcd = fetchPly(totalply_path)
# # except:
pcd = None
scene_info = SceneInfo(point_cloud=pcd,
train_cameras=train_cam_infos,
test_cameras=test_cam_infos,
nerf_normalization=nerf_normalization,
ply_path=totalply_path)
return scene_info
def readColmapSceneInfo(path, images, eval, llffhold=8, multiview=False, duration=50):
try:
cameras_extrinsic_file = os.path.join(path, "sparse/0", "images.bin")
cameras_intrinsic_file = os.path.join(path, "sparse/0", "cameras.bin")
cam_extrinsics = read_extrinsics_binary(cameras_extrinsic_file)
cam_intrinsics = read_intrinsics_binary(cameras_intrinsic_file)
except:
cameras_extrinsic_file = os.path.join(path, "sparse/0", "images.txt")
cameras_intrinsic_file = os.path.join(path, "sparse/0", "cameras.txt")
cam_extrinsics = read_extrinsics_text(cameras_extrinsic_file)
cam_intrinsics = read_intrinsics_text(cameras_intrinsic_file)
reading_dir = "images" if images == None else images
parentdir = os.path.dirname(path)
near = 0.01
far = 100
starttime = os.path.basename(path).split("_")[1] # colmap_0,
assert starttime.isdigit(), "Colmap folder name must be colmap_<startime>_<duration>!"
starttime = int(starttime)
cam_infos_unsorted = readColmapCameras(cam_extrinsics=cam_extrinsics, cam_intrinsics=cam_intrinsics, images_folder=os.path.join(path, reading_dir), near=near, far=far, startime=starttime, duration=duration)
cam_infos = sorted(cam_infos_unsorted.copy(), key = lambda x : x.image_name)
if eval:
train_cam_infos = cam_infos[duration:]
test_cam_infos = cam_infos[:duration]
uniquecheck = []
for cam_info in test_cam_infos:
if cam_info.image_name not in uniquecheck:
uniquecheck.append(cam_info.image_name)
assert len(uniquecheck) == 1
sanitycheck = []
for cam_info in train_cam_infos:
if cam_info.image_name not in sanitycheck:
sanitycheck.append(cam_info.image_name)
for testname in uniquecheck:
assert testname not in sanitycheck
else:
train_cam_infos = cam_infos
test_cam_infos = cam_infos[:2] #dummy
nerf_normalization = getNerfppNorm(train_cam_infos)
ply_path = os.path.join(path, "sparse/0/points3D.ply")
bin_path = os.path.join(path, "sparse/0/points3D.bin")
txt_path = os.path.join(path, "sparse/0/points3D.txt")
totalply_path = os.path.join(path, "sparse/0/points3D_total" + str(duration) + ".ply")
if not os.path.exists(totalply_path):
print("Converting point3d.bin to .ply, will happen only the first time you open the scene.")
totalxyz = []
totalrgb = []
totaltime = []
for i in range(starttime, starttime + duration):
thisbin_path = os.path.join(path, "sparse/0/points3D.bin").replace("colmap_"+ str(starttime), "colmap_" + str(i), 1)
xyz, rgb, _ = read_points3D_binary(thisbin_path)
totalxyz.append(xyz)
totalrgb.append(rgb)
totaltime.append(np.ones((xyz.shape[0], 1)) * (i-starttime) / duration)
xyz = np.concatenate(totalxyz, axis=0)
rgb = np.concatenate(totalrgb, axis=0)
totaltime = np.concatenate(totaltime, axis=0)
assert xyz.shape[0] == rgb.shape[0]
xyzt =np.concatenate( (xyz, totaltime), axis=1)
storePly(totalply_path, xyzt, rgb)
try:
pcd = fetchPly(totalply_path)
except:
pcd = None
scene_info = SceneInfo(point_cloud=pcd,
train_cameras=train_cam_infos,
test_cameras=test_cam_infos,
nerf_normalization=nerf_normalization,
ply_path=totalply_path)
return scene_info
def readColmapSceneInfoTechnicolor(path, images, eval, llffhold=8, multiview=False, duration=50):
try:
cameras_extrinsic_file = os.path.join(path, "sparse/0", "images.bin")
cameras_intrinsic_file = os.path.join(path, "sparse/0", "cameras.bin")
cam_extrinsics = read_extrinsics_binary(cameras_extrinsic_file)
cam_intrinsics = read_intrinsics_binary(cameras_intrinsic_file)
except:
cameras_extrinsic_file = os.path.join(path, "sparse/0", "images.txt")
cameras_intrinsic_file = os.path.join(path, "sparse/0", "cameras.txt")
cam_extrinsics = read_extrinsics_text(cameras_extrinsic_file)
cam_intrinsics = read_intrinsics_text(cameras_intrinsic_file)
reading_dir = "images" if images == None else images
parentdir = os.path.dirname(path)
starttime = os.path.basename(path).split("_")[1] # colmap_0,
assert starttime.isdigit(), "Colmap folder name must be colmap_<startime>_<duration>!"
starttime = int(starttime)
near = 0.01
far = 100
cam_infos_unsorted = readColmapCamerasTechnicolor(cam_extrinsics=cam_extrinsics, cam_intrinsics=cam_intrinsics, images_folder=os.path.join(path, reading_dir), near=near, far=far, startime=starttime, duration=duration)
cam_infos = sorted(cam_infos_unsorted.copy(), key = lambda x : x.image_name)
# for cam in cam_infos:
# print(cam.image_name)
# for cam_info in cam_infos:
# print(cam_info.uid, cam_info.R, cam_info.T, cam_info.FovY, cam_info.image_name)
if eval:
train_cam_infos = [_ for _ in cam_infos if "cam10" not in _.image_name]
test_cam_infos = [_ for _ in cam_infos if "cam10" in _.image_name]
if len(test_cam_infos) > 0:
uniquecheck = []
for cam_info in test_cam_infos:
if cam_info.image_name not in uniquecheck:
uniquecheck.append(cam_info.image_name)
assert len(uniquecheck) == 1
sanitycheck = []
for cam_info in train_cam_infos:
if cam_info.image_name not in sanitycheck:
sanitycheck.append(cam_info.image_name)
for testname in uniquecheck:
assert testname not in sanitycheck
else:
first_cam = cam_infos[0].image_name
print("do custom loader training, select first cam as test frame: ", first_cam)
cam_infos = natsort.natsorted(cam_infos_unsorted.copy(), key = lambda x : x.image_name)
train_cam_infos = [_ for _ in cam_infos if first_cam not in _.image_name]
test_cam_infos = [_ for _ in cam_infos if first_cam in _.image_name]
else:
train_cam_infos = cam_infos
test_cam_infos = cam_infos[:4]
nerf_normalization = getNerfppNorm(train_cam_infos)
ply_path = os.path.join(path, "sparse/0/points3D.ply")
bin_path = os.path.join(path, "sparse/0/points3D.bin")
txt_path = os.path.join(path, "sparse/0/points3D.txt")
totalply_path = os.path.join(path, "sparse/0/points3D_total" + str(duration) + ".ply")
if not os.path.exists(totalply_path):
print("Converting point3d.bin to .ply, will happen only the first time you open the scene.")
totalxyz = []
totalrgb = []
totaltime = []
for i in range(starttime, starttime + duration):
thisbin_path = os.path.join(path, "sparse/0/points3D.bin").replace("colmap_"+ str(starttime), "colmap_" + str(i), 1)
xyz, rgb, _ = read_points3D_binary(thisbin_path)
totalxyz.append(xyz)
totalrgb.append(rgb)
totaltime.append(np.ones((xyz.shape[0], 1)) * (i-starttime) / duration)
xyz = np.concatenate(totalxyz, axis=0)
rgb = np.concatenate(totalrgb, axis=0)
totaltime = np.concatenate(totaltime, axis=0)
assert xyz.shape[0] == rgb.shape[0]
xyzt =np.concatenate( (xyz, totaltime), axis=1)
storePly(totalply_path, xyzt, rgb)
try:
pcd = fetchPly(totalply_path)
except:
pcd = None
scene_info = SceneInfo(point_cloud=pcd,
train_cameras=train_cam_infos,
test_cameras=test_cam_infos,
nerf_normalization=nerf_normalization,
ply_path=totalply_path)
return scene_info
def readCamerasFromTransforms(path, transformsfile, white_background, extension=".png"):
cam_infos = []
with open(os.path.join(path, transformsfile)) as json_file:
contents = json.load(json_file)
fovx = contents["camera_angle_x"]
frames = contents["frames"]
for idx, frame in enumerate(frames):
cam_name = os.path.join(path, frame["file_path"] + extension)
matrix = np.linalg.inv(np.array(frame["transform_matrix"]))
R = -np.transpose(matrix[:3,:3])
R[:,0] = -R[:,0]
T = -matrix[:3, 3]
image_path = os.path.join(path, cam_name)
image_name = Path(cam_name).stem
image = Image.open(image_path)
im_data = np.array(image.convert("RGBA"))
bg = np.array([1,1,1]) if white_background else np.array([0, 0, 0])
norm_data = im_data / 255.0
arr = norm_data[:,:,:3] * norm_data[:, :, 3:4] + bg * (1 - norm_data[:, :, 3:4])
image = Image.fromarray(np.array(arr*255.0, dtype=np.byte), "RGB")
fovy = focal2fov(fov2focal(fovx, image.size[0]), image.size[1])
FovY = fovy
FovX = fovx
for j in range(20):
cam_infos.append(CameraInfo(uid=idx*20 + j, R=R, T=T, FovY=FovY, FovX=FovX, image=image,
image_path=image_path, image_name=image_name, width=image.size[0], height=image.size[1]))
return cam_infos
def readNerfSyntheticInfo(path, white_background, eval, extension=".png", multiview=False):
print("Reading Training Transforms")
train_cam_infos = readCamerasFromTransforms(path, "transforms_train.json", white_background, extension)
print("Reading Test Transforms")
test_cam_infos = readCamerasFromTransforms(path, "transforms_test.json", white_background, extension)
if not eval:
train_cam_infos.extend(test_cam_infos)
test_cam_infos = []
nerf_normalization = getNerfppNorm(train_cam_infos)
ply_path = os.path.join(path, "points3d.ply")
if not os.path.exists(ply_path):
# Since this data set has no colmap data, we start with random points
num_pts = 100_000
print(f"Generating random point cloud ({num_pts})...")