Skip to content

Commit

Permalink
adds example live_camera_edge_detection #30
Browse files Browse the repository at this point in the history
  • Loading branch information
mxochicale committed Feb 20, 2024
1 parent 7b3ef93 commit 13fe768
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
19 changes: 19 additions & 0 deletions rerun/examples/live_camera_edge_detection/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# live_camera_edge_detection

## Download scripts
```
wget https://raw.githubusercontent.com/rerun-io/rerun/main/examples/python/live_camera_edge_detection/requirements.txt
wget https://raw.githubusercontent.com/rerun-io/rerun/main/examples/python/live_camera_edge_detection/main.py
```


## Run scripts
mamba activate rrVE
pip install -r requirements.txt
python main.py


## Reference
https://github.com/rerun-io/rerun/tree/main/examples/python/live_camera_edge_detection

73 changes: 73 additions & 0 deletions rerun/examples/live_camera_edge_detection/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/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


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_nanos("frame_time", int(frame_time_ms * 1_000_000))

rr.set_time_sequence("frame_nr", frame_nr)
frame_nr += 1

# Log the original image
rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
rr.log("image/rgb", rr.Image(rgb))

# 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")

run_canny(args.num_frames)

rr.script_teardown(args)


if __name__ == "__main__":
main()
2 changes: 2 additions & 0 deletions rerun/examples/live_camera_edge_detection/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
opencv-python
rerun-sdk

0 comments on commit 13fe768

Please sign in to comment.