此页面由 AI 自动翻译。查看英文原版

本页目录

  • 入门
  • 功能
  • API 参考
  • RemoteConnection 类
  • 添加主题
  • 与神经网络集成
  • 示例应用程序
  • 多摄像头流
  • 对象检测可视化

OAK Visualizer

Visualizer 允许您直接在 Web 浏览器中查看 OAK 设备上的多个摄像头流和数据输出。它提供了比 OpenCV 的 imshow() 函数更灵活的替代方案,支持同时查看多个流和交互式控件。目前在 Chrome 浏览器中完全支持。请注意,Visualizer 可与 DepthAI API 配合使用,并兼容所有 OAK 设备。

入门

基本设置

要使用 Visualizer,您需要在 DepthAI 应用程序中创建一个 RemoteConnection
Python
1import depthai as dai
2
3# 创建一个 RemoteConnection (Visualizer 服务器)
4visualizer = dai.RemoteConnection()
5
6# 创建 pipeline
7with dai.Pipeline() as pipeline:
8    # 创建摄像头节点
9    # 不带参数的 build 方法将使用默认摄像头
10    rgb_cam = pipeline.create(dai.node.Camera).build()
11
12    # 从摄像头请求分辨率为 1280x720 的 NV12 流
13    rgb_stream = rgb_cam.requestOutput(size=(1280, 720), type=dai.ImgFrame.Type.NV12)
14
15    # 将摄像头流添加为 Visualizer 的一个主题
16    visualizer.addTopic("rgb", rgb_stream)
17
18    # 构建并启动 pipeline
19    pipeline.start()
20
21    # 将 pipeline 图注册到 Visualizer 以便可视化
22    visualizer.registerPipeline(pipeline)
23
24    # 在 pipeline 运行时执行的主循环
25    while pipeline.isRunning():
26        # 检查 Visualizer 中的按键事件
27        if visualizer.waitKey(1) == ord('q'):
28            # 如果按下 'q',则停止 pipeline
29            pipeline.stop()
运行此代码后,请在浏览器中打开 http://localhost:8080 来查看流。

立体设备设置

要使用 Visualizer,您需要在 DepthAI 应用程序中创建一个 RemoteConnection
Python
1import depthai as dai
2
3# 创建一个 RemoteConnection (Visualizer 服务器)
4visualizer = dai.RemoteConnection()
5
6# 创建 pipeline
7with dai.Pipeline() as pipeline:
8    # 创建摄像头节点
9    rgb_cam = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_A)
10    left_cam = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_B)
11    right_cam = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_C)
12
13    # 请求摄像头的 NV12 流
14    rgb_stream = rgb_cam.requestOutput(size=(1280, 720), type=dai.ImgFrame.Type.NV12)
15    left_stream = left_cam.requestOutput(size=(800, 600), type=dai.ImgFrame.Type.NV12)
16    right_stream = right_cam.requestOutput(size=(800, 600), type=dai.ImgFrame.Type.NV12)
17
18    # 将摄像头流添加为 Visualizer 的主题
19    visualizer.addTopic("rgb", rgb_stream)
20    visualizer.addTopic("left", left_stream)
21    visualizer.addTopic("right", right_stream)
22
23    # 构建并启动 pipeline
24    pipeline.start()
25
26    # 将 pipeline 图注册到 Visualizer 以便可视化
27    visualizer.registerPipeline(pipeline)
28
29    # 在 pipeline 运行时执行的主循环
30    while pipeline.isRunning():
31        # 检查 Visualizer 中的按键事件
32        if visualizer.waitKey(1) == ord('q'):
33            # 如果按下 'q',则停止 pipeline
34            pipeline.stop()
运行此代码后,请在浏览器中打开 http://localhost:8080 来查看流。

高级配置

