chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
<!--[metadata]
|
||||
title = "Live depth sensor"
|
||||
tags = ["2D", "3D", "Live", "Depth", "RealSense"]
|
||||
thumbnail = "https://static.rerun.io/live_depth_sensor/d3c0392bebe2003d24110a779d6f6748167772d8/480w.png"
|
||||
thumbnail_dimensions = [480, 360]
|
||||
-->
|
||||
|
||||
Visualize the live-streaming frames from an Intel RealSense depth sensor.
|
||||
|
||||
<picture>
|
||||
<source media="(max-width: 480px)" srcset="https://static.rerun.io/live_depth_sensor/d3c0392bebe2003d24110a779d6f6748167772d8/480w.png">
|
||||
<source media="(max-width: 768px)" srcset="https://static.rerun.io/live_depth_sensor/d3c0392bebe2003d24110a779d6f6748167772d8/768w.png">
|
||||
<source media="(max-width: 1024px)" srcset="https://static.rerun.io/live_depth_sensor/d3c0392bebe2003d24110a779d6f6748167772d8/1024w.png">
|
||||
<source media="(max-width: 1200px)" srcset="https://static.rerun.io/live_depth_sensor/d3c0392bebe2003d24110a779d6f6748167772d8/1200w.png">
|
||||
<img src="https://static.rerun.io/live_depth_sensor/d3c0392bebe2003d24110a779d6f6748167772d8/full.png" alt="Live Depth Sensor example screenshot">
|
||||
</picture>
|
||||
|
||||
This example requires a connected realsense depth sensor.
|
||||
|
||||
## Used Rerun types
|
||||
[`Pinhole`](https://www.rerun.io/docs/reference/types/archetypes/pinhole), [`Transform3D`](https://www.rerun.io/docs/reference/types/archetypes/transform3d), [`Image`](https://www.rerun.io/docs/reference/types/archetypes/image), [`DepthImage`](https://www.rerun.io/docs/reference/types/archetypes/depth_image)
|
||||
|
||||
## Background
|
||||
The Intel RealSense depth sensor can stream live depth and color data. To visualize this data output, we utilized Rerun.
|
||||
|
||||
## Logging and visualizing with Rerun
|
||||
|
||||
The RealSense sensor captures data in both RGB and depth formats, which are logged using the [`Image`](https://www.rerun.io/docs/reference/types/archetypes/image) and [`DepthImage`](https://www.rerun.io/docs/reference/types/archetypes/depth_image) archetypes, respectively.
|
||||
Additionally, to provide a 3D view, the visualization includes a pinhole camera using the [`Pinhole`](https://www.rerun.io/docs/reference/types/archetypes/pinhole) and [`Transform3D`](https://www.rerun.io/docs/reference/types/archetypes/transform3d) archetypes.
|
||||
|
||||
The visualization in this example were created with the following Rerun code.
|
||||
|
||||
```python
|
||||
rr.log("realsense", rr.ViewCoordinates.RDF, static=True) # Visualize the data as RDF
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Image
|
||||
|
||||
First, the pinhole camera is set using the [`Pinhole`](https://www.rerun.io/docs/reference/types/archetypes/pinhole) and [`Transform3D`](https://www.rerun.io/docs/reference/types/archetypes/transform3d) archetypes. Then, the images captured by the RealSense sensor are logged as an [`Image`](https://www.rerun.io/docs/reference/types/archetypes/image) object, and they're associated with the time they were taken.
|
||||
|
||||
|
||||
|
||||
```python
|
||||
rgb_from_depth = depth_profile.get_extrinsics_to(rgb_profile)
|
||||
rr.log(
|
||||
"realsense/rgb",
|
||||
rr.Transform3D(
|
||||
translation=rgb_from_depth.translation,
|
||||
mat3x3=np.reshape(rgb_from_depth.rotation, (3, 3)),
|
||||
relation=rr.TransformRelation.ChildFromParent,
|
||||
),
|
||||
static=True,
|
||||
)
|
||||
```
|
||||
|
||||
```python
|
||||
rr.log(
|
||||
"realsense/rgb/image",
|
||||
rr.Pinhole(
|
||||
resolution=[rgb_intr.width, rgb_intr.height],
|
||||
focal_length=[rgb_intr.fx, rgb_intr.fy],
|
||||
principal_point=[rgb_intr.ppx, rgb_intr.ppy],
|
||||
),
|
||||
static=True,
|
||||
)
|
||||
```
|
||||
```python
|
||||
rr.set_time("frame_nr", sequence=frame_nr)
|
||||
rr.log("realsense/rgb/image", rr.Image(color_image))
|
||||
```
|
||||
|
||||
### Depth image
|
||||
|
||||
Just like the RGB images, the RealSense sensor also captures depth data. The depth images are logged as [`DepthImage`](https://www.rerun.io/docs/reference/types/archetypes/depth_image) objects and are linked with the time they were captured.
|
||||
|
||||
```python
|
||||
rr.log(
|
||||
"realsense/depth/image",
|
||||
rr.Pinhole(
|
||||
resolution=[depth_intr.width, depth_intr.height],
|
||||
focal_length=[depth_intr.fx, depth_intr.fy],
|
||||
principal_point=[depth_intr.ppx, depth_intr.ppy],
|
||||
),
|
||||
static=True,
|
||||
)
|
||||
```
|
||||
```python
|
||||
rr.set_time("frame_nr", sequence=frame_nr)
|
||||
rr.log("realsense/depth/image", rr.DepthImage(depth_image, meter=1.0 / depth_units))
|
||||
```
|
||||
|
||||
## Run the code
|
||||
To run this example, make sure you have the Rerun repository checked out and the latest SDK installed:
|
||||
```bash
|
||||
pip install --upgrade rerun-sdk # install the latest Rerun SDK
|
||||
git clone git@github.com:rerun-io/rerun.git # Clone the repository
|
||||
cd rerun
|
||||
git checkout latest # Check out the commit matching the latest SDK release
|
||||
```
|
||||
Install the necessary libraries specified in the requirements file:
|
||||
```bash
|
||||
pip install -e examples/python/live_depth_sensor
|
||||
```
|
||||
To experiment with the provided example, simply execute the main Python script:
|
||||
```bash
|
||||
python -m live_depth_sensor # run the example
|
||||
```
|
||||
If you wish to customize it, explore additional features, or save it use the CLI with the `--help` option for guidance:
|
||||
```bash
|
||||
python -m live_depth_sensor --help
|
||||
```
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
#!/usr/bin/env python3
|
||||
"""A minimal example of streaming frames live from an Intel RealSense depth sensor."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
|
||||
import numpy as np
|
||||
import pyrealsense2 as rs
|
||||
|
||||
import rerun as rr # pip install rerun-sdk
|
||||
|
||||
|
||||
def run_realsense(num_frames: int | None) -> None:
|
||||
# Visualize the data as RDF
|
||||
rr.log("realsense", rr.ViewCoordinates.RDF, static=True)
|
||||
|
||||
# Open the pipe
|
||||
pipe = rs.pipeline()
|
||||
profile = pipe.start()
|
||||
|
||||
# We don't log the depth exstrinsics. We treat the "realsense" space as being at
|
||||
# the origin of the depth sensor so that "realsense/depth" = Identity
|
||||
|
||||
# Get and log depth intrinsics
|
||||
depth_profile = profile.get_stream(rs.stream.depth)
|
||||
depth_intr = depth_profile.as_video_stream_profile().get_intrinsics()
|
||||
|
||||
rr.log(
|
||||
"realsense/depth/image",
|
||||
rr.Pinhole(
|
||||
resolution=[depth_intr.width, depth_intr.height],
|
||||
focal_length=[depth_intr.fx, depth_intr.fy],
|
||||
principal_point=[depth_intr.ppx, depth_intr.ppy],
|
||||
),
|
||||
static=True,
|
||||
)
|
||||
|
||||
# Get and log color extrinsics
|
||||
rgb_profile = profile.get_stream(rs.stream.color)
|
||||
|
||||
rgb_from_depth = depth_profile.get_extrinsics_to(rgb_profile)
|
||||
rr.log(
|
||||
"realsense/rgb",
|
||||
rr.Transform3D(
|
||||
translation=rgb_from_depth.translation,
|
||||
mat3x3=np.reshape(rgb_from_depth.rotation, (3, 3)),
|
||||
relation=rr.TransformRelation.ChildFromParent,
|
||||
),
|
||||
static=True,
|
||||
)
|
||||
|
||||
# Get and log color intrinsics
|
||||
rgb_intr = rgb_profile.as_video_stream_profile().get_intrinsics()
|
||||
|
||||
rr.log(
|
||||
"realsense/rgb/image",
|
||||
rr.Pinhole(
|
||||
resolution=[rgb_intr.width, rgb_intr.height],
|
||||
focal_length=[rgb_intr.fx, rgb_intr.fy],
|
||||
principal_point=[rgb_intr.ppx, rgb_intr.ppy],
|
||||
),
|
||||
static=True,
|
||||
)
|
||||
|
||||
# Read frames in a loop
|
||||
frame_nr = 0
|
||||
try:
|
||||
while True:
|
||||
if num_frames and frame_nr >= num_frames:
|
||||
break
|
||||
|
||||
rr.set_time("frame_nr", sequence=frame_nr)
|
||||
frame_nr += 1
|
||||
|
||||
frames = pipe.wait_for_frames()
|
||||
for _f in frames:
|
||||
# Log the depth frame
|
||||
depth_frame = frames.get_depth_frame()
|
||||
depth_units = depth_frame.get_units()
|
||||
depth_image = np.asanyarray(depth_frame.get_data())
|
||||
rr.log("realsense/depth/image", rr.DepthImage(depth_image, meter=1.0 / depth_units))
|
||||
|
||||
# Log the color frame
|
||||
color_frame = frames.get_color_frame()
|
||||
color_image = np.asanyarray(color_frame.get_data())
|
||||
rr.log("realsense/rgb/image", rr.Image(color_image))
|
||||
finally:
|
||||
pipe.stop()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description="Streams frames from a connected realsense depth sensor.")
|
||||
parser.add_argument("--num-frames", type=int, default=None, help="The number of frames to log")
|
||||
|
||||
rr.script_add_args(parser)
|
||||
args = parser.parse_args()
|
||||
|
||||
rr.script_setup(args, "rerun_example_live_depth_sensor")
|
||||
|
||||
run_realsense(args.num_frames)
|
||||
|
||||
rr.script_teardown(args)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,21 @@
|
||||
[project]
|
||||
name = "live_depth_sensor"
|
||||
version = "0.1.0"
|
||||
requires-python = "<3.11"
|
||||
readme = "README.md"
|
||||
dependencies = [
|
||||
"numpy",
|
||||
"pyrealsense2-mac; sys_platform == 'darwin'",
|
||||
"pyrealsense2; sys_platform != 'darwin'",
|
||||
"rerun-sdk",
|
||||
]
|
||||
|
||||
[project.scripts]
|
||||
live_depth_sensor = "live_depth_sensor:main"
|
||||
|
||||
[tool.rerun-example]
|
||||
skip = true # requires hardware
|
||||
|
||||
[build-system]
|
||||
requires = ["hatchling"]
|
||||
build-backend = "hatchling.build"
|
||||
Reference in New Issue
Block a user