Commit 
							
							·
						
						0d7318b
	
0
								Parent(s):
							
							
Initial commit
Browse filesThis view is limited to 50 files because it contains too many changes.  
							See raw diff
- .gitattributes +29 -0
- README.md +18 -0
- app.py +108 -0
- best_model.pth +3 -0
- examples/example_1.jpg +3 -0
- examples/example_10.jpg +3 -0
- examples/example_100.jpg +3 -0
- examples/example_11.jpg +3 -0
- examples/example_12.jpg +3 -0
- examples/example_13.jpg +3 -0
- examples/example_14.jpg +3 -0
- examples/example_15.jpg +3 -0
- examples/example_16.jpg +3 -0
- examples/example_17.jpg +3 -0
- examples/example_18.jpg +3 -0
- examples/example_19.jpg +3 -0
- examples/example_2.jpg +3 -0
- examples/example_20.jpg +3 -0
- examples/example_21.jpg +3 -0
- examples/example_22.jpg +3 -0
- examples/example_23.jpg +3 -0
- examples/example_24.jpg +3 -0
- examples/example_25.jpg +3 -0
- examples/example_26.jpg +3 -0
- examples/example_27.jpg +3 -0
- examples/example_28.jpg +3 -0
- examples/example_29.jpg +3 -0
- examples/example_3.jpg +3 -0
- examples/example_30.jpg +3 -0
- examples/example_31.jpg +3 -0
- examples/example_32.jpg +3 -0
- examples/example_33.jpg +3 -0
- examples/example_34.jpg +3 -0
- examples/example_35.jpg +3 -0
- examples/example_36.jpg +3 -0
- examples/example_37.jpg +3 -0
- examples/example_38.jpg +3 -0
- examples/example_39.jpg +3 -0
- examples/example_4.jpg +3 -0
- examples/example_40.jpg +3 -0
- examples/example_41.jpg +3 -0
- examples/example_42.jpg +3 -0
- examples/example_43.jpg +3 -0
- examples/example_44.jpg +3 -0
- examples/example_45.jpg +3 -0
- examples/example_46.jpg +3 -0
- examples/example_47.jpg +3 -0
- examples/example_48.jpg +3 -0
- examples/example_49.jpg +3 -0
- examples/example_5.jpg +3 -0
    	
        .gitattributes
    ADDED
    
    | @@ -0,0 +1,29 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            *.7z filter=lfs diff=lfs merge=lfs -text
         | 
| 2 | 
            +
            *.arrow filter=lfs diff=lfs merge=lfs -text
         | 
| 3 | 
            +
            *.bin filter=lfs diff=lfs merge=lfs -text
         | 
| 4 | 
            +
            *.bz2 filter=lfs diff=lfs merge=lfs -text
         | 
| 5 | 
            +
            *.ckpt filter=lfs diff=lfs merge=lfs -text
         | 
| 6 | 
            +
            *.ftz filter=lfs diff=lfs merge=lfs -text
         | 
| 7 | 
            +
            *.gz filter=lfs diff=lfs merge=lfs -text
         | 
| 8 | 
            +
            *.h5 filter=lfs diff=lfs merge=lfs -text
         | 
| 9 | 
            +
            *.joblib filter=lfs diff=lfs merge=lfs -text
         | 
| 10 | 
            +
            *.lfs.* filter=lfs diff=lfs merge=lfs -text
         | 
| 11 | 
            +
            *.model filter=lfs diff=lfs merge=lfs -text
         | 
| 12 | 
            +
            *.msgpack filter=lfs diff=lfs merge=lfs -text
         | 
| 13 | 
            +
            *.onnx filter=lfs diff=lfs merge=lfs -text
         | 
| 14 | 
            +
            *.ot filter=lfs diff=lfs merge=lfs -text
         | 
| 15 | 
            +
            *.parquet filter=lfs diff=lfs merge=lfs -text
         | 
| 16 | 
            +
            *.pb filter=lfs diff=lfs merge=lfs -text
         | 
| 17 | 
            +
            *.pt filter=lfs diff=lfs merge=lfs -text
         | 
| 18 | 
            +
            *.pth filter=lfs diff=lfs merge=lfs -text
         | 
| 19 | 
            +
            *.rar filter=lfs diff=lfs merge=lfs -text
         | 
| 20 | 
            +
            saved_model/**/* filter=lfs diff=lfs merge=lfs -text
         | 
| 21 | 
            +
            *.tar.* filter=lfs diff=lfs merge=lfs -text
         | 
| 22 | 
            +
            *.tflite filter=lfs diff=lfs merge=lfs -text
         | 
| 23 | 
            +
            *.tgz filter=lfs diff=lfs merge=lfs -text
         | 
| 24 | 
            +
            *.wasm filter=lfs diff=lfs merge=lfs -text
         | 
| 25 | 
            +
            *.xz filter=lfs diff=lfs merge=lfs -text
         | 
