chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
<!--[metadata]
|
||||
title = "Live camera edge detection"
|
||||
tags = ["2D", "Canny", "Live", "OpenCV"]
|
||||
thumbnail = "https://static.rerun.io/live-camera-edge-detection/f747bcf9ff3039c895f0bf0290e2dea0a72631ea/480w.png"
|
||||
thumbnail_dimensions = [480, 480]
|
||||
-->
|
||||
|
||||
Visualize the [OpenCV Canny Edge Detection](https://docs.opencv.org/4.x/da/d22/tutorial_py_canny.html) results from a live camera stream.
|
||||
|
||||
<picture>
|
||||
<source media="(max-width: 480px)" srcset="https://static.rerun.io/live_camera_edge_detection/bf877bffd225f6c62cae3b87eecbc8e247abb202/480w.png">
|
||||
<source media="(max-width: 768px)" srcset="https://static.rerun.io/live_camera_edge_detection/bf877bffd225f6c62cae3b87eecbc8e247abb202/768w.png">
|
||||
<source media="(max-width: 1024px)" srcset="https://static.rerun.io/live_camera_edge_detection/bf877bffd225f6c62cae3b87eecbc8e247abb202/1024w.png">
|
||||
<source media="(max-width: 1200px)" srcset="https://static.rerun.io/live_camera_edge_detection/bf877bffd225f6c62cae3b87eecbc8e247abb202/1200w.png">
|
||||
<img src="https://static.rerun.io/live_camera_edge_detection/bf877bffd225f6c62cae3b87eecbc8e247abb202/full.png" alt="Live Camera Edge Detection example screenshot">
|
||||
</picture>
|
||||
|
||||
## Used Rerun types
|
||||
[`Image`](https://www.rerun.io/docs/reference/types/archetypes/image)
|
||||
|
||||
## Background
|
||||
In this example, the results of the [OpenCV Canny Edge Detection](https://docs.opencv.org/4.x/da/d22/tutorial_py_canny.html) algorithm are visualized.
|
||||
Canny Edge Detection is a popular edge detection algorithm, and can efficiently extract important structural information from visual objects while notably reducing the computational load.
|
||||
The process in this example involves converting the input image to RGB, then to grayscale, and finally applying the Canny Edge Detector for precise edge detection.
|
||||
|
||||
## Logging and visualizing with Rerun
|
||||
|
||||
The visualization in this example were created with the following Rerun code:
|
||||
### RGB image
|
||||
|
||||
The original image is read and logged in RGB format under the entity "image/rgb".
|
||||
```python
|
||||
# Log the original image
|
||||
rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
||||
rr.log("image/rgb", rr.Image(rgb))
|
||||
```
|
||||
|
||||
### Grayscale image
|
||||
|
||||
The input image is converted from BGR color space to grayscale, and the resulting grayscale image is logged under the entity "image/gray".
|
||||
```python
|
||||
# Convert to grayscale
|
||||
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
||||
rr.log("image/gray", rr.Image(gray))
|
||||
```
|
||||
|
||||
### Canny edge detection image
|
||||
|
||||
The Canny edge detector is applied to the grayscale image, and the resulting edge-detected image is logged under the entity "image/canny".
|
||||
```python
|
||||
# Run the canny edge detector
|
||||
canny = cv2.Canny(gray, 50, 200)
|
||||
rr.log("image/canny", rr.Image(canny))
|
||||
```
|
||||
|
||||
|
||||
## 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_camera_edge_detection
|
||||
```
|
||||
To experiment with the provided example, simply execute the main Python script:
|
||||
```bash
|
||||
python -m live_camera_edge_detection # 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_camera_edge_detection --help
|
||||
```
|
||||
@@ -0,0 +1,89 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Very simple example of capturing from a live camera.
|
||||
|
||||
Runs the opencv canny edge detector on the image stream.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
|
||||
import cv2
|
||||
|
||||
import rerun as rr # pip install rerun-sdk
|
||||
import rerun.blueprint as rrb
|
||||
|
||||
|
||||
def run_canny(num_frames: int | None) -> None:
|
||||
# Create a new video capture
|
||||
cap = cv2.VideoCapture(0)
|
||||
|
||||
frame_nr = 0
|
||||
|
||||
while cap.isOpened():
|
||||
if num_frames and frame_nr >= num_frames:
|
||||
break
|
||||
|
||||
# Read the frame
|
||||
ret, img = cap.read()
|
||||
if not ret:
|
||||
if frame_nr == 0:
|
||||
print("Failed to capture any frame. No camera connected?")
|
||||
else:
|
||||
print("Can't receive frame (stream end?). Exiting…")
|
||||
break
|
||||
|
||||
# Get the current frame time. On some platforms it always returns zero.
|
||||
frame_time_ms = cap.get(cv2.CAP_PROP_POS_MSEC)
|
||||
if frame_time_ms != 0:
|
||||
rr.set_time("frame_time", duration=1e-3 * frame_time_ms)
|
||||
|
||||
rr.set_time("frame_nr", sequence=frame_nr)
|
||||
frame_nr += 1
|
||||
|
||||
# Log the original image
|
||||
rr.log("image/rgb", rr.Image(img, color_model="BGR"))
|
||||
|
||||
# Convert to grayscale
|
||||
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
||||
rr.log("image/gray", rr.Image(gray))
|
||||
|
||||
# Run the canny edge detector
|
||||
canny = cv2.Canny(gray, 50, 200)
|
||||
rr.log("image/canny", rr.Image(canny))
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description="Streams a local system camera and runs the canny edge detector.")
|
||||
parser.add_argument(
|
||||
"--device",
|
||||
type=int,
|
||||
default=0,
|
||||
help="Which camera device to use. (Passed to `cv2.VideoCapture()`)",
|
||||
)
|
||||
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_camera_edge_detection",
|
||||
default_blueprint=rrb.Vertical(
|
||||
rrb.Horizontal(
|
||||
rrb.Spatial2DView(origin="/image/rgb", name="Video"),
|
||||
rrb.Spatial2DView(origin="/image/gray", name="Video (Grayscale)"),
|
||||
),
|
||||
rrb.Spatial2DView(origin="/image/canny", name="Canny Edge Detector"),
|
||||
row_shares=[1, 2],
|
||||
),
|
||||
)
|
||||
|
||||
run_canny(args.num_frames)
|
||||
|
||||
rr.script_teardown(args)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,16 @@
|
||||
[project]
|
||||
name = "live_camera_edge_detection"
|
||||
version = "0.1.0"
|
||||
# requires-python = "<3.12"
|
||||
readme = "README.md"
|
||||
dependencies = ["opencv-python", "rerun-sdk"]
|
||||
|
||||
[project.scripts]
|
||||
live_camera_edge_detection = "live_camera_edge_detection:main"
|
||||
|
||||
[tool.rerun-example]
|
||||
extra-args = "--num-frames=30"
|
||||
|
||||
[build-system]
|
||||
requires = ["hatchling"]
|
||||
build-backend = "hatchling.build"
|
||||
Reference in New Issue
Block a user