physicalai-runtime-adding-a-camera-backend
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAdding a Camera Backend
添加相机后端
Cameras implement the interface in . Factory entry: in maps lowercase type names (, , , …) to implementations. Reference layouts: , , .
Camerasrc/physicalai/capture/camera.pycreate_camera()src/physicalai/capture/factory.pyuvcrealsensebaslersrc/physicalai/capture/cameras/uvc/src/physicalai/capture/cameras/realsense/src/physicalai/capture/cameras/basler/相机需实现中的接口。工厂入口:中的会将小写类型名称(、、等)映射到对应实现。参考目录结构:、、。
src/physicalai/capture/camera.pyCamerasrc/physicalai/capture/factory.pycreate_camera()uvcrealsensebaslersrc/physicalai/capture/cameras/uvc/src/physicalai/capture/cameras/realsense/src/physicalai/capture/cameras/basler/Workflow
工作流程
- Pick a reference backend closest to the new hardware (UVC for USB video, RealSense for RGB-D, Basler for GenICam industrial).
- Done when: you can list which modules to mirror (,
_camera.py,_discover.pyexports).__init__.py
- Done when: you can list which modules to mirror (
- Implement :
Camera,connect(),disconnect()/read(), context manager support, monotonic timestamps onread_latest()(Frame).src/physicalai/capture/frame.py- Done when: fake or mocked device tests can exercise connect/read without hardware.
- Wire discovery if the device is enumerable — add helpers under or backend-specific
src/physicalai/capture/discovery.py._discover.py - Register the type in and export public class from
src/physicalai/capture/factory.pywhen user-facing.src/physicalai/capture/__init__.py - Optional extra in for vendor SDKs; lazy-import inside the camera module so
pyproject.tomlstays light.pip install physicalai- Done when: works without the extra; importing the camera class fails with a clear message if the extra is missing.
import physicalai.capture
- Done when:
- Tests in using existing fakes (
tests/unit/capture/,tests/unit/capture/fake.pypatterns).conftest.py- Done when: passes.
uv run pytest tests/unit/capture -k <backend>
- Done when:
- 选择最贴近新硬件的参考后端(UVC适用于USB视频设备,RealSense适用于RGB-D设备,Basler适用于GenICam工业相机)。
- 完成标志:可列出需要镜像的模块(、
_camera.py、_discover.py导出内容)。__init__.py
- 完成标志:可列出需要镜像的模块(
- 实现接口:
Camera、connect()、disconnect()/read()、上下文管理器支持,以及为read_latest()(Frame)添加单调时间戳。src/physicalai/capture/frame.py- 完成标志:无需硬件即可通过虚拟或模拟设备测试执行连接/读取操作。
- 配置设备发现功能(如果设备可枚举)——在或后端专属的
src/physicalai/capture/discovery.py中添加辅助工具。_discover.py - 在中注册类型,若面向用户,则从
src/physicalai/capture/factory.py导出公共类。src/physicalai/capture/__init__.py - 为厂商SDK配置可选额外依赖(在中);在相机模块内使用延迟导入,确保
pyproject.toml保持轻量。pip install physicalai- 完成标志:无需安装额外依赖即可正常;若缺少额外依赖,导入相机类时会显示清晰的错误提示。
import physicalai.capture
- 完成标志:无需安装额外依赖即可正常
- 在中编写测试,使用现有虚拟设备(
tests/unit/capture/、tests/unit/capture/fake.py中的模式)。conftest.py- 完成标志:执行测试通过。
uv run pytest tests/unit/capture -k <backend>
- 完成标志:执行
Shared transport
共享传输
For multi-process access, wraps with ( / extra, iceoryx2). Only document shared mode when the transport extra is installed.
create_camera(..., shared=True)SharedCameraphysicalai[capture]transport对于多进程访问场景,会通过(需安装 / 额外依赖,基于iceoryx2)进行包装。仅当安装了传输额外依赖时,才需文档化共享模式。
create_camera(..., shared=True)SharedCameraphysicalai[capture]transportRequired checks
必要检查
- or factory string is documented in
CameraTypewhen user-visible.docs/reference/camera-api.md - Frame shapes and dtypes match README examples (RGB ).
(H, W, 3) - No blocking discovery at import time.
- 若面向用户,需在中记录
docs/reference/camera-api.md或工厂字符串。CameraType - 帧的形状和数据类型需与README示例一致(RGB格式为)。
(H, W, 3) - 导入时不得执行阻塞式设备发现操作。
Verify
验证
bash
uv run pytest tests/unit/capture -q
prek run --all-filesbash
uv run pytest tests/unit/capture -q
prek run --all-files