| 26 | 
            +
            *.zip filter=lfs diff=lfs merge=lfs -text
         | 
| 27 | 
            +
            *.zstandard filter=lfs diff=lfs merge=lfs -text
         | 
| 28 | 
            +
            *tfevents* filter=lfs diff=lfs merge=lfs -text
         | 
| 29 | 
            +
            *.jpg filter=lfs diff=lfs merge=lfs -text
         | 
    	
        README.md
    ADDED
    
    | @@ -0,0 +1,18 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            ---
         | 
| 2 | 
            +
            title: Mars DTM Estimation
         | 
| 3 | 
            +
            emoji: ⚡
         | 
| 4 | 
            +
            colorFrom: yellow
         | 
| 5 | 
            +
            colorTo: purple
         | 
| 6 | 
            +
            python_version: 3.11
         | 
| 7 | 
            +
            sdk: gradio
         | 
| 8 | 
            +
            sdk_version: 4.36.1
         | 
| 9 | 
            +
            app_file: app.py
         | 
| 10 | 
            +
            pinned: false
         | 
| 11 | 
            +
            ---
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            ```
         | 
| 16 | 
            +
            pip install -r requirements.txt
         | 
| 17 | 
            +
            python3.11 app.py
         | 
| 18 | 
            +
            ```
         | 
    	
        app.py
    ADDED
    
    | @@ -0,0 +1,108 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            import os
         | 
| 2 | 
            +
            from pathlib import Path
         | 
| 3 | 
            +
            import gradio as gr
         | 
| 4 | 
            +
            import matplotlib.pyplot as plt
         | 
| 5 | 
            +
            import numpy as np
         | 
| 6 | 
            +
            from PIL import Image
         | 
| 7 | 
            +
            import torch
         | 
| 8 | 
            +
            from torchvision import transforms
         | 
| 9 | 
            +
            from model.model import GLPDepth
         | 
| 10 | 
            +
            import open3d as o3d
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
         | 
| 13 | 
            +
            W, H = 512, 512
         | 
| 14 | 
            +
             | 
| 15 | 
            +
             | 
| 16 | 
            +
            def load_model(path):
         | 
| 17 | 
            +
                model = GLPDepth(max_depth=700.0).to(device)
         | 
| 18 | 
            +
                weights = torch.load(path, map_location=torch.device('cpu'))
         | 
| 19 | 
            +
                model.load_state_dict(weights)
         | 
| 20 | 
            +
                model.eval()
         | 
| 21 | 
            +
                return model
         | 
| 22 | 
            +
             | 
| 23 | 
            +
             | 
| 24 | 
            +
            def generate_mesh(dtm, image, image_path):
         | 
| 25 | 
            +
                # prepare points
         | 
| 26 | 
            +
                points = np.zeros(shape=(W * H, 3), dtype='float32')
         | 
| 27 | 
            +
                colors = np.zeros(shape=(W * H, 3), dtype='float32')
         | 
| 28 | 
            +
                for i in range(H):
         | 
| 29 | 
            +
                    for j in range(W):
         | 
| 30 | 
            +
                        points[i * H + j, 0] = j
         | 
| 31 | 
            +
                        points[i * H + j, 1] = i
         | 
| 32 | 
            +
                        points[i * H + j, 2] = dtm[i, j]
         | 
| 33 | 
            +
                        colors[i * H + j, :3] = image[i, j]
         | 
| 34 | 
            +
                # point cloud
         | 
| 35 | 
            +
                pcd = o3d.geometry.PointCloud()
         | 
| 36 | 
            +
                pcd.points = o3d.utility.Vector3dVector(points)
         | 
| 37 | 
            +
                pcd.colors = o3d.utility.Vector3dVector(colors)
         | 
| 38 | 
            +
                # normals
         | 
| 39 | 
            +
                pcd.estimate_normals()
         | 
| 40 | 
            +
                pcd.orient_normals_to_align_with_direction()
         | 
| 41 | 
            +
                # surface reconstruction
         | 
| 42 | 
            +
                mesh = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(pcd, depth=7, n_threads=1)[0]
         | 
| 43 | 
            +
                # mesh transformations
         | 
| 44 | 
            +
                rotation = mesh.get_rotation_matrix_from_xyz((np.pi, 0, 0))
         | 
| 45 | 
            +
                mesh.rotate(rotation, center=(0, 0, 0))
         | 
| 46 | 
            +
                mesh.compute_vertex_normals()
         | 
| 47 | 
            +
                # remove weird artifacts
         | 
| 48 | 
            +
                mesh.remove_degenerate_triangles()
         | 
| 49 | 
            +
                mesh.remove_duplicated_triangles()
         | 
| 50 | 
            +
                mesh.remove_duplicated_vertices()
         | 
| 51 | 
            +
                mesh.remove_non_manifold_edges()
         | 
| 52 | 
            +
                # save mesh
         | 
| 53 | 
            +
                out_path = f'./{image_path.stem}.obj'
         | 
