他的回复:
修改后代码:/* * Copyright (c) 2020.Huawei Technologies Co., Ltd. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include "MxBase/Log/Log.h" #include "MxStream/StreamManager/MxStreamManager.h" #include "opencv4/opencv2/opencv.hpp" #include #include #include #include #include #include #include using namespace std; 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 copy file path(" 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, the image path is (" 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 (" path ")."; 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) { LogError pipelineConfigPath " file dose not exist."; return ""; } file.seekg(0, std::ifstream::end); uint32_t fileSize = file.tellg(); file.seekg(0); std::unique_ptr data(new char[fileSize]); file.read(data.get(), fileSize); file.close(); std::string pipelineConfig(data.get(), fileSize); return pipelineConfig; } } vector getFiles(string cate_dir) { vector files; DIR *dir; struct dirent *ptr; //char base[1000]; if ((dir = opendir(cate_dir.c_str())) == NULL) { perror("Open dir error..."); exit(1); } while ((ptr = readdir(dir)) != NULL) { if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) ///current dir OR parrent dir continue; else if (ptr->d_type == 8) ///file //printf("d_name:%s/%s\n",basePath,ptr->d_name); files.push_back(ptr->d_name); else if (ptr->d_type == 10) ///link file //printf("d_name:%s/%s\n",basePath,ptr->d_name); continue; else if (ptr->d_type == 4) ///dir { files.push_back(ptr->d_name); /* memset(base,'\0',sizeof(base)); strcpy(base,basePath); strcat(base,"/"); strcat(base,ptr->d_nSame); readFileList(base); */ } } closedir(dir); //sort(files.begin(), files.end()); return files; } int main(int argc, char* argv[]) { // read pipeline config file std::string pipelineConfigPath = "../pipeline/behaviors.pipeline"; std::string pipelineConfig = ReadPipelineConfig(pipelineConfigPath); if (pipelineConfig == "") { LogError "Read pipeline failed."; return APP_ERR_COMM_INIT_FAIL; } // init stream manager MxStream::MxStreamManager mxStreamManager; APP_ERROR 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; } string filepath = "./inputs/"; string respath = "./outputs/"; vector files; files = getFiles(filepath); int size = files.size(); cout size endl; int j; for (j = 0; j size; j++) { // read image file and build stream input MxStream::MxstDataInput dataBuffer; ret = ReadFile(filepath + files[j], dataBuffer); cout filepath + files[j] endl; if (ret != APP_ERR_OK) { LogError "Failed to read image file, ret = " ret "."; return ret; } std::string streamName = "classification+detection"; int inPluginId = 0; auto startTime = std::chrono::high_resolution_clock::now(); // send data into stream ret = mxStreamManager.SendData(streamName, inPluginId, dataBuffer); if (ret != APP_ERR_OK) { LogError "Failed to send data to stream, ret = " ret "."; return ret; } // get stream output MxStream::MxstDataOutput* output = mxStreamManager.GetResult(streamName, inPluginId); auto endTime = std::chrono::high_resolution_clock::now(); double costMs = std::chrono::duration(endTime - startTime).count(); LogInfo "[SendData-GetResult] cost: " costMs "ms. "; LogInfo "[SendData-GetResult] fps: " 1000 / costMs "fps"; if (output == nullptr) { LogError "Failed to get pipeline output."; return ret; } std::string result = std::string((char *)output->dataPtr, output->dataSize); LogInfo "Results:" result; web::json::value jsonText = web::json::value::parse(result); if (jsonText.is_object()) { web::json::object textObject = jsonText.as_object(); auto itInferObject = textObject.find("MxpiObject"); if (itInferObject == textObject.end() || (!itInferObject->second.is_array())) { return 0; } auto iter = itInferObject->second.as_array().begin(); cv::Mat src = cv::imread(filepath + files[j]); for (; iter != itInferObject->second.as_array().end(); iter++) { if (iter->is_object()) { auto modelInferObject = iter->as_object(); float x0 = 0; float x1 = 0; float y0 = 0; float y1 = 0; auto it = modelInferObject.find("x0"); if (it != modelInferObject.end()) { x0 = float(it->second.as_double()); } it = modelInferObject.find("x1"); if (it != modelInferObject.end()) { x1 = float(it->second.as_double()); } it = modelInferObject.find("y0"); if (it != modelInferObject.end()) { y0 = float(it->second.as_double()); } it = modelInferObject.find("y1"); if (it != modelInferObject.end()) { y1 = float(it->second.as_double()); } cv::Rect rect(x0, y0, x1 - x0, y1 - y0); cv::rectangle(src, rect, cv::Scalar(0, 255, 0), 5, cv::LINE_8, 0); auto classVecObject = modelInferObject.find("classVec"); if (classVecObject == modelInferObject.end() || (!classVecObject->second.is_array())) { return 0; } auto iterC = classVecObject->second.as_array().begin(); for (; iterC != classVecObject->second.as_array().end(); iterC++) { auto modelObject = iterC->as_object(); std::string classId; std::string className; std::string confidence; it = modelObject.find("classId"); if (it != modelObject.end()) { classId = std::to_string(it->second.as_integer()); } it = modelObject.find("className"); if (it != modelObject.end()) { className = it->second.as_string(); } it = modelObject.find("confidence"); if (it != modelObject.end()) { confidence = std::to_string(it->second.as_double()); } std::string All = className + ":" + confidence; cv::Point origin; origin.x = x0 + 1; origin.y = y0 - 5; cv::putText(src, All, origin, cv::FONT_HERSHEY_PLAIN, 1.2, cv::Scalar(0xFF, 0xFF, 0xFF), 1, 8, 0); } } cv::imwrite(respath + '_' + files[j], src); } } // destroy streams mxStreamManager.DestroyAllStreams(); delete dataBuffer.dataPtr; dataBuffer.dataPtr = nullptr; delete output; } return 0; }报错截图: