Skip to content

Commit

Permalink
Very simple example streaming from an opencv camera (#1502)
Browse files Browse the repository at this point in the history
* Very simple example streaming from an opencv camera

* Warning about the memory limitations

* Update comments and link to known issue

---------

Co-authored-by: Clement Rey <[email protected]>
  • Loading branch information
jleibs and teh-cmc authored Mar 6, 2023
1 parent 51cdb49 commit 86160a0
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
96 changes: 96 additions & 0 deletions examples/python/opencv_canny/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env python3
"""
Very simple example of capturing from a live camera.
Runs the opencv canny edge detector on the image stream.
Known issues:
- Only two layers of image transparency are visible. [#1503](https://github.com/rerun-io/rerun/issues/1503)
NOTE: this example currently runs forever and will eventually exhaust your
system memory. It is advised you run an independent rerun viewer with a memory
limit:
```
rerun --memory-limit 4GB
```
And then connect using:
```
python examples/python/opencv_canny/main.py --connect
```
"""

import argparse

import cv2
import rerun as rr


def run_canny() -> None:
# Create a new video capture
cap = cv2.VideoCapture(0)

while cap.isOpened():
# Read the frame
ret, img = cap.read()
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
break

# Get the current frame time
frame_time = cap.get(cv2.CAP_PROP_POS_MSEC)
rr.set_time_nanos("frame_time", int(frame_time * 1000000))

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

# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
rr.log_image("image/gray", gray)

# Run the canny edge detector
canny = cv2.Canny(gray, 50, 200)
rr.log_image("image/canny", 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()`)"
)

rr.script_add_args(parser)
args = parser.parse_args()

rr.script_setup(args, "opencv_canny")

print(args.connect)

if not args.connect:
print(
"""
################################################################################
NOTE: this example currently runs forever and will eventually exhaust your
system memory. It is advised you run an independent rerun viewer with a memory
limit:
```
rerun --memory-limit 4GB
```
And then connect using:
```
python examples/python/opencv_canny/main.py --connect
```
################################################################################
"""
)

run_canny()

rr.script_teardown(args)


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

1 comment on commit 86160a0

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rust Benchmark

Benchmark suite Current: 86160a0 Previous: 51cdb49 Ratio
datastore/insert/batch/rects/insert 547790 ns/iter (± 3333) 556439 ns/iter (± 1821) 0.98
datastore/latest_at/batch/rects/query 1846 ns/iter (± 5) 1830 ns/iter (± 3) 1.01
datastore/latest_at/missing_components/primary 353 ns/iter (± 3) 355 ns/iter (± 1) 0.99
datastore/latest_at/missing_components/secondaries 422 ns/iter (± 2) 424 ns/iter (± 0) 1.00
datastore/range/batch/rects/query 153238 ns/iter (± 1513) 153688 ns/iter (± 680) 1.00
mono_points_arrow/generate_message_bundles 45959542 ns/iter (± 1391146) 52397319 ns/iter (± 514396) 0.88
mono_points_arrow/generate_messages 123948350 ns/iter (± 1326179) 137290912 ns/iter (± 1260578) 0.90
mono_points_arrow/encode_log_msg 155920249 ns/iter (± 1089546) 170355282 ns/iter (± 1122469) 0.92
mono_points_arrow/encode_total 328636773 ns/iter (± 1628142) 362512127 ns/iter (± 2203933) 0.91
mono_points_arrow/decode_log_msg 174774609 ns/iter (± 1145465) 190468128 ns/iter (± 1511680) 0.92
mono_points_arrow/decode_message_bundles 65417625 ns/iter (± 782776) 77072670 ns/iter (± 1159949) 0.85
mono_points_arrow/decode_total 238488997 ns/iter (± 1963785) 261648081 ns/iter (± 1914859) 0.91
batch_points_arrow/generate_message_bundles 316770 ns/iter (± 2869) 320346 ns/iter (± 1066) 0.99
batch_points_arrow/generate_messages 6044 ns/iter (± 75) 6227 ns/iter (± 17) 0.97
batch_points_arrow/encode_log_msg 374618 ns/iter (± 2109) 376020 ns/iter (± 1541) 1.00
batch_points_arrow/encode_total 709658 ns/iter (± 4241) 720479 ns/iter (± 3178) 0.98
batch_points_arrow/decode_log_msg 351716 ns/iter (± 1072) 356727 ns/iter (± 1095) 0.99
batch_points_arrow/decode_message_bundles 2053 ns/iter (± 27) 2077 ns/iter (± 5) 0.99
batch_points_arrow/decode_total 357714 ns/iter (± 1780) 365148 ns/iter (± 4762) 0.98
arrow_mono_points/insert 6141130075 ns/iter (± 14866013) 7400939365 ns/iter (± 51456077) 0.83
arrow_mono_points/query 1748952 ns/iter (± 14932) 1847610 ns/iter (± 36001) 0.95
arrow_batch_points/insert 2696058 ns/iter (± 19743) 2839734 ns/iter (± 94461) 0.95
arrow_batch_points/query 16183 ns/iter (± 128) 16151 ns/iter (± 56) 1.00
arrow_batch_vecs/insert 42705 ns/iter (± 283) 42382 ns/iter (± 111) 1.01
arrow_batch_vecs/query 500780 ns/iter (± 5333) 507275 ns/iter (± 1747) 0.99
tuid/Tuid::random 34 ns/iter (± 0) 34 ns/iter (± 0) 1

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.