| 54 | 
            +
                o3d.io.write_triangle_mesh(out_path, mesh)
         | 
| 55 | 
            +
                return out_path
         | 
| 56 | 
            +
             | 
| 57 | 
            +
             | 
| 58 | 
            +
            def predict(image_path):
         | 
| 59 | 
            +
                image_path = Path(image_path)
         | 
| 60 | 
            +
                pil_image = Image.open(image_path).convert('L')
         | 
| 61 | 
            +
                # transform image to torch
         | 
| 62 | 
            +
                to_tensor = transforms.ToTensor()
         | 
| 63 | 
            +
                torch_image = to_tensor(pil_image).to(device).unsqueeze(0)
         | 
| 64 | 
            +
                # model predict
         | 
| 65 | 
            +
                with torch.no_grad():
         | 
| 66 | 
            +
                    pred_dtm = model(torch_image)
         | 
| 67 | 
            +
                # transform torch to numpy
         | 
| 68 | 
            +
                pred_dtm = pred_dtm.squeeze().cpu().detach().numpy()
         | 
| 69 | 
            +
                pred_dtm = pred_dtm.max() - pred_dtm
         | 
| 70 | 
            +
                # create 3d model
         | 
| 71 | 
            +
                image_scaled = np.asarray(pil_image) / 255.0
         | 
| 72 | 
            +
                obj_path = generate_mesh(pred_dtm, image_scaled, image_path)
         | 
| 73 | 
            +
             | 
| 74 | 
            +
                # return correct image
         | 
| 75 | 
            +
                fig, ax = plt.subplots()
         | 
| 76 | 
            +
                im = ax.imshow(pred_dtm, cmap='jet', vmin=0, vmax=np.max(pred_dtm))
         | 
| 77 | 
            +
                plt.colorbar(im, ax=ax)
         | 
| 78 | 
            +
             | 
| 79 | 
            +
                fig.canvas.draw()
         | 
| 80 | 
            +
                data = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
         | 
| 81 | 
            +
                data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
         | 
| 82 | 
            +
             | 
| 83 | 
            +
                return [data, obj_path]
         | 
| 84 | 
            +
             | 
| 85 | 
            +
             | 
| 86 | 
            +
            model = load_model('best_model.pth')
         | 
| 87 | 
            +
             | 
| 88 | 
            +
            title = 'Mars DTM Estimation'
         | 
| 89 | 
            +
            description = 'This demo predicts a DTM from an image of the martian surface. Then, by using a surface reconstruction ' \
         | 
| 90 | 
            +
                          'algorithm, the 3D model is generated and it can also be downloaded. Uploaded images must have a ' \
         | 
| 91 | 
            +
                          '<i>1m/px</i> resolution to get predictions in the correct range. You can download the mesh by clicking ' \
         | 
| 92 | 
            +
                          'the top-right button on the 3D viewer. '
         | 
| 93 | 
            +
            examples = [f'examples/{name}' for name in sorted(os.listdir('examples'))]
         | 
| 94 | 
            +
             | 
| 95 | 
            +
            iface = gr.Interface(
         | 
| 96 | 
            +
                fn=predict,
         | 
| 97 | 
            +
                inputs=gr.Image(type='filepath', label='Input Image', sources=['upload', 'clipboard']),
         | 
| 98 | 
            +
                outputs=[
         | 
| 99 | 
            +
                    gr.Image(label='DTM'),
         | 
| 100 | 
            +
                    gr.Model3D(label='3D Model', clear_color=[0.0, 0.0, 0.0, 0.0])
         | 
| 101 | 
            +
                ],
         | 
| 102 | 
            +
                examples=examples,
         | 
| 103 | 
            +
                allow_flagging='never',
         | 
| 104 | 
            +
                cache_examples=False,
         | 
| 105 | 
            +
                title=title,
         | 
| 106 | 
            +
                description=description
         | 
| 107 | 
            +
            )
         | 
| 108 | 
            +
            iface.launch()
         | 
    	
        best_model.pth
    ADDED
    
    | @@ -0,0 +1,3 @@ | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            version https://git-lfs.github.com/spec/v1
         | 
| 2 | 
            +
            oid sha256:48dac038a70efe5a8e5887e41aac49117ff216cba67bd3ab5f4da5813972dec1
         | 
| 3 | 
            +
            size 245133901
         | 
    	
        examples/example_1.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_10.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_100.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_11.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_12.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_13.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_14.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_15.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_16.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_17.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_18.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_19.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_2.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_20.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_21.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_22.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_23.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_24.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_25.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_26.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_27.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_28.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_29.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_3.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_30.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_31.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_32.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_33.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_34.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_35.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_36.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_37.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_38.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_39.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_4.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_40.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_41.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_42.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_43.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_44.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_45.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_46.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_47.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_48.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_49.jpg
    ADDED
    
    |   | 
| Git LFS Details
 | 
    	
        examples/example_5.jpg
    ADDED
    
    |   | 
| Git LFS Details
 |