diff --git a/VisionSDK/PutText/PutText/CaptionGenManager.cpp b/VisionSDK/PutText/PutText/CaptionGenManager.cpp index 018d7068d7bb8033a2fb3688a34fc563371e6810..44e151b966c6f879793af95dc7ece78935e97a55 100644 --- a/VisionSDK/PutText/PutText/CaptionGenManager.cpp +++ b/VisionSDK/PutText/PutText/CaptionGenManager.cpp @@ -36,6 +36,7 @@ static const FontFile timesNewRoman = {"times", "60px"}; static const FontFile simsun = {"simsun", "60px"}; FontFile FONT_LIST[] = {timesNewRoman, simsun}; int FONT_NUMBER = sizeof(FONT_LIST) / sizeof(FONT_LIST[0]); +const long MAX_FILE_SIZE = 1024 * 1024 * 1024; // 1G CaptionGenManager::CaptionGenManager() { @@ -180,13 +181,19 @@ std::vector split(const std::string &str, const std::string &delim) // 读入文本文件到内存对象中 bool CaptionGenManager::_loadVocab(const std::string &vocabFile, FontInfo &singleFont) { - std::ifstream fin(vocabFile, std::ios::in); + std::ifstream fin(vocabFile, std::ios::in | std::ios::ate); std::string word; if (!fin.is_open()) { LogError << "Unable to read file " << vocabFile; return false; } + long fileSize = fin.tellg(); + fin.seekg(0); + if (fileSize <= 0 || fileSize > MAX_FILE_SIZE){ + LogError << "Vocab size invalid!"; + return false; + } int index = 0; while (getline(fin, word)) { // 每一行按+号拆分成数组 @@ -213,9 +220,22 @@ bool CaptionGenManager::_loadMapBin(const std::string &filePath, cv::Mat &map, F LogError << "_loadMapBin can not open the file " << filePath << "."; return false; } + if (fseek(fpr, 0, SEEK_END) !=0) { + LogError << "_loadMapBin can not seek file " << filePath << "."; + return false; + } + long fileSize = ftell(fpr); + if (fseek(fpr, 0, SEEK_SET) !=0) { + LogError << "_loadMapBin can not seek file " << filePath << "."; + return false; + } int channels = 1; int type = 0; int vocabImageRows = singleFont.wordHeight * singleFont.wordNum; + if (fileSize < vocabImageRows * imageCols || fileSize > MAX_FILE_SIZE){ + LogError << "Map bin file size invalid!"; + return false; + } map = cv::Mat::zeros(vocabImageRows, imageCols, type); auto *pData = (uchar *) map.data; for (int i = 0; i < vocabImageRows * imageCols; i++) { diff --git a/contrib/OCR/plugins/TextInfoPlugin/TextInfoPlugin.cpp b/contrib/OCR/plugins/TextInfoPlugin/TextInfoPlugin.cpp index 6fea0d53edca111d488eba73d626856432a30c7d..7d275281dca2186dfda61f2bea0076b4b7dd1967 100644 --- a/contrib/OCR/plugins/TextInfoPlugin/TextInfoPlugin.cpp +++ b/contrib/OCR/plugins/TextInfoPlugin/TextInfoPlugin.cpp @@ -27,7 +27,10 @@ using namespace std; APP_ERROR TextInfoPlugin::Init(std::map> &configParamMap) { LogInfo << "Begin to initialize TextInfoPlugin(" << pluginName_ << ")."; - + if (!configParamMap["dataSource"]) { + LogError << "dataSource is nullptr."; + return APP_ERR_COMM_FAILURE; + } dataSource_ = *std::static_pointer_cast(configParamMap["dataSource"]); do_lower_case_ = false; never_split_ = { "[UNK]", "[SEP]", "[PAD]", "[CLS]", "[MASK]" }; @@ -107,11 +110,19 @@ APP_ERROR TextInfoPlugin::Process(std::vector &mxpiBuffer) LogInfo << "Begin to process MxpiMotSimpleSort(" << elementName_ << ")."; // Get MxpiVisionList and MxpiTrackletList from mxpibuffer MxpiBuffer *inputMxpiBuffer = mxpiBuffer[0]; // deviceID[0] + if (!inputMxpiBuffer) { + LogError << "mxpiBuffer is nullptr."; + return APP_ERR_COMM_FAILURE; + } MxpiMetadataManager mxpiMetadataManager(*inputMxpiBuffer); // Get the metadata from buffer std::shared_ptr metadata = mxpiMetadataManager.GetMetadata(dataSource_); auto textInfoList = std::static_pointer_cast(metadata); + if (!textInfoList) { + LogError << "metadata is nullptr."; + return APP_ERR_COMM_FAILURE; + } std::vector texts; int length = 0; Covert(textInfoList, texts); @@ -370,7 +381,8 @@ std::string TextInfoPlugin::_clean_text(std::string text) std::string output; int len = 0; char* char_array = new char[text.length() + 1]; - strcpy(char_array, text.c_str()); + std::copy(text.begin(), text.end(), char_array); + char_array[text.length()] = '\0'; while (char_array[len] != '\0') { int cp = int(char_array[len]); @@ -398,7 +410,8 @@ vector TextInfoPlugin::_run_split_on_punc(std::string text) } int len_char_array = text.length(); char* char_array = new char[text.length() + 1]; - strcpy(char_array, text.c_str()); + std::copy(text.begin(), text.end(), char_array); + char_array[text.length()] = '\0'; int i = 0; bool start_new_word = true; vector> output; @@ -484,7 +497,8 @@ std::vector TextInfoPlugin::tokenize2(std::string& text) int len_char_array = token.length(); char* char_array = new char[token.length() + 1]; - strcpy(char_array, token.c_str()); + std::copy(token.begin(), token.end(), char_array); + char_array[token.length()] = '\0'; if (len_char_array > max_input_chars_per_word_) { output_tokens.push_back(unk_token_); diff --git a/contrib/OpenCVPlugin/OpenCVPlugin/src/OpenCVPlugin/OpenCVPlugin.cpp b/contrib/OpenCVPlugin/OpenCVPlugin/src/OpenCVPlugin/OpenCVPlugin.cpp index 63c35881010be32c0658704e6627899eefeca7c0..7ca1aa560a5ee430df0b0e9c21111e58cd90362d 100644 --- a/contrib/OpenCVPlugin/OpenCVPlugin/src/OpenCVPlugin/OpenCVPlugin.cpp +++ b/contrib/OpenCVPlugin/OpenCVPlugin/src/OpenCVPlugin/OpenCVPlugin.cpp @@ -15,6 +15,7 @@ */ #include "OpenCVPlugin.h" +#include #include "MxBase/Log/Log.h" #include "MxBase/Tensor/TensorBase/TensorBase.h" using namespace MxBase; @@ -25,10 +26,10 @@ namespace { const string SAMPLE_KEY = "MxpiVisionList"; const int YUV_U = 2; const int YUV_V = 3; - const int san = 3; - const int er = 2; - const int yi = 1; - const float yiwu = 1.5; + const int THREE = 3; + const int TWO = 2; + const int ONE = 1; + const float RATIO = 1.5; } APP_ERROR MxpiSamplePlugin::Init(std::map>& configParamMap) @@ -101,7 +102,7 @@ APP_ERROR MxpiSamplePlugin::openCV(size_t idx, const MxTools::MxpiVision srcMxpi cv::Mat imgRgb; MxBase::MemoryData memoryNewDst(dst.data, MxBase::MemoryData::MEMORY_HOST_NEW); if (option == "resize") { - if (memorySrc.type == er) { + if (memorySrc.type == TWO) { cv::resize(imgBgr, dst, cv::Size(width, height), fx, fy, interpolation); } else { @@ -110,13 +111,14 @@ APP_ERROR MxpiSamplePlugin::openCV(size_t idx, const MxTools::MxpiVision srcMxpi } else { cv::Rect ori(startRow, startCol, endCol, endRow); - if (memorySrc.type == san) { + if (memorySrc.type == THREE) { dst = src(ori).clone(); } else { dst = imgBgr(ori).clone(); } } + MxBase::MemoryHelper::MxbsFree(memoryDst); Output(dst, idx, dstMxpiVision); auto ret = APP_ERR_OK; if (ret != APP_ERR_OK) { @@ -135,7 +137,7 @@ void MxpiSamplePlugin::Judge(auto& visionData, auto& visionInfo, cv::Mat &imgBgr else { imgBgr = cv::Mat(visionInfo.heightaligned(), visionInfo.widthaligned(), CV_8UC3); } - if (memorySrc.type == san) { + if (memorySrc.type == THREE) { if (visionData.datatype() == MxTools::MxpiDataType::MXPI_DATA_TYPE_FLOAT32) { src = cv::Mat(visionInfo.heightaligned(), visionInfo.widthaligned(), CV_32FC3, memoryDst.ptrData); @@ -207,8 +209,8 @@ APP_ERROR MxpiSamplePlugin::Bgr2Yuv(cv::Mat src, cv::Mat &dst) { int w_img = src.cols; int h_img = src.rows; - dst = cv::Mat(h_img * yiwu, w_img, CV_8UC1); - cv::Mat src_YUV_I420(h_img * yiwu, w_img, CV_8UC1); + dst = cv::Mat(h_img * RATIO, w_img, CV_8UC1); + cv::Mat src_YUV_I420(h_img * RATIO, w_img, CV_8UC1); cvtColor(src, src_YUV_I420, cv::COLOR_BGR2YUV_I420); swapYUV_I420toNV12(src_YUV_I420.data, dst.data, w_img, h_img); return APP_ERR_OK; @@ -219,12 +221,12 @@ void MxpiSamplePlugin::swapYUV_I420toNV12(unsigned char* i420bytes, unsigned ch int nLenY = width * height; int nLenU = nLenY / 4; - memcpy(nv12bytes, i420bytes, width * height); + std::copy(i420bytes, i420bytes + width * height, nv12bytes); for (int i = 0; i < nLenU; i++) { - nv12bytes[nLenY + er * i] = i420bytes[nLenY + i]; // U - nv12bytes[nLenY + er * i + 1] = i420bytes[nLenY + nLenU + i]; // V + nv12bytes[nLenY + TWO * i] = i420bytes[nLenY + i]; // U + nv12bytes[nLenY + TWO * i + 1] = i420bytes[nLenY + nLenU + i]; // V } } @@ -329,12 +331,16 @@ APP_ERROR MxpiSamplePlugin::Process(std::vector& mxpiBuffer) { LogInfo << "MxpiSamplePlugin::Process start"; MxpiBuffer* buffer = mxpiBuffer[0]; + if (!buffer) { + LogError << "mxpiBuffer is nullptr."; + return APP_ERR_COMM_FAILURE; + } MxpiMetadataManager mxpiMetadataManager(*buffer); MxpiErrorInfo mxpiErrorInfo; ErrorInfo_.str(""); auto errorInfoPtr = mxpiMetadataManager.GetErrorInfo(); if (errorInfoPtr != nullptr) { - ErrorInfo_ << GetError(APP_ERR_COMM_FAILURE, pluginName_) << "MxpiSamplePlugin process is not implemented"; + ErrorInfo_ << GetErrorInfo(APP_ERR_COMM_FAILURE, pluginName_) << "MxpiSamplePlugin process is not implemented"; mxpiErrorInfo.ret = APP_ERR_COMM_FAILURE; mxpiErrorInfo.errorInfo = ErrorInfo_.str(); SetMxpiErrorInfo(*buffer, pluginName_, mxpiErrorInfo); @@ -351,7 +357,7 @@ APP_ERROR MxpiSamplePlugin::Process(std::vector& mxpiBuffer) google::protobuf::Message* msg = (google::protobuf::Message*)metadata.get(); const google::protobuf::Descriptor* desc = msg->GetDescriptor(); if (desc->name() != SAMPLE_KEY) { - ErrorInfo_ << GetError(APP_ERR_PROTOBUF_NAME_MISMATCH, pluginName_) + ErrorInfo_ << GetErrorInfo(APP_ERR_PROTOBUF_NAME_MISMATCH, pluginName_) << "Proto struct name is not MxpiVisionList, failed with:" << desc->name(); mxpiErrorInfo.ret = APP_ERR_PROTOBUF_NAME_MISMATCH; mxpiErrorInfo.errorInfo = ErrorInfo_.str(); @@ -364,7 +370,7 @@ APP_ERROR MxpiSamplePlugin::Process(std::vector& mxpiBuffer) LogInfo << "generate"; APP_ERROR ret = GenerateVisionList(*srcMxpiVisionListSptr, *dstMxpiVisionListptr); if (ret != APP_ERR_OK) { - LogError << GetError(ret, pluginName_) << "MxpiSamplePlugin gets inference information failed."; + LogError << GetErrorInfo(ret, pluginName_) << "MxpiSamplePlugin gets inference information failed."; mxpiErrorInfo.ret = ret; mxpiErrorInfo.errorInfo = ErrorInfo_.str(); SetMxpiErrorInfo(*buffer, pluginName_, mxpiErrorInfo); @@ -373,7 +379,7 @@ APP_ERROR MxpiSamplePlugin::Process(std::vector& mxpiBuffer) // Add Generated data to metedata ret = mxpiMetadataManager.AddProtoMetadata(pluginName_, static_pointer_cast(dstMxpiVisionListptr)); if (ret != APP_ERR_OK) { - ErrorInfo_ << GetError(ret, pluginName_) << "MxpiSamplePlugin add metadata failed."; + ErrorInfo_ << GetErrorInfo(ret, pluginName_) << "MxpiSamplePlugin add metadata failed."; mxpiErrorInfo.ret = ret; mxpiErrorInfo.errorInfo = ErrorInfo_.str(); SetMxpiErrorInfo(*buffer, pluginName_, mxpiErrorInfo);