簡體   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