-
-
Notifications
You must be signed in to change notification settings - Fork 420
feat: Add SAM 3D Objects support for 3D reconstruction #489
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,228 @@ | ||
| { | ||
| "cells": [ | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "# 3D Object Reconstruction with SAM 3D\n", | ||
| "\n", | ||
| "[](https://colab.research.google.com/github/opengeos/segment-geospatial/blob/main/docs/examples/sam3d.ipynb)\n", | ||
| "\n", | ||
| "This notebook demonstrates how to use SAM 3D Objects to reconstruct 3D models from segmented objects in images.\n", | ||
| "\n", | ||
| "## Overview\n", | ||
| "\n", | ||
| "[SAM 3D Objects](https://github.com/facebookresearch/sam-3d-objects) is a foundation model from Meta that converts masked objects in images into 3D models with pose, shape, texture, and layout. It excels in real-world scenarios with occlusion and clutter.\n", | ||
| "\n", | ||
| "**Requirements:**\n", | ||
| "- Linux 64-bit system\n", | ||
| "- NVIDIA GPU with at least 32GB VRAM\n", | ||
| "- HuggingFace authentication for checkpoint access\n", | ||
| "\n", | ||
| "**Reference:**\n", | ||
| "SAM 3D Team (2025). SAM 3D: 3Dfy Anything in Images. https://arxiv.org/abs/2511.16624" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Installation\n", | ||
| "\n", | ||
| "SAM 3D Objects requires a separate installation. Print the instructions:" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "from samgeo.sam3d import print_install_instructions\n", | ||
| "\n", | ||
| "print_install_instructions()" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Setup Environment Variable\n", | ||
| "\n", | ||
| "After installing SAM 3D Objects, set the path to the repository:" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "\n", | ||
| "# Set the path to your sam-3d-objects installation\n", | ||
| "# os.environ[\"SAM3D_PATH\"] = \"/path/to/sam-3d-objects\"" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Basic Usage\n", | ||
| "\n", | ||
| "Reconstruct a 3D object from an image and mask:" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "\n", | ||
| "# Initialize the reconstructor\n", | ||
| "# reconstructor = Sam3DReconstructor()\n", | ||
| "\n", | ||
| "# Reconstruct a single object\n", | ||
| "# output = reconstructor.reconstruct(\n", | ||
| "# image=\"path/to/image.png\",\n", | ||
| "# mask=\"path/to/mask.png\",\n", | ||
| "# seed=42,\n", | ||
| "# )\n", | ||
| "\n", | ||
| "# Save the Gaussian splat\n", | ||
| "# reconstructor.save_ply(output, \"object.ply\")" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Workflow: Segment with SAM, Reconstruct with SAM 3D\n", | ||
| "\n", | ||
| "A typical workflow combines SAM/SAM2/SAM3 for segmentation with SAM 3D for 3D reconstruction:" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "# Step 1: Segment objects using SamGeo\n", | ||
| "# from samgeo import SamGeo\n", | ||
| "# \n", | ||
| "# sam = SamGeo()\n", | ||
| "# sam.set_image(\"image.tif\")\n", | ||
| "# sam.generate(output=\"masks.gpkg\")\n", | ||
| "\n", | ||
| "# Step 2: Reconstruct 3D models from segmented objects\n", | ||
| "# from samgeo.sam3d import reconstruct_from_samgeo\n", | ||
| "# import geopandas as gpd\n", | ||
| "# \n", | ||
| "# masks = gpd.read_file(\"masks.gpkg\")\n", | ||
| "# ply_files = reconstruct_from_samgeo(\n", | ||
| "# samgeo_result=masks,\n", | ||
| "# image_path=\"image.tif\",\n", | ||
| "# output_dir=\"./3d_models\",\n", | ||
| "# max_objects=5, # Limit number of objects\n", | ||
| "# )" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Multiple Object Reconstruction\n", | ||
| "\n", | ||
| "Reconstruct multiple objects from a single image:" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "# reconstructor = Sam3DReconstructor()\n", | ||
| "\n", | ||
| "# masks = [\"mask1.png\", \"mask2.png\", \"mask3.png\"]\n", | ||
| "# outputs = reconstructor.reconstruct_multiple(\n", | ||
| "# image=\"image.png\",\n", | ||
| "# masks=masks,\n", | ||
| "# )\n", | ||
| "\n", | ||
| "# for i, output in enumerate(outputs):\n", | ||
| "# reconstructor.save_ply(output, f\"object_{i}.ply\")" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Output Formats\n", | ||
| "\n", | ||
| "SAM 3D Objects outputs Gaussian splats which can be:\n", | ||
| "- Saved as PLY files for viewing in 3D viewers\n", | ||
| "- Converted to meshes for use in 3D software\n", | ||
| "- Visualized in web-based viewers\n", | ||
| "\n", | ||
| "For viewing PLY files, you can use:\n", | ||
| "- [MeshLab](https://www.meshlab.net/)\n", | ||
| "- [CloudCompare](https://www.cloudcompare.org/)\n", | ||
| "- [Three.js](https://threejs.org/) web viewer" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Tips and Best Practices\n", | ||
| "\n", | ||
| "1. **GPU Memory**: SAM 3D requires at least 32GB VRAM. Use smaller images or process objects one at a time if you have memory constraints.\n", | ||
| "\n", | ||
| "2. **Mask Quality**: Better segmentation masks lead to better 3D reconstructions. Use SAM2 or SAM3 for high-quality masks.\n", | ||
| "\n", | ||
| "3. **Object Selection**: SAM 3D works best on:\n", | ||
| " - Complete, unoccluded objects\n", | ||
| " - Objects with clear boundaries\n", | ||
| " - Objects with sufficient texture\n", | ||
| "\n", | ||
| "4. **Seed Consistency**: Use the same seed for reproducible results.\n", | ||
| "\n", | ||
| "5. **Batch Processing**: For multiple objects, use `reconstruct_multiple()` or `reconstruct_from_samgeo()` for efficiency." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## References\n", | ||
| "\n", | ||
| "- [SAM 3D Objects GitHub](https://github.com/facebookresearch/sam-3d-objects)\n", | ||
| "- [SAM 3D Website](https://ai.meta.com/sam3d/)\n", | ||
| "- [SAM 3D Paper](https://arxiv.org/abs/2511.16624)\n", | ||
| "- [SAM 3D Demo](https://www.aidemos.meta.com/segment-anything/editor/convert-image-to-3d)\n", | ||
| "- [SAM 3D Body](https://github.com/facebookresearch/sam-3d-body) (for human mesh recovery)" | ||
| ] | ||
| } | ||
| ], | ||
| "metadata": { | ||
| "kernelspec": { | ||
| "display_name": "Python 3", | ||
| "language": "python", | ||
| "name": "python3" | ||
| }, | ||
| "language_info": { | ||
| "codemirror_mode": { | ||
| "name": "ipython", | ||
| "version": 3 | ||
| }, | ||
| "file_extension": ".py", | ||
| "mimetype": "text/x-python", | ||
| "name": "python", | ||
| "version": "3.11.0" | ||
| } | ||
| }, | ||
| "nbformat": 4, | ||
| "nbformat_minor": 4 | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # sam3d module | ||
|
|
||
| ::: samgeo.sam3d |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -21,3 +21,10 @@ | |||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
| except ImportError: | ||||||||||||||||||||||||||||
| pass # detectree2 not installed | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # SAM 3D Objects support for 3D reconstruction | ||||||||||||||||||||||||||||
| from .sam3d import ( | ||||||||||||||||||||||||||||
| Sam3DReconstructor, | ||||||||||||||||||||||||||||
| print_install_instructions as sam3d_install_instructions, | ||||||||||||||||||||||||||||
| reconstruct_from_samgeo, | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
|
Comment on lines
+26
to
+30
|
||||||||||||||||||||||||||||
| from .sam3d import ( | |
| Sam3DReconstructor, | |
| print_install_instructions as sam3d_install_instructions, | |
| reconstruct_from_samgeo, | |
| ) | |
| try: | |
| from .sam3d import ( | |
| Sam3DReconstructor, | |
| print_install_instructions as sam3d_install_instructions, | |
| reconstruct_from_samgeo, | |
| ) | |
| except ImportError: | |
| pass # sam3d or its optional dependencies not installed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The arXiv ID format "2511.16624" appears suspicious. Standard arXiv IDs after April 2007 follow the format YYMM.NNNNN (e.g., 2501.12345 for January 2025). The ID "2511.16624" would represent November 2025, which is in the future. Please verify this arXiv ID is correct, as it may not exist or may be a placeholder. This same incorrect ID appears in multiple places in the documentation.