您可以使用其他参数来自定义 Visualizer 的行为:
Python
1# 自定义 RemoteConnection 配置
2remote = dai.RemoteConnection(
3    address="0.0.0.0",      # 绑定到所有接口
4    webSocketPort=8765,     # 自定义 WebSocket 端口
5    serveFrontend=True,     # 启用 Web UI
6    httpPort=8080           # 自定义 HTTP 端口
7)
8
9# 添加带有分组的主题以方便组织
10remote.addTopic("rgb", rgb_stream, group="RGB")
11remote.addTopic("left", left_stream, group="Left")
12remote.addTopic("right", right_stream, group="Right")
13
14# 添加神经网络结果
15# 将检测结果与 RGB 流分组将确保检测结果不会叠加在左右流上
16remote.addTopic("detections", nn.out, group="RGB")

功能

  • 多流查看 - 在浏览器中同时查看所有摄像头流
  • 自定义消息队列 - 创建自定义消息队列以将数据发送到 Visualizer
  • 远程查看 - 从网络上的任何设备访问 Visualizer

API 参考

RemoteConnection 类

RemoteConnection 类是 Visualizer 的主接口:
Python
1remote = dai.RemoteConnection(
2    address="0.0.0.0",      # 要绑定的 IP 地址
3    webSocketPort=8765,     # WebSocket 端口
4    serveFrontend=True,     # 是否提供前端 UI
5    httpPort=8080           # Web 界面的 HTTP 端口
6)

添加主题

您可以通过两种方式将主题(流)添加到 Visualizer。首先,通过添加一个流式传输节点输出的主题:
Python
1remote.addTopic(
2    topicName="rgb",              # 在 UI 中显示的名称
3    output=rgb_stream,            # 要流式传输的节点输出
4    group="Cameras",              # 可选组名
5    useVisualizationIfAvailable=True  # 如果存在,将调用输出对象上的 getVisualizationMessage 方法来获取可视化。
6)
或者创建一个将流式传输其内容输出队列:
Python
1rgb_queue = visualizer.addTopic(
2    topicName="rgb",              # 在 UI 中显示的名称
3    group="Cameras",              # 可选组名
4    maxSize=2,                    # 创建的队列的最大大小
5    blocking=False,               # 指示创建的队列是否将阻塞
6    useVisualizationIfAvailable=True  # 如果存在,将调用输出对象上的 getVisualizationMessage 方法来获取可视化。
7)
8# 将自定义数据发送到队列
9rgb_queue.send(my_data)

与神经网络集成

Python
1# 创建神经网络节点
2nn = pipeline.create(dai.node.DetectionNetwork).build(
3    input=rgb_cam, # 用作神经网络输入的摄像头节点
4    model="luxonis/yolov6-nano:r2-coco-512x288", # 来自 Luxonis Hub 的模型 slug
5)
6nn.setConfidenceThreshold(0.5)
7
8# 将神经网络结果添加到 Visualizer
9remote.addTopic("detections", nn.out, group="RGB")

示例应用程序

多摄像头流

Python
1# 创建摄像头
2rgb_cam = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_A)
3left_cam = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_B)
4right_cam = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_C)
5
6# 请求摄像头的 NV12 流
7rgb_stream = rgb_cam.requestOutput(size=(1280, 720), type=dai.ImgFrame.Type.NV12)
8left_stream = left_cam.requestOutput(size=(800, 600), type=dai.ImgFrame.Type.NV12)
9right_stream = right_cam.requestOutput(size=(1280, 720), type=dai.ImgFrame.Type.NV12)
10
11# ...
12
13# 将所有摄像头添加到 Visualizer
14remote.addTopic("rgb", rgb_stream, group="Cameras")
15remote.addTopic("left", left_stream, group="Cameras")
16remote.addTopic("right", right_stream, group="Cameras")

对象检测可视化

Python
1# 设置检测网络
2nn = pipeline.create(dai.node.DetectionNetwork).build(
3    input=rgb_cam, # 用作神经网络输入的摄像头节点
4    model="luxonis/yolov6-nano:r2-coco-512x288", # 来自 Luxonis Hub 的模型 slug
5)
6nn.setConfidenceThreshold(0.5)
7
8# 将 RGB 预览和检测添加到 Visualizer
9remote.addTopic("preview", rgb_stream, group="Camera")
10remote.addTopic("detections", nn.out, group="Camera")