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

本页目录

  • 演示
  • 流水线
  • 源代码

神经网络多输入

Supported on:RVC2RVC4
利用 NeuralNetwork 节点运行一个神经网络模型,该模型将两个输入图像连接起来, 并在组合图像上运行“推理”。其中一个输入图像是在启动时从主机发送的静态图像(并且在每个帧中都会重复使用),另一个是来自摄像机的实时帧。

演示

此示例需要 DepthAI v3 API,请参阅 安装说明

流水线

源代码

Python

Python
GitHub
1#!/usr/bin/env python3
2import cv2
3import depthai as dai
4import numpy as np
5from pathlib import Path
6
7# Get the absolute path of the current script's directory
8script_dir = Path(__file__).resolve().parent
9examplesRoot = (script_dir / Path('../')).resolve()  # This resolves the parent directory correctly
10models = examplesRoot / 'models'
11tagImage = models / 'lenna.png'
12
13# Decode the image using OpenCV
14lenaImage = cv2.imread(str(tagImage.resolve()))
15lenaImage = cv2.resize(lenaImage, (256, 256))
16lenaImage = np.array(lenaImage)
17
18device = dai.Device()
19platform = device.getPlatform()
20if(platform == dai.Platform.RVC2):
21    daiType = dai.ImgFrame.Type.RGB888p
22elif(platform == dai.Platform.RVC4):
23    daiType = dai.ImgFrame.Type.RGB888i
24else:
25    raise RuntimeError("Platform not supported")
26
27daiLenaImage = dai.ImgFrame()
28
29daiLenaImage.setCvFrame(lenaImage, daiType)
30
31with dai.Pipeline(device) as pipeline:
32    model = dai.NNModelDescription("depthai-test-models/simple-concatenate-model")
33    model.platform = platform.name
34
35    nnArchive = dai.NNArchive(dai.getModelFromZoo(model))
36    cam = pipeline.create(dai.node.Camera).build()
37    camOut = cam.requestOutput((256,256), daiType)
38
39    neuralNetwork = pipeline.create(dai.node.NeuralNetwork)
40    neuralNetwork.setNNArchive(nnArchive)
41    camOut.link(neuralNetwork.inputs["image1"])
42    lennaInputQueue = neuralNetwork.inputs["image2"].createInputQueue()
43    # No need to send the second image everytime
44    neuralNetwork.inputs["image2"].setReusePreviousMessage(True)
45    qNNData = neuralNetwork.out.createOutputQueue()
46    pipeline.start()
47    lennaInputQueue.send(daiLenaImage)
48    while pipeline.isRunning():
49        inNNData: dai.NNData = qNNData.get()
50        tensor : np.ndarray = inNNData.getFirstTensor()
51        # Drop the first dimension
52        tensor = tensor.squeeze().astype(np.uint8)
53        # Check the shape - in case 3 is not the last dimension, permute it to the last
54        if tensor.shape[0] == 3:
55            tensor = np.transpose(tensor, (1, 2, 0))
56        print(tensor.shape)
57        cv2.imshow("Combined", tensor)
58        key = cv2.waitKey(1)
59        if key == ord('q'):
60            break

C++

1#include <atomic>
2#include <csignal>
3#include <iostream>
4#include <opencv2/opencv.hpp>
5#include <xtensor/containers/xarray.hpp>
6
7#include "depthai/depthai.hpp"
8#include "depthai/modelzoo/Zoo.hpp"
9
10std::atomic<bool> quitEvent(false);
11
12void signalHandler(int) {
13    quitEvent = true;
14}
15
16int main() {
17    signal(SIGTERM, signalHandler);
18    signal(SIGINT, signalHandler);
19
20    // Decode the image using OpenCV
21    cv::Mat lenaImage = cv::imread(LENNA_PATH);
22    cv::resize(lenaImage, lenaImage, cv::Size(256, 256));
23
24    // Create pipeline
25    dai::Pipeline pipeline;
26
27    // Create model description
28    dai::NNModelDescription model;
29    model.model = "depthai-test-models/simple-concatenate-model";
30    model.platform = pipeline.getDefaultDevice()->getPlatformAsString();
31    dai::NNArchive archive(dai::getModelFromZoo(model));
32
33    dai::ImgFrame::Type daiType;
34    if(pipeline.getDefaultDevice()->getPlatform() == dai::Platform::RVC2) {
35        daiType = dai::ImgFrame::Type::RGB888p;
36    } else {
37        daiType = dai::ImgFrame::Type::RGB888i;
38    }
39
40    // Create and set up nodes
41    auto cam = pipeline.create<dai::node::Camera>()->build();
42    auto camOut = cam->requestOutput(std::make_pair(256, 256), daiType);
43
44    auto neuralNetwork = pipeline.create<dai::node::NeuralNetwork>();
45    neuralNetwork->setNNArchive(archive);
46    camOut->link(neuralNetwork->inputs["image1"]);
47
48    auto lennaInputQueue = neuralNetwork->inputs["image2"].createInputQueue();
49    // No need to send the second image everytime
50    neuralNetwork->inputs["image2"].setReusePreviousMessage(true);
51
52    auto qNNData = neuralNetwork->out.createOutputQueue();
53
54    // Stt pipeline
55    pipeline.start();
56    // Create and set the image frame
57    auto daiLenaImage = std::make_shared<dai::ImgFrame>();
58    daiLenaImage->setCvFrame(lenaImage, daiType);
59    lennaInputQueue->send(daiLenaImage);
60
61    // Main loop
62    while(pipeline.isRunning() && !quitEvent) {
63        auto inNNData = qNNData->get<dai::NNData>();
64        auto tensor = inNNData->getFirstTensor<float>();
65        auto tensor_uint8 = xt::eval(xt::squeeze(xt::cast<uint8_t>(tensor), 0));
66
67        cv::Mat output;
68        if(tensor_uint8.shape()[0] == 3) {
69            tensor_uint8 = xt::transpose(tensor_uint8, {1, 2, 0});
70        }
71        output = cv::Mat(tensor_uint8.shape()[0], tensor_uint8.shape()[1], CV_8UC3);
72        std::memcpy(output.data, tensor_uint8.data(), tensor_uint8.size());
73
74        cv::imshow("Combined", output);
75
76        char key = cv::waitKey(1);
77        if(key == 'q') {
78            break;
79        }
80    }
81
82    pipeline.stop();
83    pipeline.wait();
84
85    return 0;
86}

需要帮助?

请前往 Discussion Forum 获取技术支持或提出您可能有的任何其他问题。