文档
注册

C++样例

样例一

函数原型:
APP_ERROR SendDataWithUniqueId(const std::string& streamName, int inPluginId, MxstDataInput& dataBuffer, uint64_t& uniqueId);
#include <cstring>
#include "MxBase/Log/Log.h"
#include "MxStream/StreamManager/MxStreamManager.h"

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";
int inPluginId = 0;
// send data into stream
uint64_t uniqueId;
ret = mxStreamManager.SendDataWithUniqueId(streamName, inPluginId, dataBuffer, uniqueId);
if (ret != APP_ERR_OK ) {
LogError << "Failed to send data to stream, ret = " << ret << ".";
return ret;
}
// get stream output
MxStream::MxstDataOutput* output = mxStreamManager.GetResultWithUniqueId(streamName, uniqueId, 3000);
if (output == nullptr) {
LogError << "Failed to get pipeline output";
return ret;
}
std::string result = std::string((char *)output->dataPtr, output->dataSize);
LogInfo << "Results:" << result;
// destroy streams
mxStreamManager.DestroyAllStreams();
delete dataBuffer.dataPtr;
dataBuffer.dataPtr = nullptr;
delete output;
output = nullptr;
return 0;
}

样例二

函数原型:

APP_ERROR SendDataWithUniqueId(const std::string& streamName, const std::string& elementName,MxstDataInput& dataBuffer, uint64_t& uniqueId);
std::string elementName = "appsrc0";
uint64_t uniqueId = 0;
APP_ERROR ret = mxStreamManager->SendDataWithUniqueId(streamName, elementName, mxstDataInput, uniqueId);
if (ret != APP_ERR_OK) {
    LogError << GetError(ret) << "SendDataWithUniqueId failed.";
    return ret;
}
const uint32_t waitTime = 1000; // 1000 ms
auto *mxstDataOutput = mxStreamManager->GetResultWithUniqueId(streamName, uniqueId, waitTime);
if (mxstDataOutput == nullptr) {
    LogError << "GetResultWithUniqueId mxstDataOutput is nullptr.";
    return APP_ERR_COMM_INVALID_POINTER;
}
std::string result((char *) mxstDataOutput->dataPtr, mxstDataOutput->dataSize);
LogInfo << "result:" << result;

delete mxstDataOutput;
mxstDataOutput = nullptr;

样例三

函数原型:

APP_ERROR SendDataWithUniqueId(const std::string& streamName, const std::string& elementName,MxstDataInput& dataBuffer, uint64_t& uniqueId);
std::string elementName = "appsrc0";
uint64_t uniqueId = 0;
APP_ERROR ret = mxStreamManager->SendDataWithUniqueId(streamName, elementName, mxstDataInput, uniqueId);
if (ret != APP_ERR_OK) {
    LogError << GetError(ret) << "SendDataWithUniqueId failed.";
    return ret;
}
const uint32_t waitTime = 1000; // 1000 ms
auto mxstDataOutput = mxStreamManager->GetResultWithUniqueIdSP(streamName, uniqueId, waitTime);
if (mxstDataOutput == nullptr) {
    LogError << "GetResultWithUniqueId mxstDataOutput is nullptr.";
    return APP_ERR_COMM_INVALID_POINTER;
}
std::string result((char *) mxstDataOutput->dataPtr, mxstDataOutput->dataSize);
LogInfo << "result:" << result;
搜索结果
找到“0”个结果

当前产品无相关内容

未找到相关内容,请尝试其他搜索词