File size: 33,538 Bytes
8cf8c7b
be6ccaa
 
8cf8c7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8309413
8cf8c7b
 
bfe7c38
8cf8c7b
 
bec6532
8cf8c7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bec6532
 
8cf8c7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import spaces
import torch
print("cuda is available: ", torch.cuda.is_available())

import gradio as gr
import os
import shutil
import rembg
import numpy as np
import math
import open3d as o3d
from PIL import Image
import torchvision
import trimesh
from skimage.io import imsave
import imageio
import cv2
import matplotlib.pyplot as pl
pl.ion()

CaPE_TYPE = "6DoF"
device = 'cuda' #if torch.cuda.is_available() else 'cpu'
weight_dtype = torch.float16
torch.backends.cuda.matmul.allow_tf32 = True  # for gpu >= Ampere and pytorch >= 1.12

# EscherNet
# create angles in archimedean spiral with N steps
def get_archimedean_spiral(sphere_radius, num_steps=250):
    # x-z plane, around upper y
    '''
    https://en.wikipedia.org/wiki/Spiral, section "Spherical spiral". c = a / pi
    '''
    a = 40
    r = sphere_radius

    translations = []
    angles = []

    # i = a / 2
    i = 0.01
    while i < a:
        theta = i / a * math.pi
        x = r * math.sin(theta) * math.cos(-i)
        z = r * math.sin(-theta + math.pi) * math.sin(-i)
        y = r * - math.cos(theta)

        # translations.append((x, y, z))    # origin
        translations.append((x, z, -y))
        angles.append([np.rad2deg(-i), np.rad2deg(theta)])

        # i += a / (2 * num_steps)
        i += a / (1 * num_steps)

    return np.array(translations), np.stack(angles)

def look_at(origin, target, up):
    forward = (target - origin)
    forward = forward / np.linalg.norm(forward)
    right = np.cross(up, forward)
    right = right / np.linalg.norm(right)
    new_up = np.cross(forward, right)
    rotation_matrix = np.column_stack((right, new_up, -forward, target))
    matrix = np.row_stack((rotation_matrix, [0, 0, 0, 1]))
    return matrix

import einops
import sys

sys.path.insert(0, "./6DoF/")   # TODO change it when deploying
# use the customized diffusers modules
from diffusers import DDIMScheduler
from dataset import get_pose
from CN_encoder import CN_encoder
from pipeline_zero1to3 import Zero1to3StableDiffusionPipeline

pretrained_model_name_or_path = "kxic/EscherNet_demo"
resolution = 256
h,w = resolution,resolution
guidance_scale = 3.0
radius = 2.2
bg_color = [1., 1., 1., 1.]
image_transforms = torchvision.transforms.Compose(
        [
            torchvision.transforms.Resize((resolution, resolution)),  # 256, 256
            torchvision.transforms.ToTensor(),
            torchvision.transforms.Normalize([0.5], [0.5])
        ]
    )
xyzs_spiral, angles_spiral = get_archimedean_spiral(1.5, 200)
# only half toop
xyzs_spiral = xyzs_spiral[:100]
angles_spiral = angles_spiral[:100]

# Init pipeline
scheduler = DDIMScheduler.from_pretrained(pretrained_model_name_or_path, subfolder="scheduler", revision=None)
image_encoder = CN_encoder.from_pretrained(pretrained_model_name_or_path, subfolder="image_encoder", revision=None)
pipeline = Zero1to3StableDiffusionPipeline.from_pretrained(
    pretrained_model_name_or_path,
    revision=None,
    scheduler=scheduler,
    image_encoder=None,
    safety_checker=None,
    feature_extractor=None,
    torch_dtype=weight_dtype,
)
pipeline.image_encoder = image_encoder.to(weight_dtype)

pipeline.set_progress_bar_config(disable=False)

# pipeline.enable_xformers_memory_efficient_attention()
# enable vae slicing
pipeline.enable_vae_slicing()
# pipeline = pipeline.to(device)




