NeuralNetwork
- Open Model Zoo(200 多个预训练模型)
- DepthAI 模型库
.blob、.superblob 或 NNArchive)。在 Python 中构建 NeuralNetwork 节点
build() 类方法之一,在一次调用中构建并链接节点:Python
1import depthai as dai
2
3# 1. 从张量输入 + NNArchive
4tensor_input = ... # 例如其他节点的输出
5nn_archive = dai.NNArchive('path/to/archive.tar.gz')
6nn = dai.node.NeuralNetwork.build(tensor_input, nn_archive)
7
8# 2. 从 Camera 节点 + NNModelDescription(+ 可选 fps)
9cam = pipeline.create(dai.node.ColorCamera)
10model_desc = dai.NNModelDescription(
11 model='yolov6-nano',
12 platform='' # 留空以自动检测
13)
14nn = dai.node.NeuralNetwork.build(cam, model_desc, fps=30.0)
15
16# 3. 从 ReplayVideo 节点 + NNArchive(+ 可选 fps)
17replay = pipeline.create(dai.node.ReplayVideo)
18replay.setSourcePath('video.mp4')
19nn = dai.node.NeuralNetwork.build(replay, nn_archive, fps=15.0)- 下载或接受本地模型归档
- 验证归档是否为 NNArchive 格式
- 配置输入帧能力(分辨率、类型、帧率)
- 直接将相机或张量输出链接到
nn.input
手动实例化
Python
1pipeline = dai.Pipeline()
2
3# 创建节点
4nn = pipeline.create(dai.node.NeuralNetwork)
5
6# 加载 NNArchive
7nn_archive = dai.NNArchive('path/to/archive.tar.gz')
8
9# 将 NNArchive 设置到 NN 节点
10nn.setNNArchive('path/to/archive.tar.gz')输入与输出
| 输入 | 类型 | 描述 |
|---|---|---|
input | 任意张量/ImgFrame | 用于推理的张量或 ImgFrame |
passthrough | ImgFrame | 原始帧 |
| 输出 | 类型 | 描述 |
|---|---|---|
out | NNData | 推理结果(层 blob、输出) |
示例与实验
- 神经网络 - 创建一个包含相机和神经网络节点的简单管线。
- 神经网络多输入 - 运行一个将相机帧与静态图像拼接的神经网络模型,使用两个输入张量。
- 神经网络多输入组合 - 运行一个将两个输入图像合并为一个输出图像的神经网络模型。
参考
class
dai::node::NeuralNetwork
variable
Input input
Input message with data to be inferred upon
variable
variable
Output passthrough
Passthrough message on which the inference was performed.Suitable for when input queue is set to non-blocking behavior.
variable
InputMap inputs
Inputs mapped to network inputs. Useful for inferring from separate data sources Default input is non-blocking with queue size 1 and waits for messages
variable
OutputMap passthroughs
Passthroughs which correspond to specified input
function
~NeuralNetwork()function
std::shared_ptr< NeuralNetwork > build(Node::Output & input, const NNArchive & nnArchive)function
std::shared_ptr< NeuralNetwork > build(const std::shared_ptr< Camera > & input, const Model & model, std::optional< float > fps, std::optional< dai::ImgResizeMode > resizeMode)function
std::shared_ptr< NeuralNetwork > build(const std::shared_ptr< Camera > & input, const Model & model, const ImgFrameCapability & capability)function
std::shared_ptr< NeuralNetwork > build(const std::shared_ptr< ReplayVideo > & input, const Model & model, std::optional< float > fps)function
std::optional< std::reference_wrapper< const NNArchive > > getNNArchive()function
void setNNArchive(const NNArchive & nnArchive)function
void setNNArchive(const NNArchive & nnArchive, int numShaves)function
void setFromModelZoo(NNModelDescription description, bool useCached)function
void setBlobPath(const std::filesystem::path & path)Load network blob into assets and use once pipeline is started.
Parameters
- Error: if file doesn't exist or isn't a valid network blob.
Parameters
- path: Path to network blob
function
void setBlob(OpenVINO::Blob blob)Load network blob into assets and use once pipeline is started.
Parameters
- blob: Network blob
function
void setBlob(const std::filesystem::path & path)Same functionality as the setBlobPath(). Load network blob into assets and use once pipeline is started.
Parameters
- Error: if file doesn't exist or isn't a valid network blob.
Parameters
- path: Path to network blob
function
void setOtherModelFormat(std::vector< uint8_t > model)Load network model into assets and use once pipeline is started.
Parameters
- model: Network model
function
void setOtherModelFormat(const std::filesystem::path & path)Load network model into assets and use once pipeline is started.
Parameters
- Error: if file doesn't exist or isn't a valid network model.
Parameters
- path: Path to the network model
function
void setModelPath(const std::filesystem::path & modelPath)Load network xml and bin files into assets.
Parameters
- xmlModelPath: Path to the neural network model file.
function
void setNumPoolFrames(int numFrames)Specifies how many frames will be available in the pool
Parameters
- numFrames: How many frames will pool have
function
void setNumInferenceThreads(int numThreads)How many threads should the node use to run the network.
Parameters
- numThreads: Number of threads to dedicate to this node
function
void setNumNCEPerInferenceThread(int numNCEPerThread)How many Neural Compute Engines should a single thread use for inference
Parameters
- numNCEPerThread: Number of NCE per thread
function
void setNumShavesPerInferenceThread(int numShavesPerThread)How many Shaves should a single thread use for inference
Parameters
- numShavesPerThread: Number of shaves per thread
function
void setBackend(const std::string & backend)Specifies backend to use
Parameters
- backend: String specifying backend to use
function
void setBackendProperties(const std::map< std::string, std::string > & properties)Set backend properties
Parameters
- backendProperties: backend properties map
function
int getNumInferenceThreads()How many inference threads will be used to run the network
Returns
Number of threads, 0, 1 or 2. Zero means AUTO
function
void setModelFromDeviceZoo(DeviceModelZoo model)Set model from Device Model Zoo
Parameters
- model: DeviceModelZoo model enum
Parameters
Only applicable for RVC4 devices with OS 1.20.5 or higher
inline function
DeviceNodeCRTP()inline function
DeviceNodeCRTP(const std::shared_ptr< Device > & device)inline function
DeviceNodeCRTP(std::unique_ptr< Properties > props)inline function
DeviceNodeCRTP(std::unique_ptr< Properties > props, bool confMode)inline function
DeviceNodeCRTP(const std::shared_ptr< Device > & device, std::unique_ptr< Properties > props, bool confMode)enum
std::variant< NNModelDescription, NNArchive, std::string > Model
需要帮助?
请前往 OAKChina 官网 获取技术支持或解答您的任何疑问。