From 149c37bba4c2a336d90e07657206ce8ca1d22a44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AC=A2=E4=B9=90=E9=A9=AC?= Date: Fri, 27 Jun 2025 07:25:54 +0000 Subject: [PATCH 1/5] update contrib/OCR/plugins/TextInfoPlugin/TextInfoPlugin.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 欢乐马 --- .../plugins/TextInfoPlugin/TextInfoPlugin.cpp | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/contrib/OCR/plugins/TextInfoPlugin/TextInfoPlugin.cpp b/contrib/OCR/plugins/TextInfoPlugin/TextInfoPlugin.cpp index 6fea0d53e..7d275281d 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_); -- Gitee From c864bd11b1d33e9e2b932dd4b59afe958e26428f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AC=A2=E4=B9=90=E9=A9=AC?= Date: Fri, 27 Jun 2025 07:27:04 +0000 Subject: [PATCH 2/5] update VisionSDK/PutText/PutText/CaptionGenManager.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 欢乐马 --- VisionSDK/PutText/PutText/CaptionGenManager.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/VisionSDK/PutText/PutText/CaptionGenManager.cpp b/VisionSDK/PutText/PutText/CaptionGenManager.cpp index 018d7068d..2921ec7e9 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,6 +220,13 @@ bool CaptionGenManager::_loadMapBin(const std::string &filePath, cv::Mat &map, F LogError << "_loadMapBin can not open the file " << filePath << "."; return false; } + fseek(fpr, 0, SEEK_END); + long fileSize = ftell(fpr); + fseek(fpr, 0, SEEK_SET); + if (fileSize < vocabImageRows * imageCols || fileSize > MAX_FILE_SIZE){ + LogError << "Map bin file size invalid!"; + return false; + } int channels = 1; int type = 0; int vocabImageRows = singleFont.wordHeight * singleFont.wordNum; -- Gitee From 9bdff112409e3d1f0fe7a08886dbdf8b3f6e3581 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AC=A2=E4=B9=90=E9=A9=AC?= Date: Fri, 27 Jun 2025 09:41:10 +0000 Subject: [PATCH 3/5] update contrib/OpenCVPlugin/OpenCVPlugin/src/OpenCVPlugin/OpenCVPlugin.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 欢乐马 --- .../src/OpenCVPlugin/OpenCVPlugin.cpp | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/contrib/OpenCVPlugin/OpenCVPlugin/src/OpenCVPlugin/OpenCVPlugin.cpp b/contrib/OpenCVPlugin/OpenCVPlugin/src/OpenCVPlugin/OpenCVPlugin.cpp index 63c358810..7ca1aa560 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); -- Gitee From 21554ef534a7fa102f03767bbda53b3132505189 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AC=A2=E4=B9=90=E9=A9=AC?= Date: Sat, 28 Jun 2025 09:07:45 +0000 Subject: [PATCH 4/5] update VisionSDK/PutText/PutText/CaptionGenManager.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 欢乐马 --- VisionSDK/PutText/PutText/CaptionGenManager.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/VisionSDK/PutText/PutText/CaptionGenManager.cpp b/VisionSDK/PutText/PutText/CaptionGenManager.cpp index 2921ec7e9..576452098 100644 --- a/VisionSDK/PutText/PutText/CaptionGenManager.cpp +++ b/VisionSDK/PutText/PutText/CaptionGenManager.cpp @@ -220,9 +220,15 @@ bool CaptionGenManager::_loadMapBin(const std::string &filePath, cv::Mat &map, F LogError << "_loadMapBin can not open the file " << filePath << "."; return false; } - fseek(fpr, 0, SEEK_END); + if (fseek(fpr, 0, SEEK_END) !=0) { + LogError << "_loadMapBin can not seek file " << filePath << "."; + return false; + } long fileSize = ftell(fpr); - fseek(fpr, 0, SEEK_SET); + if (fseek(fpr, 0, SEEK_SET) !=0) { + LogError << "_loadMapBin can not seek file " << filePath << "."; + return false; + } if (fileSize < vocabImageRows * imageCols || fileSize > MAX_FILE_SIZE){ LogError << "Map bin file size invalid!"; return false; -- Gitee From 0d2067b83ade018a01704f2a32829b1f981ecb9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AC=A2=E4=B9=90=E9=A9=AC?= Date: Sat, 28 Jun 2025 09:13:21 +0000 Subject: [PATCH 5/5] update VisionSDK/PutText/PutText/CaptionGenManager.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 欢乐马 --- VisionSDK/PutText/PutText/CaptionGenManager.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/VisionSDK/PutText/PutText/CaptionGenManager.cpp b/VisionSDK/PutText/PutText/CaptionGenManager.cpp index 576452098..44e151b96 100644 --- a/VisionSDK/PutText/PutText/CaptionGenManager.cpp +++ b/VisionSDK/PutText/PutText/CaptionGenManager.cpp @@ -36,7 +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 +const long MAX_FILE_SIZE = 1024 * 1024 * 1024; // 1G CaptionGenManager::CaptionGenManager() { @@ -229,13 +229,13 @@ bool CaptionGenManager::_loadMapBin(const std::string &filePath, cv::Mat &map, F 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; } - int channels = 1; - int type = 0; - int vocabImageRows = singleFont.wordHeight * singleFont.wordNum; map = cv::Mat::zeros(vocabImageRows, imageCols, type); auto *pData = (uchar *) map.data; for (int i = 0; i < vocabImageRows * imageCols; i++) { -- Gitee