chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
<!--[metadata]
|
||||
title = "NV12"
|
||||
tags = ["2D", "Image encoding", "YUV"]
|
||||
thumbnail = "https://static.rerun.io/nv12/3bffd358462a453c6358aa3cc4c8555b12fc0d35/480w.png"
|
||||
thumbnail_dimensions = [480, 480]
|
||||
-->
|
||||
|
||||
This example displays an NV12 encoded video stream from a webcam in rerun.
|
||||
|
||||
<img src="https://static.rerun.io/nv12/3bffd358462a453c6358aa3cc4c8555b12fc0d35/480w.png">
|
||||
|
||||
|
||||
## Run the code
|
||||
|
||||
```bash
|
||||
pip install -e examples/python/nv12
|
||||
python -m nv12
|
||||
```
|
||||
Executable
+61
@@ -0,0 +1,61 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Stream NV12 images from a webcam."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import time
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
import rerun as rr # pip install rerun-sdk
|
||||
|
||||
|
||||
def bgr2nv12(bgr: cv2.typing.MatLike) -> cv2.typing.MatLike:
|
||||
yuv = cv2.cvtColor(bgr, cv2.COLOR_BGR2YUV_I420)
|
||||
uv_row_cnt = yuv.shape[0] // 3
|
||||
uv_plane = np.transpose(yuv[uv_row_cnt * 2 :].reshape(2, -1), [1, 0])
|
||||
yuv[uv_row_cnt * 2 :] = uv_plane.reshape(uv_row_cnt, -1)
|
||||
return yuv
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description="Example of using the Rerun visualizer to display NV12 images.")
|
||||
rr.script_add_args(parser)
|
||||
parser.add_argument(
|
||||
"-t",
|
||||
"--timeout",
|
||||
type=float,
|
||||
default=5,
|
||||
help="Timeout in seconds, after which the script will stop streaming frames.",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
rr.script_setup(args, "rerun_example_nv12")
|
||||
|
||||
cap = cv2.VideoCapture(0)
|
||||
if not cap.isOpened():
|
||||
raise RuntimeError("This example requires a webcam.")
|
||||
start_time = time.time()
|
||||
print(f"Started streaming NV12 images for {args.timeout} seconds.")
|
||||
while start_time + args.timeout > time.time():
|
||||
ret, frame = cap.read()
|
||||
if not ret:
|
||||
time.sleep(0.01)
|
||||
continue
|
||||
rr.log(
|
||||
"NV12",
|
||||
rr.Image(
|
||||
bytes=bytes(bgr2nv12(frame)),
|
||||
width=frame.shape[1],
|
||||
height=frame.shape[0],
|
||||
pixel_format=rr.PixelFormat.NV12,
|
||||
),
|
||||
)
|
||||
time.sleep(0.01)
|
||||
rr.script_teardown(args)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,12 @@
|
||||
[project]
|
||||
name = "nv12"
|
||||
version = "0.1.0"
|
||||
readme = "README.md"
|
||||
dependencies = ["rerun-sdk>=0.10", "opencv-python", "numpy"]
|
||||
|
||||
[project.scripts]
|
||||
nv12 = "nv12:main"
|
||||
|
||||
[build-system]
|
||||
requires = ["hatchling"]
|
||||
build-backend = "hatchling.build"
|
||||
Reference in New Issue
Block a user