bartduis commited on
Commit
b3d1630
·
verified ·
1 Parent(s): b78737e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -60
app.py CHANGED
@@ -17,10 +17,10 @@ import trimesh
17
  from moge.model.v1 import MoGeModel
18
  from utils.geometry import compute_pointmap
19
  import cv2
20
- # from huggingface_hub import hf_hub_download
21
- # from PIL import Image
22
- # import matplotlib.pyplot as plt
23
- # from eval_wrapper.eval import EvalWrapper, eval_scene
24
 
25
 
26
  device = 'cuda' if torch.cuda.is_available() else 'cpu'
@@ -28,21 +28,27 @@ device = 'cuda' if torch.cuda.is_available() else 'cpu'
28
  outdir = "/tmp/rayst3r"
29
 
30
  moge_model = MoGeModel.from_pretrained("Ruicheng/moge-vitl").to(device)
 
 
 
31
 
32
-
33
- # # loading all necessary models
 
 
 
34
 
35
  # print("Loading MoGe model")
36
  # # Load the model from huggingface hub (or load from local).
37
 
38
 
39
- # def depth2uint16(depth):
40
- # return depth * torch.iinfo(torch.uint16).max / 10.0 # threshold is in m, convert to uint16 value
41
 
42
- # def save_tensor_as_png(tensor: torch.Tensor, path: str, dtype: torch.dtype | None = None):
43
- # if dtype is None:
44
- # dtype = tensor.dtype
45
- # Image.fromarray(tensor.to(dtype).cpu().numpy()).save(path)
46
 
47
  # def colorize_points_with_turbo_all_dims(points, method='norm',cmap='turbo'):
48
  # """
@@ -78,62 +84,56 @@ moge_model = MoGeModel.from_pretrained("Ruicheng/moge-vitl").to(device)
78
 
79
  # return colors
80
 
81
- # def prep_for_rayst3r(img,depth_dict,mask):
82
- # H, W = img.shape[:2]
83
- # intrinsics = depth_dict["intrinsics"].detach().cpu()
84
- # intrinsics[0] *= W
85
- # intrinsics[1] *= H
86
-
87
- # input_dir = os.path.join(outdir, "input")
88
- # if os.path.exists(input_dir):
89
- # shutil.rmtree(input_dir)
90
- # os.makedirs(input_dir, exist_ok=True)
91
- # # save intrinsics
92
- # torch.save(intrinsics, os.path.join(input_dir, "intrinsics.pt"))
93
-
94
- # # save depth
95
- # depth = depth_dict["depth"].cpu()
96
- # depth = depth2uint16(depth)
97
- # save_tensor_as_png(depth, os.path.join(input_dir, "depth.png"),dtype=torch.uint16)
98
-
99
- # # save mask as bool
100
- # save_tensor_as_png(torch.from_numpy(mask).bool(), os.path.join(input_dir, "mask.png"),dtype=torch.bool)
101
- # # save image
102
- # save_tensor_as_png(torch.from_numpy(img), os.path.join(input_dir, "rgb.png"))
103
-
104
- # @GPU(duration = 180)
105
- # def rayst3r_to_glb(img,depth_dict,mask,max_total_points=10e6,rotated=False):
106
- # prep_for_rayst3r(img,depth_dict,mask)
 
107
 
108
- # dino_model = torch.hub.load('facebookresearch/dinov2', "dinov2_vitl14_reg")
109
- # dino_model.eval()
110
- # dino_model.to(device)
111
 
112
- # print("Loading RaySt3R model")
113
- # rayst3r_checkpoint = hf_hub_download("bartduis/rayst3r", "rayst3r.pth")
114
- # rayst3r_model = EvalWrapper(rayst3r_checkpoint,device='cpu')
115
- # rayst3r_model = rayst3r_model.to(device)
116
 
117
- # rayst3r_points = eval_scene(rayst3r_model,os.path.join(outdir, "input"),do_filter_all_masks=True,dino_model=dino_model, device = device).cpu()
118
 
119
- # # subsample points
120
- # n_points = min(max_total_points,rayst3r_points.shape[0])
121
- # rayst3r_points = rayst3r_points[torch.randperm(rayst3r_points.shape[0])[:n_points]].numpy()
122
 
123
- # rayst3r_points[:,1] = -rayst3r_points[:,1]
124
- # rayst3r_points[:,2] = -rayst3r_points[:,2]
125
 
126
- # # make all points red
127
- # colors = colorize_points_with_turbo_all_dims(rayst3r_points)
128
 
129
- # # load the input glb
130
- # scene = trimesh.Scene()
131
- # pct = trimesh.PointCloud(rayst3r_points, colors=colors, radius=0.01)
132
- # scene.add_geometry(pct)
133
 
134
- # outfile = os.path.join(outdir, "rayst3r.glb")
135
- # scene.export(outfile)
136
- # return outfile
137
 
138
 
139
  def input_to_glb(outdir,img,depth_dict,mask,rotated=False):
@@ -202,6 +202,7 @@ def process_image(input_img):
202
  shutil.rmtree(outdir)
203
  os.makedirs(outdir)
