C++样例
样例一
函数原型:
APP_ERROR SendMultiDataWithUniqueId(const std::string& streamName, std::vector<int> inPluginIdVec, std::vector<MxstDataInput>& dataBufferVec, uint64_t& uniqueId);
#include <cstring> #include "MxBase/Log/Log.h" #include "MxStream/StreamManager/MxStreamManager.h" #include "MxStream/StreamManager/MxsmDataType.h" using namespace MxStream; namespace { 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; } // Get the absolute path of input file 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; } // Open file with reading mode FILE *fp = fopen(path, "rb"); if (fp == nullptr) { LogError << "Failed to open file"; return APP_ERR_COMM_OPEN_FAIL; } // Get the length of input file fseek(fp, 0, SEEK_END); long fileSize = ftell(fp); fseek(fp, 0, SEEK_SET); // If file not empty, read it into FileInfo and return it 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(const std::string& pipelineConfigPath) { std::ifstream file(pipelineConfigPath.c_str(), std::ifstream::binary); if (!file) { 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[]) { // read image file and build stream input MxStream::MxstDataInput dataBuffer; APP_ERROR ret = ReadFile("./test.jpg", dataBuffer); if (ret != APP_ERR_OK) { LogError << "Failed to read image file, ret = " << ret; return ret; } // read pipeline config file std::string pipelineConfigPath = "../pipeline/Sample.pipeline"; std::string pipelineConfig = ReadPipelineConfig(pipelineConfigPath); if (pipelineConfig == "") { return APP_ERR_COMM_INIT_FAIL; } // init stream manager MxStream::MxStreamManager mxStreamManager; ret = mxStreamManager.InitManager(); if (ret != APP_ERR_OK) { LogError << "Failed to init Stream manager, ret = " << ret << "."; return ret; } // create stream by pipeline config file ret = mxStreamManager.CreateMultipleStreams(pipelineConfig); if (ret != APP_ERR_OK) { LogError << "Failed to create Stream, ret = " << ret << "."; return ret; } std::string streamName = "classification+detection"; // send data into stream std::vector<MxstDataInput> dataBufferVec{dataBuffer}; std::vector<int> inPluginIdVec{0}; uint64_t uniqueId; ret = mxStreamManager.SendMultiDataWithUniqueId(streamName, inPluginIdVec, dataBufferVec, uniqueId); if (ret != APP_ERR_OK ) { LogError << "Failed to send data to stream, ret = " << ret << "."; return ret; } // get stream output std::vector<MxstDataOutput*> outputVec = mxStreamManager.GetMultiResultWithUniqueId(streamName, uniqueId, 3000); for (MxstDataOutput* output : outputVec) { if (output == nullptr) { LogError << "Failed to get pipeline output"; return ret; } std::string result = std::string((char *)output->dataPtr, output->dataSize); LogInfo << "Results:" << result; delete output; output = nullptr; } // destroy streams mxStreamManager.DestroyAllStreams(); delete dataBuffer.dataPtr; dataBuffer.dataPtr = nullptr; return 0; }
样例二
函数原型:
APP_ERROR SendMultiDataWithUniqueId(const std::string& streamName, std::vector<int> inPluginIdVec, std::vector<MxstDataInput>& dataBufferVec, uint64_t& uniqueId);
std::vector<MxstDataInput> dataBufferVec{dataBuffer}; std::vector<int> inPluginIdVec{0}; uint64_t uniqueId; ret = mxStreamManager.SendMultiDataWithUniqueId(streamName, inPluginIdVec, dataBufferVec, uniqueId); if (ret != APP_ERR_OK ) { LogError << "Failed to send data to stream, ret = " << ret << "."; return ret; } // get stream output std::vector<MxstDataOutput*> outputVec = mxStreamManager.GetMultiResultWithUniqueId(streamName, uniqueId, 3000); for (MxstDataOutput* output : outputVec) { if (output == nullptr) { LogError << "Failed to get pipeline output"; return ret; } std::string result = std::string((char *)output->dataPtr, output->dataSize); LogInfo << "Results:" << result; delete output; output = nullptr; } // destroy streams mxStreamManager.DestroyAllStreams(); delete dataBuffer.dataPtr; dataBuffer.dataPtr = nullptr;