@spaces.GPU(duration=120)
def run_eschernet(tmpdirname, eschernet_input_dict, sample_steps, sample_seed, nvs_num, nvs_mode):
    # set the random seed
    generator = torch.Generator(device=device).manual_seed(sample_seed)
    T_out = nvs_num
    T_in = len(eschernet_input_dict['imgs'])
    ####### output pose
    # TODO choose T_out number of poses sequentially from the spiral
    xyzs = xyzs_spiral[::(len(xyzs_spiral) // T_out)]
    angles_out = angles_spiral[::(len(xyzs_spiral) // T_out)]

    ####### input's max radius for translation scaling
    radii = eschernet_input_dict['radii']
    max_t = np.max(radii)
    min_t = np.min(radii)

    ####### input pose
    pose_in = []
    for T_in_index in range(T_in):
        pose = get_pose(np.linalg.inv(eschernet_input_dict['poses'][T_in_index]))
        pose[1:3, :] *= -1   # coordinate system conversion
        pose[3, 3] *= 1. / max_t * radius    # scale radius to [1.5, 2.2]
        pose_in.append(torch.from_numpy(pose))

    ####### input image
    img = eschernet_input_dict['imgs'] / 255.
    img[img[:, :, :, -1] == 0.] = bg_color
    # TODO batch image_transforms
    input_image = [image_transforms(Image.fromarray(np.uint8(im[:, :, :3] * 255.)).convert("RGB")) for im in img]

    ####### nvs pose
    pose_out = []
    for T_out_index in range(T_out):
        azimuth, polar = angles_out[T_out_index]
        if CaPE_TYPE == "4DoF":
            pose_out.append(torch.tensor([np.deg2rad(polar), np.deg2rad(azimuth), 0., 0.]))
        elif CaPE_TYPE == "6DoF":
            pose = look_at(origin=np.array([0, 0, 0]), target=xyzs[T_out_index], up=np.array([0, 0, 1]))
            pose = np.linalg.inv(pose)
            pose[2, :] *= -1
            pose_out.append(torch.from_numpy(get_pose(pose)))



    # [B, T, C, H, W]
    input_image = torch.stack(input_image, dim=0).to(device).to(weight_dtype).unsqueeze(0)
    # [B, T, 4]
    pose_in = np.stack(pose_in)
    pose_out = np.stack(pose_out)

    if CaPE_TYPE == "6DoF":
        pose_in_inv = np.linalg.inv(pose_in).transpose([0, 2, 1])
        pose_out_inv = np.linalg.inv(pose_out).transpose([0, 2, 1])
        pose_in_inv = torch.from_numpy(pose_in_inv).to(device).to(weight_dtype).unsqueeze(0)
        pose_out_inv = torch.from_numpy(pose_out_inv).to(device).to(weight_dtype).unsqueeze(0)

    pose_in = torch.from_numpy(pose_in).to(device).to(weight_dtype).unsqueeze(0)
    pose_out = torch.from_numpy(pose_out).to(device).to(weight_dtype).unsqueeze(0)

    input_image = einops.rearrange(input_image, "b t c h w -> (b t) c h w")
    assert T_in == input_image.shape[0]
    assert T_in == pose_in.shape[1]
    assert T_out == pose_out.shape[1]

    # run inference
    pipeline.to(device)
    pipeline.enable_xformers_memory_efficient_attention()
    if CaPE_TYPE == "6DoF":
        with torch.autocast("cuda"):
            image = pipeline(input_imgs=input_image, prompt_imgs=input_image,
                             poses=[[pose_out, pose_out_inv], [pose_in, pose_in_inv]],
                             height=h, width=w, T_in=T_in, T_out=T_out,
                             guidance_scale=guidance_scale, num_inference_steps=50, generator=generator,
                             output_type="numpy").images
    elif CaPE_TYPE == "4DoF":
        with torch.autocast("cuda"):
            image = pipeline(input_imgs=input_image, prompt_imgs=input_image, poses=[pose_out, pose_in],
                             height=h, width=w, T_in=T_in, T_out=T_out,
                             guidance_scale=guidance_scale, num_inference_steps=50, generator=generator,
                             output_type="numpy").images

    # save output image
    output_dir = os.path.join(tmpdirname, "eschernet")
    if os.path.exists(output_dir):
        shutil.rmtree(output_dir)
    os.makedirs(output_dir, exist_ok=True)
    # save to N imgs
    for i in range(T_out):
        imsave(os.path.join(output_dir, f'{i}.png'), (image[i] * 255).astype(np.uint8))
    # make a gif
    frames = [Image.fromarray((image[i] * 255).astype(np.uint8)) for i in range(T_out)]
    frame_one = frames[0]
    frame_one.save(os.path.join(output_dir, "output.gif"), format="GIF", append_images=frames,
                   save_all=True, duration=50, loop=1)

    # get a video
    video_path = os.path.join(output_dir, "output.mp4")
    imageio.mimwrite(video_path, np.stack(frames), fps=10, codec='h264')


    return image, video_path

# TODO mesh it
@spaces.GPU(duration=120)
def make3d():
    pass



############################ Dust3r as Pose Estimation ############################
from scipy.spatial.transform import Rotation
import copy

from dust3r.inference import inference
from dust3r.model import AsymmetricCroCo3DStereo
from dust3r.image_pairs import make_pairs
from dust3r.utils.image import load_images, rgb
from dust3r.utils.device import to_numpy
from dust3r.viz import add_scene_cam, CAM_COLORS, OPENGL, pts3d_to_trimesh, cat_meshes
from dust3r.cloud_opt import global_aligner, GlobalAlignerMode

import functools
import math

@spaces.GPU
def _convert_scene_output_to_glb(outdir, imgs, pts3d, mask, focals, cams2world, cam_size=0.05,
                                 cam_color=None, as_pointcloud=False,
                                 transparent_cams=False, silent=False, same_focals=False):
    assert len(pts3d) == len(mask) <= len(imgs) <= len(cams2world)
    if not same_focals:
        assert (len(cams2world) == len(focals))
    pts3d = to_numpy(pts3d)
    imgs = to_numpy(imgs)
    focals = to_numpy(focals)
    cams2world = to_numpy(cams2world)

    scene = trimesh.Scene()

    # add axes
    scene.add_geometry(trimesh.creation.axis(axis_length=0.5, axis_radius=0.001))

    # full pointcloud
    if as_pointcloud:
        pts = np.concatenate([p[m] for p, m in zip(pts3d, mask)])
        col = np.concatenate([p[m] for p, m in zip(imgs, mask)])
        pct = trimesh.PointCloud(pts.reshape(-1, 3), colors=col.reshape(-1, 3))
        scene.add_geometry(pct)
    else:
        meshes = []
        for i in range(len(imgs)):
            meshes.append(pts3d_to_trimesh(imgs[i], pts3d[i], mask[i]))
        mesh = trimesh.Trimesh(**cat_meshes(meshes))
        scene.add_geometry(mesh)

    # add each camera
    for i, pose_c2w in enumerate(cams2world):
        if isinstance(cam_color, list):
            camera_edge_color = cam_color[i]
        else:
            camera_edge_color = cam_color or CAM_COLORS[i % len(CAM_COLORS)]
        if same_focals:
            focal = focals[0]
        else:
            focal = focals[i]
        add_scene_cam(scene, pose_c2w, camera_edge_color,
                      None if transparent_cams else imgs[i], focal,
                      imsize=imgs[i].shape[1::-1], screen_width=cam_size)

    rot = np.eye(4)
    rot[:3, :3] = Rotation.from_euler('y', np.deg2rad(180)).as_matrix()
    scene.apply_transform(np.linalg.inv(cams2world[0] @ OPENGL @ rot))
    outfile = os.path.join(outdir, 'scene.glb')
    if not silent:
        print('(exporting 3D scene to', outfile, ')')
    scene.export(file_obj=outfile)
    return outfile

@spaces.GPU(duration=120)
def get_3D_model_from_scene(outdir, silent, scene, min_conf_thr=3, as_pointcloud=False, mask_sky=False,
                            clean_depth=False, transparent_cams=False, cam_size=0.05, same_focals=False):
    """
    extract 3D_model (glb file) from a reconstructed scene
    """
    if scene is None:
        return None
    # post processes
    if clean_depth:
        scene = scene.clean_pointcloud()
    if mask_sky:
        scene = scene.mask_sky()

    # get optimized values from scene
    rgbimg = to_numpy(scene.imgs)
    focals = to_numpy(scene.get_focals().cpu())
    # cams2world = to_numpy(scene.get_im_poses().cpu())
    # TODO use the vis_poses
    cams2world = scene.vis_poses

    # 3D pointcloud from depthmap, poses and intrinsics
    # pts3d = to_numpy(scene.get_pts3d())
    # TODO use the vis_poses
    pts3d = scene.vis_pts3d
    scene.min_conf_thr = float(scene.conf_trf(torch.tensor(min_conf_thr)))
    msk = to_numpy(scene.get_masks())

    return _convert_scene_output_to_glb(outdir, rgbimg, pts3d, msk, focals, cams2world, as_pointcloud=as_pointcloud,
                                        transparent_cams=transparent_cams, cam_size=cam_size, silent=silent,
                                        same_focals=same_focals)

@spaces.GPU(duration=120)
def get_reconstructed_scene(outdir, model, device, silent, image_size, filelist, schedule, niter, min_conf_thr,
                            as_pointcloud, mask_sky, clean_depth, transparent_cams, cam_size,
                            scenegraph_type, winsize, refid, same_focals):
    """
    from a list of images, run dust3r inference, global aligner.
    then run get_3D_model_from_scene
    """
    # remove the directory if it already exists
    if os.path.exists(outdir):
        shutil.rmtree(outdir)
    os.makedirs(outdir, exist_ok=True)
    imgs, imgs_rgba = load_images(filelist, size=image_size, verbose=not silent, do_remove_background=True)
    if len(imgs) == 1:
        imgs = [imgs[0], copy.deepcopy(imgs[0])]
        imgs[1]['idx'] = 1
    if scenegraph_type == "swin":
        scenegraph_type = scenegraph_type + "-" + str(winsize)
    elif scenegraph_type == "oneref":
        scenegraph_type = scenegraph_type + "-" + str(refid)

    pairs = make_pairs(imgs, scene_graph=scenegraph_type, prefilter=None, symmetrize=True)
    output = inference(pairs, model, device, batch_size=1, verbose=not silent)

    mode = GlobalAlignerMode.PointCloudOptimizer if len(imgs) > 2 else GlobalAlignerMode.PairViewer
    scene = global_aligner(output, device=device, mode=mode, verbose=not silent, same_focals=same_focals)
    lr = 0.01

    if mode == GlobalAlignerMode.PointCloudOptimizer:
        loss = scene.compute_global_alignment(init='mst', niter=niter, schedule=schedule, lr=lr)

    # outfile = get_3D_model_from_scene(outdir, silent, scene, min_conf_thr, as_pointcloud, mask_sky,
    #                                   clean_depth, transparent_cams, cam_size, same_focals=same_focals)

    # also return rgb, depth and confidence imgs
    # depth is normalized with the max value for all images
    # we apply the jet colormap on the confidence maps
    rgbimg = scene.imgs
    # depths = to_numpy(scene.get_depthmaps())
    # confs = to_numpy([c for c in scene.im_conf])
    # cmap = pl.get_cmap('jet')
    # depths_max = max([d.max() for d in depths])
    # depths = [d / depths_max for d in depths]
    # confs_max = max([d.max() for d in confs])
    # confs = [cmap(d / confs_max) for d in confs]

    imgs = []
    rgbaimg = []
    for i in range(len(rgbimg)):   # when only 1 image, scene.imgs is two
        imgs.append(rgbimg[i])
        # imgs.append(rgb(depths[i]))
        # imgs.append(rgb(confs[i]))
        # imgs.append(imgs_rgba[i])
        if len(imgs_rgba) == 1 and i == 1:
            imgs.append(imgs_rgba[0])
            rgbaimg.append(np.array(imgs_rgba[0]))
        else:
            imgs.append(imgs_rgba[i])
            rgbaimg.append(np.array(imgs_rgba[i]))

    rgbaimg = np.array(rgbaimg)

    # for eschernet
    # get optimized values from scene
    rgbimg = to_numpy(scene.imgs)
    focals = to_numpy(scene.get_focals().cpu())
    cams2world = to_numpy(scene.get_im_poses().cpu())

    # 3D pointcloud from depthmap, poses and intrinsics
    pts3d = to_numpy(scene.get_pts3d())
    scene.min_conf_thr = float(scene.conf_trf(torch.tensor(min_conf_thr)))
    msk = to_numpy(scene.get_masks())
    obj_mask = rgbaimg[..., 3] > 0

    # TODO set global coordinate system at the center of the scene, z-axis is up
    pts = np.concatenate([p[m] for p, m in zip(pts3d, msk)]).reshape(-1, 3)
    pts_obj = np.concatenate([p[m&obj_m] for p, m, obj_m in zip(pts3d, msk, obj_mask)]).reshape(-1, 3)
    centroid = np.mean(pts_obj, axis=0) # obj center
    obj2world = np.eye(4)
    obj2world[:3, 3] = -centroid  # T_wc

    # get z_up vector
    # TODO fit a plane and get the normal vector
    pcd = o3d.geometry.PointCloud()
    pcd.points = o3d.utility.Vector3dVector(pts)
    plane_model, inliers = pcd.segment_plane(distance_threshold=0.01, ransac_n=3, num_iterations=1000)
    # get the normalised normal vector dim = 3
    normal = plane_model[:3] / np.linalg.norm(plane_model[:3])
    # the normal direction should be pointing up
    if normal[1] < 0:
        normal = -normal
    # print("normal", normal)

    # # TODO z-up 180
    # z_up = np.array([[1,0,0,0],
    #                       [0,-1,0,0],
    #                       [0,0,-1,0],
    #                       [0,0,0,1]])
    # obj2world = z_up @ obj2world

    # # avg the y
    # z_up_avg = cams2world[:,:3,3].sum(0) / np.linalg.norm(cams2world[:,:3,3].sum(0), axis=-1)    # average direction in cam coordinate
    # # import pdb; pdb.set_trace()
    # rot_axis = np.cross(np.array([0, 0, 1]), z_up_avg)
    # rot_angle = np.arccos(np.dot(np.array([0, 0, 1]), z_up_avg) / (np.linalg.norm(z_up_avg) + 1e-6))
    # rot = Rotation.from_rotvec(rot_angle * rot_axis)
    # z_up = np.eye(4)
    # z_up[:3, :3] = rot.as_matrix()

    # get the rotation matrix from normal to z-axis
    z_axis = np.array([0, 0, 1])
    rot_axis = np.cross(normal, z_axis)
    rot_angle = np.arccos(np.dot(normal, z_axis) / (np.linalg.norm(normal) + 1e-6))
    rot = Rotation.from_rotvec(rot_angle * rot_axis)
    z_up = np.eye(4)
    z_up[:3, :3] = rot.as_matrix()
    obj2world = z_up @ obj2world
    # flip 180
    flip_rot = np.array([[1, 0, 0, 0],
                         [0, -1, 0, 0],
                         [0, 0, -1, 0],
                         [0, 0, 0, 1]])
    obj2world = flip_rot @ obj2world

    # get new cams2obj
    cams2obj = []
    for i, cam2world in enumerate(cams2world):
        cams2obj.append(obj2world @ cam2world)
    # TODO transform pts3d to the new coordinate system
    for i, pts in enumerate(pts3d):
        pts3d[i] = (obj2world @ np.concatenate([pts, np.ones_like(pts)[..., :1]], axis=-1).transpose(2, 0, 1).reshape(4,
                                                                                                                      -1)) \
                       .reshape(4, pts.shape[0], pts.shape[1]).transpose(1, 2, 0)[..., :3]
    cams2world = np.array(cams2obj)
    # TODO rewrite hack
    scene.vis_poses = cams2world.copy()
    scene.vis_pts3d = pts3d.copy()

    # TODO save cams2world and rgbimg to each file, file name "000.npy", "001.npy", ... and "000.png", "001.png", ...
    for i, (img, img_rgba, pose) in enumerate(zip(rgbimg, rgbaimg, cams2world)):
        np.save(os.path.join(outdir, f"{i:03d}.npy"), pose)
        pl.imsave(os.path.join(outdir, f"{i:03d}.png"), img)
        pl.imsave(os.path.join(outdir, f"{i:03d}_rgba.png"), img_rgba)
        # np.save(os.path.join(outdir, f"{i:03d}_focal.npy"), to_numpy(focal))
    # save the min/max radius of camera
    radii = np.linalg.norm(np.linalg.inv(cams2world)[..., :3, 3])
    np.save(os.path.join(outdir, "radii.npy"), radii)

    eschernet_input = {"poses": cams2world,
                       "radii": radii,
                       "imgs": rgbaimg}

    outfile = get_3D_model_from_scene(outdir, silent, scene, min_conf_thr, as_pointcloud, mask_sky,
                                      clean_depth, transparent_cams, cam_size, same_focals=same_focals)

    return scene, outfile, imgs, eschernet_input


def set_scenegraph_options(inputfiles, winsize, refid, scenegraph_type):
    num_files = len(inputfiles) if inputfiles is not None else 1
    max_winsize = max(1, math.ceil((num_files - 1) / 2))
    if scenegraph_type == "swin":
        winsize = gr.Slider(label="Scene Graph: Window Size", value=max_winsize,
                                minimum=1, maximum=max_winsize, step=1, visible=True)
        refid = gr.Slider(label="Scene Graph: Id", value=0, minimum=0,
                              maximum=num_files - 1, step=1, visible=False)
    elif scenegraph_type == "oneref":
        winsize = gr.Slider(label="Scene Graph: Window Size", value=max_winsize,
                                minimum=1, maximum=max_winsize, step=1, visible=False)
        refid = gr.Slider(label="Scene Graph: Id", value=0, minimum=0,
                              maximum=num_files - 1, step=1, visible=True)
    else:
        winsize = gr.Slider(label="Scene Graph: Window Size", value=max_winsize,
                                minimum=1, maximum=max_winsize, step=1, visible=False)
        refid = gr.Slider(label="Scene Graph: Id", value=0, minimum=0,
                              maximum=num_files - 1, step=1, visible=False)
    return winsize, refid


def get_examples(path):
    objs = []
    for obj_name in sorted(os.listdir(path)):
        img_files = []
        for img_file in sorted(os.listdir(os.path.join(path, obj_name))):
            img_files.append(os.path.join(path, obj_name, img_file))
        objs.append([img_files])
    print("objs = ", objs)
    return objs

def preview_input(inputfiles):
    if inputfiles is None:
        return None
    imgs = []
    for img_file in inputfiles:
        img = pl.imread(img_file)
        imgs.append(img)
    return imgs

def main():
    # dustr init
    silent = False
    image_size = 224
    weights_path = 'checkpoints/DUSt3R_ViTLarge_BaseDecoder_224_linear.pth'
    model = AsymmetricCroCo3DStereo.from_pretrained(weights_path).to(device)
    # dust3r will write the 3D model inside tmpdirname
    # with tempfile.TemporaryDirectory(suffix='dust3r_gradio_demo') as tmpdirname:
    tmpdirname = os.path.join('logs/user_object')
    # remove the directory if it already exists
    if os.path.exists(tmpdirname):
        shutil.rmtree(tmpdirname)
    os.makedirs(tmpdirname, exist_ok=True)
    if not silent:
        print('Outputing stuff in', tmpdirname)

    recon_fun = functools.partial(get_reconstructed_scene, tmpdirname, model, device, silent, image_size)
    model_from_scene_fun = functools.partial(get_3D_model_from_scene, tmpdirname, silent)

    generate_mvs = functools.partial(run_eschernet, tmpdirname)

    _HEADER_ = '''
    <h2><b>[CVPR'24 Oral] EscherNet: A Generative Model for Scalable View Synthesis</b></h2>
    <b>EscherNet</b> is a multiview diffusion model for scalable generative any-to-any number/pose novel view synthesis. 
    
    Image views are treated as tokens and the camera pose is encoded by <b>CaPE (Camera Positional Encoding)</b>.
    
    <a href='https://kxhit.github.io/EscherNet' target='_blank'>Project</a> <b>|</b>
    <a href='https://github.com/kxhit/EscherNet' target='_blank'>GitHub</a> <b>|</b>
    <a href='https://arxiv.org/abs/2402.03908' target='_blank'>ArXiv</a>
    
    <h4><b>Tips:</b></h4>
    
    - Our model can take <b>any number input images</b>. The more images you provide, the better the results.
    
    - Our model can generate <b>any number and any pose</b> novel views. You can specify the number of views you want to generate. In this demo, we set novel views on an <b>archemedian spiral</b> for simplicity.
    
    - The pose estimation is done using <a href='https://github.com/naver/dust3r' target='_blank'>DUSt3R</a>. You can also provide your own poses or get pose via any SLAM system.
    
    - The current checkpoint supports 6DoF camera pose and is trained on 30k 3D <a href='https://objaverse.allenai.org/' target='_blank'>Objaverse</a> objects for demo. Scaling is on the roadmap!
    
    '''

    _CITE_ = r"""
    📝 <b>Citation</b>:
    ```bibtex
    @article{kong2024eschernet,
        title={EscherNet: A Generative Model for Scalable View Synthesis},
        author={Kong, Xin and Liu, Shikun and Lyu, Xiaoyang and Taher, Marwan and Qi, Xiaojuan and Davison, Andrew J},
        journal={arXiv preprint arXiv:2402.03908},
        year={2024}
        }
    ```
    """

    with gr.Blocks() as demo:
        gr.Markdown(_HEADER_)
        mv_images = gr.State()
        scene = gr.State(None)
        eschernet_input = gr.State(None)
        with gr.Row(variant="panel"):
            # left column
            with gr.Column():
                with gr.Row():
                    input_image = gr.File(file_count="multiple")
                # with gr.Row():
                #     # set the size of the window
                #     preview_image = gr.Gallery(label='Input Views', rows=1,
                with gr.Row():
                    run_dust3r = gr.Button("Get Pose!", elem_id="dust3r")
                with gr.Row():
                    processed_image = gr.Gallery(label='Input Views', columns=2, height="100%")
                with gr.Row(variant="panel"):
                    # input examples under "examples" folder
                    gr.Examples(
                        examples=get_examples('examples'),
                        # examples=[
                        #            [['examples/controller/frame000077.jpg', 'examples/controller/frame000032.jpg', 'examples/controller/frame000172.jpg']],
                        #            [['examples/hairdryer/frame000081.jpg', 'examples/hairdryer/frame000162.jpg', 'examples/hairdryer/frame000003.jpg']],
                        #           ],
                        inputs=[input_image],
                        label="Examples (click one set of images to start!)",
                        examples_per_page=20
                    )





            # right column
            with gr.Column():

                with gr.Row():
                    outmodel = gr.Model3D()

                with gr.Row():
                    gr.Markdown('''
                    <h4><b>Check if the pose and segmentation looks correct. If not, remove the incorrect images and try again.</b></h4>
                    ''')

                with gr.Row():
                    with gr.Group():
                        do_remove_background = gr.Checkbox(
                            label="Remove Background", value=True
                        )
                        sample_seed = gr.Number(value=42, label="Seed Value", precision=0)

                        sample_steps = gr.Slider(
                            label="Sample Steps",
                            minimum=30,
                            maximum=75,
                            value=50,
                            step=5,
                            visible=False
                        )

                        nvs_num = gr.Slider(
                            label="Number of Novel Views",
                            minimum=5,
                            maximum=100,
                            value=30,
                            step=1
                        )

                        nvs_mode = gr.Dropdown(["archimedes circle"],   # "fixed 4 views", "fixed 8 views"
                                           value="archimedes circle", label="Novel Views Pose Chosen", visible=True)

                with gr.Row():
                    gr.Markdown('''
                    <h4><b>Choose your desired novel view poses number and generate! The more output images the longer it takes.</b></h4>
                    ''')

                with gr.Row():
                    submit = gr.Button("Submit", elem_id="eschernet", variant="primary")

                with gr.Row():
                    # mv_show_images = gr.Image(
                    #     label="Generated Multi-views",
                    #     type="pil",
                    #     width=379,
                    #     interactive=False
                    # )
                    with gr.Column():
                        output_video = gr.Video(
                            label="video", format="mp4",
                            width=379,
                            autoplay=True,
                            interactive=False
                        )

                # with gr.Row():
                #     with gr.Tab("OBJ"):
                #         output_model_obj = gr.Model3D(
                #             label="Output Model (OBJ Format)",
                #             #width=768,
                #             interactive=False,
                #         )
                #         gr.Markdown("Note: Downloaded .obj model will be flipped. Export .glb instead or manually flip it before usage.")
                #     with gr.Tab("GLB"):
                #         output_model_glb = gr.Model3D(
                #             label="Output Model (GLB Format)",
                #             #width=768,
                #             interactive=False,
                #         )
                #         gr.Markdown("Note: The model shown here has a darker appearance. Download to get correct results.")

                with gr.Row():
                    gr.Markdown('''The novel views are generated on an archimedean spiral. You can download the video''')

        gr.Markdown(_CITE_)

        # set dust3r parameter invisible to be clean
        with gr.Column():
            with gr.Row():
                schedule = gr.Dropdown(["linear", "cosine"],
                                           value='linear', label="schedule", info="For global alignment!", visible=False)
                niter = gr.Number(value=300, precision=0, minimum=0, maximum=5000,
                                      label="num_iterations", info="For global alignment!", visible=False)
                scenegraph_type = gr.Dropdown(["complete", "swin", "oneref"],
                                                  value='complete', label="Scenegraph",
                                                  info="Define how to make pairs",
                                                  interactive=True, visible=False)
                same_focals = gr.Checkbox(value=True, label="Focal", info="Use the same focal for all cameras", visible=False)
                winsize = gr.Slider(label="Scene Graph: Window Size", value=1,
                                        minimum=1, maximum=1, step=1, visible=False)
                refid = gr.Slider(label="Scene Graph: Id", value=0, minimum=0, maximum=0, step=1, visible=False)

            with gr.Row():
                # adjust the confidence threshold
                min_conf_thr = gr.Slider(label="min_conf_thr", value=3.0, minimum=1.0, maximum=20, step=0.1, visible=False)
                # adjust the camera size in the output pointcloud
                cam_size = gr.Slider(label="cam_size", value=0.05, minimum=0.01, maximum=0.5, step=0.001, visible=False)
            with gr.Row():
                as_pointcloud = gr.Checkbox(value=False, label="As pointcloud", visible=False)
                # two post process implemented
                mask_sky = gr.Checkbox(value=False, label="Mask sky", visible=False)
                clean_depth = gr.Checkbox(value=True, label="Clean-up depthmaps", visible=False)
                transparent_cams = gr.Checkbox(value=False, label="Transparent cameras", visible=False)

            # events
            # scenegraph_type.change(set_scenegraph_options,
            #                        inputs=[input_image, winsize, refid, scenegraph_type],
            #                        outputs=[winsize, refid])
            input_image.change(set_scenegraph_options,
                              inputs=[input_image, winsize, refid, scenegraph_type],
                              outputs=[winsize, refid])
            # min_conf_thr.release(fn=model_from_scene_fun,
            #                      inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,
            #                              clean_depth, transparent_cams, cam_size, same_focals],
            #                      outputs=outmodel)
            # cam_size.change(fn=model_from_scene_fun,
            #                 inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,
            #                         clean_depth, transparent_cams, cam_size, same_focals],
            #                 outputs=outmodel)
            # as_pointcloud.change(fn=model_from_scene_fun,
            #                      inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,
            #                              clean_depth, transparent_cams, cam_size, same_focals],
            #                      outputs=outmodel)
            # mask_sky.change(fn=model_from_scene_fun,
            #                 inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,
            #                         clean_depth, transparent_cams, cam_size, same_focals],
            #                 outputs=outmodel)
            # clean_depth.change(fn=model_from_scene_fun,
            #                    inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,
            #                            clean_depth, transparent_cams, cam_size, same_focals],
            #                    outputs=outmodel)
            # transparent_cams.change(model_from_scene_fun,
            #                         inputs=[scene, min_conf_thr, as_pointcloud, mask_sky,
            #                                 clean_depth, transparent_cams, cam_size, same_focals],
            #                         outputs=outmodel)
            run_dust3r.click(fn=recon_fun,
                          inputs=[input_image, schedule, niter, min_conf_thr, as_pointcloud,
                                  mask_sky, clean_depth, transparent_cams, cam_size,
                                  scenegraph_type, winsize, refid, same_focals],
                          outputs=[scene, outmodel, processed_image, eschernet_input])


        # events
        # preview images on input change
        input_image.change(fn=preview_input,
                           inputs=[input_image],
                           outputs=[processed_image])

        submit.click(fn=generate_mvs,
            inputs=[eschernet_input, sample_steps, sample_seed,
                    nvs_num, nvs_mode],
            outputs=[mv_images, output_video],
        )#.success(
        # #     fn=make3d,
        # #     inputs=[mv_images],
        # #     outputs=[output_video, output_model_obj, output_model_glb]
        # # )



    demo.queue(max_size=10)
    demo.launch(share=True, server_name="0.0.0.0", server_port=None)

if __name__ == '__main__':
    main()