繁体   English   中英

如何在 C++ 中翻译解码(数据包)function?

[英]How do I translate Decode(Packet) function in C++?

我正在学习深度人工智能,我在他们的回购中找到了这个例子。 https://github.com/luxonis/depthai-experiments/tree/master/gen2-road-segmentation 我开始在 C++ 中翻译此代码,以与我正在整理的项目保持一致。 我遇到了这个名为“解码”的 function

def decode(packet):
    data = np.squeeze(toTensorResult(packet)["L0317_ReWeight_SoftMax"])
    class_colors = [[0, 0, 0], [0, 255, 0], [255, 0, 0], [0, 0, 255]]
    class_colors = np.asarray(class_colors, dtype=np.uint8)
    indices = np.argmax(data, axis=0)
    output_colors = np.take(class_colors, indices, axis=0)
    return output_colors

添加有关该问题的更多详细信息。 DepthAI 在其核心仓库 https://github.com/luxonis/depthai-core中提供了很多示例

我使用其中一些示例来开始构建分段脚本,因为我没有发现它是在所有示例之间用 C++ 编写的功能。

这是我到目前为止的进展。


#include <chrono>
#include "depthai-core/examples/utility/utility.hpp"
#include <depthai/depthai.hpp>
#include "slar.hpp"

using namespace slar;
using namespace std;
using namespace std::chrono;

static std::atomic<bool> syncNN{true};

void slar_depth_segmentation::segment(int argc, char **argv, dai::Pipeline &pipeline,
                                      cv::Mat frame,
                                      dai::Device *device_unused) {
    // blob model
    std::string nnPath("/Users/alessiograncini/road-segmentation-adas-0001.blob");
    if (argc > 1) {
        nnPath = std::string(argv[1]);
    }
    printf("Using blob at path: %s\n", nnPath.c_str());

    // in
    auto camRgb = pipeline.create<dai::node::ColorCamera>();
    auto imageManip = pipeline.create<dai::node::ImageManip>();
    auto mobilenetDet = pipeline.create<dai::node::MobileNetDetectionNetwork>();
    // out
    auto xoutRgb = pipeline.create<dai::node::XLinkOut>();
    auto nnOut = pipeline.create<dai::node::XLinkOut>();
    auto xoutManip = pipeline.create<dai::node::XLinkOut>();
    // stream names
    xoutRgb->setStreamName("camera");
    xoutManip->setStreamName("manip");
    nnOut->setStreamName("segmentation");
    //
    imageManip->initialConfig.setResize(300, 300);
    imageManip->initialConfig.setFrameType(dai::ImgFrame::Type::BGR888p);

    // properties
    camRgb->setPreviewSize(300, 300);
    camRgb->setBoardSocket(dai::CameraBoardSocket::RGB);
    camRgb->setResolution(dai::ColorCameraProperties::SensorResolution::THE_1080_P);
    camRgb->setInterleaved(false);
    camRgb->setColorOrder(dai::ColorCameraProperties::ColorOrder::RGB);
    //
    mobilenetDet->setConfidenceThreshold(0.5f);
    mobilenetDet->setBlobPath(nnPath);
    mobilenetDet->setNumInferenceThreads(2);
    mobilenetDet->input.setBlocking(false);
    // link
    camRgb->preview.link(xoutRgb->input);
    imageManip->out.link(mobilenetDet->input);
    //
    if (syncNN) {
        mobilenetDet->passthrough.link(xoutManip->input);
    } else {
        imageManip->out.link(xoutManip->input);
    }
    //
    mobilenetDet->out.link(nnOut->input);
    // device
    dai::Device device(pipeline);

    // queues
    auto previewQueue = device.getOutputQueue("camera", 4, false);
    auto detectionNNQueue = device.getOutputQueue("segmentation", 4, false);

    // fps
    auto startTime = steady_clock::now();
    int counter = 0;
    float fps = 0;
    auto color = cv::Scalar(255, 255, 255);

    // main
    while (true) {
        auto inRgb = previewQueue->get<dai::ImgFrame>();
        auto inSeg = detectionNNQueue->get<dai::NNData>();
        //?
        auto segmentations = inSeg->getData();
        //
        counter++;
        auto currentTime = steady_clock::now();
        auto elapsed = duration_cast<duration<float>>(currentTime - startTime);
        if(elapsed > seconds(1)) {
            fps = counter / elapsed.count();
            counter = 0;
            startTime = currentTime;
        }

        // testing if mat is a good replacement for
        // the input array as in "decode" the inSeg data is manipulated
        // cv::Mat img(500, 1000, CV_8UC1, cv::Scalar(70));
        // slar_depth_segmentation::draw(segmentations, frame);
        std::stringstream fpsStr;
        fpsStr << std::fixed << std::setprecision(2) << fps;

        cv::imshow("camera window", inRgb->getCvFrame());
        //cv::imshow("camera window", frame);


        int key = cv::waitKey(1);
        if (key == 'q' || key == 'Q') {
            break;
        }
    }
}


void slar_depth_segmentation::draw(cv::InputArray data, cv::OutputArray frame) {
    cv::addWeighted(frame, 1, data, 0.2, 0, frame);
}
//https://jclay.github.io/dev-journal/simple_cpp_argmax_argmin.html
void slar_depth_segmentation::decode( cv::InputArray data) {
    vector <int> class_colors [4] =
            {{0,0,0},{0,255,0},{255, 0, 0}, {0, 0, 255}};
  
}

我可以使用这个脚本成功地播放相机 - 但是你可以告诉你,翻译的唯一部分是绘制方法,即 py 和 c++ 的 function 等效项,因为它是 Z6AB69B2A586624BF91EDZD44 库的一部分。 我一直在尝试编写等效的解码方法。 谢谢

#I think you are looking for the 
cv::argmax # function.

cv::argmax # returns the index of the maximum value in an array.
cv::argmax # is available in OpenCV 3.4.3 and later.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM