From 1fcad82d1a71c142b2fdf7338d60988145d31277 Mon Sep 17 00:00:00 2001 From: xuxiaolong Date: Tue, 3 Sep 2024 15:47:11 +0800 Subject: [PATCH 1/9] =?UTF-8?q?=E5=9B=9B=E8=B7=AF=E8=A7=86=E9=A2=91?= =?UTF-8?q?=E6=8A=BD=E5=B8=A7=E6=8B=BC=E5=9B=BE=E6=8E=A8=E7=90=86=EF=BC=8C?= =?UTF-8?q?todo:=E5=AD=98=E5=9C=A8=E9=83=A8=E5=88=86=E6=98=AF=E7=BB=93?= =?UTF-8?q?=E6=9E=9C=E6=98=AF=E9=9D=9E=E6=8B=BC=E6=8E=A5=E5=9B=BE=EF=BC=8C?= =?UTF-8?q?=E9=83=A8=E5=88=86=E6=8B=BC=E6=8E=A5=E5=90=8E=E6=8E=A8=E7=90=86?= =?UTF-8?q?=E6=A0=87=E6=A1=86=E5=8F=AA=E6=9C=89=E4=B8=80=E4=B8=AA=E8=A7=92?= =?UTF-8?q?=E8=90=BD=EF=BC=8C=E9=83=A8=E5=88=86=E6=A0=87=E6=A1=86=E5=92=8C?= =?UTF-8?q?=E5=AE=9E=E9=99=85=E4=B8=8D=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/src/AclLiteModel.cpp | 2 +- .../sampleYOLOV7MultiInput/inc/Params.h | 6 +- .../sampleYOLOV7MultiInput/scripts/env.sh | 15 + .../sampleYOLOV7MultiInput/src/CMakeLists.txt | 3 + .../src/convergence/convergence.cpp | 310 ++++++++++++++++++ .../src/convergence/convergence.h | 60 ++++ .../src/dataInput/dataInput.cpp | 52 ++- .../src/dataInput/dataInput.h | 6 +- .../src/dataOutput/dataOutput.cpp | 23 +- .../src/detectPreprocess/detectPreprocess.cpp | 29 +- .../sampleYOLOV7MultiInput/src/main.cpp | 53 ++- 11 files changed, 521 insertions(+), 38 deletions(-) create mode 100644 inference/modelInference/sampleYOLOV7MultiInput/scripts/env.sh create mode 100644 inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp create mode 100644 inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.h diff --git a/inference/modelInference/sampleYOLOV7MultiInput/common/src/AclLiteModel.cpp b/inference/modelInference/sampleYOLOV7MultiInput/common/src/AclLiteModel.cpp index 7be8426ef..30cc33305 100755 --- a/inference/modelInference/sampleYOLOV7MultiInput/common/src/AclLiteModel.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/common/src/AclLiteModel.cpp @@ -447,7 +447,7 @@ AclLiteError AclLiteModel::GetOutputItem(InferenceOutput& out, uint32_t idx, boo ACL_MEM_MALLOC_NORMAL_ONLY); if (ret != ACL_SUCCESS) { ACLLITE_LOG_ERROR("Create output failed for malloc " - "device failed, size %d", (int)bufferSize); + "device failed, size %d, ret:%d", (int)bufferSize, ret); return ACLLITE_ERROR_MALLOC_DEVICE; } ret = aclUpdateDataBuffer(dataBuffer, outputBuffer, bufferSize); diff --git a/inference/modelInference/sampleYOLOV7MultiInput/inc/Params.h b/inference/modelInference/sampleYOLOV7MultiInput/inc/Params.h index c4d939da8..a87b6d098 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/inc/Params.h +++ b/inference/modelInference/sampleYOLOV7MultiInput/inc/Params.h @@ -44,6 +44,7 @@ const int MSG_OUTPUT_FRAME = 6; const int MSG_ENCODE_FINISH = 7; const int MSG_RTSP_DISPLAY = 8; const int MSG_APP_EXIT = 9; +const int MSG_CONVERGENCE = 10; const std::string kDataInputName = "dataInput"; const std::string kPreName = "pre"; @@ -51,6 +52,7 @@ const std::string kInferName = "infer"; const std::string kPostName = "post"; const std::string kDataOutputName = "dataOutput"; const std::string kRtspDisplayName = "rtspDisplay"; +const std::string kConvergenceName = "convergence"; } struct DetectDataMsg { @@ -59,11 +61,13 @@ struct DetectDataMsg { int detectPostThreadId; int dataOutputThreadId; int rtspDisplayThreadId; + int convergenceThreadId; int postId; uint32_t deviceId; uint32_t channelId; // record msg belongs to which rtsp/video channel bool isLastFrame; // whether the last frame of rtsp/video of this channel has been decoded int msgNum; // record frameID in rtsp/video of this channel + int convergenceNum; // how many channels to converge std::vector decodedImg; // original image (NV12) ImageData modelInputImg; // image after detect preprocess std::vector frame; // original image (BGR) needed by postprocess @@ -71,4 +75,4 @@ struct DetectDataMsg { std::vector textPrint; }; -#endif \ No newline at end of file +#endif diff --git a/inference/modelInference/sampleYOLOV7MultiInput/scripts/env.sh b/inference/modelInference/sampleYOLOV7MultiInput/scripts/env.sh new file mode 100644 index 000000000..7d98a6364 --- /dev/null +++ b/inference/modelInference/sampleYOLOV7MultiInput/scripts/env.sh @@ -0,0 +1,15 @@ +. /usr/local/Ascend/ascend-toolkit/set_env.sh +rm -rf plog +export ASCEND_PROCESS_LOG_PATH=./plog + +export INSTALL_DIR=/usr/local/Ascend/ascend-toolkit/latest + +export DDK_PATH=/usr/local/Ascend/ascend-toolkit/latest + +export NPU_HOST_LIB=$DDK_PATH/runtime/lib64/stub + +export THIRDPART_PATH=${DDK_PATH}/thirdpart + +export LD_LIBRARY_PATH=${THIRDPART_PATH}/lib:$LD_LIBRARY_PATH + +export LD_LIBRARY_PATH=/usr/local/Ascend/ascend-toolkit/latest/aarch64-linux/lib64:$LD_LIBRARY_PATH diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/CMakeLists.txt b/inference/modelInference/sampleYOLOV7MultiInput/src/CMakeLists.txt index 3e48d4133..41d60e9a3 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/CMakeLists.txt +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/CMakeLists.txt @@ -41,6 +41,7 @@ include_directories( ${INC_PATH}/runtime/include/ ../inc/ ../common/include/ + $ENV{INSTALL_DIR}/runtime/include/ ) if(target STREQUAL "Simulator_Function") @@ -61,7 +62,9 @@ add_executable(main dataOutput/dataOutput.cpp pushrtsp/pictortsp.cpp pushrtsp/pushrtspthread.cpp + convergence/convergence.cpp main.cpp) +set_target_properties(main PROPERTIES LINKER_LANGUAGE CXX) target_sources(main PUBLIC diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp new file mode 100644 index 000000000..f70a70134 --- /dev/null +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp @@ -0,0 +1,310 @@ +/** +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2022. 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. + +* File sample_process.cpp +* Description: handle acl resource +*/ +#include +#include +#include "assert.h" + +#include "Params.h" +#include "convergence.h" +#include "AclLiteApp.h" + + +#include "AclLiteResource.h" +#include "AclLiteUtils.h" + +#include "acl/acl.h" +#include "acl/ops/acl_dvpp.h" + +using namespace std; + +namespace { +const uint32_t kSleepTime = 500; +} + +uint32_t AlignmentHelper(uint32_t origSize, uint32_t alignment) +{ + if (alignment == 0) { + return 0; + } + uint32_t alignmentH = alignment - 1; + return (origSize + alignmentH) / alignment * alignment; +} + +ConvergenceThread::ConvergenceThread(aclrtRunMode runMode, uint32_t channelNum ,uint32_t modelWidth, uint32_t modelHeight, + uint32_t batch) + :runMode_(runMode), channelNum_(channelNum), modelWidth_(modelWidth), modelHeight_(modelHeight), isReleased(false), batch_(batch) +{ +} + +ConvergenceThread::~ConvergenceThread() +{ + if(!isReleased) { + dvpp_.DestroyResource(); + } + isReleased = true; +} + +AclLiteError ConvergenceThread::Init() +{ + selfThreadId_ = SelfInstanceId(); + AclLiteError aclRet = dvpp_.Init("DVPP_CHNMODE_VPC"); + if (aclRet) { + ACLLITE_LOG_ERROR("Dvpp init failed, error %d", aclRet); + return ACLLITE_ERROR; + } + + return ACLLITE_OK; +} + +AclLiteError ConvergenceThread::Process(int msgId, shared_ptr data) { + shared_ptr ldata = static_pointer_cast(data); + switch (msgId) { + case MSG_CONVERGENCE: + MsgProcess(ldata); + MsgSend(ldata); + break; + default: + ACLLITE_LOG_INFO("Convergence thread ignore msg %d", msgId); + break; + } + + return ACLLITE_OK; +} + +// to-do convergence frames +std::shared_ptr ConvergenceThread::CropAndPaste() { + int32_t deviceId = convQueue_.front()->deviceId; + aclrtSetDevice(deviceId); + aclrtContext context; + aclrtCreateContext(&context, deviceId); + aclrtStream stream; + aclrtCreateStream(&stream); + aclrtGetRunMode(&runMode_); + int inputWidth = convQueue_.front()->decodedImg[0].width; // decode出来的结构图长宽更大? + //int inputWidth = modelWidth_; + + int inputHeight = convQueue_.front()->decodedImg[0].height; + //int inputHeight = modelHeight_; + + uint32_t singleImageBufferSize = convQueue_.front()->decodedImg[0].size; + std::cout<< "===================single decode image buffer size:" << singleImageBufferSize< new_message = std::make_shared(*convQueue_[0]); + + std:cout<< "000000000000000000000000=======channel, msgNum:" << new_message->channelId << "," << new_message->msgNum << std::endl; + new_message->decodedImg.clear(); + new_message->frame.clear(); + for (int i = 0; i < convQueue_[0]->decodedImg.size(); i++) { + std::vector frames; + for (int j = 0; j < convQueue_.size(); j++) { + void *inBufferDev = nullptr; + acldvppMalloc(&inBufferDev, singleImageBufferSize); + if (runMode_ == ACL_HOST) { + aclrtMemcpy(inBufferDev, singleImageBufferSize, + convQueue_[j]->decodedImg[i].data.get(), + singleImageBufferSize, ACL_MEMCPY_HOST_TO_DEVICE); + } else { + aclrtMemcpy(inBufferDev, singleImageBufferSize, + convQueue_[j]->decodedImg[i].data.get(), + singleImageBufferSize, ACL_MEMCPY_DEVICE_TO_DEVICE); + } + + uint32_t oddNum = 1; + uint32_t cropSizeWidth = inputWidth; + uint32_t cropSizeHeight = inputHeight; + + uint32_t cropLeftOffset = 0; // must even + uint32_t cropRightOffset = + cropLeftOffset + cropSizeWidth - oddNum; // must odd 0+1024-1 + uint32_t cropTopOffset = 0; // must even + uint32_t cropBottomOffset = + cropTopOffset + cropSizeHeight - oddNum; // must odd 0+1068-1 + acldvppRoiConfig *cropArea_ = acldvppCreateRoiConfig( + cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset); + // 通过for循环改 + uint32_t pasteLeftOffset = 0 + (i % 2) * inputWidth; // must even + uint32_t pasteRightOffset = + pasteLeftOffset + cropSizeWidth - oddNum; // must odd + uint32_t pasteTopOffset = 0 + (i / 2) * inputHeight; // must even + uint32_t pasteBottomOffset = + pasteTopOffset + cropSizeHeight - oddNum; // must odd + acldvppRoiConfig *pasteArea_ = + acldvppCreateRoiConfig(pasteLeftOffset, pasteRightOffset, + pasteTopOffset, pasteBottomOffset); + + uint32_t jpegOutWidthStride = + AlignmentHelper(inputWidth, widthAlignment); + uint32_t jpegOutHeightStride = + AlignmentHelper(inputHeight, heightAlignment); + uint32_t jpegOutBufferSize = + jpegOutWidthStride * jpegOutHeightStride * sizeAlignment / sizeNum; + acldvppPicDesc *vpcInputDesc_ = acldvppCreatePicDesc(); + acldvppSetPicDescData(vpcInputDesc_, + inBufferDev); // JpegD -> vpcCropAndPaste + acldvppSetPicDescFormat(vpcInputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); + acldvppSetPicDescWidth(vpcInputDesc_, inputWidth); + acldvppSetPicDescHeight(vpcInputDesc_, inputHeight); + acldvppSetPicDescWidthStride(vpcInputDesc_, jpegOutWidthStride); + acldvppSetPicDescHeightStride(vpcInputDesc_, jpegOutHeightStride); + acldvppSetPicDescSize(vpcInputDesc_, jpegOutBufferSize); + + // crop and patse pic + acldvppVpcCropAndPasteAsync(dvppChannelDesc_, vpcInputDesc_, + vpcOutputDesc_, cropArea_, pasteArea_, + stream); + aclrtSynchronizeStream(stream); + + (void)acldvppDestroyRoiConfig(cropArea_); + cropArea_ = nullptr; + (void)acldvppDestroyRoiConfig(pasteArea_); + pasteArea_ = nullptr; + (void)acldvppDestroyPicDesc(vpcInputDesc_); + vpcInputDesc_ = nullptr; + frames.push_back(convQueue_[j]->frame[i]); + } + + ImageData image; + image.format = acldvppGetPicDescFormat(vpcOutputDesc_); + image.width = acldvppGetPicDescWidth(vpcOutputDesc_); + image.height = acldvppGetPicDescHeight(vpcOutputDesc_); + image.alignWidth = acldvppGetPicDescWidthStride(vpcOutputDesc_); + image.alignHeight = acldvppGetPicDescHeightStride(vpcOutputDesc_); + image.size = acldvppGetPicDescSize(vpcOutputDesc_); + + void *vdecOutBufferDev = acldvppGetPicDescData(vpcOutputDesc_); + image.data = SHARED_PTR_DVPP_BUF(vdecOutBufferDev); + new_message->decodedImg.push_back(image); + + assert(convQueue_[0]->convergenceNum == frames.size()); + new_message->convergenceNum = frames.size(); + + cv::Mat hcat; + + cv::hconcat(frames[0], frames[1], hcat); + cv::Mat vcat; + cv::hconcat(frames[2], frames[3], vcat); + cv::Mat res; + cv::vconcat(hcat, vcat, res); + new_message->frame.push_back(res); + + frames.clear(); + } + + // clear and reset messageOrder + convQueue_.clear(); + messageOrder_ = -1; + + (void)acldvppDestroyPicDesc(vpcOutputDesc_); + vpcOutputDesc_ = nullptr; + (void)acldvppDestroyChannel(dvppChannelDesc_); + (void)acldvppDestroyChannelDesc(dvppChannelDesc_); + dvppChannelDesc_ = nullptr; + + if (vpcOutBufferDev_ != nullptr) { + (void)acldvppFree(vpcOutBufferDev_); + vpcOutBufferDev_ = nullptr; + } + aclrtDestroyStream(stream); + stream = nullptr; + aclrtDestroyContext(context); + context = nullptr; + + return new_message; +} + + + + +AclLiteError ConvergenceThread::MsgProcess(shared_ptr& detectDataMsg) +{ + assert(!detectDataMsg->decodedImg.empty()); + detectDataMsg->convergenceNum = channelNum_; + int size = convQueue_.size(); + // mark the messageOrder + // if(!size) { + // messageOrder_ = detectDataMsg->msgNum; + // } + // // resend the message to pipeline if the msg Order is not same with stacked message + // if(detectDataMsg->msgNum != messageOrder_ ) { + // SendMessage(selfThreadId_, MSG_CONVERGENCE, detectDataMsg); + // return ACLLITE_OK; + // } + if(size < channelNum_) { + convQueue_.push_back(detectDataMsg); + assert(!convQueue_.back()->decodedImg.empty()); + return ACLLITE_OK; + } + + // replace message with new convergenced message + detectDataMsg = CropAndPaste(); + std::cout << "=========================after crop and paster, decode images count:"<< detectDataMsg->decodedImg.size() << "=============================\n"; + return ACLLITE_OK; +} + +AclLiteError ConvergenceThread::MsgSend(shared_ptr detectDataMsg) +{ + while (1) { + AclLiteError ret = SendMessage(detectDataMsg->detectPreThreadId, MSG_PREPROC_DETECTDATA, detectDataMsg); + if (ret == ACLLITE_ERROR_ENQUEUE) { + usleep(kSleepTime); + continue; + } else if(ret == ACLLITE_OK) { + break; + } else { + ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); + return ret; + } + } + return ACLLITE_OK; +} diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.h b/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.h new file mode 100644 index 000000000..3bf4277d9 --- /dev/null +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.h @@ -0,0 +1,60 @@ +/** +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2022. 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. + +* File sample_process.h +* Description: handle acl resource +*/ +#ifndef CONVERGENCETHREAD_H +#define CONVERGENCETHREAD_H +#pragma once +#include +#include +#include +#include "acl/acl.h" +#include "VideoCapture.h" +#include "AclLiteThread.h" +#include "AclLiteImageProc.h" +#include "Params.h" + +#define INFO_LOG(fmt, args...) fprintf(stdout, "[INFO] " fmt "\n", ##args) + +class ConvergenceThread : public AclLiteThread { +public: + ConvergenceThread(aclrtRunMode runMode_, uint32_t channelNum_, uint32_t modelWidth, uint32_t modelHeight, + uint32_t batch); + ~ConvergenceThread(); + AclLiteError Init(); + AclLiteError Process(int msgId, std::shared_ptr data); + std::shared_ptr CropAndPaste(); + +private: + AclLiteError MsgProcess(std::shared_ptr& detectDataMsg); + AclLiteError MsgSend(std::shared_ptr detectDataMsg); + +private: + uint32_t channelNum_; // how many channels to converge + uint32_t modelWidth_; + uint32_t modelHeight_; + AclLiteImageProc dvpp_; + bool isReleased; + uint32_t batch_; + uint32_t messageOrder_ = -1; + int selfThreadId_; + + std::vector> convQueue_; + aclrtRunMode runMode_; +}; + +#endif diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.cpp index d9c343012..88acfa011 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.cpp @@ -31,10 +31,10 @@ namespace{ using namespace std; DataInputThread::DataInputThread( - int32_t deviceId, int32_t channelId, aclrtRunMode& runMode, + bool convergence, int32_t deviceId, int32_t channelId, aclrtRunMode& runMode, string inputDataType, string inputDataPath, string inferName, int postThreadNum, uint32_t batch, int framesPerSecond) - :deviceId_(deviceId), channelId_(channelId), runMode_(runMode), postproId_(0), + :convergence_(convergence), deviceId_(deviceId), channelId_(channelId), runMode_(runMode), postproId_(0), inputDataType_(inputDataType), inputDataPath_(inputDataPath), inferName_(inferName), cap_(nullptr), frameCnt_(0), postThreadNum_(postThreadNum), selfThreadId_(INVALID_INSTANCE_ID), preThreadId_(INVALID_INSTANCE_ID), @@ -122,6 +122,7 @@ AclLiteError DataInputThread::Init() preThreadId_ = GetAclLiteThreadIdByName(kPreName + to_string(channelId_)); dataOutputThreadId_ = GetAclLiteThreadIdByName(kDataOutputName + to_string(channelId_)); rtspDisplayThreadId_ = GetAclLiteThreadIdByName(kRtspDisplayName + to_string(channelId_)); + convergenceThreadId_ = GetAclLiteThreadIdByName(kConvergenceName); for (int i = 0; i < postThreadNum_; i++) { postThreadId_[i] = GetAclLiteThreadIdByName(kPostName + to_string(channelId_) + "_" + to_string(i)); if (postThreadId_[i] == INVALID_INSTANCE_ID) { @@ -150,13 +151,19 @@ AclLiteError DataInputThread::Init() AclLiteError DataInputThread::Process(int msgId, shared_ptr msgData) { + AclLiteError ret; shared_ptr detectDataMsg = make_shared(); + detectDataMsg->convergenceNum = 0; switch (msgId) { case MSG_APP_START: AppStart(); break; case MSG_READ_FRAME: - MsgRead(detectDataMsg); + ret = MsgRead(detectDataMsg); + if(ret != ACLLITE_OK) { + ACLLITE_LOG_ERROR("datainput message read error, ret:%d", ret); + return ret; + } MsgSend(detectDataMsg); break; default: @@ -298,21 +305,30 @@ AclLiteError DataInputThread::MsgRead(shared_ptr &detectDataMsg) detectDataMsg->postId = postproId_; detectDataMsg->dataOutputThreadId = dataOutputThreadId_; detectDataMsg->rtspDisplayThreadId = rtspDisplayThreadId_; + detectDataMsg->convergenceThreadId = convergenceThreadId_; detectDataMsg->deviceId = deviceId_; detectDataMsg->channelId = channelId_; detectDataMsg->msgNum = msgNum_; msgNum_++; - GetOneFrame(detectDataMsg); + ret = GetOneFrame(detectDataMsg); + if (ret != ACLLITE_OK) { + ACLLITE_LOG_ERROR("Get Frame Error, ret:%d", ret); + return ret; + } if (detectDataMsg->isLastFrame) { return ACLLITE_OK; } frameCnt_++; while (frameCnt_ % batch_) { - GetOneFrame(detectDataMsg); - if (detectDataMsg->isLastFrame) { - break; - } - frameCnt_++; + ret = GetOneFrame(detectDataMsg); + if (ret != ACLLITE_OK) { + ACLLITE_LOG_ERROR("Get Frame Error, ret:%d", ret); + return ret; + } + if (detectDataMsg->isLastFrame) { + break; + } + frameCnt_++; } return ACLLITE_OK; @@ -323,7 +339,14 @@ AclLiteError DataInputThread::MsgSend(shared_ptr &detectDataMsg) AclLiteError ret; if (detectDataMsg->isLastFrame == false) { while (1) { - ret = SendMessage(detectDataMsg->detectPreThreadId, MSG_PREPROC_DETECTDATA, detectDataMsg); + if (convergence_) { + assert(!detectDataMsg->decodedImg.empty()); + ret = SendMessage(detectDataMsg->convergenceThreadId, MSG_CONVERGENCE, + detectDataMsg); + } else { + ret = SendMessage(detectDataMsg->detectPreThreadId, + MSG_PREPROC_DETECTDATA, detectDataMsg); + } if (ret == ACLLITE_ERROR_ENQUEUE) { usleep(kSleepTime); continue; @@ -343,7 +366,14 @@ AclLiteError DataInputThread::MsgSend(shared_ptr &detectDataMsg) } else { for (int i = 0; i < postThreadNum_; i++) { while (1) { - ret = SendMessage(detectDataMsg->detectPreThreadId, MSG_PREPROC_DETECTDATA, detectDataMsg); + if (convergence_) { + assert(!detectDataMsg->decodedImg.empty()); + ret = SendMessage(detectDataMsg->convergenceThreadId, + MSG_CONVERGENCE, detectDataMsg); + } else { + ret = SendMessage(detectDataMsg->detectPreThreadId, + MSG_PREPROC_DETECTDATA, detectDataMsg); + } if (ret == ACLLITE_ERROR_ENQUEUE) { usleep(kSleepTime); continue; diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.h b/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.h index 9d79afb52..4a269e2fa 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.h +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.h @@ -31,7 +31,7 @@ class DataInputThread : public AclLiteThread { public: - DataInputThread(int32_t deviceId, int32_t channelId, aclrtRunMode& runMode, + DataInputThread(bool convergence, int32_t deviceId, int32_t channelId, aclrtRunMode& runMode, std::string inputDataType, std::string inputDataPath, std::string inferName, int postThreadNum, uint32_t batch, int framesPerSecond); @@ -50,6 +50,7 @@ private: AclLiteError GetOneFrame(std::shared_ptr &detectDataMsg); private: + bool convergence_ = false; uint32_t deviceId_; uint32_t channelId_; int frameCnt_; @@ -69,6 +70,7 @@ private: int selfThreadId_; int preThreadId_; int inferThreadId_; + int convergenceThreadId_; std::vector postThreadId_; int dataOutputThreadId_; int rtspDisplayThreadId_; @@ -80,4 +82,4 @@ private: int framesPerSecond_; }; -#endif \ No newline at end of file +#endif diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/dataOutput/dataOutput.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/dataOutput/dataOutput.cpp index bcc42e27a..e969701ac 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/dataOutput/dataOutput.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/dataOutput/dataOutput.cpp @@ -187,16 +187,21 @@ AclLiteError DataOutputThread::SaveResultVideo(shared_ptr &detect return ACLLITE_OK; } -AclLiteError DataOutputThread::SaveResultPic(shared_ptr &detectDataMsg) -{ - for(int i = 0; i < detectDataMsg->frame.size(); i++) { - stringstream sstream; - sstream.str(""); - sstream << "../out/channel_" << detectDataMsg->channelId - << "_out_pic_" << detectDataMsg->msgNum << i << ".jpg"; - cv::imwrite(sstream.str(), detectDataMsg->frame[i]); +AclLiteError DataOutputThread::SaveResultPic( + shared_ptr &detectDataMsg) { + for (int i = 0; i < detectDataMsg->frame.size(); i++) { + stringstream sstream; + sstream.str(""); + if (detectDataMsg->convergenceNum) { + sstream << "../out/pics/convergence_" + << "out_pic_" << detectDataMsg->msgNum << i << ".jpg"; + } else { + sstream << "../out/channel_" << detectDataMsg->channelId << "_out_pic_" + << detectDataMsg->msgNum << i << ".jpg"; } - return ACLLITE_OK; + cv::imwrite(sstream.str(), detectDataMsg->frame[i]); + } + return ACLLITE_OK; } AclLiteError DataOutputThread::PrintResult(shared_ptr &detectDataMsg) diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/detectPreprocess/detectPreprocess.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/detectPreprocess/detectPreprocess.cpp index 504f73649..6c7c4cad7 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/detectPreprocess/detectPreprocess.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/detectPreprocess/detectPreprocess.cpp @@ -18,6 +18,7 @@ */ #include #include +#include #include "Params.h" #include "detectPreprocess.h" #include "AclLiteApp.h" @@ -71,8 +72,8 @@ AclLiteError DetectPreprocessThread::Process(int msgId, shared_ptr data) AclLiteError DetectPreprocessThread::MsgProcess(shared_ptr detectDataMsg) { AclLiteError ret; - - uint32_t modelInputSize = YUV420SP_SIZE(modelWidth_, modelHeight_) * batch_; + uint32_t modelInputSize = YUV420SP_SIZE(modelWidth_ , modelHeight_ ) * batch_; + void* buf = nullptr; ret = aclrtMalloc(&buf, modelInputSize, ACL_MEM_MALLOC_HUGE_FIRST); if ((buf == nullptr) || (ret != ACL_ERROR_NONE)) { @@ -86,17 +87,19 @@ AclLiteError DetectPreprocessThread::MsgProcess(shared_ptr detect size_t pos = 0; for (int i = 0; i < detectDataMsg->decodedImg.size(); i++) { - ImageData resizedImg; - ret = dvpp_.Resize(resizedImg, - detectDataMsg->decodedImg[i], modelWidth_, modelHeight_); - if (ret == ACLLITE_ERROR) { - ACLLITE_LOG_ERROR("Resize image failed"); - return ACLLITE_ERROR; - } - uint32_t dataSize = YUV420SP_SIZE(modelWidth_, modelHeight_); - ret = aclrtMemcpy(batchBuffer + pos, dataSize, - resizedImg.data.get(), resizedImg.size, ACL_MEMCPY_DEVICE_TO_DEVICE); - pos = pos + dataSize; + ImageData resizedImg; + ret = dvpp_.Resize(resizedImg, detectDataMsg->decodedImg[i], modelWidth_, + modelHeight_); + if (ret == ACLLITE_ERROR) { + ACLLITE_LOG_ERROR("Resize image failed"); + return ACLLITE_ERROR; + } + + uint32_t dataSize = + YUV420SP_SIZE(modelWidth_, modelHeight_ ); + ret = aclrtMemcpy(batchBuffer + pos, dataSize, resizedImg.data.get(), + resizedImg.size, ACL_MEMCPY_DEVICE_TO_DEVICE); + pos = pos + dataSize; } detectDataMsg->modelInputImg.data = SHARED_PTR_DEV_BUF(batchBuffer); diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/main.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/main.cpp index 7fd44e01a..a15f66f61 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/main.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/main.cpp @@ -35,6 +35,7 @@ #include "detectInference/detectInference.h" #include "detectPreprocess/detectPreprocess.h" #include "detectPostprocess/detectPostprocess.h" +#include "convergence/convergence.h" #include "pushrtsp/pushrtspthread.h" using namespace std; @@ -47,6 +48,20 @@ int kPostNum = 1; int kFramesPerSecond = 1000; uint32_t kMsgQueueSize = 3; uint32_t argNum = 2; + +bool convergence = false; +} + +bool isPerfectSquare(int num) { + if (num < 0) { + return false; // 负数不是完全平方数 + } + + // 计算平方根并将其四舍五入为整数 + int root = static_cast(round(sqrt(num))); + + // 判断平方后的值是否与原值相等 + return root * root == num; } int MainThreadProcess(uint32_t msgId, @@ -118,6 +133,42 @@ void CreateALLThreadInstance(vector& threadTbl, AclLiteResou modelWidth, modelHeigth, kBatch, kPostNum, kFramesPerSecond); return; } + + // whether convergence + string s = + root["device_config"][i]["model_config"][j]["convergence"] + .asString(); + if (s == "True") { + convergence = true; + int channels = + root["device_config"][i]["model_config"][j]["io_info"] + .size(); + if (!isPerfectSquare(channels)) { + ACLLITE_LOG_ERROR( + "Invalid model config is given! convergence channels: " + "%d " + "is not a perfect square number!", + channels); + } + } + + int channelNum = + root["device_config"][i]["model_config"][j]["io_info"] + .size(); + + if (convergence) { + std::cout << "=============================== convergence " + "===============================" + << std::endl; + AclLiteThreadParam convergenceParam; + convergenceParam.threadInst = new ConvergenceThread( + runMode, channelNum, modelWidth, modelHeigth, kBatch); + convergenceParam.threadInstName.assign( + kConvergenceName.c_str()); + convergenceParam.context = context; + threadTbl.push_back(convergenceParam); + } + // Create inferThread AclLiteThreadParam inferParam; inferParam.threadInst = new DetectInferenceThread(modelPath); @@ -139,7 +190,7 @@ void CreateALLThreadInstance(vector& threadTbl, AclLiteResou string rtspDisplayName = kRtspDisplayName + to_string(channelId); // Create Thread for the input data: AclLiteThreadParam dataInputParam; - dataInputParam.threadInst = new DataInputThread(deviceId, channelId, runMode, + dataInputParam.threadInst = new DataInputThread(convergence, deviceId, channelId, runMode, inputType, inputPath, inferName, kPostNum, kBatch, kFramesPerSecond); dataInputParam.threadInstName.assign(dataInputName.c_str()); dataInputParam.context = context; -- Gitee From a282efc81b9a675b15dfd8cf63e82971ddab8eeb Mon Sep 17 00:00:00 2001 From: xuxiaolong Date: Tue, 3 Sep 2024 22:24:54 +0800 Subject: [PATCH 2/9] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E5=A4=9A=E8=B7=AF=E8=A7=86=E9=A2=91=E6=8E=A8=E7=90=86=EF=BC=8C?= =?UTF-8?q?=E6=AF=8F=E5=BC=A0=E5=9B=BE=E9=83=BD=E6=98=AF=E6=8B=BC=E6=8E=A5?= =?UTF-8?q?=E5=B9=B6=E6=9C=89=E6=8E=A8=E7=90=86=E7=BB=93=E6=9E=9C=EF=BC=8C?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=E9=97=AE=E9=A2=98:=E6=B2=A1=E6=B3=95?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E7=BB=93=E6=9D=9F=E8=BF=9B=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/convergence/convergence.cpp | 80 +++++++++++-------- .../src/convergence/convergence.h | 6 +- 2 files changed, 51 insertions(+), 35 deletions(-) diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp index f70a70134..a95e0fa48 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp @@ -87,22 +87,23 @@ AclLiteError ConvergenceThread::Process(int msgId, shared_ptr data) { return ACLLITE_OK; } -// to-do convergence frames -std::shared_ptr ConvergenceThread::CropAndPaste() { - int32_t deviceId = convQueue_.front()->deviceId; +// converge pictures and clear target convQueue_ buffer +std::shared_ptr ConvergenceThread::CropAndPaste(int msgNum) { + auto target_queue = convQueue_[msgNum]; + int32_t deviceId = target_queue.front()->deviceId; aclrtSetDevice(deviceId); aclrtContext context; aclrtCreateContext(&context, deviceId); aclrtStream stream; aclrtCreateStream(&stream); aclrtGetRunMode(&runMode_); - int inputWidth = convQueue_.front()->decodedImg[0].width; // decode出来的结构图长宽更大? + int inputWidth = target_queue.front()->decodedImg[0].width; // decode出来的结构图长宽更大? //int inputWidth = modelWidth_; - int inputHeight = convQueue_.front()->decodedImg[0].height; + int inputHeight = target_queue.front()->decodedImg[0].height; //int inputHeight = modelHeight_; - uint32_t singleImageBufferSize = convQueue_.front()->decodedImg[0].size; + uint32_t singleImageBufferSize = target_queue.front()->decodedImg[0].size; std::cout<< "===================single decode image buffer size:" << singleImageBufferSize< ConvergenceThread::CropAndPaste() { uint32_t vpcOutBufferSize_ = dvppOutWidthStride * dvppOutHeightStride * sizeAlignment / sizeNum; - //debug - std::cout << "==================== outputwidth x outputHeight:" << outputWidth << "," << outputHeight << std::endl; - std::cout << "==================== dvppOutWidthStride x dvppOutHeightStride:" << dvppOutWidthStride << "," << dvppOutHeightStride << std::endl; - //debug - - aclError aclRet = acldvppMalloc(&vpcOutBufferDev_, vpcOutBufferSize_); acldvppPicDesc *vpcOutputDesc_ = acldvppCreatePicDesc(); INFO_LOG("acldvppCreatePicDesc w=%d/%d,h=%d/%d,vpcOutBufferSize=%u",dvppOutWidth,dvppOutWidthStride,dvppOutHeight,dvppOutHeightStride,vpcOutBufferSize_); @@ -140,23 +135,23 @@ std::shared_ptr ConvergenceThread::CropAndPaste() { acldvppSetPicDescSize(vpcOutputDesc_, vpcOutBufferSize_); // make a new shared ptr over a copy buffer - std::shared_ptr new_message = std::make_shared(*convQueue_[0]); + std::shared_ptr new_message = std::make_shared(*target_queue[0]); std:cout<< "000000000000000000000000=======channel, msgNum:" << new_message->channelId << "," << new_message->msgNum << std::endl; new_message->decodedImg.clear(); new_message->frame.clear(); - for (int i = 0; i < convQueue_[0]->decodedImg.size(); i++) { + for (int i = 0; i < target_queue[0]->decodedImg.size(); i++) { std::vector frames; - for (int j = 0; j < convQueue_.size(); j++) { + for (int j = 0; j < target_queue.size(); j++) { void *inBufferDev = nullptr; acldvppMalloc(&inBufferDev, singleImageBufferSize); if (runMode_ == ACL_HOST) { aclrtMemcpy(inBufferDev, singleImageBufferSize, - convQueue_[j]->decodedImg[i].data.get(), + target_queue[j]->decodedImg[i].data.get(), singleImageBufferSize, ACL_MEMCPY_HOST_TO_DEVICE); } else { aclrtMemcpy(inBufferDev, singleImageBufferSize, - convQueue_[j]->decodedImg[i].data.get(), + target_queue[j]->decodedImg[i].data.get(), singleImageBufferSize, ACL_MEMCPY_DEVICE_TO_DEVICE); } @@ -211,7 +206,7 @@ std::shared_ptr ConvergenceThread::CropAndPaste() { pasteArea_ = nullptr; (void)acldvppDestroyPicDesc(vpcInputDesc_); vpcInputDesc_ = nullptr; - frames.push_back(convQueue_[j]->frame[i]); + frames.push_back(target_queue[j]->frame[i]); } ImageData image; @@ -226,7 +221,7 @@ std::shared_ptr ConvergenceThread::CropAndPaste() { image.data = SHARED_PTR_DVPP_BUF(vdecOutBufferDev); new_message->decodedImg.push_back(image); - assert(convQueue_[0]->convergenceNum == frames.size()); + assert(target_queue[0]->convergenceNum == frames.size()); new_message->convergenceNum = frames.size(); cv::Mat hcat; @@ -241,9 +236,8 @@ std::shared_ptr ConvergenceThread::CropAndPaste() { frames.clear(); } - // clear and reset messageOrder - convQueue_.clear(); - messageOrder_ = -1; + // clear + convQueue_.erase(msgNum); (void)acldvppDestroyPicDesc(vpcOutputDesc_); vpcOutputDesc_ = nullptr; @@ -270,25 +264,45 @@ AclLiteError ConvergenceThread::MsgProcess(shared_ptr& detectData { assert(!detectDataMsg->decodedImg.empty()); detectDataMsg->convergenceNum = channelNum_; - int size = convQueue_.size(); - // mark the messageOrder + + int msgNum = detectDataMsg->msgNum; + int channelId = detectDataMsg->channelId; + if(convQueue_.count(msgNum)) { + convQueue_[msgNum].push_back(detectDataMsg); + } else { + convQueue_[msgNum] = std::vector>{detectDataMsg}; + } + if(convQueue_[msgNum].size() a, std::shared_ptr b) { + return a->channelId < b->channelId; + }); + + // int size = convQueue_.size(); + // // mark the messageOrder // if(!size) { // messageOrder_ = detectDataMsg->msgNum; // } - // // resend the message to pipeline if the msg Order is not same with stacked message + // // // resend the message to pipeline if the msg Order is not same with stacked message // if(detectDataMsg->msgNum != messageOrder_ ) { // SendMessage(selfThreadId_, MSG_CONVERGENCE, detectDataMsg); // return ACLLITE_OK; // } - if(size < channelNum_) { - convQueue_.push_back(detectDataMsg); - assert(!convQueue_.back()->decodedImg.empty()); - return ACLLITE_OK; - } - + // if(size < channelNum_) { + // convQueue_.push_back(detectDataMsg); + // assert(!convQueue_.back()->decodedImg.empty()); + // } + // size = convQueue_.size(); + // if(size < channelNum_) return ACLLITE_OK; // replace message with new convergenced message - detectDataMsg = CropAndPaste(); - std::cout << "=========================after crop and paster, decode images count:"<< detectDataMsg->decodedImg.size() << "=============================\n"; + detectDataMsg = CropAndPaste(msgNum); + std::cout << "=========================after crop and paste, decode images count:"<< detectDataMsg->decodedImg.size() << "=============================\n"; + + std::cout << "wwwwwwwwwwwwwwwwwwwwwwwwwwwwww======after crop and paste, outpuut decode image w h:" << detectDataMsg->decodedImg[0].width << "," << detectDataMsg->decodedImg[0].height << std::endl; + // assert(false); return ACLLITE_OK; } diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.h b/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.h index 3bf4277d9..31b99ccc8 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.h +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.h @@ -22,6 +22,7 @@ #include #include #include +#include #include "acl/acl.h" #include "VideoCapture.h" #include "AclLiteThread.h" @@ -37,7 +38,7 @@ public: ~ConvergenceThread(); AclLiteError Init(); AclLiteError Process(int msgId, std::shared_ptr data); - std::shared_ptr CropAndPaste(); + std::shared_ptr CropAndPaste(int msgNum); private: AclLiteError MsgProcess(std::shared_ptr& detectDataMsg); @@ -53,7 +54,8 @@ private: uint32_t messageOrder_ = -1; int selfThreadId_; - std::vector> convQueue_; + // std::vector> convQueue_; + std::unordered_map>> convQueue_; aclrtRunMode runMode_; }; -- Gitee From 72c82b339b8bc436e758d939d40431054f5a6b41 Mon Sep 17 00:00:00 2001 From: xuxiaolong Date: Wed, 4 Sep 2024 09:38:52 +0800 Subject: [PATCH 3/9] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=A4=9Apost?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/convergence/convergence.cpp | 27 +---------- .../src/convergence/convergence.h | 1 - .../src/dataInput/dataInput.cpp | 48 +++++++++---------- .../src/dataOutput/dataOutput.cpp | 13 ++++- .../src/dataOutput/dataOutput.h | 3 +- .../sampleYOLOV7MultiInput/src/main.cpp | 2 +- 6 files changed, 39 insertions(+), 55 deletions(-) diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp index a95e0fa48..b34d5ff5f 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp @@ -97,14 +97,11 @@ std::shared_ptr ConvergenceThread::CropAndPaste(int msgNum) { aclrtStream stream; aclrtCreateStream(&stream); aclrtGetRunMode(&runMode_); - int inputWidth = target_queue.front()->decodedImg[0].width; // decode出来的结构图长宽更大? - //int inputWidth = modelWidth_; + int inputWidth = target_queue.front()->decodedImg[0].width; int inputHeight = target_queue.front()->decodedImg[0].height; - //int inputHeight = modelHeight_; uint32_t singleImageBufferSize = target_queue.front()->decodedImg[0].size; - std::cout<< "===================single decode image buffer size:" << singleImageBufferSize< ConvergenceThread::CropAndPaste(int msgNum) { // make a new shared ptr over a copy buffer std::shared_ptr new_message = std::make_shared(*target_queue[0]); - std:cout<< "000000000000000000000000=======channel, msgNum:" << new_message->channelId << "," << new_message->msgNum << std::endl; new_message->decodedImg.clear(); new_message->frame.clear(); for (int i = 0; i < target_queue[0]->decodedImg.size(); i++) { @@ -281,28 +277,7 @@ AclLiteError ConvergenceThread::MsgProcess(shared_ptr& detectData return a->channelId < b->channelId; }); - // int size = convQueue_.size(); - // // mark the messageOrder - // if(!size) { - // messageOrder_ = detectDataMsg->msgNum; - // } - // // // resend the message to pipeline if the msg Order is not same with stacked message - // if(detectDataMsg->msgNum != messageOrder_ ) { - // SendMessage(selfThreadId_, MSG_CONVERGENCE, detectDataMsg); - // return ACLLITE_OK; - // } - // if(size < channelNum_) { - // convQueue_.push_back(detectDataMsg); - // assert(!convQueue_.back()->decodedImg.empty()); - // } - // size = convQueue_.size(); - // if(size < channelNum_) return ACLLITE_OK; - // replace message with new convergenced message detectDataMsg = CropAndPaste(msgNum); - std::cout << "=========================after crop and paste, decode images count:"<< detectDataMsg->decodedImg.size() << "=============================\n"; - - std::cout << "wwwwwwwwwwwwwwwwwwwwwwwwwwwwww======after crop and paste, outpuut decode image w h:" << detectDataMsg->decodedImg[0].width << "," << detectDataMsg->decodedImg[0].height << std::endl; - // assert(false); return ACLLITE_OK; } diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.h b/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.h index 31b99ccc8..cef83075b 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.h +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.h @@ -51,7 +51,6 @@ private: AclLiteImageProc dvpp_; bool isReleased; uint32_t batch_; - uint32_t messageOrder_ = -1; int selfThreadId_; // std::vector> convQueue_; diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.cpp index 88acfa011..a2fde1449 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.cpp @@ -229,7 +229,7 @@ AclLiteError DataInputThread::ReadStream(shared_ptr &detectDataMs AclLiteError ret = ACLLITE_OK; ImageData decodedImg; - + while (realWaitTime_ < waitTime_) { ret = cap_->Read(decodedImg); if (ret == ACLLITE_ERROR_DECODE_FINISH) { @@ -338,31 +338,31 @@ AclLiteError DataInputThread::MsgSend(shared_ptr &detectDataMsg) { AclLiteError ret; if (detectDataMsg->isLastFrame == false) { - while (1) { - if (convergence_) { - assert(!detectDataMsg->decodedImg.empty()); - ret = SendMessage(detectDataMsg->convergenceThreadId, MSG_CONVERGENCE, - detectDataMsg); - } else { - ret = SendMessage(detectDataMsg->detectPreThreadId, - MSG_PREPROC_DETECTDATA, detectDataMsg); - } - if (ret == ACLLITE_ERROR_ENQUEUE) { - usleep(kSleepTime); - continue; - } else if(ret == ACLLITE_OK) { - break; - } else { - ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); - return ret; - } + while (1) { + if (convergence_) { + assert(!detectDataMsg->decodedImg.empty()); + ret = SendMessage(detectDataMsg->convergenceThreadId, MSG_CONVERGENCE, + detectDataMsg); + } else { + ret = SendMessage(detectDataMsg->detectPreThreadId, + MSG_PREPROC_DETECTDATA, detectDataMsg); } - - ret = SendMessage(selfThreadId_, MSG_READ_FRAME, nullptr); - if (ret != ACLLITE_OK) { - ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); - return ret; + if (ret == ACLLITE_ERROR_ENQUEUE) { + usleep(kSleepTime); + continue; + } else if (ret == ACLLITE_OK) { + break; + } else { + ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); + return ret; } + } + + ret = SendMessage(selfThreadId_, MSG_READ_FRAME, nullptr); + if (ret != ACLLITE_OK) { + ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); + return ret; + } } else { for (int i = 0; i < postThreadNum_; i++) { while (1) { diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/dataOutput/dataOutput.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/dataOutput/dataOutput.cpp index e969701ac..07d07c88e 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/dataOutput/dataOutput.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/dataOutput/dataOutput.cpp @@ -34,8 +34,8 @@ const uint32_t kOneMSec = 1000; const uint32_t kCountFps = 100; } -DataOutputThread::DataOutputThread(aclrtRunMode& runMode, string outputDataType, string outputPath, int postThreadNum) - :runMode_(runMode), outputDataType_(outputDataType), +DataOutputThread::DataOutputThread(bool convergence, aclrtRunMode& runMode, string outputDataType, string outputPath, int postThreadNum) + :convergence_(convergence), runMode_(runMode), outputDataType_(outputDataType), outputPath_(outputPath), shutdown_(0), postNum_(postThreadNum) { } @@ -123,6 +123,15 @@ AclLiteError DataOutputThread::RecordQueue(shared_ptr detectDataM AclLiteError DataOutputThread::DataProcess() { + if (convergence_) { + for (int i = 0; i < postNum_; i++) { + if (postQueue_[i].empty()) continue; + shared_ptr detectDataMsg = postQueue_[i].front(); + ProcessOutput(detectDataMsg); + postQueue_[i].pop(); + } + return ACLLITE_OK; + } int flag = 0; for (int i = 0; i < postNum_; i++) { if (!postQueue_[i].empty()) diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/dataOutput/dataOutput.h b/inference/modelInference/sampleYOLOV7MultiInput/src/dataOutput/dataOutput.h index 245f0f832..fcb098c2a 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/dataOutput/dataOutput.h +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/dataOutput/dataOutput.h @@ -33,7 +33,7 @@ class DataOutputThread : public AclLiteThread { public: - DataOutputThread(aclrtRunMode& runMode, + DataOutputThread(bool convergence, aclrtRunMode& runMode, std::string outputDataType, std::string outputPath, int postThreadNum); ~DataOutputThread(); @@ -66,6 +66,7 @@ private: uint32_t frameCnt_; int64_t lastDecodeTime_; int64_t lastRecordTime_; + bool convergence_ = false; }; #endif diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/main.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/main.cpp index a15f66f61..bf4c80364 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/main.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/main.cpp @@ -218,7 +218,7 @@ void CreateALLThreadInstance(vector& threadTbl, AclLiteResou } AclLiteThreadParam dataOutputParam; - dataOutputParam.threadInst = new DataOutputThread(runMode, outputType, outputPath, kPostNum); + dataOutputParam.threadInst = new DataOutputThread(convergence, runMode, outputType, outputPath, kPostNum); dataOutputParam.threadInstName.assign(dataOutputName.c_str()); dataOutputParam.context = context; dataOutputParam.runMode = runMode; -- Gitee From 7c1ddf3b44e1f44e62182444bc7e6b9a8e25743a Mon Sep 17 00:00:00 2001 From: xuxiaolong Date: Tue, 3 Sep 2024 15:47:11 +0800 Subject: [PATCH 4/9] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=9B=9B=E8=B7=AF?= =?UTF-8?q?=E8=A7=86=E9=A2=91=E8=BF=9B=E8=A1=8C=E6=8A=BD=E5=B8=A7=E6=8B=BC?= =?UTF-8?q?=E6=8E=A5=E6=8E=A8=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/src/AclLiteModel.cpp | 2 +- .../sampleYOLOV7MultiInput/inc/Params.h | 6 +- .../sampleYOLOV7MultiInput/scripts/env.sh | 15 + .../sampleYOLOV7MultiInput/src/CMakeLists.txt | 3 + .../src/convergence/convergence.cpp | 299 ++++++++++++++++++ .../src/convergence/convergence.h | 61 ++++ .../src/dataInput/dataInput.cpp | 84 +++-- .../src/dataInput/dataInput.h | 6 +- .../src/dataOutput/dataOutput.cpp | 36 ++- .../src/dataOutput/dataOutput.h | 3 +- .../src/detectPreprocess/detectPreprocess.cpp | 29 +- .../sampleYOLOV7MultiInput/src/main.cpp | 55 +++- 12 files changed, 541 insertions(+), 58 deletions(-) create mode 100644 inference/modelInference/sampleYOLOV7MultiInput/scripts/env.sh create mode 100644 inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp create mode 100644 inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.h diff --git a/inference/modelInference/sampleYOLOV7MultiInput/common/src/AclLiteModel.cpp b/inference/modelInference/sampleYOLOV7MultiInput/common/src/AclLiteModel.cpp index 7be8426ef..30cc33305 100755 --- a/inference/modelInference/sampleYOLOV7MultiInput/common/src/AclLiteModel.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/common/src/AclLiteModel.cpp @@ -447,7 +447,7 @@ AclLiteError AclLiteModel::GetOutputItem(InferenceOutput& out, uint32_t idx, boo ACL_MEM_MALLOC_NORMAL_ONLY); if (ret != ACL_SUCCESS) { ACLLITE_LOG_ERROR("Create output failed for malloc " - "device failed, size %d", (int)bufferSize); + "device failed, size %d, ret:%d", (int)bufferSize, ret); return ACLLITE_ERROR_MALLOC_DEVICE; } ret = aclUpdateDataBuffer(dataBuffer, outputBuffer, bufferSize); diff --git a/inference/modelInference/sampleYOLOV7MultiInput/inc/Params.h b/inference/modelInference/sampleYOLOV7MultiInput/inc/Params.h index c4d939da8..a87b6d098 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/inc/Params.h +++ b/inference/modelInference/sampleYOLOV7MultiInput/inc/Params.h @@ -44,6 +44,7 @@ const int MSG_OUTPUT_FRAME = 6; const int MSG_ENCODE_FINISH = 7; const int MSG_RTSP_DISPLAY = 8; const int MSG_APP_EXIT = 9; +const int MSG_CONVERGENCE = 10; const std::string kDataInputName = "dataInput"; const std::string kPreName = "pre"; @@ -51,6 +52,7 @@ const std::string kInferName = "infer"; const std::string kPostName = "post"; const std::string kDataOutputName = "dataOutput"; const std::string kRtspDisplayName = "rtspDisplay"; +const std::string kConvergenceName = "convergence"; } struct DetectDataMsg { @@ -59,11 +61,13 @@ struct DetectDataMsg { int detectPostThreadId; int dataOutputThreadId; int rtspDisplayThreadId; + int convergenceThreadId; int postId; uint32_t deviceId; uint32_t channelId; // record msg belongs to which rtsp/video channel bool isLastFrame; // whether the last frame of rtsp/video of this channel has been decoded int msgNum; // record frameID in rtsp/video of this channel + int convergenceNum; // how many channels to converge std::vector decodedImg; // original image (NV12) ImageData modelInputImg; // image after detect preprocess std::vector frame; // original image (BGR) needed by postprocess @@ -71,4 +75,4 @@ struct DetectDataMsg { std::vector textPrint; }; -#endif \ No newline at end of file +#endif diff --git a/inference/modelInference/sampleYOLOV7MultiInput/scripts/env.sh b/inference/modelInference/sampleYOLOV7MultiInput/scripts/env.sh new file mode 100644 index 000000000..7d98a6364 --- /dev/null +++ b/inference/modelInference/sampleYOLOV7MultiInput/scripts/env.sh @@ -0,0 +1,15 @@ +. /usr/local/Ascend/ascend-toolkit/set_env.sh +rm -rf plog +export ASCEND_PROCESS_LOG_PATH=./plog + +export INSTALL_DIR=/usr/local/Ascend/ascend-toolkit/latest + +export DDK_PATH=/usr/local/Ascend/ascend-toolkit/latest + +export NPU_HOST_LIB=$DDK_PATH/runtime/lib64/stub + +export THIRDPART_PATH=${DDK_PATH}/thirdpart + +export LD_LIBRARY_PATH=${THIRDPART_PATH}/lib:$LD_LIBRARY_PATH + +export LD_LIBRARY_PATH=/usr/local/Ascend/ascend-toolkit/latest/aarch64-linux/lib64:$LD_LIBRARY_PATH diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/CMakeLists.txt b/inference/modelInference/sampleYOLOV7MultiInput/src/CMakeLists.txt index 3e48d4133..41d60e9a3 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/CMakeLists.txt +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/CMakeLists.txt @@ -41,6 +41,7 @@ include_directories( ${INC_PATH}/runtime/include/ ../inc/ ../common/include/ + $ENV{INSTALL_DIR}/runtime/include/ ) if(target STREQUAL "Simulator_Function") @@ -61,7 +62,9 @@ add_executable(main dataOutput/dataOutput.cpp pushrtsp/pictortsp.cpp pushrtsp/pushrtspthread.cpp + convergence/convergence.cpp main.cpp) +set_target_properties(main PROPERTIES LINKER_LANGUAGE CXX) target_sources(main PUBLIC diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp new file mode 100644 index 000000000..b34d5ff5f --- /dev/null +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp @@ -0,0 +1,299 @@ +/** +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2022. 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. + +* File sample_process.cpp +* Description: handle acl resource +*/ +#include +#include +#include "assert.h" + +#include "Params.h" +#include "convergence.h" +#include "AclLiteApp.h" + + +#include "AclLiteResource.h" +#include "AclLiteUtils.h" + +#include "acl/acl.h" +#include "acl/ops/acl_dvpp.h" + +using namespace std; + +namespace { +const uint32_t kSleepTime = 500; +} + +uint32_t AlignmentHelper(uint32_t origSize, uint32_t alignment) +{ + if (alignment == 0) { + return 0; + } + uint32_t alignmentH = alignment - 1; + return (origSize + alignmentH) / alignment * alignment; +} + +ConvergenceThread::ConvergenceThread(aclrtRunMode runMode, uint32_t channelNum ,uint32_t modelWidth, uint32_t modelHeight, + uint32_t batch) + :runMode_(runMode), channelNum_(channelNum), modelWidth_(modelWidth), modelHeight_(modelHeight), isReleased(false), batch_(batch) +{ +} + +ConvergenceThread::~ConvergenceThread() +{ + if(!isReleased) { + dvpp_.DestroyResource(); + } + isReleased = true; +} + +AclLiteError ConvergenceThread::Init() +{ + selfThreadId_ = SelfInstanceId(); + AclLiteError aclRet = dvpp_.Init("DVPP_CHNMODE_VPC"); + if (aclRet) { + ACLLITE_LOG_ERROR("Dvpp init failed, error %d", aclRet); + return ACLLITE_ERROR; + } + + return ACLLITE_OK; +} + +AclLiteError ConvergenceThread::Process(int msgId, shared_ptr data) { + shared_ptr ldata = static_pointer_cast(data); + switch (msgId) { + case MSG_CONVERGENCE: + MsgProcess(ldata); + MsgSend(ldata); + break; + default: + ACLLITE_LOG_INFO("Convergence thread ignore msg %d", msgId); + break; + } + + return ACLLITE_OK; +} + +// converge pictures and clear target convQueue_ buffer +std::shared_ptr ConvergenceThread::CropAndPaste(int msgNum) { + auto target_queue = convQueue_[msgNum]; + int32_t deviceId = target_queue.front()->deviceId; + aclrtSetDevice(deviceId); + aclrtContext context; + aclrtCreateContext(&context, deviceId); + aclrtStream stream; + aclrtCreateStream(&stream); + aclrtGetRunMode(&runMode_); + int inputWidth = target_queue.front()->decodedImg[0].width; + + int inputHeight = target_queue.front()->decodedImg[0].height; + + uint32_t singleImageBufferSize = target_queue.front()->decodedImg[0].size; + + int outputWidth = inputWidth * 2; + int outputHeight = inputHeight * 2; + acldvppChannelDesc* dvppChannelDesc_ = acldvppCreateChannelDesc(); + acldvppCreateChannel(dvppChannelDesc_); + uint32_t widthAlignment = 16; + uint32_t heightAlignment = 1; + uint32_t sizeAlignment = 3; + uint32_t sizeNum = 2; + + void *vpcOutBufferDev_ = nullptr; + int dvppOutWidth = outputWidth; + int dvppOutHeight = outputHeight; + int dvppOutWidthStride = AlignmentHelper(dvppOutWidth, widthAlignment); + int dvppOutHeightStride = AlignmentHelper(dvppOutHeight, heightAlignment); + uint32_t vpcOutBufferSize_ = + dvppOutWidthStride * dvppOutHeightStride * sizeAlignment / sizeNum; + + aclError aclRet = acldvppMalloc(&vpcOutBufferDev_, vpcOutBufferSize_); + acldvppPicDesc *vpcOutputDesc_ = acldvppCreatePicDesc(); + INFO_LOG("acldvppCreatePicDesc w=%d/%d,h=%d/%d,vpcOutBufferSize=%u",dvppOutWidth,dvppOutWidthStride,dvppOutHeight,dvppOutHeightStride,vpcOutBufferSize_); + acldvppSetPicDescData(vpcOutputDesc_, vpcOutBufferDev_); + acldvppSetPicDescFormat(vpcOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); + acldvppSetPicDescWidth(vpcOutputDesc_, dvppOutWidth); + acldvppSetPicDescHeight(vpcOutputDesc_, dvppOutHeight); + acldvppSetPicDescWidthStride(vpcOutputDesc_, dvppOutWidthStride); + acldvppSetPicDescHeightStride(vpcOutputDesc_, dvppOutHeightStride); + acldvppSetPicDescSize(vpcOutputDesc_, vpcOutBufferSize_); + + // make a new shared ptr over a copy buffer + std::shared_ptr new_message = std::make_shared(*target_queue[0]); + + new_message->decodedImg.clear(); + new_message->frame.clear(); + for (int i = 0; i < target_queue[0]->decodedImg.size(); i++) { + std::vector frames; + for (int j = 0; j < target_queue.size(); j++) { + void *inBufferDev = nullptr; + acldvppMalloc(&inBufferDev, singleImageBufferSize); + if (runMode_ == ACL_HOST) { + aclrtMemcpy(inBufferDev, singleImageBufferSize, + target_queue[j]->decodedImg[i].data.get(), + singleImageBufferSize, ACL_MEMCPY_HOST_TO_DEVICE); + } else { + aclrtMemcpy(inBufferDev, singleImageBufferSize, + target_queue[j]->decodedImg[i].data.get(), + singleImageBufferSize, ACL_MEMCPY_DEVICE_TO_DEVICE); + } + + uint32_t oddNum = 1; + uint32_t cropSizeWidth = inputWidth; + uint32_t cropSizeHeight = inputHeight; + + uint32_t cropLeftOffset = 0; // must even + uint32_t cropRightOffset = + cropLeftOffset + cropSizeWidth - oddNum; // must odd 0+1024-1 + uint32_t cropTopOffset = 0; // must even + uint32_t cropBottomOffset = + cropTopOffset + cropSizeHeight - oddNum; // must odd 0+1068-1 + acldvppRoiConfig *cropArea_ = acldvppCreateRoiConfig( + cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset); + // 通过for循环改 + uint32_t pasteLeftOffset = 0 + (i % 2) * inputWidth; // must even + uint32_t pasteRightOffset = + pasteLeftOffset + cropSizeWidth - oddNum; // must odd + uint32_t pasteTopOffset = 0 + (i / 2) * inputHeight; // must even + uint32_t pasteBottomOffset = + pasteTopOffset + cropSizeHeight - oddNum; // must odd + acldvppRoiConfig *pasteArea_ = + acldvppCreateRoiConfig(pasteLeftOffset, pasteRightOffset, + pasteTopOffset, pasteBottomOffset); + + uint32_t jpegOutWidthStride = + AlignmentHelper(inputWidth, widthAlignment); + uint32_t jpegOutHeightStride = + AlignmentHelper(inputHeight, heightAlignment); + uint32_t jpegOutBufferSize = + jpegOutWidthStride * jpegOutHeightStride * sizeAlignment / sizeNum; + acldvppPicDesc *vpcInputDesc_ = acldvppCreatePicDesc(); + acldvppSetPicDescData(vpcInputDesc_, + inBufferDev); // JpegD -> vpcCropAndPaste + acldvppSetPicDescFormat(vpcInputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); + acldvppSetPicDescWidth(vpcInputDesc_, inputWidth); + acldvppSetPicDescHeight(vpcInputDesc_, inputHeight); + acldvppSetPicDescWidthStride(vpcInputDesc_, jpegOutWidthStride); + acldvppSetPicDescHeightStride(vpcInputDesc_, jpegOutHeightStride); + acldvppSetPicDescSize(vpcInputDesc_, jpegOutBufferSize); + + // crop and patse pic + acldvppVpcCropAndPasteAsync(dvppChannelDesc_, vpcInputDesc_, + vpcOutputDesc_, cropArea_, pasteArea_, + stream); + aclrtSynchronizeStream(stream); + + (void)acldvppDestroyRoiConfig(cropArea_); + cropArea_ = nullptr; + (void)acldvppDestroyRoiConfig(pasteArea_); + pasteArea_ = nullptr; + (void)acldvppDestroyPicDesc(vpcInputDesc_); + vpcInputDesc_ = nullptr; + frames.push_back(target_queue[j]->frame[i]); + } + + ImageData image; + image.format = acldvppGetPicDescFormat(vpcOutputDesc_); + image.width = acldvppGetPicDescWidth(vpcOutputDesc_); + image.height = acldvppGetPicDescHeight(vpcOutputDesc_); + image.alignWidth = acldvppGetPicDescWidthStride(vpcOutputDesc_); + image.alignHeight = acldvppGetPicDescHeightStride(vpcOutputDesc_); + image.size = acldvppGetPicDescSize(vpcOutputDesc_); + + void *vdecOutBufferDev = acldvppGetPicDescData(vpcOutputDesc_); + image.data = SHARED_PTR_DVPP_BUF(vdecOutBufferDev); + new_message->decodedImg.push_back(image); + + assert(target_queue[0]->convergenceNum == frames.size()); + new_message->convergenceNum = frames.size(); + + cv::Mat hcat; + + cv::hconcat(frames[0], frames[1], hcat); + cv::Mat vcat; + cv::hconcat(frames[2], frames[3], vcat); + cv::Mat res; + cv::vconcat(hcat, vcat, res); + new_message->frame.push_back(res); + + frames.clear(); + } + + // clear + convQueue_.erase(msgNum); + + (void)acldvppDestroyPicDesc(vpcOutputDesc_); + vpcOutputDesc_ = nullptr; + (void)acldvppDestroyChannel(dvppChannelDesc_); + (void)acldvppDestroyChannelDesc(dvppChannelDesc_); + dvppChannelDesc_ = nullptr; + + if (vpcOutBufferDev_ != nullptr) { + (void)acldvppFree(vpcOutBufferDev_); + vpcOutBufferDev_ = nullptr; + } + aclrtDestroyStream(stream); + stream = nullptr; + aclrtDestroyContext(context); + context = nullptr; + + return new_message; +} + + + + +AclLiteError ConvergenceThread::MsgProcess(shared_ptr& detectDataMsg) +{ + assert(!detectDataMsg->decodedImg.empty()); + detectDataMsg->convergenceNum = channelNum_; + + int msgNum = detectDataMsg->msgNum; + int channelId = detectDataMsg->channelId; + if(convQueue_.count(msgNum)) { + convQueue_[msgNum].push_back(detectDataMsg); + } else { + convQueue_[msgNum] = std::vector>{detectDataMsg}; + } + if(convQueue_[msgNum].size() a, std::shared_ptr b) { + return a->channelId < b->channelId; + }); + + detectDataMsg = CropAndPaste(msgNum); + return ACLLITE_OK; +} + +AclLiteError ConvergenceThread::MsgSend(shared_ptr detectDataMsg) +{ + while (1) { + AclLiteError ret = SendMessage(detectDataMsg->detectPreThreadId, MSG_PREPROC_DETECTDATA, detectDataMsg); + if (ret == ACLLITE_ERROR_ENQUEUE) { + usleep(kSleepTime); + continue; + } else if(ret == ACLLITE_OK) { + break; + } else { + ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); + return ret; + } + } + return ACLLITE_OK; +} diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.h b/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.h new file mode 100644 index 000000000..cef83075b --- /dev/null +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.h @@ -0,0 +1,61 @@ +/** +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2022. 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. + +* File sample_process.h +* Description: handle acl resource +*/ +#ifndef CONVERGENCETHREAD_H +#define CONVERGENCETHREAD_H +#pragma once +#include +#include +#include +#include +#include "acl/acl.h" +#include "VideoCapture.h" +#include "AclLiteThread.h" +#include "AclLiteImageProc.h" +#include "Params.h" + +#define INFO_LOG(fmt, args...) fprintf(stdout, "[INFO] " fmt "\n", ##args) + +class ConvergenceThread : public AclLiteThread { +public: + ConvergenceThread(aclrtRunMode runMode_, uint32_t channelNum_, uint32_t modelWidth, uint32_t modelHeight, + uint32_t batch); + ~ConvergenceThread(); + AclLiteError Init(); + AclLiteError Process(int msgId, std::shared_ptr data); + std::shared_ptr CropAndPaste(int msgNum); + +private: + AclLiteError MsgProcess(std::shared_ptr& detectDataMsg); + AclLiteError MsgSend(std::shared_ptr detectDataMsg); + +private: + uint32_t channelNum_; // how many channels to converge + uint32_t modelWidth_; + uint32_t modelHeight_; + AclLiteImageProc dvpp_; + bool isReleased; + uint32_t batch_; + int selfThreadId_; + + // std::vector> convQueue_; + std::unordered_map>> convQueue_; + aclrtRunMode runMode_; +}; + +#endif diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.cpp index d9c343012..a2fde1449 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.cpp @@ -31,10 +31,10 @@ namespace{ using namespace std; DataInputThread::DataInputThread( - int32_t deviceId, int32_t channelId, aclrtRunMode& runMode, + bool convergence, int32_t deviceId, int32_t channelId, aclrtRunMode& runMode, string inputDataType, string inputDataPath, string inferName, int postThreadNum, uint32_t batch, int framesPerSecond) - :deviceId_(deviceId), channelId_(channelId), runMode_(runMode), postproId_(0), + :convergence_(convergence), deviceId_(deviceId), channelId_(channelId), runMode_(runMode), postproId_(0), inputDataType_(inputDataType), inputDataPath_(inputDataPath), inferName_(inferName), cap_(nullptr), frameCnt_(0), postThreadNum_(postThreadNum), selfThreadId_(INVALID_INSTANCE_ID), preThreadId_(INVALID_INSTANCE_ID), @@ -122,6 +122,7 @@ AclLiteError DataInputThread::Init() preThreadId_ = GetAclLiteThreadIdByName(kPreName + to_string(channelId_)); dataOutputThreadId_ = GetAclLiteThreadIdByName(kDataOutputName + to_string(channelId_)); rtspDisplayThreadId_ = GetAclLiteThreadIdByName(kRtspDisplayName + to_string(channelId_)); + convergenceThreadId_ = GetAclLiteThreadIdByName(kConvergenceName); for (int i = 0; i < postThreadNum_; i++) { postThreadId_[i] = GetAclLiteThreadIdByName(kPostName + to_string(channelId_) + "_" + to_string(i)); if (postThreadId_[i] == INVALID_INSTANCE_ID) { @@ -150,13 +151,19 @@ AclLiteError DataInputThread::Init() AclLiteError DataInputThread::Process(int msgId, shared_ptr msgData) { + AclLiteError ret; shared_ptr detectDataMsg = make_shared(); + detectDataMsg->convergenceNum = 0; switch (msgId) { case MSG_APP_START: AppStart(); break; case MSG_READ_FRAME: - MsgRead(detectDataMsg); + ret = MsgRead(detectDataMsg); + if(ret != ACLLITE_OK) { + ACLLITE_LOG_ERROR("datainput message read error, ret:%d", ret); + return ret; + } MsgSend(detectDataMsg); break; default: @@ -222,7 +229,7 @@ AclLiteError DataInputThread::ReadStream(shared_ptr &detectDataMs AclLiteError ret = ACLLITE_OK; ImageData decodedImg; - + while (realWaitTime_ < waitTime_) { ret = cap_->Read(decodedImg); if (ret == ACLLITE_ERROR_DECODE_FINISH) { @@ -298,21 +305,30 @@ AclLiteError DataInputThread::MsgRead(shared_ptr &detectDataMsg) detectDataMsg->postId = postproId_; detectDataMsg->dataOutputThreadId = dataOutputThreadId_; detectDataMsg->rtspDisplayThreadId = rtspDisplayThreadId_; + detectDataMsg->convergenceThreadId = convergenceThreadId_; detectDataMsg->deviceId = deviceId_; detectDataMsg->channelId = channelId_; detectDataMsg->msgNum = msgNum_; msgNum_++; - GetOneFrame(detectDataMsg); + ret = GetOneFrame(detectDataMsg); + if (ret != ACLLITE_OK) { + ACLLITE_LOG_ERROR("Get Frame Error, ret:%d", ret); + return ret; + } if (detectDataMsg->isLastFrame) { return ACLLITE_OK; } frameCnt_++; while (frameCnt_ % batch_) { - GetOneFrame(detectDataMsg); - if (detectDataMsg->isLastFrame) { - break; - } - frameCnt_++; + ret = GetOneFrame(detectDataMsg); + if (ret != ACLLITE_OK) { + ACLLITE_LOG_ERROR("Get Frame Error, ret:%d", ret); + return ret; + } + if (detectDataMsg->isLastFrame) { + break; + } + frameCnt_++; } return ACLLITE_OK; @@ -322,28 +338,42 @@ AclLiteError DataInputThread::MsgSend(shared_ptr &detectDataMsg) { AclLiteError ret; if (detectDataMsg->isLastFrame == false) { - while (1) { - ret = SendMessage(detectDataMsg->detectPreThreadId, MSG_PREPROC_DETECTDATA, detectDataMsg); - if (ret == ACLLITE_ERROR_ENQUEUE) { - usleep(kSleepTime); - continue; - } else if(ret == ACLLITE_OK) { - break; - } else { - ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); - return ret; - } + while (1) { + if (convergence_) { + assert(!detectDataMsg->decodedImg.empty()); + ret = SendMessage(detectDataMsg->convergenceThreadId, MSG_CONVERGENCE, + detectDataMsg); + } else { + ret = SendMessage(detectDataMsg->detectPreThreadId, + MSG_PREPROC_DETECTDATA, detectDataMsg); } - - ret = SendMessage(selfThreadId_, MSG_READ_FRAME, nullptr); - if (ret != ACLLITE_OK) { - ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); - return ret; + if (ret == ACLLITE_ERROR_ENQUEUE) { + usleep(kSleepTime); + continue; + } else if (ret == ACLLITE_OK) { + break; + } else { + ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); + return ret; } + } + + ret = SendMessage(selfThreadId_, MSG_READ_FRAME, nullptr); + if (ret != ACLLITE_OK) { + ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); + return ret; + } } else { for (int i = 0; i < postThreadNum_; i++) { while (1) { - ret = SendMessage(detectDataMsg->detectPreThreadId, MSG_PREPROC_DETECTDATA, detectDataMsg); + if (convergence_) { + assert(!detectDataMsg->decodedImg.empty()); + ret = SendMessage(detectDataMsg->convergenceThreadId, + MSG_CONVERGENCE, detectDataMsg); + } else { + ret = SendMessage(detectDataMsg->detectPreThreadId, + MSG_PREPROC_DETECTDATA, detectDataMsg); + } if (ret == ACLLITE_ERROR_ENQUEUE) { usleep(kSleepTime); continue; diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.h b/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.h index 9d79afb52..4a269e2fa 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.h +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.h @@ -31,7 +31,7 @@ class DataInputThread : public AclLiteThread { public: - DataInputThread(int32_t deviceId, int32_t channelId, aclrtRunMode& runMode, + DataInputThread(bool convergence, int32_t deviceId, int32_t channelId, aclrtRunMode& runMode, std::string inputDataType, std::string inputDataPath, std::string inferName, int postThreadNum, uint32_t batch, int framesPerSecond); @@ -50,6 +50,7 @@ private: AclLiteError GetOneFrame(std::shared_ptr &detectDataMsg); private: + bool convergence_ = false; uint32_t deviceId_; uint32_t channelId_; int frameCnt_; @@ -69,6 +70,7 @@ private: int selfThreadId_; int preThreadId_; int inferThreadId_; + int convergenceThreadId_; std::vector postThreadId_; int dataOutputThreadId_; int rtspDisplayThreadId_; @@ -80,4 +82,4 @@ private: int framesPerSecond_; }; -#endif \ No newline at end of file +#endif diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/dataOutput/dataOutput.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/dataOutput/dataOutput.cpp index bcc42e27a..07d07c88e 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/dataOutput/dataOutput.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/dataOutput/dataOutput.cpp @@ -34,8 +34,8 @@ const uint32_t kOneMSec = 1000; const uint32_t kCountFps = 100; } -DataOutputThread::DataOutputThread(aclrtRunMode& runMode, string outputDataType, string outputPath, int postThreadNum) - :runMode_(runMode), outputDataType_(outputDataType), +DataOutputThread::DataOutputThread(bool convergence, aclrtRunMode& runMode, string outputDataType, string outputPath, int postThreadNum) + :convergence_(convergence), runMode_(runMode), outputDataType_(outputDataType), outputPath_(outputPath), shutdown_(0), postNum_(postThreadNum) { } @@ -123,6 +123,15 @@ AclLiteError DataOutputThread::RecordQueue(shared_ptr detectDataM AclLiteError DataOutputThread::DataProcess() { + if (convergence_) { + for (int i = 0; i < postNum_; i++) { + if (postQueue_[i].empty()) continue; + shared_ptr detectDataMsg = postQueue_[i].front(); + ProcessOutput(detectDataMsg); + postQueue_[i].pop(); + } + return ACLLITE_OK; + } int flag = 0; for (int i = 0; i < postNum_; i++) { if (!postQueue_[i].empty()) @@ -187,16 +196,21 @@ AclLiteError DataOutputThread::SaveResultVideo(shared_ptr &detect return ACLLITE_OK; } -AclLiteError DataOutputThread::SaveResultPic(shared_ptr &detectDataMsg) -{ - for(int i = 0; i < detectDataMsg->frame.size(); i++) { - stringstream sstream; - sstream.str(""); - sstream << "../out/channel_" << detectDataMsg->channelId - << "_out_pic_" << detectDataMsg->msgNum << i << ".jpg"; - cv::imwrite(sstream.str(), detectDataMsg->frame[i]); +AclLiteError DataOutputThread::SaveResultPic( + shared_ptr &detectDataMsg) { + for (int i = 0; i < detectDataMsg->frame.size(); i++) { + stringstream sstream; + sstream.str(""); + if (detectDataMsg->convergenceNum) { + sstream << "../out/pics/convergence_" + << "out_pic_" << detectDataMsg->msgNum << i << ".jpg"; + } else { + sstream << "../out/channel_" << detectDataMsg->channelId << "_out_pic_" + << detectDataMsg->msgNum << i << ".jpg"; } - return ACLLITE_OK; + cv::imwrite(sstream.str(), detectDataMsg->frame[i]); + } + return ACLLITE_OK; } AclLiteError DataOutputThread::PrintResult(shared_ptr &detectDataMsg) diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/dataOutput/dataOutput.h b/inference/modelInference/sampleYOLOV7MultiInput/src/dataOutput/dataOutput.h index 245f0f832..fcb098c2a 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/dataOutput/dataOutput.h +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/dataOutput/dataOutput.h @@ -33,7 +33,7 @@ class DataOutputThread : public AclLiteThread { public: - DataOutputThread(aclrtRunMode& runMode, + DataOutputThread(bool convergence, aclrtRunMode& runMode, std::string outputDataType, std::string outputPath, int postThreadNum); ~DataOutputThread(); @@ -66,6 +66,7 @@ private: uint32_t frameCnt_; int64_t lastDecodeTime_; int64_t lastRecordTime_; + bool convergence_ = false; }; #endif diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/detectPreprocess/detectPreprocess.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/detectPreprocess/detectPreprocess.cpp index 504f73649..6c7c4cad7 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/detectPreprocess/detectPreprocess.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/detectPreprocess/detectPreprocess.cpp @@ -18,6 +18,7 @@ */ #include #include +#include #include "Params.h" #include "detectPreprocess.h" #include "AclLiteApp.h" @@ -71,8 +72,8 @@ AclLiteError DetectPreprocessThread::Process(int msgId, shared_ptr data) AclLiteError DetectPreprocessThread::MsgProcess(shared_ptr detectDataMsg) { AclLiteError ret; - - uint32_t modelInputSize = YUV420SP_SIZE(modelWidth_, modelHeight_) * batch_; + uint32_t modelInputSize = YUV420SP_SIZE(modelWidth_ , modelHeight_ ) * batch_; + void* buf = nullptr; ret = aclrtMalloc(&buf, modelInputSize, ACL_MEM_MALLOC_HUGE_FIRST); if ((buf == nullptr) || (ret != ACL_ERROR_NONE)) { @@ -86,17 +87,19 @@ AclLiteError DetectPreprocessThread::MsgProcess(shared_ptr detect size_t pos = 0; for (int i = 0; i < detectDataMsg->decodedImg.size(); i++) { - ImageData resizedImg; - ret = dvpp_.Resize(resizedImg, - detectDataMsg->decodedImg[i], modelWidth_, modelHeight_); - if (ret == ACLLITE_ERROR) { - ACLLITE_LOG_ERROR("Resize image failed"); - return ACLLITE_ERROR; - } - uint32_t dataSize = YUV420SP_SIZE(modelWidth_, modelHeight_); - ret = aclrtMemcpy(batchBuffer + pos, dataSize, - resizedImg.data.get(), resizedImg.size, ACL_MEMCPY_DEVICE_TO_DEVICE); - pos = pos + dataSize; + ImageData resizedImg; + ret = dvpp_.Resize(resizedImg, detectDataMsg->decodedImg[i], modelWidth_, + modelHeight_); + if (ret == ACLLITE_ERROR) { + ACLLITE_LOG_ERROR("Resize image failed"); + return ACLLITE_ERROR; + } + + uint32_t dataSize = + YUV420SP_SIZE(modelWidth_, modelHeight_ ); + ret = aclrtMemcpy(batchBuffer + pos, dataSize, resizedImg.data.get(), + resizedImg.size, ACL_MEMCPY_DEVICE_TO_DEVICE); + pos = pos + dataSize; } detectDataMsg->modelInputImg.data = SHARED_PTR_DEV_BUF(batchBuffer); diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/main.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/main.cpp index 7fd44e01a..bf4c80364 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/main.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/main.cpp @@ -35,6 +35,7 @@ #include "detectInference/detectInference.h" #include "detectPreprocess/detectPreprocess.h" #include "detectPostprocess/detectPostprocess.h" +#include "convergence/convergence.h" #include "pushrtsp/pushrtspthread.h" using namespace std; @@ -47,6 +48,20 @@ int kPostNum = 1; int kFramesPerSecond = 1000; uint32_t kMsgQueueSize = 3; uint32_t argNum = 2; + +bool convergence = false; +} + +bool isPerfectSquare(int num) { + if (num < 0) { + return false; // 负数不是完全平方数 + } + + // 计算平方根并将其四舍五入为整数 + int root = static_cast(round(sqrt(num))); + + // 判断平方后的值是否与原值相等 + return root * root == num; } int MainThreadProcess(uint32_t msgId, @@ -118,6 +133,42 @@ void CreateALLThreadInstance(vector& threadTbl, AclLiteResou modelWidth, modelHeigth, kBatch, kPostNum, kFramesPerSecond); return; } + + // whether convergence + string s = + root["device_config"][i]["model_config"][j]["convergence"] + .asString(); + if (s == "True") { + convergence = true; + int channels = + root["device_config"][i]["model_config"][j]["io_info"] + .size(); + if (!isPerfectSquare(channels)) { + ACLLITE_LOG_ERROR( + "Invalid model config is given! convergence channels: " + "%d " + "is not a perfect square number!", + channels); + } + } + + int channelNum = + root["device_config"][i]["model_config"][j]["io_info"] + .size(); + + if (convergence) { + std::cout << "=============================== convergence " + "===============================" + << std::endl; + AclLiteThreadParam convergenceParam; + convergenceParam.threadInst = new ConvergenceThread( + runMode, channelNum, modelWidth, modelHeigth, kBatch); + convergenceParam.threadInstName.assign( + kConvergenceName.c_str()); + convergenceParam.context = context; + threadTbl.push_back(convergenceParam); + } + // Create inferThread AclLiteThreadParam inferParam; inferParam.threadInst = new DetectInferenceThread(modelPath); @@ -139,7 +190,7 @@ void CreateALLThreadInstance(vector& threadTbl, AclLiteResou string rtspDisplayName = kRtspDisplayName + to_string(channelId); // Create Thread for the input data: AclLiteThreadParam dataInputParam; - dataInputParam.threadInst = new DataInputThread(deviceId, channelId, runMode, + dataInputParam.threadInst = new DataInputThread(convergence, deviceId, channelId, runMode, inputType, inputPath, inferName, kPostNum, kBatch, kFramesPerSecond); dataInputParam.threadInstName.assign(dataInputName.c_str()); dataInputParam.context = context; @@ -167,7 +218,7 @@ void CreateALLThreadInstance(vector& threadTbl, AclLiteResou } AclLiteThreadParam dataOutputParam; - dataOutputParam.threadInst = new DataOutputThread(runMode, outputType, outputPath, kPostNum); + dataOutputParam.threadInst = new DataOutputThread(convergence, runMode, outputType, outputPath, kPostNum); dataOutputParam.threadInstName.assign(dataOutputName.c_str()); dataOutputParam.context = context; dataOutputParam.runMode = runMode; -- Gitee From 0126dbad420e09e8ebb85966e3f9caf65501e731 Mon Sep 17 00:00:00 2001 From: xuxiaolong Date: Thu, 5 Sep 2024 12:35:15 +0800 Subject: [PATCH 5/9] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E8=BF=9B=E7=A8=8B?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E8=87=AA=E5=8A=A8=E9=80=80=E5=87=BA=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/convergence/convergence.cpp | 2 +- .../src/dataInput/dataInput.cpp | 13 ++----------- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp index b34d5ff5f..8ca6b343b 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp @@ -258,7 +258,7 @@ std::shared_ptr ConvergenceThread::CropAndPaste(int msgNum) { AclLiteError ConvergenceThread::MsgProcess(shared_ptr& detectDataMsg) { - assert(!detectDataMsg->decodedImg.empty()); + if(detectDataMsg->decodedImg.empty()) return ACLLITE_OK; detectDataMsg->convergenceNum = channelNum_; int msgNum = detectDataMsg->msgNum; diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.cpp index a2fde1449..5155e5fdb 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.cpp @@ -310,21 +310,13 @@ AclLiteError DataInputThread::MsgRead(shared_ptr &detectDataMsg) detectDataMsg->channelId = channelId_; detectDataMsg->msgNum = msgNum_; msgNum_++; - ret = GetOneFrame(detectDataMsg); - if (ret != ACLLITE_OK) { - ACLLITE_LOG_ERROR("Get Frame Error, ret:%d", ret); - return ret; - } + GetOneFrame(detectDataMsg); if (detectDataMsg->isLastFrame) { return ACLLITE_OK; } frameCnt_++; while (frameCnt_ % batch_) { - ret = GetOneFrame(detectDataMsg); - if (ret != ACLLITE_OK) { - ACLLITE_LOG_ERROR("Get Frame Error, ret:%d", ret); - return ret; - } + GetOneFrame(detectDataMsg); if (detectDataMsg->isLastFrame) { break; } @@ -367,7 +359,6 @@ AclLiteError DataInputThread::MsgSend(shared_ptr &detectDataMsg) for (int i = 0; i < postThreadNum_; i++) { while (1) { if (convergence_) { - assert(!detectDataMsg->decodedImg.empty()); ret = SendMessage(detectDataMsg->convergenceThreadId, MSG_CONVERGENCE, detectDataMsg); } else { -- Gitee From 048d3653358c26fba00c8ec4a8c468067d26c145 Mon Sep 17 00:00:00 2001 From: xuxiaolong Date: Sat, 14 Sep 2024 13:41:34 +0800 Subject: [PATCH 6/9] clean code --- .../common/include/AclLiteApp.h | 1 + .../common/src/AclLiteApp.cpp | 23 ++++++ .../sampleYOLOV7MultiInput/scripts/env.sh | 1 + .../src/convergence/convergence.cpp | 23 +----- .../src/dataInput/dataInput.cpp | 73 ++++++++----------- .../src/detectInference/detectInference.cpp | 16 +--- .../detectPostprocess/detectPostprocess.cpp | 57 +++++---------- .../src/detectPreprocess/detectPreprocess.cpp | 15 +--- 8 files changed, 80 insertions(+), 129 deletions(-) diff --git a/inference/modelInference/sampleYOLOV7MultiInput/common/include/AclLiteApp.h b/inference/modelInference/sampleYOLOV7MultiInput/common/include/AclLiteApp.h index 074d2ff1c..7d659acc2 100755 --- a/inference/modelInference/sampleYOLOV7MultiInput/common/include/AclLiteApp.h +++ b/inference/modelInference/sampleYOLOV7MultiInput/common/include/AclLiteApp.h @@ -77,5 +77,6 @@ private: AclLiteApp& CreateAclLiteAppInstance(); AclLiteApp& GetAclLiteAppInstance(); AclLiteError SendMessage(int dest, int msgId, std::shared_ptr data); +AclLiteError PromiseSendMessage(int dest, int msgId, std::shared_ptr data); int GetAclLiteThreadIdByName(const std::string& threadName); #endif \ No newline at end of file diff --git a/inference/modelInference/sampleYOLOV7MultiInput/common/src/AclLiteApp.cpp b/inference/modelInference/sampleYOLOV7MultiInput/common/src/AclLiteApp.cpp index ef07f9614..a9eab852b 100755 --- a/inference/modelInference/sampleYOLOV7MultiInput/common/src/AclLiteApp.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/common/src/AclLiteApp.cpp @@ -256,6 +256,29 @@ AclLiteError SendMessage(int dest, int msgId, shared_ptr data) return app.SendMessage(dest, msgId, data); } +AclLiteError PromiseSendMessage(int dest, int msgId, sharead_prt data) +{ + AclLiteError ret; + while (1) + { + ret = SendMessage(dest, msgId, data); + if (ret == ACLLITE_ERROR_ENQUEUE) + { + usleep(kSleepTime); + continue; + } + else if (ret == ACLLITE_OK) + { + return ret; + } + else + { + ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); + return ret; + } + } +} + int GetAclLiteThreadIdByName(const string& threadName) { AclLiteApp& app = AclLiteApp::GetInstance(); diff --git a/inference/modelInference/sampleYOLOV7MultiInput/scripts/env.sh b/inference/modelInference/sampleYOLOV7MultiInput/scripts/env.sh index 7d98a6364..e8f9baa72 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/scripts/env.sh +++ b/inference/modelInference/sampleYOLOV7MultiInput/scripts/env.sh @@ -1,3 +1,4 @@ +#!/bin/bash . /usr/local/Ascend/ascend-toolkit/set_env.sh rm -rf plog export ASCEND_PROCESS_LOG_PATH=./plog diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp index 8ca6b343b..b2f351538 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp @@ -18,11 +18,11 @@ */ #include #include -#include "assert.h" +#include "cassert" #include "Params.h" -#include "convergence.h" #include "AclLiteApp.h" +#include "convergence.h" #include "AclLiteResource.h" @@ -217,7 +217,6 @@ std::shared_ptr ConvergenceThread::CropAndPaste(int msgNum) { image.data = SHARED_PTR_DVPP_BUF(vdecOutBufferDev); new_message->decodedImg.push_back(image); - assert(target_queue[0]->convergenceNum == frames.size()); new_message->convergenceNum = frames.size(); cv::Mat hcat; @@ -253,9 +252,6 @@ std::shared_ptr ConvergenceThread::CropAndPaste(int msgNum) { return new_message; } - - - AclLiteError ConvergenceThread::MsgProcess(shared_ptr& detectDataMsg) { if(detectDataMsg->decodedImg.empty()) return ACLLITE_OK; @@ -283,17 +279,6 @@ AclLiteError ConvergenceThread::MsgProcess(shared_ptr& detectData AclLiteError ConvergenceThread::MsgSend(shared_ptr detectDataMsg) { - while (1) { - AclLiteError ret = SendMessage(detectDataMsg->detectPreThreadId, MSG_PREPROC_DETECTDATA, detectDataMsg); - if (ret == ACLLITE_ERROR_ENQUEUE) { - usleep(kSleepTime); - continue; - } else if(ret == ACLLITE_OK) { - break; - } else { - ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); - return ret; - } - } - return ACLLITE_OK; + AclLiteError ret = PromiseSendMessage(detectDataMsg->detectPreThreadId, MSG_PREPROC_DETECTDATA, detectDataMsg); + return ret; } diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.cpp index 5155e5fdb..330d2e339 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.cpp @@ -329,52 +329,39 @@ AclLiteError DataInputThread::MsgRead(shared_ptr &detectDataMsg) AclLiteError DataInputThread::MsgSend(shared_ptr &detectDataMsg) { AclLiteError ret; - if (detectDataMsg->isLastFrame == false) { - while (1) { - if (convergence_) { - assert(!detectDataMsg->decodedImg.empty()); - ret = SendMessage(detectDataMsg->convergenceThreadId, MSG_CONVERGENCE, - detectDataMsg); - } else { - ret = SendMessage(detectDataMsg->detectPreThreadId, - MSG_PREPROC_DETECTDATA, detectDataMsg); + if (detectDataMsg->isLastFrame == false) + { + if (convergence_) + { + ret = PromiseSendMessage(detectDataMsg->convergenceThreadId, MSG_CONVERGENCE, detectDataMsg); } - if (ret == ACLLITE_ERROR_ENQUEUE) { - usleep(kSleepTime); - continue; - } else if (ret == ACLLITE_OK) { - break; - } else { - ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); - return ret; + else + { + ret = PromiseSendMessage(detectDataMsg->detectPreThreadId, MSG_PREPROC_DETECTDATA, detectDataMsg); } - } - - ret = SendMessage(selfThreadId_, MSG_READ_FRAME, nullptr); - if (ret != ACLLITE_OK) { - ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); - return ret; - } - } else { - for (int i = 0; i < postThreadNum_; i++) { - while (1) { - if (convergence_) { - ret = SendMessage(detectDataMsg->convergenceThreadId, - MSG_CONVERGENCE, detectDataMsg); - } else { - ret = SendMessage(detectDataMsg->detectPreThreadId, - MSG_PREPROC_DETECTDATA, detectDataMsg); - } - if (ret == ACLLITE_ERROR_ENQUEUE) { - usleep(kSleepTime); - continue; - } else if (ret == ACLLITE_OK) { - break; - } else { - ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); - return ret; - } + if (ret != ACLLITE_OK) + return ret; + ret = SendMessage(selfThreadId_, MSG_READ_FRAME, nullptr); + if (ret != ACLLITE_OK) + { + ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); + return ret; + } + } + else + { + for (int i = 0; i < postThreadNum_; i++) + { + if (convergence_) + { + ret = PromiseSendMessage(detectDataMsg->convergenceThreadId, MSG_CONVERGENCE, detectDataMsg); } + else + { + ret = PromiseSendMessage(detectDataMsg->detectPreThreadId, MSG_PREPROC_DETECTDATA, detectDataMsg); + } + if (ret != ACLLITE_OK) + return ret; } } return ACLLITE_OK; diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/detectInference/detectInference.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/detectInference/detectInference.cpp index dea1e0f3e..4c5d865ca 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/detectInference/detectInference.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/detectInference/detectInference.cpp @@ -75,20 +75,8 @@ AclLiteError DetectInferenceThread::ModelExecute(shared_ptr detec AclLiteError DetectInferenceThread::MsgSend(shared_ptr detectDataMsg) { - while (1) { - AclLiteError ret = SendMessage(detectDataMsg->detectPostThreadId, MSG_POSTPROC_DETECTDATA, detectDataMsg); - if (ret == ACLLITE_ERROR_ENQUEUE) { - usleep(kSleepTime); - continue; - } else if(ret == ACLLITE_OK) { - break; - } else { - ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); - return ret; - } - } - - return ACLLITE_OK; + AclLiteError ret = PromiseSendMessage(detectDataMsg->detectPostThreadId, MSG_POSTPROC_DETECTDATA, detectDataMsg); + return ret; } AclLiteError DetectInferenceThread::Process(int msgId, shared_ptr data) diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/detectPostprocess/detectPostprocess.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/detectPostprocess/detectPostprocess.cpp index 657a63490..1c2d7e1fd 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/detectPostprocess/detectPostprocess.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/detectPostprocess/detectPostprocess.cpp @@ -247,50 +247,27 @@ AclLiteError DetectPostprocessThread::InferOutputProcess(shared_ptr detectDataMsg) { - if (!sendLastBatch_) { - while (1) { - AclLiteError ret = SendMessage(detectDataMsg->dataOutputThreadId, MSG_OUTPUT_FRAME, detectDataMsg); - if (ret == ACLLITE_ERROR_ENQUEUE) { - usleep(kSleepTime); - continue; - } else if(ret == ACLLITE_OK) { - break; - } else { - ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); - return ret; - } - } + AclLiteError ret; + if (!sendLastBatch_) + { + ret = PromiseSendMessage(detectDataMsg->dataOutputThreadId, MSG_OUTPUT_FRAME, detectDataMsg); + if (ret != ACLLITE_OK) + return ret; } - if (detectDataMsg->isLastFrame && sendLastBatch_) { - while (1) { - AclLiteError ret = SendMessage(detectDataMsg->dataOutputThreadId, MSG_ENCODE_FINISH, detectDataMsg); - if (ret == ACLLITE_ERROR_ENQUEUE) { - usleep(kSleepTime); - continue; - } else if(ret == ACLLITE_OK) { - break; - } else { - ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); - return ret; - } - } + if (detectDataMsg->isLastFrame && sendLastBatch_) + { + ret = PromiseSendMessage(detectDataMsg->dataOutputThreadId, MSG_ENCODE_FINISH, detectDataMsg); + if (ret != ACLLITE_OK) + return ret; } - if (detectDataMsg->isLastFrame && !sendLastBatch_) { - while (1) { - AclLiteError ret = SendMessage(detectDataMsg->dataOutputThreadId, MSG_ENCODE_FINISH, detectDataMsg); - if (ret == ACLLITE_ERROR_ENQUEUE) { - usleep(kSleepTime); - continue; - } else if(ret == ACLLITE_OK) { - break; - } else { - ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); - return ret; - } - } + if (detectDataMsg->isLastFrame && !sendLastBatch_) + { + ret = PromiseSendMessage(detectDataMsg->dataOutputThreadId, MSG_ENCODE_FINISH, detectDataMsg); + if (ret != ACLLITE_OK) + return ret; sendLastBatch_ = true; } diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/detectPreprocess/detectPreprocess.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/detectPreprocess/detectPreprocess.cpp index 6c7c4cad7..5562dbacd 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/detectPreprocess/detectPreprocess.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/detectPreprocess/detectPreprocess.cpp @@ -109,17 +109,6 @@ AclLiteError DetectPreprocessThread::MsgProcess(shared_ptr detect AclLiteError DetectPreprocessThread::MsgSend(shared_ptr detectDataMsg) { - while (1) { - AclLiteError ret = SendMessage(detectDataMsg->detectInferThreadId, MSG_DO_DETECT_INFER, detectDataMsg); - if (ret == ACLLITE_ERROR_ENQUEUE) { - usleep(kSleepTime); - continue; - } else if(ret == ACLLITE_OK) { - break; - } else { - ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); - return ret; - } - } - return ACLLITE_OK; + AclLiteError ret = PromiseSendMessage(detectDataMsg->detectInferThreadId, MSG_DO_DETECT_INFER, detectDataMsg); + return ret; } -- Gitee From 0436aa8c8d7c584b6fbff8bc5bc668ffc195e764 Mon Sep 17 00:00:00 2001 From: xuxiaolong Date: Sat, 14 Sep 2024 13:49:10 +0800 Subject: [PATCH 7/9] clean code --- .../common/src/AclLiteApp.cpp | 3 ++- .../sampleYOLOV7MultiInput/inc/Params.h | 2 ++ .../src/convergence/convergence.cpp | 3 --- .../src/dataInput/dataInput.cpp | 1 - .../src/dataOutput/dataOutput.cpp | 21 ++++--------------- .../src/detectInference/detectInference.cpp | 4 ---- .../detectPostprocess/detectPostprocess.cpp | 1 - .../src/detectPreprocess/detectPreprocess.cpp | 4 ---- 8 files changed, 8 insertions(+), 31 deletions(-) diff --git a/inference/modelInference/sampleYOLOV7MultiInput/common/src/AclLiteApp.cpp b/inference/modelInference/sampleYOLOV7MultiInput/common/src/AclLiteApp.cpp index a9eab852b..4cb37e075 100755 --- a/inference/modelInference/sampleYOLOV7MultiInput/common/src/AclLiteApp.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/common/src/AclLiteApp.cpp @@ -10,6 +10,7 @@ #include "acl/acl.h" #include "AclLiteApp.h" #include "AclLiteThreadMgr.h" +#include "Params.h" using namespace std; namespace { @@ -256,7 +257,7 @@ AclLiteError SendMessage(int dest, int msgId, shared_ptr data) return app.SendMessage(dest, msgId, data); } -AclLiteError PromiseSendMessage(int dest, int msgId, sharead_prt data) +AclLiteError PromiseSendMessage(int dest, int msgId, shared_ptr data) { AclLiteError ret; while (1) diff --git a/inference/modelInference/sampleYOLOV7MultiInput/inc/Params.h b/inference/modelInference/sampleYOLOV7MultiInput/inc/Params.h index a87b6d098..90557cc0c 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/inc/Params.h +++ b/inference/modelInference/sampleYOLOV7MultiInput/inc/Params.h @@ -53,6 +53,8 @@ const std::string kPostName = "post"; const std::string kDataOutputName = "dataOutput"; const std::string kRtspDisplayName = "rtspDisplay"; const std::string kConvergenceName = "convergence"; + +const uint32_t kSleepTime = 500; } struct DetectDataMsg { diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp index b2f351538..14485369b 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp @@ -33,9 +33,6 @@ using namespace std; -namespace { -const uint32_t kSleepTime = 500; -} uint32_t AlignmentHelper(uint32_t origSize, uint32_t alignment) { diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.cpp index 330d2e339..8aaece05c 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/dataInput/dataInput.cpp @@ -24,7 +24,6 @@ namespace{ const uint32_t kYuvMultiplier = 3; const uint32_t kYuvDivisor = 2; - const uint32_t kSleepTime = 500; const uint32_t kOneSec = 1000000; const uint32_t kOneMSec = 1000; } diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/dataOutput/dataOutput.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/dataOutput/dataOutput.cpp index 07d07c88e..f0e837c60 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/dataOutput/dataOutput.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/dataOutput/dataOutput.cpp @@ -27,7 +27,6 @@ using namespace std; namespace { const uint32_t kOutputWidth = 540; const uint32_t kOutputHeigth = 270; -const uint32_t kSleepTime = 500; uint32_t kWaitTime = 1000; const uint32_t kOneSec = 1000000; const uint32_t kOneMSec = 1000; @@ -254,22 +253,10 @@ AclLiteError DataOutputThread::SendCVImshow(shared_ptr &detectDat AclLiteError DataOutputThread::DisplayMsgSend(shared_ptr detectDataMsg) { AclLiteError ret; - while (1) { - if (outputDataType_ == "rtsp") { - ret = SendMessage(detectDataMsg->rtspDisplayThreadId, MSG_RTSP_DISPLAY, detectDataMsg); - } - if (ret == ACLLITE_ERROR_ENQUEUE) { - usleep(kSleepTime); - continue; - } else if (ret == ACLLITE_OK) { - break; - } else { - ACLLITE_LOG_ERROR("Send rtsp display message failed, error %d", ret); - return ret; - } - } - - return ACLLITE_OK; + if(outputDataType_ != "rtsp") return ACLLITE_OK; + ret = PromiseSendMessage(detectDataMsg->rtspDisplayThreadId, MSG_RTSP_DISPLAY, detectDataMsg); + if(ret!=ACLLITE_OK) ACLLITE_LOG_ERROR("Send rtsp display message failed, error %d", ret); + return ret; } AclLiteError DataOutputThread::SendImageToRtsp(shared_ptr &detectDataMsg) diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/detectInference/detectInference.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/detectInference/detectInference.cpp index 4c5d865ca..7195d7584 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/detectInference/detectInference.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/detectInference/detectInference.cpp @@ -28,10 +28,6 @@ using namespace std; -namespace { -const uint32_t kSleepTime = 500; -} - DetectInferenceThread::DetectInferenceThread(string modelPath) :model_(modelPath), isReleased(false) { diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/detectPostprocess/detectPostprocess.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/detectPostprocess/detectPostprocess.cpp index 1c2d7e1fd..f213c0592 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/detectPostprocess/detectPostprocess.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/detectPostprocess/detectPostprocess.cpp @@ -27,7 +27,6 @@ using namespace std; namespace { - const uint32_t kSleepTime = 500; const double kFountScale = 0.5; const cv::Scalar kFountColor(0, 0, 255); const uint32_t kLabelOffset = 11; diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/detectPreprocess/detectPreprocess.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/detectPreprocess/detectPreprocess.cpp index 5562dbacd..c14e93849 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/detectPreprocess/detectPreprocess.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/detectPreprocess/detectPreprocess.cpp @@ -25,10 +25,6 @@ using namespace std; -namespace { -const uint32_t kSleepTime = 500; -} - DetectPreprocessThread::DetectPreprocessThread(uint32_t modelWidth, uint32_t modelHeight, uint32_t batch) :modelWidth_(modelWidth), modelHeight_(modelHeight), isReleased(false), batch_(batch) -- Gitee From 0abaf209e29917bb414b09ff98cf6f7372884396 Mon Sep 17 00:00:00 2001 From: xuxiaolong Date: Sat, 14 Sep 2024 15:50:00 +0800 Subject: [PATCH 8/9] cleancode --- .../src/convergence/convergence.cpp | 259 +++++++++--------- .../src/dataOutput/dataOutput.cpp | 30 +- .../src/detectPreprocess/detectPreprocess.cpp | 2 +- 3 files changed, 152 insertions(+), 139 deletions(-) diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp index 14485369b..1ae3b508c 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp @@ -16,15 +16,14 @@ * File sample_process.cpp * Description: handle acl resource */ +#include "cassert" #include #include -#include "cassert" -#include "Params.h" #include "AclLiteApp.h" +#include "Params.h" #include "convergence.h" - #include "AclLiteResource.h" #include "AclLiteUtils.h" @@ -33,25 +32,27 @@ using namespace std; - uint32_t AlignmentHelper(uint32_t origSize, uint32_t alignment) { - if (alignment == 0) { + if (alignment == 0) + { return 0; } uint32_t alignmentH = alignment - 1; return (origSize + alignmentH) / alignment * alignment; } -ConvergenceThread::ConvergenceThread(aclrtRunMode runMode, uint32_t channelNum ,uint32_t modelWidth, uint32_t modelHeight, - uint32_t batch) - :runMode_(runMode), channelNum_(channelNum), modelWidth_(modelWidth), modelHeight_(modelHeight), isReleased(false), batch_(batch) +ConvergenceThread::ConvergenceThread(aclrtRunMode runMode, uint32_t channelNum, uint32_t modelWidth, + uint32_t modelHeight, uint32_t batch) + : runMode_(runMode), channelNum_(channelNum), modelWidth_(modelWidth), modelHeight_(modelHeight), isReleased(false), + batch_(batch) { } ConvergenceThread::~ConvergenceThread() { - if(!isReleased) { + if (!isReleased) + { dvpp_.DestroyResource(); } isReleased = true; @@ -61,7 +62,8 @@ AclLiteError ConvergenceThread::Init() { selfThreadId_ = SelfInstanceId(); AclLiteError aclRet = dvpp_.Init("DVPP_CHNMODE_VPC"); - if (aclRet) { + if (aclRet) + { ACLLITE_LOG_ERROR("Dvpp init failed, error %d", aclRet); return ACLLITE_ERROR; } @@ -69,23 +71,26 @@ AclLiteError ConvergenceThread::Init() return ACLLITE_OK; } -AclLiteError ConvergenceThread::Process(int msgId, shared_ptr data) { - shared_ptr ldata = static_pointer_cast(data); - switch (msgId) { +AclLiteError ConvergenceThread::Process(int msgId, shared_ptr data) +{ + shared_ptr ldata = static_pointer_cast(data); + switch (msgId) + { case MSG_CONVERGENCE: - MsgProcess(ldata); - MsgSend(ldata); - break; + MsgProcess(ldata); + MsgSend(ldata); + break; default: - ACLLITE_LOG_INFO("Convergence thread ignore msg %d", msgId); - break; - } + ACLLITE_LOG_INFO("Convergence thread ignore msg %d", msgId); + break; + } - return ACLLITE_OK; + return ACLLITE_OK; } // converge pictures and clear target convQueue_ buffer -std::shared_ptr ConvergenceThread::CropAndPaste(int msgNum) { +std::shared_ptr ConvergenceThread::CropAndPaste(int msgNum) +{ auto target_queue = convQueue_[msgNum]; int32_t deviceId = target_queue.front()->deviceId; aclrtSetDevice(deviceId); @@ -102,7 +107,7 @@ std::shared_ptr ConvergenceThread::CropAndPaste(int msgNum) { int outputWidth = inputWidth * 2; int outputHeight = inputHeight * 2; - acldvppChannelDesc* dvppChannelDesc_ = acldvppCreateChannelDesc(); + acldvppChannelDesc *dvppChannelDesc_ = acldvppCreateChannelDesc(); acldvppCreateChannel(dvppChannelDesc_); uint32_t widthAlignment = 16; uint32_t heightAlignment = 1; @@ -114,12 +119,12 @@ std::shared_ptr ConvergenceThread::CropAndPaste(int msgNum) { int dvppOutHeight = outputHeight; int dvppOutWidthStride = AlignmentHelper(dvppOutWidth, widthAlignment); int dvppOutHeightStride = AlignmentHelper(dvppOutHeight, heightAlignment); - uint32_t vpcOutBufferSize_ = - dvppOutWidthStride * dvppOutHeightStride * sizeAlignment / sizeNum; + uint32_t vpcOutBufferSize_ = dvppOutWidthStride * dvppOutHeightStride * sizeAlignment / sizeNum; aclError aclRet = acldvppMalloc(&vpcOutBufferDev_, vpcOutBufferSize_); acldvppPicDesc *vpcOutputDesc_ = acldvppCreatePicDesc(); - INFO_LOG("acldvppCreatePicDesc w=%d/%d,h=%d/%d,vpcOutBufferSize=%u",dvppOutWidth,dvppOutWidthStride,dvppOutHeight,dvppOutHeightStride,vpcOutBufferSize_); + INFO_LOG("acldvppCreatePicDesc w=%d/%d,h=%d/%d,vpcOutBufferSize=%u", dvppOutWidth, dvppOutWidthStride, + dvppOutHeight, dvppOutHeightStride, vpcOutBufferSize_); acldvppSetPicDescData(vpcOutputDesc_, vpcOutBufferDev_); acldvppSetPicDescFormat(vpcOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); acldvppSetPicDescWidth(vpcOutputDesc_, dvppOutWidth); @@ -133,99 +138,95 @@ std::shared_ptr ConvergenceThread::CropAndPaste(int msgNum) { new_message->decodedImg.clear(); new_message->frame.clear(); - for (int i = 0; i < target_queue[0]->decodedImg.size(); i++) { - std::vector frames; - for (int j = 0; j < target_queue.size(); j++) { - void *inBufferDev = nullptr; - acldvppMalloc(&inBufferDev, singleImageBufferSize); - if (runMode_ == ACL_HOST) { - aclrtMemcpy(inBufferDev, singleImageBufferSize, - target_queue[j]->decodedImg[i].data.get(), - singleImageBufferSize, ACL_MEMCPY_HOST_TO_DEVICE); - } else { - aclrtMemcpy(inBufferDev, singleImageBufferSize, - target_queue[j]->decodedImg[i].data.get(), - singleImageBufferSize, ACL_MEMCPY_DEVICE_TO_DEVICE); + for (int i = 0; i < target_queue[0]->decodedImg.size(); i++) + { + std::vector frames; + for (int j = 0; j < target_queue.size(); j++) + { + void *inBufferDev = nullptr; + acldvppMalloc(&inBufferDev, singleImageBufferSize); + if (runMode_ == ACL_HOST) + { + aclrtMemcpy(inBufferDev, singleImageBufferSize, target_queue[j]->decodedImg[i].data.get(), + singleImageBufferSize, ACL_MEMCPY_HOST_TO_DEVICE); + } + else + { + aclrtMemcpy(inBufferDev, singleImageBufferSize, target_queue[j]->decodedImg[i].data.get(), + singleImageBufferSize, ACL_MEMCPY_DEVICE_TO_DEVICE); + } + + uint32_t oddNum = 1; + uint32_t cropSizeWidth = inputWidth; + uint32_t cropSizeHeight = inputHeight; + + uint32_t cropLeftOffset = 0; // must even + uint32_t cropRightOffset = cropLeftOffset + cropSizeWidth - oddNum; // must odd 0+1024-1 + uint32_t cropTopOffset = 0; // must even + uint32_t cropBottomOffset = cropTopOffset + cropSizeHeight - oddNum; // must odd 0+1068-1 + acldvppRoiConfig *cropArea_ = + acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset); + // 通过for循环改 + uint32_t pasteLeftOffset = 0 + (i % 2) * inputWidth; // must even + uint32_t pasteRightOffset = pasteLeftOffset + cropSizeWidth - oddNum; // must odd + uint32_t pasteTopOffset = 0 + (i / 2) * inputHeight; // must even + uint32_t pasteBottomOffset = pasteTopOffset + cropSizeHeight - oddNum; // must odd + acldvppRoiConfig *pasteArea_ = + acldvppCreateRoiConfig(pasteLeftOffset, pasteRightOffset, pasteTopOffset, pasteBottomOffset); + + uint32_t jpegOutWidthStride = AlignmentHelper(inputWidth, widthAlignment); + uint32_t jpegOutHeightStride = AlignmentHelper(inputHeight, heightAlignment); + uint32_t jpegOutBufferSize = jpegOutWidthStride * jpegOutHeightStride * sizeAlignment / sizeNum; + acldvppPicDesc *vpcInputDesc_ = acldvppCreatePicDesc(); + acldvppSetPicDescData(vpcInputDesc_, + inBufferDev); // JpegD -> vpcCropAndPaste + acldvppSetPicDescFormat(vpcInputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); + acldvppSetPicDescWidth(vpcInputDesc_, inputWidth); + acldvppSetPicDescHeight(vpcInputDesc_, inputHeight); + acldvppSetPicDescWidthStride(vpcInputDesc_, jpegOutWidthStride); + acldvppSetPicDescHeightStride(vpcInputDesc_, jpegOutHeightStride); + acldvppSetPicDescSize(vpcInputDesc_, jpegOutBufferSize); + + // crop and patse pic + acldvppVpcCropAndPasteAsync(dvppChannelDesc_, vpcInputDesc_, vpcOutputDesc_, cropArea_, pasteArea_, stream); + aclrtSynchronizeStream(stream); + + (void)acldvppDestroyRoiConfig(cropArea_); + cropArea_ = nullptr; + (void)acldvppDestroyRoiConfig(pasteArea_); + pasteArea_ = nullptr; + (void)acldvppDestroyPicDesc(vpcInputDesc_); + vpcInputDesc_ = nullptr; + frames.push_back(target_queue[j]->frame[i]); } - uint32_t oddNum = 1; - uint32_t cropSizeWidth = inputWidth; - uint32_t cropSizeHeight = inputHeight; - - uint32_t cropLeftOffset = 0; // must even - uint32_t cropRightOffset = - cropLeftOffset + cropSizeWidth - oddNum; // must odd 0+1024-1 - uint32_t cropTopOffset = 0; // must even - uint32_t cropBottomOffset = - cropTopOffset + cropSizeHeight - oddNum; // must odd 0+1068-1 - acldvppRoiConfig *cropArea_ = acldvppCreateRoiConfig( - cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset); - // 通过for循环改 - uint32_t pasteLeftOffset = 0 + (i % 2) * inputWidth; // must even - uint32_t pasteRightOffset = - pasteLeftOffset + cropSizeWidth - oddNum; // must odd - uint32_t pasteTopOffset = 0 + (i / 2) * inputHeight; // must even - uint32_t pasteBottomOffset = - pasteTopOffset + cropSizeHeight - oddNum; // must odd - acldvppRoiConfig *pasteArea_ = - acldvppCreateRoiConfig(pasteLeftOffset, pasteRightOffset, - pasteTopOffset, pasteBottomOffset); - - uint32_t jpegOutWidthStride = - AlignmentHelper(inputWidth, widthAlignment); - uint32_t jpegOutHeightStride = - AlignmentHelper(inputHeight, heightAlignment); - uint32_t jpegOutBufferSize = - jpegOutWidthStride * jpegOutHeightStride * sizeAlignment / sizeNum; - acldvppPicDesc *vpcInputDesc_ = acldvppCreatePicDesc(); - acldvppSetPicDescData(vpcInputDesc_, - inBufferDev); // JpegD -> vpcCropAndPaste - acldvppSetPicDescFormat(vpcInputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); - acldvppSetPicDescWidth(vpcInputDesc_, inputWidth); - acldvppSetPicDescHeight(vpcInputDesc_, inputHeight); - acldvppSetPicDescWidthStride(vpcInputDesc_, jpegOutWidthStride); - acldvppSetPicDescHeightStride(vpcInputDesc_, jpegOutHeightStride); - acldvppSetPicDescSize(vpcInputDesc_, jpegOutBufferSize); - - // crop and patse pic - acldvppVpcCropAndPasteAsync(dvppChannelDesc_, vpcInputDesc_, - vpcOutputDesc_, cropArea_, pasteArea_, - stream); - aclrtSynchronizeStream(stream); - - (void)acldvppDestroyRoiConfig(cropArea_); - cropArea_ = nullptr; - (void)acldvppDestroyRoiConfig(pasteArea_); - pasteArea_ = nullptr; - (void)acldvppDestroyPicDesc(vpcInputDesc_); - vpcInputDesc_ = nullptr; - frames.push_back(target_queue[j]->frame[i]); - } - - ImageData image; - image.format = acldvppGetPicDescFormat(vpcOutputDesc_); - image.width = acldvppGetPicDescWidth(vpcOutputDesc_); - image.height = acldvppGetPicDescHeight(vpcOutputDesc_); - image.alignWidth = acldvppGetPicDescWidthStride(vpcOutputDesc_); - image.alignHeight = acldvppGetPicDescHeightStride(vpcOutputDesc_); - image.size = acldvppGetPicDescSize(vpcOutputDesc_); - - void *vdecOutBufferDev = acldvppGetPicDescData(vpcOutputDesc_); - image.data = SHARED_PTR_DVPP_BUF(vdecOutBufferDev); - new_message->decodedImg.push_back(image); - - new_message->convergenceNum = frames.size(); - - cv::Mat hcat; - - cv::hconcat(frames[0], frames[1], hcat); - cv::Mat vcat; - cv::hconcat(frames[2], frames[3], vcat); - cv::Mat res; - cv::vconcat(hcat, vcat, res); - new_message->frame.push_back(res); - - frames.clear(); + ImageData image; + image.format = acldvppGetPicDescFormat(vpcOutputDesc_); + image.width = acldvppGetPicDescWidth(vpcOutputDesc_); + image.height = acldvppGetPicDescHeight(vpcOutputDesc_); + image.alignWidth = acldvppGetPicDescWidthStride(vpcOutputDesc_); + image.alignHeight = acldvppGetPicDescHeightStride(vpcOutputDesc_); + image.size = acldvppGetPicDescSize(vpcOutputDesc_); + + void *vdecOutBufferDev = acldvppGetPicDescData(vpcOutputDesc_); + image.data = SHARED_PTR_DVPP_BUF(vdecOutBufferDev); + new_message->decodedImg.push_back(image); + + new_message->convergenceNum = frames.size(); + + cv::Mat hcat; + int firstChannelIdx = 0; + int secondChannelIdx = 1; + int thirdChannelIdx = 2; + int ForthChannelIdx = 3; + cv::hconcat(frames[firstChannelIdx], frames[secondChannelIdx], hcat); + cv::Mat vcat; + cv::hconcat(frames[thirdChannelIdx], frames[ForthChannelIdx], vcat); + cv::Mat res; + cv::vconcat(hcat, vcat, res); + new_message->frame.push_back(res); + + frames.clear(); } // clear @@ -237,9 +238,10 @@ std::shared_ptr ConvergenceThread::CropAndPaste(int msgNum) { (void)acldvppDestroyChannelDesc(dvppChannelDesc_); dvppChannelDesc_ = nullptr; - if (vpcOutBufferDev_ != nullptr) { - (void)acldvppFree(vpcOutBufferDev_); - vpcOutBufferDev_ = nullptr; + if (vpcOutBufferDev_ != nullptr) + { + (void)acldvppFree(vpcOutBufferDev_); + vpcOutBufferDev_ = nullptr; } aclrtDestroyStream(stream); stream = nullptr; @@ -249,26 +251,29 @@ std::shared_ptr ConvergenceThread::CropAndPaste(int msgNum) { return new_message; } -AclLiteError ConvergenceThread::MsgProcess(shared_ptr& detectDataMsg) +AclLiteError ConvergenceThread::MsgProcess(shared_ptr &detectDataMsg) { - if(detectDataMsg->decodedImg.empty()) return ACLLITE_OK; + if (detectDataMsg->decodedImg.empty()) + return ACLLITE_OK; detectDataMsg->convergenceNum = channelNum_; int msgNum = detectDataMsg->msgNum; int channelId = detectDataMsg->channelId; - if(convQueue_.count(msgNum)) { + if (convQueue_.count(msgNum)) + { convQueue_[msgNum].push_back(detectDataMsg); - } else { + } + else + { convQueue_[msgNum] = std::vector>{detectDataMsg}; } - if(convQueue_[msgNum].size() a, std::shared_ptr b) { - return a->channelId < b->channelId; - }); + [](std::shared_ptr a, std::shared_ptr b) { return a->channelId < b->channelId; }); detectDataMsg = CropAndPaste(msgNum); return ACLLITE_OK; diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/dataOutput/dataOutput.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/dataOutput/dataOutput.cpp index f0e837c60..821803377 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/dataOutput/dataOutput.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/dataOutput/dataOutput.cpp @@ -122,22 +122,30 @@ AclLiteError DataOutputThread::RecordQueue(shared_ptr detectDataM AclLiteError DataOutputThread::DataProcess() { - if (convergence_) { - for (int i = 0; i < postNum_; i++) { - if (postQueue_[i].empty()) continue; - shared_ptr detectDataMsg = postQueue_[i].front(); - ProcessOutput(detectDataMsg); - postQueue_[i].pop(); + if (convergence_) + { + for (int i = 0; i < postNum_; i++) + { + if (postQueue_[i].empty()) + { + continue; + } + shared_ptr detectDataMsg = postQueue_[i].front(); + ProcessOutput(detectDataMsg); + postQueue_[i].pop(); + } + return ACLLITE_OK; } - return ACLLITE_OK; - } int flag = 0; - for (int i = 0; i < postNum_; i++) { + for (int i = 0; i < postNum_; i++) + { if (!postQueue_[i].empty()) flag++; } - if (flag == postNum_) { - for (int i = 0; i < postNum_; i++) { + if (flag == postNum_) + { + for (int i = 0; i < postNum_; i++) + { shared_ptr detectDataMsg = postQueue_[i].front(); ProcessOutput(detectDataMsg); postQueue_[i].pop(); diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/detectPreprocess/detectPreprocess.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/detectPreprocess/detectPreprocess.cpp index c14e93849..b45d7f5e5 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/detectPreprocess/detectPreprocess.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/detectPreprocess/detectPreprocess.cpp @@ -18,7 +18,7 @@ */ #include #include -#include +#include #include "Params.h" #include "detectPreprocess.h" #include "AclLiteApp.h" -- Gitee From f187a0e0838ed9b3974e414227bc4deb7b6c228a Mon Sep 17 00:00:00 2001 From: xuxiaolong Date: Wed, 18 Sep 2024 11:00:16 +0800 Subject: [PATCH 9/9] cleancode --- .../sampleYOLOV7MultiInput/src/convergence/convergence.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp b/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp index 1ae3b508c..c6543a546 100644 --- a/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp +++ b/inference/modelInference/sampleYOLOV7MultiInput/src/convergence/convergence.cpp @@ -16,7 +16,6 @@ * File sample_process.cpp * Description: handle acl resource */ -#include "cassert" #include #include -- Gitee