freealise commited on
Commit
581f136
1 Parent(s): 08cb08c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -46
app.py CHANGED
@@ -8,7 +8,7 @@ import torch
8
  import torch.nn.functional as F
9
  from torchvision import transforms
10
  from torchvision.transforms import Compose
11
- import trimesh
12
  #from geometry import create_triangles
13
  import tempfile
14
  from functools import partial
@@ -136,7 +136,7 @@ def make_video(video_path, outdir='./vis_video_depth', encoder='vits'):
136
  break
137
 
138
  frame = cv2.cvtColor(raw_frame, cv2.COLOR_BGR2RGB) / 255.0
139
- frame_pil = Image.fromarray((frame * 255).astype(np.uint8))
140
  frame = transform({'image': frame})['image']
141
 
142
  frame = torch.from_numpy(frame).unsqueeze(0).to(DEVICE)
@@ -322,9 +322,23 @@ def get_mesh(image, depth, blur_data, loadall):
322
  #if (clrs[j] == pcolors[i]).all():
323
  #cverts[i].append(verts[j])
324
  #j=j+1
325
- mesh.append(trimesh.PointCloud(verts, colors=clrs))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
326
  print(mesh[len(mesh)-1])
327
- mesh[len(mesh)-1].merge_vertices()
328
  #i=i+1
329
  mesh_n.append(str(fnum))
330
 
@@ -362,7 +376,7 @@ def get_mesh(image, depth, blur_data, loadall):
362
  #texture = trimesh.visual.TextureVisuals(uv=uv, image=image, material=material)
363
  #mesh.visual = texture
364
 
365
- scene = trimesh.Scene(mesh)
366
  print('mesh - ok')
367
 
368
  # Save as glb
@@ -370,7 +384,7 @@ def get_mesh(image, depth, blur_data, loadall):
370
  #f = open(glb_file.name, "wb")
371
  #f.write(binary)
372
  #f.close()
373
- scene.export(glb_file.name)
374
  print('file - ok')
375
  return glb_file.name, glb_file.name, ",".join(mesh_n)
376
 