204
  input_glb = input_to_glb(outdir,input_img,depth_dict,mask,rotated=rotated)
 
205
  print(input_glb)
206
  return input_img, input_img
207
 
 
17
  from moge.model.v1 import MoGeModel
18
  from utils.geometry import compute_pointmap
19
  import cv2
20
+ from huggingface_hub import hf_hub_download
21
+ from PIL import Image
22
+ import matplotlib.pyplot as plt
23
+ from eval_wrapper.eval import EvalWrapper, eval_scene
24
 
25
 
26
  device = 'cuda' if torch.cuda.is_available() else 'cpu'
 
28
  outdir = "/tmp/rayst3r"
29
 
30
  moge_model = MoGeModel.from_pretrained("Ruicheng/moge-vitl").to(device)
31
+ dino_model = torch.hub.load('facebookresearch/dinov2', "dinov2_vitl14_reg")
32
+ dino_model.eval()
33
+ dino_model.to(device)
34
 
35
+ print("Loading RaySt3R model")
36
+ rayst3r_checkpoint = hf_hub_download("bartduis/rayst3r", "rayst3r.pth")
37
+ rayst3r_model = EvalWrapper(rayst3r_checkpoint,device='cpu')
38
+ rayst3r_model = rayst3r_model.to(device)
39
+ print("Loaded all models")
40
 
41
  # print("Loading MoGe model")
42
  # # Load the model from huggingface hub (or load from local).
43
 
44
 
45
+ def depth2uint16(depth):
46
+ return depth * torch.iinfo(torch.uint16).max / 10.0 # threshold is in m, convert to uint16 value
47
 
48
+ def save_tensor_as_png(tensor: torch.Tensor, path: str, dtype: torch.dtype | None = None):
49
+ if dtype is None:
50
+ dtype = tensor.dtype
51
+ Image.fromarray(tensor.to(dtype).cpu().numpy()).save(path)
52
 
53
  # def colorize_points_with_turbo_all_dims(points, method='norm',cmap='turbo'):
54
  # """
 
84
 
85
  # return colors
86
 
87
+ def prep_for_rayst3r(img,depth_dict,mask):
88
+ H, W = img.shape[:2]
89
+ intrinsics = depth_dict["intrinsics"].detach().cpu()
90
+ intrinsics[0] *= W
91
+ intrinsics[1] *= H
92
+
93
+ input_dir = os.path.join(outdir, "input")
94
+ if os.path.exists(input_dir):
95
+ shutil.rmtree(input_dir)
96
+ os.makedirs(input_dir, exist_ok=True)
97
+ # save intrinsics
98
+ torch.save(intrinsics, os.path.join(input_dir, "intrinsics.pt"))
99
+
100
+ # save depth
101
+ depth = depth_dict["depth"].cpu()
102
+ depth = depth2uint16(depth)
103
+ save_tensor_as_png(depth, os.path.join(input_dir, "depth.png"),dtype=torch.uint16)
104
+
105
+ # save mask as bool
106
+ save_tensor_as_png(torch.from_numpy(mask).bool(), os.path.join(input_dir, "mask.png"),dtype=torch.bool)
107
+ # save image
108
+ save_tensor_as_png(torch.from_numpy(img), os.path.join(input_dir, "rgb.png"))
109
+
110
+ @GPU(duration = 180)
111
+ def rayst3r_to_glb(img,depth_dict,mask,max_total_points=10e6,rotated=False):
112
+ prep_for_rayst3r(img,depth_dict,mask)
113
+ print('Doneneee')
114
 
 
 
 
115
 
 
 
 
 
116
 
117
+ # rayst3r_points = eval_scene(rayst3r_model,os.path.join(outdir, "input"),do_filter_all_masks=True,dino_model=dino_model, device = device).cpu()
118
 
119
+ # # subsample points
120
+ # n_points = min(max_total_points,rayst3r_points.shape[0])
121
+ # rayst3r_points = rayst3r_points[torch.randperm(rayst3r_points.shape[0])[:n_points]].numpy()
122
 
123
+ # rayst3r_points[:,1] = -rayst3r_points[:,1]
124
+ # rayst3r_points[:,2] = -rayst3r_points[:,2]
125
 
126
+ # # make all points red
127
+ # colors = colorize_points_with_turbo_all_dims(rayst3r_points)
128
 
129
+ # # load the input glb
130
+ # scene = trimesh.Scene()
131
+ # pct = trimesh.PointCloud(rayst3r_points, colors=colors, radius=0.01)
132
+ # scene.add_geometry(pct)
133
 
134
+ # outfile = os.path.join(outdir, "rayst3r.glb")
135
+ # scene.export(outfile)
136
+ # return outfile
137
 
138
 
139
  def input_to_glb(outdir,img,depth_dict,mask,rotated=False):
 
202
  shutil.rmtree(outdir)
203
  os.makedirs(outdir)
204
  input_glb = input_to_glb(outdir,input_img,depth_dict,mask,rotated=rotated)
205
+ inference_glb = rayst3r_to_glb(input_img,depth_dict,mask,rotated=rotated)
206
  print(input_glb)
207
  return input_img, input_img
208