| 1 | # Example using a custom video |
| 2 | |
| 3 | A new open-source SLAM system [SLAM3R](https://github.com/PKU-VCL-3DV/SLAM3R) just released. We found it is great for reconstructing point clouds from RGB inputs as well. Here we provide an example of how to use it to reconstruct a point cloud from a video, and use our model to estimate the layout of the scene. Following steps work for MASt3R-SLAM, and other reconstruction methods as well. |
| 4 | |
| 5 | ## 1. Reconstruct a point cloud with SLAM3R |
| 6 | |
| 7 | Please refer to the [SLAM3R Replica Example](https://github.com/PKU-VCL-3DV/SLAM3R?tab=readme-ov-file#replica-dataset) for the details of how to run SLAM3R on the provided Replica example. |
| 8 | |
| 9 | You can set `sample_freq` in this [line](https://github.com/PKU-VCL-3DV/SLAM3R/blob/3831478e15b83509f3f317abc1d4f7bd3cc21e2d/scripts/demo_replica.sh#L8) to 2 to decrease the GPU memory usage and speed up the reconstruction. And you can also try increase the `CONF_THRES_L2W` threshold [here](https://github.com/PKU-VCL-3DV/SLAM3R/blob/3831478e15b83509f3f317abc1d4f7bd3cc21e2d/scripts/demo_replica.sh#L22) to get cleaner point cloud. |
| 10 | |
| 11 | If you are using MASt3R-SLAM, we recommend you to increase the `C_conf_threshold` and add some point cloud cleaning steps for each keyframe point cloud exportation in [line](https://github.com/rmurai0610/MASt3R-SLAM/blob/c3d0d5b67bf51d558d7640ff6032407f68041f92/mast3r_slam/evaluate.py#L65), such as removing statistical outliers with following code. These will generally help to get cleaner point clouds. |
| 12 | |
| 13 | ```python |
| 14 | ... |
| 15 | pcd = o3d.geometry.PointCloud() |
| 16 | pcd.points = o3d.utility.Vector3dVector(valid_pts) |
| 17 | pcd.colors = o3d.utility.Vector3dVector(valid_colors) |
| 18 | pcd, trace = pcd.remove_statistical_outlier(nb_neighbors=10, std_ratio=1.5) |
| 19 | points = np.asarray(pcd.points) |
| 20 | color = (np.asarray(pcd.colors) * 255.0).astype(np.uint8) |
| 21 | pointclouds.append(points) |
| 22 | colors.append(color) |
| 23 | ``` |
| 24 | |
| 25 | ## 2. Align the output point cloud |
| 26 | |
| 27 | In the current version of SpatialLM, input point clouds are considered axis-aligned where the z-axis is the up axis. The input point clouds should follow the ScanNet scenes orientation convension, which is "_Transforms scans to z-up alignment for scans and tries to align walls to x-y planes._" in [Alignment](https://github.com/ScanNet/ScanNet/tree/master/Alignment). Refer to [Issue #7](https://github.com/manycore-research/SpatialLM/issues/7) for more details. |
| 28 | |
| 29 | For now, you can choose your way to align the point cloud. We recommend you to check out [VanishingPoint Estimation](https://github.com/yanconglin/VanishingPoint_HoughTransform_GaussianSphere), [Manhattan Frame Estimation from video](https://github.com/jstraub/rtmf), Manhattan Frame Estimation from surface normals (ref paper: Globally Optimal Manhattan Frame Estimation in Real-time, Manhattan World Inference in the Space of Surface Normals) for estimating the Manhattan Frame from image, video or point cloud. And more recent methods such as [U-ARE-ME](https://github.com/callum-rhodes/U-ARE-ME), [Perspective Fields for Single Image Camera Calibration](https://github.com/jinlinyi/PerspectiveFields). Or even use tools like Blender to manually align the point cloud. |
| 30 | |
| 31 | | Before Alignment | After Alignment | |
| 32 | | :------------------------------------------------------: | :---------------------------------------------------: | |
| 33 | |  |  | |
| 34 | |
| 35 | ## 3. Ensure the scale of the point cloud |
| 36 | |
| 37 | Our model is trained on point clouds with a metric accurate scale. The value 1 refer to 1 meter in the real world. Although we did some scale augmentation during training, but the size may vary from different SLAM systems. |
| 38 | |
| 39 | For indoor scenes, you can have a rough estimation of the scale of the scene by the height of the walls. For example, you can resize the aligned point cloud to have a height of 2.5 meters in this example. |
| 40 | |
| 41 | ```python |
| 42 | import open3d as o3d |
| 43 | from spatiallm.pcd import load_o3d_pcd, get_points_and_colors |
| 44 | |
| 45 | point_cloud = load_o3d_pcd(point_cloud_file) |
| 46 | points, colors = get_points_and_colors(point_cloud) |
| 47 | min_z = np.min(points[:, 2]) |
| 48 | max_z = np.max(points[:, 2]) |
| 49 | height = max_z - min_z |
| 50 | estimated_height = 2.5 |
| 51 | scale = estimated_height / height |
| 52 | points = points * scale |
| 53 | |
| 54 | # export the point cloud |
| 55 | point_cloud = o3d.geometry.PointCloud() |
| 56 | point_cloud.points = o3d.utility.Vector3dVector(points) |
| 57 | point_cloud.colors = o3d.utility.Vector3dVector(colors.astype(np.float32)/ 255.0) |
| 58 | o3d.io.write_point_cloud(f"scaled_point_cloud.ply", point_cloud) |
| 59 | ``` |
| 60 | |
| 61 | You can also explore more creative approaches to estimate the scale of the point cloud. For example, you can use [Depth Pro](https://github.com/apple/ml-depth-pro) to estimate keyframe depth maps and compare them with keyframe point clouds generated by reconstruction methods like MASt3R-SLAM. |
| 62 | |
| 63 | ## 4. Run the inference |
| 64 | |
| 65 | Now we can run the layout estimation with the aligned and scaled point cloud. |
| 66 | |
| 67 | ```bash |
| 68 | python inference.py --point_cloud scaled_point_cloud.ply --output replica_layout.txt --model_path manycore-research/SpatialLM-Llama-1B |
| 69 | # visualize the layout |
| 70 | python visualize.py --point_cloud scaled_point_cloud.ply --layout replica_layout.txt --save replica_layout.rrd |
| 71 | rerun replica_layout.rrd |
| 72 | ``` |
| 73 | |
| 74 | | View 1 | View 2 | |
| 75 | | :----------------------------------------------: | :----------------------------------------------: | |
| 76 | |  |  | |
| 77 | |
| 78 | ## 5. Summary |
| 79 | |
| 80 | In this example, we demonstrated how to estimate scene layout from a custom video. The workflow involves four main steps: reconstructing a point cloud from video, aligning it to match the ScanNet orientation convention, scale the point cloud to a roughly metric accurate scale, and finally running our layout estimation model. We will keep improving to make these steps more automated, efficient and robust. |
| 81 | |
| 82 | Note that in this example, one sofa is not detected in the prediction. This is because our training data primarily consists of Chinese residential apartments where living rooms typically feature a single main sofa facing a TV, which is on the other side of the room. The model currently has its limitation on generalize very well to more diverse scenarios, such as non-standard layouts, cluttered spaces, or outdoor environments. We are actively working on it, and will try to improve the model's performance to more diverse scenes in the next release. |