C++样例
样例一
函数原型:
APP_ERROR SendProtobuf(const std::string& streamName, int inPluginId, std::vector<MxstProtobufIn>& protoVec);
- C++版 MxTools.MxpiVisionLists使用样例(MxpiVisionList.cpp)
#include <string.h> #include "MxBase/Log/Log.h" #include "MxStream/StreamManager/MxStreamManager.h" #include "MxTools/Proto/MxpiDataType.pb.h" APP_ERROR ReadFile(const std::string& filePath, MxStream::MxstDataInput& dataBuffer) { char c[PATH_MAX + 1] = { 0x00 }; size_t count = filePath.copy(c, PATH_MAX + 1); if (count != filePath.length()) { LogError << "Failed to strcpy " << c; return APP_ERR_COMM_FAILURE; } char path[PATH_MAX + 1] = { 0x00 }; if ((strlen(c) > PATH_MAX) || (realpath(c, path) == nullptr)) { LogError << "Failed to get image path(" << filePath << ")."; return APP_ERR_COMM_NO_EXIST; } FILE *fp = fopen(path, "rb"); if (fp == nullptr) { LogError << "Failed to open file"; return APP_ERR_COMM_OPEN_FAIL; } fseek(fp, 0, SEEK_END); long fileSize = ftell(fp); fseek(fp, 0, SEEK_SET); if (fileSize > 0) { dataBuffer.dataSize = fileSize; dataBuffer.dataPtr = new (std::nothrow) uint32_t[fileSize]; if (dataBuffer.dataPtr == nullptr) { LogError << "allocate memory with \"new uint32_t\" failed."; return APP_ERR_COMM_FAILURE; } uint32_t readRet = fread(dataBuffer.dataPtr, 1, fileSize, fp); if (readRet <= 0) { fclose(fp); return APP_ERR_COMM_READ_FAIL; } fclose(fp); return APP_ERR_OK; } fclose(fp); return APP_ERR_COMM_FAILURE; } std::string ReadPipelineConfig(std::string &pipelineConfigPath) { std::ifstream file(pipelineConfigPath.c_str(), std::ifstream::binary); if (!file) { LogError << pipelineConfigPath <<" file is not exists"; return ""; } file.seekg(0, std::ifstream::end); uint32_t fileSize = file.tellg(); file.seekg(0); std::unique_ptr<char[]> data(new char[fileSize]); file.read(data.get(), fileSize); file.close(); std::string pipelineConfig(data.get(), fileSize); return pipelineConfig; } int main(int argc, char*argv[]) { MxStream::MxstDataInput dataInput; APP_ERROR ret = ReadFile("./test.yuv", dataInput); if (ret != APP_ERR_OK) { LogError << "Failed to read image file, ret = " << ret; return ret; } std::string pipelineConfigPath = "./Sample.pipeline"; std::string pipelineConfig = ReadPipelineConfig(pipelineConfigPath); if (pipelineConfig == "") { return APP_ERR_COMM_INIT_FAIL; } auto *mxStreamManager = new MxStream::MxStreamManager(); ret = mxStreamManager->InitManager(); if (ret != APP_ERR_OK) { LogError << "Failed to init Stream manager, ret = " << ret << "."; return ret; } ret = mxStreamManager->CreateMultipleStreams(pipelineConfig); if (ret != APP_ERR_OK) { LogError << "Failed to create Stream, ret = " << ret << "."; return ret; } std::shared_ptr<MxTools::MxpiVisionList> objectList = std::make_shared<MxTools::MxpiVisionList>(); MxTools::MxpiVision* MxpiVision = objectList->add_visionvec(); MxTools::MxpiVisionInfo *visioninfo = MxpiVision->mutable_visioninfo(); visioninfo->set_format(12); visioninfo->set_width(416); visioninfo->set_height(416); visioninfo->set_widthaligned(416); visioninfo->set_heightaligned(416); MxTools::MxpiVisionData *visiondata = MxpiVision->mutable_visiondata(); visiondata->set_dataptr((uint64_t)dataInput.dataPtr); visiondata->set_datasize(dataInput.dataSize); visiondata->set_memtype(MxTools::MXPI_MEMORY_HOST_NEW); MxStream::MxstProtobufIn dataBuffer; dataBuffer.key = "appsrc0"; dataBuffer.messagePtr = std::static_pointer_cast<google::protobuf::Message>(objectList); std::vector<MxStream::MxstProtobufIn> dataBufferVec; dataBufferVec.push_back(dataBuffer); std::string streamName = "classification"; int inPluginId = 0; std::vector<std::string> keyVec; keyVec.push_back(dataBuffer.key); ret = mxStreamManager->SendProtobuf(streamName, inPluginId, dataBufferVec); std::vector<MxStream::MxstProtobufOut> output = mxStreamManager->GetProtobuf(streamName, inPluginId, keyVec); if (output.size() == 0) { LogError << "output size is 0"; return APP_ERR_ACL_FAILURE; } if (output[0].errorCode != APP_ERR_OK) { LogError << "GetProtobuf error. errorCode=" << output[0].errorCode; return output[0].errorCode; } LogInfo << "errorCode=" << output[0].errorCode; LogInfo << "key=" << output[0].messageName; LogInfo << "value=" << output[0].messagePtr.get()->DebugString(); mxStreamManager->DestroyAllStreams(); delete mxStreamManager; mxStreamManager = nullptr; return 0; }
- Sample.pipeline
{ "classification": { "stream_config": { "deviceId": "0" }, "appsrc0": { "props": { "blocksize": "409600" }, "factory": "appsrc", "next": "mxpi_datatransfer0" }, "mxpi_datatransfer0": { "props": { "dataSource": "appsrc0", "transferMode": "auto", "removeSourceData": "yes" }, "factory": "mxpi_datatransfer", "next": "mxpi_tensorinfer0" }, "mxpi_tensorinfer0": { "props": { "dataSource": "mxpi_datatransfer0", "modelPath": "../models/yolov3/yolov3_tf_bs1_fp16.om" }, "factory": "mxpi_tensorinfer", "next": "mxpi_objectpostprocessor0" }, "mxpi_objectpostprocessor0": { "props": { "dataSource": "mxpi_tensorinfer0", "postProcessConfigPath": "../models/yolov3/yolov3_tf_bs1_fp16.cfg", "labelPath": "../models/yolov3/yolov3.names", "postProcessLibPath": "libyolov3postprocess.so" }, "factory": "mxpi_objectpostprocessor", "next": "mxpi_dataserialize0" }, "mxpi_dataserialize0": { "props": { "outputDataKeys": "mxpi_objectpostprocessor0" }, "factory": "mxpi_dataserialize", "next": "appsink0" }, "appsink0": { "props": { "blocksize": "4096000" }, "factory": "appsink" } } }
- 编译脚本“run.sh”。
set -e CUR_PATH=$(cd "$(dirname "$0")" || { warn "Failed to check path/to/run.sh" ; exit ; } ; pwd) # Simple log helper functions info() { echo -e "\033[1;34m[INFO ][MxStream] $1\033[1;37m" ; } warn() { echo >&2 -e "\033[1;31m[WARN ][MxStream] $1\033[1;37m" ; } export MX_SDK_HOME="${CUR_PATH}/../../.." export LD_LIBRARY_PATH="${MX_SDK_HOME}/lib":"${MX_SDK_HOME}/opensource/lib":"${MX_SDK_HOME}/opensource/lib64":"/usr/local/Ascend/ascend-toolkit/latest/acllib/lib64":"/usr/local/Ascend/driver/lib64":${LD_LIBRARY_PATH} export GST_PLUGIN_SCANNER="${MX_SDK_HOME}/opensource/libexec/gstreamer-1.0/gst-plugin-scanner" export GST_PLUGIN_PATH="${MX_SDK_HOME}/opensource/lib/gstreamer-1.0":"${MX_SDK_HOME}/lib/plugins" # complie cmake -S . -Bbuild make -C ./build -j # run ./main exit 0
样例二
函数原型:
APP_ERROR SendProtobuf(const std::string& streamName, const std::string& elementName, std::vector<MxstProtobufIn>& protoVec);
std::string elementName = "appsrc0" APP_ERROR ret = mxStreamManager->SendProtobuf(streamName, elementName, mxstProtobufInVec); if (ret != APP_ERR_OK) { LogError << GetError(ret) << "SendProtobuf failed."; return ret; } std::vector<std::string> keys = {"appsrc0"}; std::vector<MxStream::MxstProtobufOut> mxstProtobufOutVec = mxStreamManager->GetProtobuf(streamName, outPluginId, keys); for (uint32_t i = 0; i < mxstProtobufOutVec.size(); i++) { auto mxstProtobufOut = mxstProtobufOutVec[i]; if (mxstProtobufOut.errorCode != APP_ERR_OK) { LogError << GetError(mxstProtobufOut.errorCode) << "GetProtobuf failed"; return mxstProtobufOut.errorCode; } LogInfo << "result:" << mxstProtobufOut.messagePtr->DebugString(); }