-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds example live_camera_edge_detection #30
- Loading branch information
1 parent
7b3ef93
commit 13fe768
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
opencv-python | ||
rerun-sdk |