From 062a679b7fb9879b7c37a9157f99817c13d4f378 Mon Sep 17 00:00:00 2001 From: hid54630209 Date: Sun, 7 Apr 2024 22:33:01 +0800 Subject: [PATCH 1/3] =?UTF-8?q?PutText=20=E9=80=82=E9=85=8DdeviceId?= =?UTF-8?q?=EF=BC=9BCaptionManager=E6=94=B9=E4=B8=BA=E9=A5=BF=E6=B1=89?= =?UTF-8?q?=E5=BC=8F=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PutText/PutText/CaptionGenManager.cpp | 2 ++ mxVision/PutText/PutText/CaptionGenManager.h | 4 ++- .../PutText/PutText/CaptionGeneration.cpp | 36 ++++++++++++------- mxVision/PutText/PutText/CaptionGeneration.h | 3 +- mxVision/PutText/PutText/CaptionImpl.cpp | 25 +++++++++---- mxVision/PutText/PutText/CaptionImpl.h | 3 +- 6 files changed, 51 insertions(+), 22 deletions(-) diff --git a/mxVision/PutText/PutText/CaptionGenManager.cpp b/mxVision/PutText/PutText/CaptionGenManager.cpp index 14c2606e4..e717f5aba 100644 --- a/mxVision/PutText/PutText/CaptionGenManager.cpp +++ b/mxVision/PutText/PutText/CaptionGenManager.cpp @@ -36,6 +36,8 @@ static const FontFile simsun = {"simsun", "60px"}; FontFile FONT_LIST[] = {timesNewRoman, simsun}; int FONT_NUMBER = sizeof(FONT_LIST) / sizeof(FONT_LIST[0]); +CaptionGenManager CaptionGenManager::instance = CaptionGenManager(); + CaptionGenManager::CaptionGenManager() { bool success = this->Init(); diff --git a/mxVision/PutText/PutText/CaptionGenManager.h b/mxVision/PutText/PutText/CaptionGenManager.h index 63d1699ba..7d2edf38e 100644 --- a/mxVision/PutText/PutText/CaptionGenManager.h +++ b/mxVision/PutText/PutText/CaptionGenManager.h @@ -33,6 +33,8 @@ struct FontInfo { }; class CaptionGenManager { +public: + static CaptionGenManager instance; public: int FindIndex(const std::string &font, const std::string &fontSize, const std::string &item); int FindWidth(const std::string &font, const std::string &fontSize, const int index); @@ -45,10 +47,10 @@ public: } bool Init(); int FindHeight(const std::string &font, const std::string &fontSize); + virtual ~CaptionGenManager() {}; private: CaptionGenManager(); - virtual ~CaptionGenManager() {} std::unordered_map fontsInfo_; static bool _loadVocab(const std::string &vocabFile, FontInfo &singleFont); static bool _loadMapBin(const std::string &filePath, cv::Mat &map, FontInfo &singleFont, int imageCols); diff --git a/mxVision/PutText/PutText/CaptionGeneration.cpp b/mxVision/PutText/PutText/CaptionGeneration.cpp index dbfbd20cc..db20d7d06 100644 --- a/mxVision/PutText/PutText/CaptionGeneration.cpp +++ b/mxVision/PutText/PutText/CaptionGeneration.cpp @@ -19,6 +19,8 @@ #include "CaptionGenManager.h" #include #include +#include "MxBase/DeviceManager/DeviceManager.h" + using namespace std; const std::string UNK_SYMBOL = "*"; @@ -30,8 +32,14 @@ enum tokenType { }; APP_ERROR CaptionGeneration::init(const std::string &inputFont, const std::string &inputFontSize, - const std::string &inputFont2, const std::string &inputFontSize2) + const std::string &inputFont2, const std::string &inputFontSize2, int32_t deviceId) { + APP_ERROR ret = MxBase::DeviceManager::GetInstance()->CheckDeviceId(deviceId); + if (ret != APP_ERR_OK) { + LogError << "Device id is out of range, current deviceId is " << deviceId << "." << GetErrorInfo(ret); + return APP_ERR_COMM_FAILURE; + } + devideId_ = deviceId; // 校验字体在字库管理类中是否存在 bool fontIsValid = CaptionGenManager::getInstance().isFontValid(inputFont, inputFontSize); if (fontIsValid != true) { @@ -48,8 +56,10 @@ APP_ERROR CaptionGeneration::init(const std::string &inputFont, const std::strin font2_ = inputFont2; fontSizeMap_[inputFont] = inputFontSize; fontSizeMap_[inputFont2] = inputFontSize2; - vocabImage_ = CaptionGenManager::getInstance().getVocabImage(inputFont, inputFontSize); - vocabImage2_ = CaptionGenManager::getInstance().getVocabImage(inputFont2, inputFontSize2); + vocabImage_ = CaptionGenManager::getInstance().getVocabImage(inputFont, inputFontSize).Clone(); + vocabImage_.ToDevice(devideId_); + vocabImage2_ = CaptionGenManager::getInstance().getVocabImage(inputFont2, inputFontSize2).Clone(); + vocabImage2_.ToDevice(devideId_); startX_ = 0; startY_ = 0; // 选择中文字体和英文字体中最高的高度作为字体最终的高度 @@ -67,11 +77,11 @@ APP_ERROR CaptionGeneration::initRectAndTextColor(cv::Size bgSize, cv::Scalar te this->backgroundSize_ = bgSize; // 为字幕生成操作分配字幕变量 captionComp_ = MxBase::Tensor{std::vector{(uint32_t)backgroundSize_.height, (uint32_t)backgroundSize_.width, 1}, - MxBase::TensorDType::UINT8, 0}; + MxBase::TensorDType::UINT8, devideId_}; captionCompBGR_ = MxBase::Tensor{std::vector{(uint32_t)backgroundSize_.height, (uint32_t)backgroundSize_.width, 3}, - MxBase::TensorDType::UINT8, 0}; - captionNormalized_ = MxBase::Tensor{captionCompBGR_.GetShape(), MxBase::TensorDType::UINT8, 0}; - captionColored_ = MxBase::Tensor{captionCompBGR_.GetShape(), MxBase::TensorDType::UINT8, 0}; + MxBase::TensorDType::UINT8, devideId_}; + captionNormalized_ = MxBase::Tensor{captionCompBGR_.GetShape(), MxBase::TensorDType::UINT8, devideId_}; + captionColored_ = MxBase::Tensor{captionCompBGR_.GetShape(), MxBase::TensorDType::UINT8, devideId_}; MxBase::Tensor::TensorMalloc(captionComp_); MxBase::Tensor::TensorMalloc(captionCompBGR_); MxBase::Tensor::TensorMalloc(captionNormalized_); @@ -79,14 +89,14 @@ APP_ERROR CaptionGeneration::initRectAndTextColor(cv::Size bgSize, cv::Scalar te // 初始化字体颜色变量compTextColor_, 改变了用于为字幕上色 compTextColor_ = MxBase::Tensor{std::vector{(uint32_t)backgroundSize_.height, (uint32_t)backgroundSize_.width, 3}, - MxBase::TensorDType::UINT8, 0}; + MxBase::TensorDType::UINT8, devideId_}; MxBase::Tensor::TensorMalloc(compTextColor_); MxBase::Tensor compTextColor_r = MxBase::Tensor{std::vector{(uint32_t)backgroundSize_.height, - (uint32_t)backgroundSize_.width, 1}, MxBase::TensorDType::UINT8, 0}; + (uint32_t)backgroundSize_.width, 1}, MxBase::TensorDType::UINT8, devideId_}; MxBase::Tensor compTextColor_g = MxBase::Tensor{std::vector{(uint32_t)backgroundSize_.height, - (uint32_t)backgroundSize_.width, 1}, MxBase::TensorDType::UINT8, 0}; + (uint32_t)backgroundSize_.width, 1}, MxBase::TensorDType::UINT8, devideId_}; MxBase::Tensor compTextColor_b = MxBase::Tensor{std::vector{(uint32_t)backgroundSize_.height, - (uint32_t)backgroundSize_.width, 1}, MxBase::TensorDType::UINT8, 0}; + (uint32_t)backgroundSize_.width, 1}, MxBase::TensorDType::UINT8, devideId_}; MxBase::Tensor::TensorMalloc(compTextColor_r); MxBase::Tensor::TensorMalloc(compTextColor_g); MxBase::Tensor::TensorMalloc(compTextColor_b); @@ -113,13 +123,15 @@ APP_ERROR CaptionGeneration::initRectAndTextColor(cv::Size bgSize, cv::Scalar te } // 初始化变量captionNormalizer_, 该变量用于作为归一化操作中的除数,值为255 - captionNormalizer_ = MxBase::Tensor{captionCompBGR_.GetShape(), MxBase::TensorDType::UINT8, 0}; + captionNormalizer_ = MxBase::Tensor{captionCompBGR_.GetShape(), MxBase::TensorDType::UINT8, devideId_}; MxBase::Tensor::TensorMalloc(captionNormalizer_); ret = compTextColor_b.SetTensorValue((uint8_t)255); if (ret != APP_ERR_OK) { LogError << "Fail to set the value of captionNormalizer_."; return APP_ERR_COMM_FAILURE; } + // init Devide + MxBase::Divide(captionCompBGR_, captionNormalizer_, captionNormalized_); return APP_ERR_OK; } diff --git a/mxVision/PutText/PutText/CaptionGeneration.h b/mxVision/PutText/PutText/CaptionGeneration.h index 3d013546a..a88957a95 100644 --- a/mxVision/PutText/PutText/CaptionGeneration.h +++ b/mxVision/PutText/PutText/CaptionGeneration.h @@ -35,7 +35,7 @@ public: * @return 初始化是否成功完成 * */ APP_ERROR init(const std::string &inputFont, const std::string &inputFontSize, - const std::string &inputFont2, const std::string &inputFontSize2); + const std::string &inputFont2, const std::string &inputFontSize2, int32_t deviceId); /** * 初始化矩形框 @@ -83,6 +83,7 @@ private: MxBase::Tensor captionNormalizer_; // 值为255的Tensor,用于归一化操作中作为除数 MxBase::Tensor captionNormalized_; // 3. 归一化到[0, 1]区间的三通道字幕 MxBase::Tensor captionColored_; // 4. 上色后的字幕 + int32_t deviceId_; APP_ERROR getCaptionImage(MxBase::Tensor &_blackboard, const std::vector> &sentenceTokens, uint32_t startX, uint32_t startY, const std::vector &returnChrIndex = {}, diff --git a/mxVision/PutText/PutText/CaptionImpl.cpp b/mxVision/PutText/PutText/CaptionImpl.cpp index fc634e7d8..3472db0ac 100644 --- a/mxVision/PutText/PutText/CaptionImpl.cpp +++ b/mxVision/PutText/PutText/CaptionImpl.cpp @@ -17,15 +17,22 @@ #include "CaptionImpl.h" #include "MxBase/MxBase.h" #include "MxBase/Log/Log.h" +#include "MxBase/DeviceManager/DeviceManager.h" enum tokenType { CHINESE, ENGLISH, ALPHA }; APP_ERROR CaptionImpl::init(const std::string &inputFont, const std::string &fontSize, - const std::string &inputFont2, const std::string &fontSize2) { + const std::string &inputFont2, const std::string &fontSize2, int32_t deviceId) { + APP_ERROR ret = MxBase::DeviceManager::GetInstance()->CheckDeviceId(deviceId); + if (ret != APP_ERR_OK) { + LogError << "Device id is out of range, current deviceId is " << deviceId << "." << GetErrorInfo(ret); + return APP_ERR_COMM_FAILURE; + } + deviceId_ = deviceId; // 初始化CaptionGenerator - auto ret = captionGenerator_.init(inputFont, fontSize, inputFont2, fontSize2); + auto ret = captionGenerator_.init(inputFont, fontSize, inputFont2, fontSize2, deviceId); if (ret != 0) { LogError << "Fail to init captionGenerator"; return APP_ERR_COMM_FAILURE; @@ -65,7 +72,7 @@ APP_ERROR CaptionImpl::initRectAndColor(cv::Scalar textColor, cv::Scalar backgro // 分配保存字幕结果的Tensor caption_ = MxBase::Tensor(std::vector{uint32_t(dstBackgroundHeight_), uint32_t(dstBackgroundWidth_), 3}, - MxBase::TensorDType::UINT8, 0); + MxBase::TensorDType::UINT8, deviceId_); ret = MxBase::Tensor::TensorMalloc(caption_); if (ret != APP_ERR_OK) { LogError << "Fail to malloc caption tensor."; @@ -74,9 +81,9 @@ APP_ERROR CaptionImpl::initRectAndColor(cv::Scalar textColor, cv::Scalar backgro // 字幕背景生成 coloredTensor_ = MxBase::Tensor(std::vector{caption_.GetShape()[0], caption_.GetShape()[1], 3}, - MxBase::TensorDType::UINT8, 0); + MxBase::TensorDType::UINT8, deviceId_); MxBase::Tensor color_r = MxBase::Tensor(std::vector{caption_.GetShape()[0], caption_.GetShape()[1], 1}, - MxBase::TensorDType::UINT8, 0); + MxBase::TensorDType::UINT8, deviceId_); MxBase::Tensor::TensorMalloc(color_r); ret = color_r.SetTensorValue((uint8_t) backgroundColor[2]); @@ -85,7 +92,7 @@ APP_ERROR CaptionImpl::initRectAndColor(cv::Scalar textColor, cv::Scalar backgro return APP_ERR_COMM_FAILURE; } MxBase::Tensor color_g = MxBase::Tensor(std::vector{caption_.GetShape()[0], caption_.GetShape()[1], 1}, - MxBase::TensorDType::UINT8, 0); + MxBase::TensorDType::UINT8, deviceId_); MxBase::Tensor::TensorMalloc(color_g); ret = color_g.SetTensorValue((uint8_t) backgroundColor[1]); if (ret != APP_ERR_OK) { @@ -93,7 +100,7 @@ APP_ERROR CaptionImpl::initRectAndColor(cv::Scalar textColor, cv::Scalar backgro return APP_ERR_COMM_FAILURE; } MxBase::Tensor color_b = MxBase::Tensor(std::vector{caption_.GetShape()[0], caption_.GetShape()[1], 1}, - MxBase::TensorDType::UINT8, 0); + MxBase::TensorDType::UINT8, deviceId_); MxBase::Tensor::TensorMalloc(color_b); ret = color_b.SetTensorValue((uint8_t) backgroundColor[0]); if (ret != APP_ERR_OK) { @@ -110,6 +117,10 @@ APP_ERROR CaptionImpl::initRectAndColor(cv::Scalar textColor, cv::Scalar backgro } APP_ERROR CaptionImpl::putText(MxBase::Tensor &img, const std::string text1, const std::string text2, cv::Point org, float opacity) { + if (img.GetDeviceId() != deviceId_) { + LogError << "The deviceId of img is not equal to that of CaptionImpl. Please check."; + return APP_ERR_COMM_FAILURE; + } // Step0: 校验字幕贴字位置 int rightBottomX = org.x + dstBackgroundWidth_; int rightBottomY = org.y + dstBackgroundHeight_; diff --git a/mxVision/PutText/PutText/CaptionImpl.h b/mxVision/PutText/PutText/CaptionImpl.h index 37e32b213..5f155bee3 100644 --- a/mxVision/PutText/PutText/CaptionImpl.h +++ b/mxVision/PutText/PutText/CaptionImpl.h @@ -28,7 +28,7 @@ public: * @return 初始化是否成功完成 * */ APP_ERROR init(const std::string &inputFont, const std::string &fontSize, - const std::string &inputFont2, const std::string &fontSize2); + const std::string &inputFont2, const std::string &fontSize2, int32_t deviceId); /** * 初始化字体颜色和文本背景宽度 @@ -72,6 +72,7 @@ private: double fontScale_; int dstBackgroundWidth_; int dstBackgroundHeight_; + int32_t deviceId_; }; #endif \ No newline at end of file -- Gitee From 7e66894e79537cbf3ebc1948b9d6e7b2982258c8 Mon Sep 17 00:00:00 2001 From: hid54630209 Date: Mon, 8 Apr 2024 12:44:13 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E2=80=9C=E7=9B=B8?= =?UTF-8?q?=E5=90=8C=E5=AD=97=E5=B9=95=E6=97=A0=E9=9C=80=E5=86=8D=E6=AC=A1?= =?UTF-8?q?=E7=94=9F=E6=88=90=E4=B8=AD=E9=97=B4=E5=8F=98=E9=87=8F=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mxVision/PutText/PutText/CaptionImpl.cpp | 20 ++++++++++++-------- mxVision/PutText/PutText/CaptionImpl.h | 3 +++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/mxVision/PutText/PutText/CaptionImpl.cpp b/mxVision/PutText/PutText/CaptionImpl.cpp index ca8451fa0..cc1fce3ae 100644 --- a/mxVision/PutText/PutText/CaptionImpl.cpp +++ b/mxVision/PutText/PutText/CaptionImpl.cpp @@ -136,26 +136,28 @@ APP_ERROR CaptionImpl::putText(MxBase::Tensor &img, const std::string text1, con } // Step1: 字幕生成 - MxBase::Tensor mask; bool isResize = true; if (fontScale_ == 1) { isResize = false; } - APP_ERROR ret = captionGenerator_.captionGen(caption_, coloredTensor_, text1, text2, mask, isResize); - if (ret != APP_ERR_OK) { - LogError << "Fail to generate caption."; - return APP_ERR_COMM_FAILURE; + if (text1 != formerText1_ || text2 != formerText2_) { + mask_ = MxBase::Tensor(); + APP_ERROR ret = captionGenerator_.captionGen(caption_, coloredTensor_, text1, text2, mask_, isResize); + if (ret != APP_ERR_OK) { + LogError << "Fail to generate caption."; + return APP_ERR_COMM_FAILURE; + } } // step2: 图片+字幕+字幕背景叠加 MxBase::Rect dstRect(org.x, org.y, org.x + dstBackgroundWidth_, org.y + dstBackgroundHeight_); MxBase::Rect srcRect(0, 0, dstBackgroundWidth_, dstBackgroundHeight_); - ret = img.SetReferRect(dstRect); + APP_ERROR ret = img.SetReferRect(dstRect); if (ret != APP_ERR_OK) { LogError << "Fail to set referRect for image."; return APP_ERR_COMM_FAILURE; } - ret = mask.SetReferRect(srcRect); + ret = mask_.SetReferRect(srcRect); if (ret != APP_ERR_OK) { LogError << "Fail to set referRect for mask."; return APP_ERR_COMM_FAILURE; @@ -170,11 +172,13 @@ APP_ERROR CaptionImpl::putText(MxBase::Tensor &img, const std::string text1, con LogError << "Fail to set referRect for caption."; return APP_ERR_COMM_FAILURE; } - int blendRet = MxBase::BlendImageCaption(img, caption_, mask, coloredTensor_, opacity); + int blendRet = MxBase::BlendImageCaption(img, caption_, mask_, coloredTensor_, opacity); if (blendRet != APP_ERR_OK) { LogError << "Fail to conduct blendImageCaption operator."; return APP_ERR_COMM_FAILURE; } + formerText1_ = text1; + formerText2_ = text2; return APP_ERR_OK; } diff --git a/mxVision/PutText/PutText/CaptionImpl.h b/mxVision/PutText/PutText/CaptionImpl.h index 5f155bee3..099422091 100644 --- a/mxVision/PutText/PutText/CaptionImpl.h +++ b/mxVision/PutText/PutText/CaptionImpl.h @@ -73,6 +73,9 @@ private: int dstBackgroundWidth_; int dstBackgroundHeight_; int32_t deviceId_; + std::string formerText1_; + std::string formerText2_; + MxBase::Tensor mask_; }; #endif \ No newline at end of file -- Gitee From ab2b65ced82124480ab43e2305bf6ec4d5b773f1 Mon Sep 17 00:00:00 2001 From: hid54630209 Date: Fri, 19 Apr 2024 19:36:51 +0800 Subject: [PATCH 3/3] =?UTF-8?q?PutTextDemo.=E6=94=AF=E6=8C=81=E5=B0=8F?= =?UTF-8?q?=E5=AD=97=E5=B9=95=E5=BC=A0=E8=B4=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PutText/PutText/CaptionGeneration.cpp | 8 +- mxVision/PutText/PutText/CaptionImpl.cpp | 114 ++++++++++++------ mxVision/PutText/PutText/CaptionImpl.h | 5 + 3 files changed, 87 insertions(+), 40 deletions(-) diff --git a/mxVision/PutText/PutText/CaptionGeneration.cpp b/mxVision/PutText/PutText/CaptionGeneration.cpp index 28a2ec44e..7d23759f4 100644 --- a/mxVision/PutText/PutText/CaptionGeneration.cpp +++ b/mxVision/PutText/PutText/CaptionGeneration.cpp @@ -74,7 +74,7 @@ APP_ERROR CaptionGeneration::init(const std::string &inputFont, const std::strin APP_ERROR CaptionGeneration::initRectAndTextColor(cv::Size bgSize, cv::Scalar textColorCompleted) { - this->backgroundSize_ = bgSize; + this->backgroundSize_ = cv::(bgSize.width, bgSize.height); // 为字幕生成操作分配字幕变量 captionComp_ = MxBase::Tensor{std::vector{(uint32_t)backgroundSize_.height, (uint32_t)backgroundSize_.width, 1}, MxBase::TensorDType::UINT8, deviceId_}; @@ -215,7 +215,11 @@ APP_ERROR CaptionGeneration::getCaptionImage(MxBase::Tensor &_blackboard, } else { word = MxBase::Tensor(vocabImage2_, RSrcAll[i]); } - subRegion.Clone(word); + auto ret = subRegion.Clone(word); + if (ret != APP_ERR_OK){ + LogError << "Fail to clone the word caption form vocab image."; + return APP_ERR_COMM_FAILURE; + } } return APP_ERR_OK; } diff --git a/mxVision/PutText/PutText/CaptionImpl.cpp b/mxVision/PutText/PutText/CaptionImpl.cpp index cc1fce3ae..2c320e0ca 100644 --- a/mxVision/PutText/PutText/CaptionImpl.cpp +++ b/mxVision/PutText/PutText/CaptionImpl.cpp @@ -22,6 +22,7 @@ enum tokenType { CHINESE, ENGLISH, ALPHA }; +const int EXTRA_MARGIN_SIZE = 64; APP_ERROR CaptionImpl::init(const std::string &inputFont, const std::string &fontSize, const std::string &inputFont2, const std::string &fontSize2, int32_t deviceId) { @@ -40,9 +41,9 @@ APP_ERROR CaptionImpl::init(const std::string &inputFont, const std::string &fon int font1Height = CaptionGenManager::getInstance().FindHeight(inputFont, fontSize); int font2Height = CaptionGenManager::getInstance().FindHeight(inputFont2, fontSize2); if (font1Height < font2Height) { - height_ = font2Height * 2; + height_ = font2Height; } else { - height_ = font1Height * 2; + height_ = font1Height; } font_ = inputFont; font2_ = inputFont2; @@ -52,18 +53,18 @@ APP_ERROR CaptionImpl::init(const std::string &inputFont, const std::string &fon } APP_ERROR CaptionImpl::initRectAndColor(cv::Scalar textColor, cv::Scalar backgroundColor, double fontScale, int width) { - if (width < 64) { + if (width < 20) { LogError << "The width of backgroundSize or the height of backgroundSize should be >= 64."; } width_ = width; textColor_ = textColor; backgroundColor_ = backgroundColor; fontScale_ = fontScale; - dstBackgroundWidth_ = int(fontScale * width_); - dstBackgroundHeight_ = int(fontScale * height_); + dstBackgroundWidth_ = int(fontScale * (width_ + EXTRA_MARGIN_SIZE)); + dstBackgroundHeight_ = int(fontScale * (height_ * 2 + EXTRA_MARGIN_SIZE)); // 初始化captionGenerator - cv::Size backgroundSize(width, height_); + cv::Size backgroundSize(width + EXTRA_MARGIN_SIZE, height_ * 2 + EXTRA_MARGIN_SIZE); APP_ERROR ret = captionGenerator_.initRectAndTextColor(backgroundSize, textColor); if (ret != APP_ERR_OK) { LogError << "Fail to init captionGenerator"; @@ -116,13 +117,47 @@ APP_ERROR CaptionImpl::initRectAndColor(cv::Scalar textColor, cv::Scalar backgro return APP_ERR_OK; } -APP_ERROR CaptionImpl::putText(MxBase::Tensor &img, const std::string text1, const std::string text2, cv::Point org, float opacity) { +APP_ERROR CaptionImpl::setTensorReferRect(MxBase::Tensor &img, MxBase::Rect srcRect, MxBase::Rect dstRect) { + APP_ERROR ret = img.SetReferRect(dstRect); + if (ret != APP_ERR_OK) { + LogError << "Fail to set referRect for image."; + return APP_ERR_COMM_FAILURE; + } + ret = mask_.SetReferRect(srcRect); + if (ret != APP_ERR_OK) { + LogError << "Fail to set referRect for mask."; + return APP_ERR_COMM_FAILURE; + } + ret = coloredTensor_.SetReferRect(srcRect); + if (ret != APP_ERR_OK) { + LogError << "Fail to set referRect for coloredTensor."; + return APP_ERR_COMM_FAILURE; + } + ret = caption_.SetReferRect(srcRect); + if (ret != APP_ERR_OK) { + LogError << "Fail to set referRect for caption."; + return APP_ERR_COMM_FAILURE; + } + return APP_ERR_OK; +} +APP_ERROR CaptionImpl::checkPutText(MxBase::Tensor &img, const std::string text1, const std::string text2, cv::Point &org) { if (img.GetDeviceId() != deviceId_) { LogError << "The deviceId of img is not equal to that of CaptionImpl. Please check."; return APP_ERR_COMM_FAILURE; } // Step0: 校验字幕贴字位置 - int rightBottomX = org.x + dstBackgroundWidth_; + int roiLength1 = getLength(text1) * fontScale_; + int roiLength2 = getLength(text2) * fontScale_; + if (roiLength1 > dstBackgroundWidth_ || roiLength2 > dstBackgroundWidth_) { + LogError << "The text length exceeds the maximum length of initialized temp tensor. Please initialize again."; + return APP_ERR_COMM_FAILURE; + } + int maxLength = (roiLength1 > roiLength2) ? roiLength1 : roiLength2; + if (maxLength > img.GetShape()[1]) { + LogError << "The text length exceeds the maximum length of image."; + return APP_ERR_COMM_FAILURE; + } + int rightBottomX = org.x + maxLength; int rightBottomY = org.y + dstBackgroundHeight_; if (rightBottomY > img.GetShape()[0]) { LogWarn << "The y part of right bottom point (" << rightBottomY << ") exceed image width (" @@ -132,7 +167,14 @@ APP_ERROR CaptionImpl::putText(MxBase::Tensor &img, const std::string text1, con if (rightBottomX > img.GetShape()[1]) { LogWarn << "The x part of right bottom point (" << rightBottomX << ") exceed image height (" << img.GetShape()[1] << ". The text is automatically putted in the margin."; - org.x = img.GetShape()[1] - dstBackgroundWidth_; + org.x = img.GetShape()[1] - maxLength; + } + return APP_ERR_OK; +} +APP_ERROR CaptionImpl::putText(MxBase::Tensor &img, const std::string text1, const std::string text2, cv::Point org, float opacity) { + if (checkPutText(img, text1, text2, org) != APP_ERR_OK) { + LogError << "The requirements of putText are not met."; + return APP_ERR_COMM_FAILURE; } // Step1: 字幕生成 @@ -144,41 +186,37 @@ APP_ERROR CaptionImpl::putText(MxBase::Tensor &img, const std::string text1, con mask_ = MxBase::Tensor(); APP_ERROR ret = captionGenerator_.captionGen(caption_, coloredTensor_, text1, text2, mask_, isResize); if (ret != APP_ERR_OK) { - LogError << "Fail to generate caption."; + LogError << "Fail to generate caption for putText function."; return APP_ERR_COMM_FAILURE; } } - // step2: 图片+字幕+字幕背景叠加 - MxBase::Rect dstRect(org.x, org.y, org.x + dstBackgroundWidth_, org.y + dstBackgroundHeight_); - MxBase::Rect srcRect(0, 0, dstBackgroundWidth_, dstBackgroundHeight_); - APP_ERROR ret = img.SetReferRect(dstRect); - if (ret != APP_ERR_OK) { - LogError << "Fail to set referRect for image."; - return APP_ERR_COMM_FAILURE; - } - ret = mask_.SetReferRect(srcRect); - if (ret != APP_ERR_OK) { - LogError << "Fail to set referRect for mask."; - return APP_ERR_COMM_FAILURE; - } - ret = coloredTensor_.SetReferRect(srcRect); - if (ret != APP_ERR_OK) { - LogError << "Fail to set referRect for coloredTensor."; - return APP_ERR_COMM_FAILURE; - } - ret = caption_.SetReferRect(srcRect); - if (ret != APP_ERR_OK) { - LogError << "Fail to set referRect for caption."; - return APP_ERR_COMM_FAILURE; + int roiHeight = height_ * fontScale_; + if (text1 != "") { + int roiLength = getLength(text1) * fontScale_; + MxBase::Rect dstRect(org.x, org.y, org.x + roiLength, org.y + roiHeight); + MxBase::Rect srcRect(0, 0, roiLength, roiHeight); + setTensorReferRect(img, srcRect, dstRect); + APP_ERROR ret = MxBase::BlendImageCaption(img, caption_, mask_, coloredTensor_, opacity); + if (ret != APP_ERR_OK) { + LogError << "Fail to conduct blendImageCaption operator."; + return APP_ERR_COMM_FAILURE; + } + formerText1_ = text1; } - int blendRet = MxBase::BlendImageCaption(img, caption_, mask_, coloredTensor_, opacity); - if (blendRet != APP_ERR_OK) { - LogError << "Fail to conduct blendImageCaption operator."; - return APP_ERR_COMM_FAILURE; + + if (text2 != "") { + int roiLength = getLength(text2) * fontScale_; + MxBase::Rect dstRect(org.x, org.y + roiHeight, org.x + roiLength, org.y + roiHeight * 2); + MxBase::Rect srcRect(0, roiHeight, roiLength, roiHeight * 2); + setTensorReferRect(img, srcRect, dstRect); + APP_ERROR ret = MxBase::BlendImageCaption(img, caption_, mask_, coloredTensor_, opacity); + if (ret != APP_ERR_OK) { + LogError << "Fail to conduct blendImageCaption operator."; + return APP_ERR_COMM_FAILURE; + } + formerText2_ = text2; } - formerText1_ = text1; - formerText2_ = text2; return APP_ERR_OK; } diff --git a/mxVision/PutText/PutText/CaptionImpl.h b/mxVision/PutText/PutText/CaptionImpl.h index 099422091..890abcd8e 100644 --- a/mxVision/PutText/PutText/CaptionImpl.h +++ b/mxVision/PutText/PutText/CaptionImpl.h @@ -58,6 +58,9 @@ public: * */ int getLength(const std::string text); +private: + APP_ERROR checkPutText(MxBase::Tensor &img, const std::string text1, const std::string text2, cv::Point &org); + private: CaptionGeneration captionGenerator_; MxBase::Tensor coloredTensor_; @@ -76,6 +79,8 @@ private: std::string formerText1_; std::string formerText2_; MxBase::Tensor mask_; + std::string formerText1 = ""; + std::string formerText2 = ""; }; #endif \ No newline at end of file -- Gitee