@@ -613,40 +627,6 @@ async(f, c, o, b, p, d, n)=>{
613
 
614
  on_click="""
615
  async(p, d, n)=>{
616
- function decycle(obj, stack = []) {
617
- if (!obj || typeof obj !== 'object')
618
- return obj;
619
-
620
- if (stack.includes(obj))
621
- return null;
622
-
623
- let s = stack.concat([obj]);
624
-
625
- return Array.isArray(obj)
626
- ? obj.map(x => decycle(x, s))
627
- : Object.fromEntries(
628
- Object.entries(obj)
629
- .map(([k, v]) => [k, decycle(v, s)]));
630
- }
631
- var objectUrl;
632
- function download3D(filename, scene) {
633
- if(objectUrl) {
634
- window.URL.revokeObjectURL(objectUrl);
635
- }
636
- var serializedScene = BABYLON.SceneSerializer.Serialize(scene);
637
- var strMesh = JSON.stringify(decycle(serializedScene));
638
- if (filename.toLowerCase().lastIndexOf(".babylon") !== filename.length - 8 || filename.length < 9){
639
- filename += ".babylon";
640
- }
641
- var blob = new Blob ( [ strMesh ], { type : "octet/stream" } );
642
-
643
- // turn blob into an object URL; saved as a member, so can be cleaned out later
644
- objectUrl = (window.webkitURL || window.URL).createObjectURL(blob);
645
-
646
- document.getElementById("file3D").getElementsByTagName("a")[0].download = filename;
647
- document.getElementById("file3D").getElementsByTagName("a")[0].innerText = blob.size / 1024 + " KB";
648
- document.getElementById("file3D").getElementsByTagName("a")[0].href = objectUrl;
649
- }
650
  var intv = setInterval(function(){
651
  if (document.getElementById("iframe3D")===null || typeof document.getElementById("iframe3D")==="undefined") {
652
  if (BABYLON && BABYLON.Engine.LastCreatedScene) {
@@ -709,10 +689,6 @@ async(p, d, n)=>{
709
  mesh.dispose(false, true);
710
  }
711
  BABYLON.Engine.LastCreatedScene.getMeshByName("dome_mesh").applyDisplacementMap(d.background.url, 0, 255, function(m){try{alert(BABYLON.Engine.Version);}catch(e){alert(e);}}, null, null, true, function(e){alert(e);});
712
-
713
- try {
714
- download3D("filename", BABYLON.Engine.LastCreatedScene);
715
- } catch (e){alert(e);}
716
 
717
  clearInterval(intv);
718
  }
@@ -745,8 +721,7 @@ css = """
745
 
746
  title = "# Depth Anything Video Demo"
747
  description = """Depth Anything on full video files.
748
- Please refer to our [paper](https://arxiv.org/abs/2401.10891), [project page](https://depth-anything.github.io), or [github](https://github.com/LiheYoung/Depth-Anything) for more details.
749
- Mesh rendering from [ZoeDepth](https://huggingface.co/spaces/shariqfarooq/ZoeDepth) ([github](https://github.com/isl-org/ZoeDepth/tree/main/ui))."""
750
 
751
  transform = Compose([
752
  Resize(
 
8
  import torch.nn.functional as F
9
  from torchvision import transforms
10
  from torchvision.transforms import Compose
11
+ import open3d as o3d
12
  #from geometry import create_triangles
13
  import tempfile
14
  from functools import partial
 
136
  break
137
 
138
  frame = cv2.cvtColor(raw_frame, cv2.COLOR_BGR2RGB) / 255.0
139
+ frame_pil = Image.fromarray((frame * 255).astype(np.uint8))
140
  frame = transform({'image': frame})['image']
141
 
142
  frame = torch.from_numpy(frame).unsqueeze(0).to(DEVICE)
 
322
  #if (clrs[j] == pcolors[i]).all():
323
  #cverts[i].append(verts[j])
324
  #j=j+1
325
+
326
+ depth_o3d = o3d.geometry.Image(Image.fromarray(gdepth.astype(np.uint8)))
327
+ image_o3d = o3d.geometry.Image(Image.fromarray(rgba.astype(np.uint8)))
328
+ rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
329
+ image_o3d, depth_o3d, convert_rgb_to_intensity=False)
330
+ w = int(depth_image.shape[1])
331
+ h = int(depth_image.shape[0])
332
+
333
+ camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
334
+ camera_intrinsic.set_intrinsics(w, h, 500, 500, w/2, h/2)
335
+
336
+ pcd = o3d.geometry.PointCloud.create_from_rgbd_image(
337
+ rgbd_image, camera_intrinsic)
338
+
339
+ mesh.append(pcd)
340
  print(mesh[len(mesh)-1])
341
+ #mesh[len(mesh)-1].merge_vertices()
342
  #i=i+1
343
  mesh_n.append(str(fnum))
344
 
 
376
  #texture = trimesh.visual.TextureVisuals(uv=uv, image=image, material=material)
377
  #mesh.visual = texture
378
 
379
+ #scene = trimesh.Scene(mesh)
380
  print('mesh - ok')
381
 
382
  # Save as glb
 
384
  #f = open(glb_file.name, "wb")
385
  #f.write(binary)
386
  #f.close()
387
+ o3d.io.write_point_cloud(glb_file.name, pcd)
388
  print('file - ok')
389
  return glb_file.name, glb_file.name, ",".join(mesh_n)
390
 
 
627
 
628
  on_click="""
629
  async(p, d, n)=>{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
630
  var intv = setInterval(function(){
631
  if (document.getElementById("iframe3D")===null || typeof document.getElementById("iframe3D")==="undefined") {
632
  if (BABYLON && BABYLON.Engine.LastCreatedScene) {
 
689
  mesh.dispose(false, true);
690
  }
691
  BABYLON.Engine.LastCreatedScene.getMeshByName("dome_mesh").applyDisplacementMap(d.background.url, 0, 255, function(m){try{alert(BABYLON.Engine.Version);}catch(e){alert(e);}}, null, null, true, function(e){alert(e);});
 
 
 
 
692
 
693
  clearInterval(intv);
694
  }
 
721
 
722
  title = "# Depth Anything Video Demo"
723
  description = """Depth Anything on full video files.
724
+ Please refer to our [paper](https://arxiv.org/abs/2401.10891), [project page](https://depth-anything.github.io), or [github](https://github.com/LiheYoung/Depth-Anything) for more details."""
 
725
 
726
  transform = Compose([
727
  Resize(