# 摄像头

Camera 节点是 [ImgFrame](https://docs.luxonis.com/software/depthai-components/messages/img_frame.md) 的数据源。您可以通过 InputControl 和
InputConfig 在运行时对其进行控制。

它旨在将 [Color Camera](https://docs.luxonis.com/software/depthai-components/nodes/color_camera.md) 和
[MonoCamera](https://docs.luxonis.com/software/depthai-components/nodes/mono_camera.md) 统一为一个节点。

与 [Color Camera](https://docs.luxonis.com/software/depthai-components/nodes/color_camera.md) 节点相比，Camera 节点：

 * 支持 cam.setSize()，它取代了 cam.setResolution() 和 cam.setIspScale()。Camera 节点会自动找到最合适的分辨率，并应用正确的缩放以实现用户选择的大小。
 * 支持 cam.setCalibrationAlpha()，示例如下：[消除相机畸变](https://docs.luxonis.com/software/depthai/examples/camera_undistort.md)
 * 支持 cam.loadMeshData() 和 cam.setMeshStep()，可用于自定义图像扭曲（去畸变、透视校正等）。
 * 如果相机的 HFOV 大于 85°，则会自动消除相机畸变。您可以通过 cam.setMeshSource(dai.CameraProperties.WarpMeshSource.NONE) 禁用此功能。

除上述几点外，与 [MonoCamera](https://docs.luxonis.com/software/depthai-components/nodes/mono_camera.md) 节点相比，Camera 节点：

 * 没有 out 输出，因为它具有与 [ColorCamera](https://docs.luxonis.com/software/depthai-components/nodes/color_camera.md)
   相同的输出（raw、isp、still、preview、video）。这意味着 preview 将输出同一灰度帧的 3 个平面（3 倍开销），而 isp / video / still 将输出亮度（有用的灰度信息）+ 色度（所有值均为 128），这将导致
   1.5 倍的带宽开销。

## 如何放置

#### Python

```python
pipeline = dai.Pipeline()
cam = pipeline.create(dai.node.Camera)
```

#### C++

```cpp
dai::Pipeline pipeline;
auto cam = pipeline.create<dai::node::Camera>();
```

## 输入和输出

消息类型

 * inputConfig - [ImageManipConfig](https://docs.luxonis.com/software/depthai-components/messages/image_manip_config.md)
 * inputControl - [CameraControl](https://docs.luxonis.com/software/depthai-components/messages/camera_control.md)
 * raw - [ImgFrame](https://docs.luxonis.com/software/depthai-components/messages/img_frame.md) - RAW10 Bayer
   数据。解包演示代码参见[此处](https://github.com/luxonis/oak-examples/blob/3f1b2b2/gen2-color-isp-raw/main.py#L13-L32)
 * isp - [ImgFrame](https://docs.luxonis.com/software/depthai-components/messages/img_frame.md) - YUV420 平面格式（与 YU12/IYUV/I420 相同）
 * still - [ImgFrame](https://docs.luxonis.com/software/depthai-components/messages/img_frame.md) - NV12，适用于较大尺寸的帧。当向 Camera
   发送捕获事件时创建图像，类似于拍照。
 * preview - [ImgFrame](https://docs.luxonis.com/software/depthai-components/messages/img_frame.md) - RGB（如果配置了 BGR
   平面或交错格式），主要用于小尺寸预览并馈送至 [NeuralNetwork](https://docs.luxonis.com/software/depthai-components/nodes/neural_network.md)
 * video - [ImgFrame](https://docs.luxonis.com/software/depthai-components/messages/img_frame.md) - NV12，适用于较大尺寸的帧

ISP（图像信号处理器）用于 Bayer 变换、去马赛克、降噪以及其他图像增强。 它与 3A 算法交互：自动对焦、自动曝光和自动白平衡，这些算法在运行时处理图像传感器 的调整，例如曝光时间、感光度（ISO）以及镜头位置（如果相机模块具有电动镜头）。
点击[此处](https://en.wikipedia.org/wiki/Image_processor)了解更多信息。

图像后处理将 ISP 输出的 YUV420 平面帧转换为 video/preview/still 帧。

still（当触发捕获时）和 isp 以最高相机分辨率工作，而 video 和 preview 限制为最大 4K（3840 x 2160）分辨率，该分辨率从 isp 中裁剪得到。 对于 IMX378（1200 万像素），后处理的工作原理如下：

上图显示了 Camera 的 isp 输出（IMX378 的 1200 万像素分辨率）。如果您不缩小 ISP， video 输出将裁剪为 4K（最大 3840x2160，受 video 输出限制），如蓝色矩形所示。 黄色矩形表示 preview
输出在预览大小设置为 1:1 纵横比时的裁剪（例如，为 MobileNet-SSD NN 模型使用 300x300 的预览大小），因为 preview 输出源自 video 输出。

## 使用

#### Python

```python
pipeline = dai.Pipeline()
cam = pipeline.create(dai.node.Camera)
cam.setPreviewSize(300, 300)
cam.setBoardSocket(dai.CameraBoardSocket.CAM_A)
# Instead of setting the resolution, user can specify size, which will set
# sensor resolution to best fit, and also apply scaling
cam.setSize(1280, 720)
```

#### C++

```cpp
dai::Pipeline pipeline;
auto cam = pipeline.create<dai::node::Camera>();
cam->setPreviewSize(300, 300);
cam->setBoardSocket(dai::CameraBoardSocket::CAM_A);
// Instead of setting the resolution, user can specify size, which will set
// sensor resolution to best fit, and also apply scaling
cam->setSize(1280, 720);
```

## 3A 算法

3A（自动曝光 AE、自动白平衡 AWB 和自动对焦 AF）算法用于优化图像质量，并直接在 RVC 上运行。

默认情况下，这些设置处于自动模式，每个传感器都有特定的限制（例如最小/最大曝光），详见[支持的传感器](https://docs.luxonis.com/hardware/platform/sensors/sensors.md)。

您可以通过以下方式手动控制这些设置：按照 [RGB 相机控制示例](https://docs.luxonis.com/software/depthai/examples/rgb_camera_control.md) 中的步骤操作，或使用
[cam_test.py 脚本](https://github.com/luxonis/depthai-python/blob/main/utilities/cam_test.py)。

 * 立体相机：传感器共享同一条 I2C 总线，可自动同步 3A 设置（AWB、AE）。
 * 独立传感器：在 OAK FFC 或 OAK-D-LR 等配置中，每个传感器拥有独立的 I2C，可使用 3a-follow 功能将 3A 设置从一个传感器同步到其他传感器。

示例用法

```python
cam['cam_b'].initialControl.setMisc("3a-follow", dai.CameraBoardSocket.CAM_A)
cam['cam_c'].initialControl.setMisc("3a-follow", dai.CameraBoardSocket.CAM_A)
```

3a-follow 功能将主摄像头（例如 CAM_A）的 3A 设置（曝光、ISO 和白平衡）复制到配置中的其他摄像头（例如 CAM_B 和 CAM_C）。

## 限制

以下是已知的 [RVC2](https://docs.luxonis.com/hardware/platform/rvc/rvc2.md#rvc2) 相机限制：

 * ISP 每秒可处理约 600 MP，当流水线同时运行神经网络和视频编码器时，约 500 MP/s。
 * 3A 算法总体可处理约 200..250 FPS（针对所有相机流）。这是我们当前实现的限制，我们计划通过一种变通方法让 3A 算法每隔 X 帧运行一次，暂无预计时间。

## 功能示例

 * [消除相机畸变](https://docs.luxonis.com/software/depthai/examples/script_camera_control.md)

## 参考

### depthai.node.Camera(depthai.Node)

Kind: Class

Camera node. Experimental node, for both mono and color types of sensors

#### getBoardSocket(self) -> depthai.CameraBoardSocket: depthai.CameraBoardSocket

Kind: Method

Retrieves which board socket to use

Returns:
Board socket to use

#### getCalibrationAlpha(self) -> float|None: float|None

Kind: Method

Get calibration alpha parameter that determines FOV of undistorted frames

#### getCamera(self) -> str: str

Kind: Method

Retrieves which camera to use by name

Returns:
Name of the camera to use

#### getFps(self) -> float: float

Kind: Method

Get rate at which camera should produce frames

Returns:
Rate in frames per second

#### getHeight(self) -> int: int

Kind: Method

Get sensor resolution height

#### getImageOrientation(self) -> depthai.CameraImageOrientation: depthai.CameraImageOrientation

Kind: Method

Get camera image orientation

#### getMeshSource(self) -> depthai.CameraProperties.WarpMeshSource: depthai.CameraProperties.WarpMeshSource

Kind: Method

Gets the source of the warp mesh

#### getMeshStep(self) -> tuple[int, int]: tuple[int, int]

Kind: Method

Gets the distance between mesh points

#### getPreviewHeight(self) -> int: int

Kind: Method

Get preview height

#### getPreviewSize(self) -> tuple[int, int]: tuple[int, int]

Kind: Method

Get preview size as tuple

#### getPreviewWidth(self) -> int: int

Kind: Method

Get preview width

#### getSize(self) -> tuple[int, int]: tuple[int, int]

Kind: Method

Get sensor resolution as size

#### getStillHeight(self) -> int: int

Kind: Method

Get still height

#### getStillSize(self) -> tuple[int, int]: tuple[int, int]

Kind: Method

Get still size as tuple

#### getStillWidth(self) -> int: int

Kind: Method

Get still width

#### getVideoHeight(self) -> int: int

Kind: Method

Get video height

#### getVideoSize(self) -> tuple[int, int]: tuple[int, int]

Kind: Method

Get video size as tuple

#### getVideoWidth(self) -> int: int

Kind: Method

Get video width

#### getWidth(self) -> int: int

Kind: Method

Get sensor resolution width

#### loadMeshData(self, warpMesh: typing_extensions.Buffer)

Kind: Method

Specify mesh calibration data for undistortion See `loadMeshFiles` for the
expected data format

#### loadMeshFile(self, warpMesh: Path)

Kind: Method

Specify local filesystem paths to the undistort mesh calibration files.

When a mesh calibration is set, it overrides the camera intrinsics/extrinsics
matrices. Overrides useHomographyRectification behavior. Mesh format: a sequence
of (y,x) points as 'float' with coordinates from the input image to be mapped in
the output. The mesh can be subsampled, configured by `setMeshStep`.

With a 1280x800 resolution and the default (16,16) step, the required mesh size
is:

width: 1280 / 16 + 1 = 81

height: 800 / 16 + 1 = 51

#### setBoardSocket(self, boardSocket: depthai.CameraBoardSocket)

Kind: Method

Specify which board socket to use

Parameter ``boardSocket``:
Board socket to use

#### setCalibrationAlpha(self, alpha: typing.SupportsFloat)

Kind: Method

Set calibration alpha parameter that determines FOV of undistorted frames

#### setCamera(self, name: str)

Kind: Method

Specify which camera to use by name

Parameter ``name``:
Name of the camera to use

#### setFps(self, fps: typing.SupportsFloat)

Kind: Method

Set rate at which camera should produce frames

Parameter ``fps``:
Rate in frames per second

#### setImageOrientation(self, imageOrientation: depthai.CameraImageOrientation)

Kind: Method

Set camera image orientation

#### setIsp3aFps(self, isp3aFps: typing.SupportsInt)

Kind: Method

Isp 3A rate (auto focus, auto exposure, auto white balance, camera controls
etc.). Default (0) matches the camera FPS, meaning that 3A is running on each
frame. Reducing the rate of 3A reduces the CPU usage on CSS, but also increases
the convergence rate of 3A. Note that camera controls will be processed at this
rate. E.g. if camera is running at 30 fps, and camera control is sent at every
frame, but 3A fps is set to 15, the camera control messages will be processed at
15 fps rate, which will lead to queueing.

#### setMeshSource(self, source: depthai.CameraProperties.WarpMeshSource)

Kind: Method

Set the source of the warp mesh or disable

#### setMeshStep(self, width: typing.SupportsInt, height: typing.SupportsInt)

Kind: Method

Set the distance between mesh points. Default: (32, 32)

#### setPreviewSize()

Kind: Method

#### setRawOutputPacked(self, packed: bool)

Kind: Method

Configures whether the camera `raw` frames are saved as MIPI-packed to memory.
The packed format is more efficient, consuming less memory on device, and less
data to send to host: RAW10: 4 pixels saved on 5 bytes, RAW12: 2 pixels saved on
3 bytes. When packing is disabled (`false`), data is saved lsb-aligned, e.g. a
RAW10 pixel will be stored as uint16, on bits 9..0: 0b0000'00pp'pppp'pppp.
Default is auto: enabled for standard color/monochrome cameras where ISP can
work with both packed/unpacked, but disabled for other cameras like ToF.

#### setSize()

Kind: Method

#### setStillSize()

Kind: Method

#### setVideoSize()

Kind: Method

#### frameEvent

Kind: Property

Outputs metadata-only ImgFrame message as an early indicator of an incoming
frame.

It's sent on the MIPI SoF (start-of-frame) event, just after the exposure of the
current frame has finished and before the exposure for next frame starts. Could
be used to synchronize various processes with camera capture. Fields populated:
camera id, sequence number, timestamp

#### initialControl

Kind: Property

Initial control options to apply to sensor

#### inputConfig

Kind: Property

Input for ImageManipConfig message, which can modify crop parameters in runtime

Default queue is non-blocking with size 8

#### inputControl

Kind: Property

Input for CameraControl message, which can modify camera parameters in runtime

Default queue is blocking with size 8

#### isp

Kind: Property

Outputs ImgFrame message that carries YUV420 planar (I420/IYUV) frame data.

Generated by the ISP engine, and the source for the 'video', 'preview' and
'still' outputs

#### preview

Kind: Property

Outputs ImgFrame message that carries BGR/RGB planar/interleaved encoded frame
data.

Suitable for use with NeuralNetwork node

#### raw

Kind: Property

Outputs ImgFrame message that carries RAW10-packed (MIPI CSI-2 format) frame
data.

Captured directly from the camera sensor, and the source for the 'isp' output.

#### still

Kind: Property

Outputs ImgFrame message that carries NV12 encoded (YUV420, UV plane
interleaved) frame data.

The message is sent only when a CameraControl message arrives to inputControl
with captureStill command set.

#### video

Kind: Property

Outputs ImgFrame message that carries NV12 encoded (YUV420, UV plane
interleaved) frame data.

Suitable for use with VideoEncoder node

### 需要帮助？

请前往 [OAKChina 官网](https://www.oakchina.cn/) 获取技术支持或解答您的任何疑问。
