From 2e11929f553bf52ffc3516a8b4ef31503c52f3db Mon Sep 17 00:00:00 2001 From: yy0131 <1595992831@qq.com> Date: Thu, 29 Sep 2022 09:25:34 +0800 Subject: [PATCH 01/14] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../YOLOV3_carColor_sample/inc/AclLiteError.h | 15 +- .../inc/AclLiteImageProc.h | 22 +- .../YOLOV3_carColor_sample/inc/AclLiteModel.h | 51 ++-- .../inc/AclLiteResource.h | 25 +- .../YOLOV3_carColor_sample/inc/AclLiteType.h | 15 +- .../YOLOV3_carColor_sample/inc/AclLiteUtils.h | 17 +- .../inc/CropAndPasteHelper.h | 47 ++-- .../YOLOV3_carColor_sample/inc/JpegDHelper.h | 23 +- .../YOLOV3_carColor_sample/inc/ResizeHelper.h | 33 +-- .../inc/color_classify.h | 39 +-- .../inc/object_detect.h | 33 +-- .../YOLOV3_carColor_sample/inc/params.h | 31 ++- .../src/AclLiteImageProc.cpp | 52 ++-- .../src/AclLiteModel.cpp | 168 ++++++------- .../src/AclLiteResource.cpp | 52 ++-- .../src/AclLiteUtils.cpp | 22 +- .../src/CropAndPasteHelper.cpp | 237 +++++++++--------- .../src/JpegDHelper.cpp | 49 ++-- .../src/ResizeHelper.cpp | 117 +++++---- .../src/color_classify.cpp | 149 ++++++----- .../YOLOV3_carColor_sample/src/main.cpp | 18 +- .../src/object_detect.cpp | 90 ++++--- .../inc/dvpp_process.h | 34 +-- .../inc/model_process.h | 36 +-- .../inc/object_detect.h | 61 ++--- .../YOLOV3_coco_detection_VENC/inc/utils.h | 16 +- .../src/dvpp_process.cpp | 161 ++++++------ .../YOLOV3_coco_detection_VENC/src/main.cpp | 46 ++-- .../src/model_process.cpp | 160 ++++++------ .../src/object_detect.cpp | 152 ++++++----- .../YOLOV3_coco_detection_VENC/src/utils.cpp | 26 +- .../inc/dvpp_jpegd.h | 28 ++- .../inc/dvpp_process.h | 16 +- .../inc/dvpp_resize.h | 32 +-- .../inc/model_process.h | 46 ++-- .../inc/object_detect.h | 40 +-- .../inc/utils.h | 12 +- .../src/dvpp_jpegd.cpp | 58 ++--- .../src/dvpp_process.cpp | 36 ++- .../src/dvpp_resize.cpp | 119 +++++---- .../src/main.cpp | 27 +- .../src/model_process.cpp | 186 +++++++------- .../src/object_detect.cpp | 152 ++++++----- .../src/utils.cpp | 27 +- .../inc/dvpp_jpegd.h | 26 +- .../inc/dvpp_process.h | 16 +- .../inc/dvpp_resize.h | 30 +-- .../inc/model_process.h | 34 +-- .../inc/object_detect.h | 40 +-- .../YOLOV3_coco_detection_picture/inc/utils.h | 13 +- .../src/dvpp_jpegd.cpp | 57 ++--- .../src/dvpp_process.cpp | 36 ++- .../src/dvpp_resize.cpp | 121 +++++---- .../src/main.cpp | 37 ++- .../src/model_process.cpp | 151 ++++++----- .../src/object_detect.cpp | 114 +++++---- .../src/utils.cpp | 15 +- .../inc/dvpp_process.h | 63 +++-- .../inc/model_process.h | 46 ++-- .../inc/sample_process.h | 33 ++- .../inc/utils.h | 31 ++- .../src/dvpp_process.cpp | 236 +++++++++-------- .../src/main.cpp | 24 +- .../src/model_process.cpp | 184 +++++++------- .../src/sample_process.cpp | 116 ++++----- .../src/utils.cpp | 30 ++- .../inc/drawing.h | 23 +- .../inc/dvpp_cropandpaste.h | 38 +-- .../inc/dvpp_jpegd.h | 28 ++- .../inc/dvpp_process.h | 17 +- .../inc/freetype_helper.h | 22 +- .../inc/model_process.h | 38 +-- .../inc/object_detect.h | 38 +-- .../inc/utils.h | 12 +- .../src/drawing.cpp | 16 ++ .../src/dvpp_cropandpaste.cpp | 150 ++++++----- .../src/dvpp_jpegd.cpp | 57 ++--- .../src/dvpp_process.cpp | 36 ++- .../src/freetype_helper.cpp | 23 +- .../src/main.cpp | 37 ++- .../src/model_process.cpp | 144 ++++++----- .../src/object_detect.cpp | 112 ++++----- .../src/utils.cpp | 35 ++- .../inc/model_process.h | 32 +-- .../inc/object_detect.h | 56 +++-- .../inc/sample_process.h | 71 ------ .../YOLOV3_coco_detection_video/inc/utils.h | 12 +- .../YOLOV3_coco_detection_video/src/main.cpp | 35 ++- .../src/model_process.cpp | 145 +++++------ .../src/object_detect.cpp | 157 ++++++------ .../YOLOV3_coco_detection_video/src/utils.cpp | 16 +- .../inc/sample_process.h | 43 ++-- .../src/main.cpp | 28 ++- .../src/sample_process.cpp | 128 +++++----- .../inc/dvpp_process.h | 64 +++-- .../inc/model_process.h | 42 ++-- .../inc/post_process.h | 42 ++-- .../YOLOV4_coco_detection_picture/inc/utils.h | 26 +- .../src/dvpp_process.cpp | 193 +++++++------- .../src/main.cpp | 12 +- .../src/model_process.cpp | 137 +++++----- .../src/post_process.cpp | 39 +-- .../src/utils.cpp | 21 +- 103 files changed, 3228 insertions(+), 3126 deletions(-) delete mode 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/inc/sample_process.h diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteError.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteError.h index 254498708..3d385cc32 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteError.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteError.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 utils.h -* Description: handle file operations */ + +#ifndef YOLOV3_CARCOLOR_SAMPLE_INC_ACLLITEERROR_H +#define YOLOV3_CARCOLOR_SAMPLE_INC_ACLLITEERROR_H + #pragma once #include @@ -189,4 +190,6 @@ const int ACLLITE_ERROR_VENC_CREATE_FRAME_CONFIG = 708; const int ACLLITE_ERROR_VENC_SEND_FRAME = 709; -const int ACLLITE_ERROR_SUBSCRIBE_REPORT = 710; \ No newline at end of file +const int ACLLITE_ERROR_SUBSCRIBE_REPORT = 710; + +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteImageProc.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteImageProc.h index ed573e209..e608b0ed6 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteImageProc.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteImageProc.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,9 +12,11 @@ * 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 AclLiteImageProc.h */ + +#ifndef YOLOV3_CARCOLOR_SAMPLE_INC_ACLLITEIMAGEPROC_H +#define YOLOV3_CARCOLOR_SAMPLE_INC_ACLLITEIMAGEPROC_H + #pragma once #include #include "AclLiteUtils.h" @@ -46,8 +48,10 @@ public: void DestroyResource(); protected: - bool isReleased_; - aclrtStream stream_; - acldvppChannelDesc *dvppChannelDesc_; - int isInitOk_; -}; \ No newline at end of file + bool g_isReleased_; + aclrtStream g_stream_; + acldvppChannelDesc *g_dvppChannelDesc_; + int g_isInitOk_; +}; + +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteModel.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteModel.h index 62e6c2338..bd2a74b5a 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteModel.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteModel.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 model_process.h -* Description: handle model process */ + +#ifndef YOLOV3_CARCOLOR_SAMPLE_INC_ACLLITEMODEL_H +#define YOLOV3_CARCOLOR_SAMPLE_INC_ACLLITEMODEL_H + #pragma once #include #include "AclLiteUtils.h" @@ -29,9 +30,9 @@ public: AclLiteError Init(); void DestroyResource(); - AclLiteError CreateInput(void *input, uint32_t inputsize); - AclLiteError CreateInput(void *input1, uint32_t input1size, - void* input2, uint32_t input2size); + AclLiteError CreateInput(void *input, uint32_t inputSize); + AclLiteError CreateInput(void *input1, uint32_t input1Size, + void* input2, uint32_t input2Size); AclLiteError CreateInput(std::vector& inputData); AclLiteError Execute(std::vector& inferOutputs, void *data, uint32_t size); @@ -51,19 +52,21 @@ private: void DestroyOutput(); private: - bool loadFlag_; // model load flag - bool isReleased_; // model release flag - uint32_t modelId_; //modelid - size_t outputsNum_; - size_t modelMemSize_; - size_t modelWorkSize_; - size_t modelWeightSize_; - aclrtRunMode runMode_; - void *modelMemPtr_; - void *modelWorkPtr_; - void *modelWeightPtr_; - aclmdlDesc *modelDesc_; - aclmdlDataset *input_; //input dataset - aclmdlDataset *output_; //output dataset - std::string modelPath_; //model path -}; \ No newline at end of file + bool g_loadFlag_; // model load flag + bool g_isReleased_; // model release flag + uint32_t g_modelId_; //modelid + size_t g_outputsNum_; + size_t g_modelMemSize_; + size_t g_modelWorkSize_; + size_t g_modelWeightSize_; + aclrtRunMode g_runMode_; + void *g_modelMemPtr_; + void *g_modelWorkPtr_; + void *g_modelWeightPtr_; + aclmdlDesc *g_modelDesc_; + aclmdlDataset *g_input_; //input dataset + aclmdlDataset *g_output_; //output dataset + std::string g_modelPath_; //model path +}; + +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteResource.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteResource.h index 9e3386b36..50ca0b5b4 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteResource.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteResource.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 AclLiteResource.h -* Description: handle AclLiteResource operations */ + +#ifndef YOLOV3_CARCOLOR_SAMPLE_INC_ACLLITERESOURCE_H +#define YOLOV3_CARCOLOR_SAMPLE_INC_ACLLITERESOURCE_H + #pragma once #include @@ -32,10 +33,12 @@ public: void Release(); private: - bool isReleased_; - bool useDefaultCtx_; - int32_t deviceId_; - aclrtRunMode runMode_; - aclrtContext context_; - std::string aclConfig_; + bool g_isReleased_; + bool g_useDefaultCtx_; + int32_t g_deviceId_; + aclrtRunMode g_runMode_; + aclrtContext g_context_; + std::string g_aclConfig_; }; + +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteType.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteType.h index d69254f64..598fddf39 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteType.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteType.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 utils.h -* Description: handle file operations */ + +#ifndef YOLOV3_CARCOLOR_SAMPLE_INC_ACLLITETYPE_H +#define YOLOV3_CARCOLOR_SAMPLE_INC_ACLLITETYPE_H + #pragma once #include @@ -110,4 +111,6 @@ struct DataInfo { struct InferenceOutput { std::shared_ptr data = nullptr; uint32_t size; -}; \ No newline at end of file +}; + +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteUtils.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteUtils.h index f23d8b90a..9441c4202 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteUtils.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteUtils.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 utils.h -* Description: handle file operations */ + +#ifndef YOLOV3_CARCOLOR_SAMPLE_INC_ACLLITEUTILS_H +#define YOLOV3_CARCOLOR_SAMPLE_INC_ACLLITEUTILS_H + #pragma once #include @@ -208,7 +209,7 @@ void GetAllFiles(const std::string &pathList, * @return AclLiteError ACLLITE_OK: read success * others: read failed */ -AclLiteError ReadBinFile(const std::string& filename, +AclLiteError ReadBinFile(const std::string& fileName, void*& data, uint32_t& size); /** @@ -231,4 +232,6 @@ AclLiteError CopyImageToDevice(ImageData& destImage, ImageData& srcImage, * @return bool true: file path is exist * false: is not exist */ -bool IsPathExist(const std::string &path); \ No newline at end of file +bool IsPathExist(const std::string &path); + +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/CropAndPasteHelper.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/CropAndPasteHelper.h index 03dc90e50..644ddc977 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/CropAndPasteHelper.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/CropAndPasteHelper.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 AclLiteImageProc.h -* Description: handle dvpp process */ + +#ifndef YOLOV3_CARCOLOR_SAMPLE_INC_CROPANDPASTEHELPER_H +#define YOLOV3_CARCOLOR_SAMPLE_INC_CROPANDPASTEHELPER_H + #pragma once #include @@ -75,20 +76,22 @@ private: void DestroyCropAndPasteResource(); private: - aclrtStream stream_; - void *vpcOutBufferDev_; - acldvppRoiConfig *cropArea_; - acldvppRoiConfig *pasteArea_; - acldvppPicDesc *vpcInputDesc_; - acldvppPicDesc *vpcOutputDesc_; - acldvppChannelDesc *dvppChannelDesc_; - - uint32_t ltHorz_; - uint32_t rbHorz_; - uint32_t ltVert_; - uint32_t rbVert_; - uint32_t vpcOutBufferSize_; - uint32_t originalImageWidth_ = 0; - uint32_t originalImageHeight_ = 0; - Resolution size_; -}; \ No newline at end of file + aclrtStream g_stream_; + void *g_vpcOutBufferDev_; + acldvppRoiConfig *g_cropArea_; + acldvppRoiConfig *g_pasteArea_; + acldvppPicDesc *g_vpcInputDesc_; + acldvppPicDesc *g_vpcOutputDesc_; + acldvppChannelDesc *g_dvppChannelDesc_; + + uint32_t g_ltHorz_; + uint32_t g_rbHorz_; + uint32_t g_ltVert_; + uint32_t g_rbVert_; + uint32_t g_vpcOutBufferSize_; + uint32_t g_originalImageWidth_ = 0; + uint32_t g_originalImageHeight_ = 0; + Resolution g_size_; +}; + +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/JpegDHelper.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/JpegDHelper.h index 9f1e231d8..e50fee64b 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/JpegDHelper.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/JpegDHelper.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 AclLiteImageProc.h -* Description: handle dvpp process */ + +#ifndef YOLOV3_CARCOLOR_SAMPLE_INC_JPEGDHELPER_H +#define YOLOV3_CARCOLOR_SAMPLE_INC_JPEGDHELPER_H + #pragma once #include @@ -78,8 +79,10 @@ private: void DestroyOutputPara(); private: - aclrtStream stream_; - void* decodeOutBufferDev_; // decode output buffer - acldvppPicDesc *decodeOutputDesc_; //decode output desc - acldvppChannelDesc *dvppChannelDesc_; -}; \ No newline at end of file + aclrtStream g_stream_; + void* g_decodeOutBufferDev_; // decode output buffer + acldvppPicDesc *g_decodeOutputDesc_; //decode output desc + acldvppChannelDesc *g_dvppChannelDesc_; +}; + +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/ResizeHelper.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/ResizeHelper.h index 28854f5de..4f161de2e 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/ResizeHelper.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/ResizeHelper.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 AclLiteImageProc.h -* Description: handle dvpp process */ + +#ifndef YOLOV3_CARCOLOR_SAMPLE_INC_RESIZEHELPER_H +#define YOLOV3_CARCOLOR_SAMPLE_INC_RESIZEHELPER_H + #pragma once #include @@ -73,14 +74,16 @@ private: void DestroyResource(); void DestroyOutputPara(); - aclrtStream stream_; - void *vpcOutBufferDev_; // vpc output buffer - acldvppPicDesc *vpcInputDesc_; // vpc input desc - acldvppPicDesc *vpcOutputDesc_; // vpc output desc - acldvppResizeConfig *resizeConfig_; - acldvppChannelDesc *dvppChannelDesc_; + aclrtStream g_stream_; + void *g_vpcOutBufferDev_; // vpc output buffer + acldvppPicDesc *g_vpcInputDesc_; // vpc input desc + acldvppPicDesc *g_vpcOutputDesc_; // vpc output desc + acldvppResizeConfig *g_resizeConfig_; + acldvppChannelDesc *g_dvppChannelDesc_; + + uint8_t *g_inDevBuffer_; // input pic dev buffer + uint32_t g_vpcOutBufferSize_; // vpc output size + Resolution g_size_; +}; - uint8_t *inDevBuffer_; // input pic dev buffer - uint32_t vpcOutBufferSize_; // vpc output size - Resolution size_; -}; \ No newline at end of file +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/color_classify.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/color_classify.h index 26fff6765..2828deb37 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/color_classify.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/color_classify.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 YOLOV3_CARCOLOR_SAMPLE_INC_COLOR_CLASSIFY_H +#define YOLOV3_CARCOLOR_SAMPLE_INC_COLOR_CLASSIFY_H + #pragma once #include "acl/acl.h" #include @@ -31,9 +32,9 @@ public: ColorClassify(); ~ColorClassify(); AclLiteError Init(); - AclLiteError Preprocess(ImageData& srcImage, std::vector &carImgs, int& flag); + AclLiteError PreProcess(ImageData& srcImage, std::vector &carImgs, int& flag); AclLiteError Inference(std::vector &carImgs, std::vector& inferenceOutput); - AclLiteError Postprocess(std::vector& inferenceOutput, + AclLiteError PostProcess(std::vector& inferenceOutput, std::vector& carInfo, const std::string& origImagePath); private: AclLiteError InitModelInput(); @@ -46,14 +47,16 @@ private: void DestroyResource(); private: - AclLiteModel model_; - const char* modelPath_; - AclLiteImageProc dvpp_; - aclrtRunMode runMode_; - bool isInited_; - bool isReleased_; - - int32_t batchSize_; - uint32_t inputSize_; - uint8_t* inputBuf_; -}; \ No newline at end of file + AclLiteModel g_model_; + const char* g_modelPath_; + AclLiteImageProc g_dvpp_; + aclrtRunMode g_runMode_; + bool g_isInited_; + bool g_isReleased_; + + int32_t g_batchSize_; + uint32_t g_inputSize_; + uint8_t* g_inputBuf_; +}; + +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/object_detect.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/object_detect.h index 626a4422a..fce5b238d 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/object_detect.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/object_detect.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 YOLOV3_CARCOLOR_SAMPLE_INC_OBJECT_DETECT_H +#define YOLOV3_CARCOLOR_SAMPLE_INC_OBJECT_DETECT_H + #pragma once #include "acl/acl.h" #include @@ -36,9 +37,9 @@ public: ObjectDetect(); ~ObjectDetect(); AclLiteError Init(); - AclLiteError Preprocess(ImageData& resizedImage, ImageData& srcImage, ImageData& yuvImage); + AclLiteError PreProcess(ImageData& resizedImage, ImageData& srcImage, ImageData& yuvImage); AclLiteError Inference(std::vector& inferenceOutput, ImageData& resizedImage); - AclLiteError Postprocess(ImageData& image, std::vector& inferenceOutput, + AclLiteError PostProcess(ImageData& image, std::vector& inferenceOutput, std::vector& carData); // void DrawCarBoxToImage(std::vector& detectionResults, // const std::string& origImagePath); @@ -47,12 +48,14 @@ private: AclLiteError InitModelInput(); private: - uint32_t imageInfoSize_; - void* imageInfoBuf_; - AclLiteModel model_; - const char* modelPath_; - AclLiteImageProc dvpp_; - aclrtRunMode runMode_; - bool isInited_; - bool isReleased_; + uint32_t g_imageInfoSize_; + void* g_imageInfoBuf_; + AclLiteModel g_model_; + const char* g_modelPath_; + AclLiteImageProc g_dvpp_; + aclrtRunMode g_runMode_; + bool g_isInited_; + bool g_isReleased_; }; + +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/params.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/params.h index 599ed4a2b..f860415b0 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/params.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/params.h @@ -1,3 +1,22 @@ +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. +*/ + +#ifndef YOLOV3_CARCOLOR_SAMPLE_INC_PARAMS_H +#define YOLOV3_CARCOLOR_SAMPLE_INC_PARAMS_H + #include "acl/acl.h" #include #include "AclLiteModel.h" @@ -15,9 +34,11 @@ struct Rectangle { struct CarInfo { ImageData cropedImgs; // cropped image from original image - ImageData resizedImgs; //resized image for inference + ImageData resizedImgs; // resized image for inference Rectangle rectangle; - std::string text; //类别 - std::string carColor_result; //颜色 - float confidence; //颜色 -}; \ No newline at end of file + std::string text; // 类别 + std::string carColor_result; // 颜色 + float confidence; // 颜色 +}; + +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteImageProc.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteImageProc.cpp index b6b39bd8d..a137a221e 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteImageProc.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteImageProc.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,8 @@ * 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 AclLiteImageProc.cpp -* Description: handle dvpp process */ + #include #include "acl/acl.h" #include "ResizeHelper.h" @@ -26,10 +24,10 @@ using namespace std; AclLiteImageProc::AclLiteImageProc(): - isReleased_(false), - stream_(nullptr), - dvppChannelDesc_(nullptr), - isInitOk_(false) { + g_isReleased_(false), + g_stream_(nullptr), + g_dvppChannelDesc_(nullptr), + g_isInitOk_(false) { } AclLiteImageProc::~AclLiteImageProc() { @@ -37,53 +35,53 @@ AclLiteImageProc::~AclLiteImageProc() { } void AclLiteImageProc::DestroyResource() { - if (isReleased_) { + if (g_isReleased_) { return; } aclError aclRet; - if (dvppChannelDesc_ != nullptr) { - aclRet = acldvppDestroyChannel(dvppChannelDesc_); + if (g_dvppChannelDesc_ != nullptr) { + aclRet = acldvppDestroyChannel(g_dvppChannelDesc_); if (aclRet != ACL_SUCCESS) { ACLLITE_LOG_ERROR("Destroy dvpp channel error: %d", aclRet); } - (void)acldvppDestroyChannelDesc(dvppChannelDesc_); - dvppChannelDesc_ = nullptr; + (void)acldvppDestroyChannelDesc(g_dvppChannelDesc_); + g_dvppChannelDesc_ = nullptr; } - if (stream_ != nullptr) { - aclRet = aclrtDestroyStream(stream_); + if (g_stream_ != nullptr) { + aclRet = aclrtDestroyStream(g_stream_); if (aclRet != ACL_SUCCESS) { ACLLITE_LOG_ERROR("Vdec destroy stream failed, error %d", aclRet); } - stream_ = nullptr; + g_stream_ = nullptr; } - isReleased_ = true; + g_isReleased_ = true; } AclLiteError AclLiteImageProc::Init() { - aclError aclRet = aclrtCreateStream(&stream_); + aclError aclRet = aclrtCreateStream(&g_stream_); if (aclRet != ACL_SUCCESS) { ACLLITE_LOG_ERROR("Create venc stream failed, error %d", aclRet); return ACLLITE_ERROR_CREATE_STREAM; } - dvppChannelDesc_ = acldvppCreateChannelDesc(); - if (dvppChannelDesc_ == nullptr) { + g_dvppChannelDesc_ = acldvppCreateChannelDesc(); + if (g_dvppChannelDesc_ == nullptr) { ACLLITE_LOG_ERROR("Create dvpp channel desc failed"); return ACLLITE_ERROR_CREATE_DVPP_CHANNEL_DESC; } - aclRet = acldvppCreateChannel(dvppChannelDesc_); + aclRet = acldvppCreateChannel(g_dvppChannelDesc_); if (aclRet != ACL_SUCCESS) { ACLLITE_LOG_ERROR("acldvppCreateChannel failed, aclRet = %d", aclRet); return ACLLITE_ERRROR_CREATE_DVPP_CHANNEL; } - isInitOk_ = true; + g_isInitOk_ = true; ACLLITE_LOG_INFO("dvpp init resource ok"); return ACLLITE_OK; @@ -91,19 +89,19 @@ AclLiteError AclLiteImageProc::Init() { AclLiteError AclLiteImageProc::Resize(ImageData& dest, ImageData& src, uint32_t width, uint32_t height) { - ResizeHelper resizeOp(stream_, dvppChannelDesc_, width, height); + ResizeHelper resizeOp(g_stream_, g_dvppChannelDesc_, width, height); return resizeOp.Process(dest, src); } AclLiteError AclLiteImageProc::JpegD(ImageData& dest, ImageData& src) { - JpegDHelper jpegD(stream_, dvppChannelDesc_); + JpegDHelper jpegD(g_stream_, g_dvppChannelDesc_); return jpegD.Process(dest, src); } AclLiteError AclLiteImageProc::Crop(ImageData& dest, ImageData& src, uint32_t ltHorz, uint32_t ltVert, uint32_t rbHorz, uint32_t rbVert) { - CropAndPasteHelper crop(stream_, dvppChannelDesc_, + CropAndPasteHelper crop(g_stream_, g_dvppChannelDesc_, ltHorz, ltVert, rbHorz, rbVert); return crop.Process(dest, src); } @@ -112,7 +110,7 @@ AclLiteError AclLiteImageProc::CropPaste(ImageData& dest, ImageData& src, uint32_t width, uint32_t height, uint32_t ltHorz, uint32_t ltVert, uint32_t rbHorz, uint32_t rbVert) { - CropAndPasteHelper crop(stream_, dvppChannelDesc_, + CropAndPasteHelper crop(g_stream_, g_dvppChannelDesc_, width, height, ltHorz, ltVert, rbHorz, rbVert); return crop.ProcessCropPaste(dest, src); diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteModel.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteModel.cpp index 9887de360..c959b9940 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteModel.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteModel.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,30 +12,28 @@ * 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 model_process.cpp -* Description: handle model process */ + #include "AclLiteModel.h" #include #include "AclLiteUtils.h" using namespace std; AclLiteModel::AclLiteModel(const string& modelPath): - loadFlag_(false), - isReleased_(false), - modelId_(0), - outputsNum_(0), - modelMemSize_(0), - modelWorkSize_(0), - modelWeightSize_(0), - modelMemPtr_(nullptr), - modelWorkPtr_(nullptr), - modelWeightPtr_(nullptr), - modelDesc_(nullptr), - input_(nullptr), - output_(nullptr), - modelPath_(modelPath) { + g_loadFlag_(false), + g_isReleased_(false), + g_modelId_(0), + g_outputsNum_(0), + g_modelMemSize_(0), + g_modelWorkSize_(0), + g_modelWeightSize_(0), + g_modelMemPtr_(nullptr), + g_modelWorkPtr_(nullptr), + g_modelWeightPtr_(nullptr), + g_modelDesc_(nullptr), + g_input_(nullptr), + g_output_(nullptr), + g_modelPath_(modelPath) { } AclLiteModel::~AclLiteModel() { @@ -43,7 +41,7 @@ AclLiteModel::~AclLiteModel() { } void AclLiteModel::DestroyResource() { - if (isReleased_) { + if (g_isReleased_) { return; } @@ -51,19 +49,19 @@ void AclLiteModel::DestroyResource() { DestroyDesc(); DestroyInput(); DestroyOutput(); - isReleased_ = true; + g_isReleased_ = true; } AclLiteError AclLiteModel::Init() { - aclError aclRet = aclrtGetRunMode(&runMode_); + aclError aclRet = aclrtGetRunMode(&g_runMode_); if (aclRet != ACL_SUCCESS) { ACLLITE_LOG_ERROR("acl get run mode failed"); return ACLLITE_ERROR_GET_RUM_MODE; } - AclLiteError ret = LoadModelFromFile(modelPath_); + AclLiteError ret = LoadModelFromFile(g_modelPath_); if (ret != ACLLITE_OK) { - ACLLITE_LOG_ERROR("Load model %s from file failed", modelPath_.c_str()); + ACLLITE_LOG_ERROR("Load model %s from file failed", g_modelPath_.c_str()); return ret; } @@ -79,42 +77,42 @@ AclLiteError AclLiteModel::Init() { return ret; } - ACLLITE_LOG_INFO("Init model %s success", modelPath_.c_str()); + ACLLITE_LOG_INFO("Init model %s success", g_modelPath_.c_str()); return ACLLITE_OK; } AclLiteError AclLiteModel::LoadModelFromFile(const string& modelPath) { - if (loadFlag_) { + if (g_loadFlag_) { ACLLITE_LOG_ERROR("%s is loaded already", modelPath.c_str()); return ACLLITE_ERROR_LOAD_MODEL_REPEATED; } - aclError ret = aclmdlLoadFromFile(modelPath.c_str(), &modelId_); + aclError ret = aclmdlLoadFromFile(modelPath.c_str(), &g_modelId_); if (ret != ACL_SUCCESS) { ACLLITE_LOG_ERROR("Load model(%s) from file return %d", modelPath.c_str(), ret); return ACLLITE_ERROR_LOAD_MODEL; } - loadFlag_ = true; + g_loadFlag_ = true; ACLLITE_LOG_INFO("Load model %s success", modelPath.c_str()); return ACLLITE_OK; } AclLiteError AclLiteModel::SetDesc() { - modelDesc_ = aclmdlCreateDesc(); - if (modelDesc_ == nullptr) { + g_modelDesc_ = aclmdlCreateDesc(); + if (g_modelDesc_ == nullptr) { ACLLITE_LOG_ERROR("Create model(%s) description failed", - modelPath_.c_str()); + g_modelPath_.c_str()); return ACLLITE_ERROR_CREATE_MODEL_DESC; } - aclError ret = aclmdlGetDesc(modelDesc_, modelId_); + aclError ret = aclmdlGetDesc(g_modelDesc_, g_modelId_); if (ret != ACL_SUCCESS) { ACLLITE_LOG_ERROR("Get model(%s) description failed", - modelPath_.c_str()); + g_modelPath_.c_str()); return ACLLITE_ERROR_GET_MODEL_DESC; } @@ -124,9 +122,9 @@ AclLiteError AclLiteModel::SetDesc() { void AclLiteModel::DestroyDesc() { - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; + if (g_modelDesc_ != nullptr) { + (void)aclmdlDestroyDesc(g_modelDesc_); + g_modelDesc_ = nullptr; } } @@ -135,14 +133,14 @@ AclLiteError AclLiteModel::CreateInput(void *input, uint32_t size) { return CreateInput(inputData); } -AclLiteError AclLiteModel::CreateInput(void *input1, uint32_t input1size, - void* input2, uint32_t input2size) { - vector inputData = {{input1, input1size}, {input2, input2size}}; +AclLiteError AclLiteModel::CreateInput(void *input1, uint32_t input1Size, + void* input2, uint32_t input2Size) { + vector inputData = {{input1, input1Size}, {input2, input2Size}}; return CreateInput(inputData); } AclLiteError AclLiteModel::CreateInput(vector& inputData) { - uint32_t dataNum = aclmdlGetNumInputs(modelDesc_); + uint32_t dataNum = aclmdlGetNumInputs(g_modelDesc_); if (dataNum == 0) { ACLLITE_LOG_ERROR("Create input failed for no input data"); @@ -153,20 +151,20 @@ AclLiteError AclLiteModel::CreateInput(vector& inputData) { return ACLLITE_ERROR_INVALID_ARGS; } - input_ = aclmdlCreateDataset(); - if (input_ == nullptr) { + g_input_ = aclmdlCreateDataset(); + if (g_input_ == nullptr) { ACLLITE_LOG_ERROR("Create input failed for create dataset failed"); return ACLLITE_ERROR_CREATE_DATASET; } for (uint32_t i = 0; i < inputData.size(); i++) { - size_t modelInputSize = aclmdlGetInputSizeByIndex(modelDesc_, i); + size_t modelInputSize = aclmdlGetInputSizeByIndex(g_modelDesc_, i); if (modelInputSize != inputData[i].size) { ACLLITE_LOG_WARNING("Input size verify failed " "input[%d] size: %ld, provide size : %d", i, modelInputSize, inputData[i].size); } - AclLiteError atlRet = AddDatasetBuffer(input_, + AclLiteError atlRet = AddDatasetBuffer(g_input_, inputData[i].data, inputData[i].size); if (atlRet != ACLLITE_OK) { @@ -181,21 +179,21 @@ AclLiteError AclLiteModel::CreateInput(vector& inputData) { AclLiteError AclLiteModel::CreateOutput() { - if (modelDesc_ == nullptr) { + if (g_modelDesc_ == nullptr) { ACLLITE_LOG_ERROR("Create output failed for no model(%s) description", - modelPath_.c_str()); + g_modelPath_.c_str()); return ACLLITE_ERROR_NO_MODEL_DESC; } - output_ = aclmdlCreateDataset(); - if (output_ == nullptr) { + g_output_ = aclmdlCreateDataset(); + if (g_output_ == nullptr) { ACLLITE_LOG_ERROR("Create output failed for create dataset error"); return ACLLITE_ERROR_CREATE_DATASET; } - outputsNum_ = aclmdlGetNumOutputs(modelDesc_); - for (size_t i = 0; i < outputsNum_; ++i) { - size_t bufSize = aclmdlGetOutputSizeByIndex(modelDesc_, i); + g_outputsNum_ = aclmdlGetNumOutputs(g_modelDesc_); + for (size_t i = 0; i < g_outputsNum_; ++i) { + size_t bufSize = aclmdlGetOutputSizeByIndex(g_modelDesc_, i); void *outputBuffer = nullptr; aclError ret = aclrtMalloc(&outputBuffer, bufSize, @@ -206,7 +204,7 @@ AclLiteError AclLiteModel::CreateOutput() return ACLLITE_ERROR_MALLOC_DEVICE; } - AclLiteError atlRet = AddDatasetBuffer(output_, outputBuffer, bufSize); + AclLiteError atlRet = AddDatasetBuffer(g_output_, outputBuffer, bufSize); if (atlRet != ACLLITE_OK) { ACLLITE_LOG_ERROR("Create output failed for " "add dataset buffer error %d", atlRet); @@ -215,7 +213,7 @@ AclLiteError AclLiteModel::CreateOutput() } } - ACLLITE_LOG_INFO("Create model(%s) output success", modelPath_.c_str()); + ACLLITE_LOG_INFO("Create model(%s) output success", g_modelPath_.c_str()); return ACLLITE_OK; } @@ -261,13 +259,13 @@ AclLiteError AclLiteModel::Execute(vector& inferOutputs, } AclLiteError AclLiteModel::Execute(vector& inferOutputs) { - aclError ret = aclmdlExecute(modelId_, input_, output_); + aclError ret = aclmdlExecute(g_modelId_, g_input_, g_output_); if (ret != ACL_SUCCESS) { - ACLLITE_LOG_ERROR("Execute model(%s) error:%d", modelPath_.c_str(), ret); + ACLLITE_LOG_ERROR("Execute model(%s) error:%d", g_modelPath_.c_str(), ret); return ACLLITE_ERROR_EXECUTE_MODEL; } - for (uint32_t i = 0; i < outputsNum_; i++) { + for (uint32_t i = 0; i < g_outputsNum_; i++) { InferenceOutput out; AclLiteError ret = GetOutputItem(out, i); if (ret != ACLLITE_OK) { @@ -283,7 +281,7 @@ AclLiteError AclLiteModel::Execute(vector& inferOutputs) { AclLiteError AclLiteModel::GetOutputItem(InferenceOutput& out, uint32_t idx) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, idx); + aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(g_output_, idx); if (dataBuffer == nullptr) { ACLLITE_LOG_ERROR("Get the %dth dataset buffer from model " "inference output failed", idx); @@ -305,7 +303,7 @@ AclLiteError AclLiteModel::GetOutputItem(InferenceOutput& out, } void* data = CopyDataToHost(dataBufferDev, bufferSize, - runMode_, MEMORY_NORMAL); + g_runMode_, MEMORY_NORMAL); if (data == nullptr) { ACLLITE_LOG_ERROR("Copy inference output to host failed"); return ACLLITE_ERROR_COPY_DATA; @@ -319,67 +317,67 @@ AclLiteError AclLiteModel::GetOutputItem(InferenceOutput& out, void AclLiteModel::DestroyInput() { - if (input_ == nullptr) { + if (g_input_ == nullptr) { return; } - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(input_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(input_, i); + for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(g_input_); ++i) { + aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(g_input_, i); aclDestroyDataBuffer(dataBuffer); dataBuffer = nullptr; } - aclmdlDestroyDataset(input_); - input_ = nullptr; + aclmdlDestroyDataset(g_input_); + g_input_ = nullptr; } void AclLiteModel::DestroyOutput() { - if (output_ == nullptr) { + if (g_output_ == nullptr) { return; } - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); + for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(g_output_); ++i) { + aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(g_output_, i); void* data = aclGetDataBufferAddr(dataBuffer); (void)aclrtFree(data); (void)aclDestroyDataBuffer(dataBuffer); dataBuffer = nullptr; } - (void)aclmdlDestroyDataset(output_); - output_ = nullptr; + (void)aclmdlDestroyDataset(g_output_); + g_output_ = nullptr; } void AclLiteModel::Unload() { - if (!loadFlag_) { + if (!g_loadFlag_) { ACLLITE_LOG_INFO("Model(%s) had not been loaded or unload already", - modelPath_.c_str()); + g_modelPath_.c_str()); return; } - aclError ret = aclmdlUnload(modelId_); + aclError ret = aclmdlUnload(g_modelId_); if (ret != ACL_SUCCESS) { - ACLLITE_LOG_ERROR("Unload model(%s) error:%d", modelPath_.c_str(), ret); + ACLLITE_LOG_ERROR("Unload model(%s) error:%d", g_modelPath_.c_str(), ret); } - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; + if (g_modelDesc_ != nullptr) { + (void)aclmdlDestroyDesc(g_modelDesc_); + g_modelDesc_ = nullptr; } - if (modelMemPtr_ != nullptr) { - aclrtFree(modelMemPtr_); - modelMemPtr_ = nullptr; - modelMemSize_ = 0; + if (g_modelMemPtr_ != nullptr) { + aclrtFree(g_modelMemPtr_); + g_modelMemPtr_ = nullptr; + g_modelMemSize_ = 0; } - if (modelWeightPtr_ != nullptr) { - aclrtFree(modelWeightPtr_); - modelWeightPtr_ = nullptr; - modelWeightSize_ = 0; + if (g_modelWeightPtr_ != nullptr) { + aclrtFree(g_modelWeightPtr_); + g_modelWeightPtr_ = nullptr; + g_modelWeightSize_ = 0; } - loadFlag_ = false; - ACLLITE_LOG_INFO("Unload model %s success", modelPath_.c_str()); + g_loadFlag_ = false; + ACLLITE_LOG_INFO("Unload model %s success", g_modelPath_.c_str()); } \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteResource.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteResource.cpp index a8dab3a4e..82c4132a7 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteResource.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteResource.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,22 +12,20 @@ * 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 utils.cpp -* Description: handle file operations */ + #include "AclLiteUtils.h" #include "AclLiteResource.h" using namespace std; AclLiteResource::AclLiteResource(): - isReleased_(false), - useDefaultCtx_(true), - deviceId_(0), - runMode_(ACL_HOST), - context_(nullptr), - aclConfig_("") { + g_isReleased_(false), + g_useDefaultCtx_(true), + g_deviceId_(0), + g_runMode_(ACL_HOST), + g_context_(nullptr), + g_aclConfig_("") { } AclLiteResource::~AclLiteResource() { @@ -36,7 +34,7 @@ AclLiteResource::~AclLiteResource() { AclLiteError AclLiteResource::Init() { // ACL init - aclError ret = aclInit(aclConfig_.c_str()); + aclError ret = aclInit(g_aclConfig_.c_str()); if (ret != ACL_SUCCESS) { ACLLITE_LOG_ERROR("Acl init failed, errorcode is: %d", ret); return ACLLITE_ERROR; @@ -44,29 +42,29 @@ AclLiteError AclLiteResource::Init() { ACLLITE_LOG_INFO("Acl init ok"); // open device - ret = aclrtSetDevice(deviceId_); + ret = aclrtSetDevice(g_deviceId_); if (ret != ACL_SUCCESS) { - ACLLITE_LOG_ERROR("Acl open device %d failed, errorCode is : %d", deviceId_, ret); + ACLLITE_LOG_ERROR("Acl open device %d failed, errorCode is : %d", g_deviceId_, ret); return ACLLITE_ERROR; } - ACLLITE_LOG_INFO("Open device %d ok", deviceId_); + ACLLITE_LOG_INFO("Open device %d ok", g_deviceId_); - if (useDefaultCtx_) { + if (g_useDefaultCtx_) { ACLLITE_LOG_INFO("Use default context currently"); - ret = aclrtGetCurrentContext(&context_); - if ((ret != ACL_SUCCESS) || (context_ == nullptr)) { + ret = aclrtGetCurrentContext(&g_context_); + if ((ret != ACL_SUCCESS) || (g_context_ == nullptr)) { ACLLITE_LOG_ERROR("Get current acl context failed, errorCode is : %d", ret); return ACLLITE_ERROR_GET_ACL_CONTEXT; } } else { - ret = aclrtCreateContext(&context_, deviceId_); + ret = aclrtCreateContext(&g_context_, g_deviceId_); if (ret != ACL_SUCCESS) { ACLLITE_LOG_ERROR("Create acl context failed, errorCode is : %d", ret); return ACLLITE_ERROR_CREATE_ACL_CONTEXT; } } - ret = aclrtGetRunMode(&runMode_); + ret = aclrtGetRunMode(&g_runMode_); if (ret != ACL_SUCCESS) { ACLLITE_LOG_WARNING("acl get run mode failed, errorCode is : %d", ret); } @@ -75,25 +73,25 @@ AclLiteError AclLiteResource::Init() { } void AclLiteResource::Release() { - if (isReleased_) { + if (g_isReleased_) { return; } aclError ret; - if ((!useDefaultCtx_) && (context_ != nullptr)) { - ret = aclrtDestroyContext(context_); + if ((!g_useDefaultCtx_) && (g_context_ != nullptr)) { + ret = aclrtDestroyContext(g_context_); if (ret != ACL_SUCCESS) { ACLLITE_LOG_ERROR("destroy context failed, errorCode is : %d", ret); } - context_ = nullptr; + g_context_ = nullptr; } ACLLITE_LOG_INFO("destroy context ok"); - ret = aclrtResetDevice(deviceId_); + ret = aclrtResetDevice(g_deviceId_); if (ret != ACL_SUCCESS) { ACLLITE_LOG_ERROR("reset device failed, errorCode is : %d", ret); } - ACLLITE_LOG_INFO("Reset device %d ok", deviceId_); + ACLLITE_LOG_INFO("Reset device %d ok", g_deviceId_); ret = aclFinalize(); if (ret != ACL_SUCCESS) { @@ -101,5 +99,5 @@ void AclLiteResource::Release() { } ACLLITE_LOG_INFO("Finalize acl ok"); - isReleased_ = true; + g_isReleased_ = true; } diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteUtils.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteUtils.cpp index d29d61bb5..dcda15061 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteUtils.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteUtils.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,8 @@ * 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 utils.cpp -* Description: handle file operations */ + #include "AclLiteUtils.h" #include #include @@ -39,10 +37,10 @@ using namespace std; #define TABLE_CHAR '\t' namespace { -const std::string kImagePathSeparator = ","; -const int kStatSuccess = 0; -const std::string kFileSperator = "/"; -const std::string kPathSeparator = "/"; +const std::string g_ImagePathSeparator = ","; +const int g_StatSuccess = 0; +const std::string g_FileSperator = "/"; +const std::string g_PathSeparator = "/"; } bool IsPathExist(const string &path) { @@ -56,7 +54,7 @@ bool IsPathExist(const string &path) { bool IsDirectory(const string &path) { // get path stat struct stat buf; - if (stat(path.c_str(), &buf) != kStatSuccess) { + if (stat(path.c_str(), &buf) != g_StatSuccess) { return false; } @@ -66,7 +64,7 @@ bool IsDirectory(const string &path) { void SplitPath(const string &path, vector &pathVec) { char *charPath = const_cast(path.c_str()); - const char *charSplit = kImagePathSeparator.c_str(); + const char *charSplit = g_ImagePathSeparator.c_str(); char *imageFile = strtok(charPath, charSplit); while (imageFile) { pathVec.emplace_back(imageFile); @@ -86,7 +84,7 @@ void GetPathFiles(const string &path, vector &fileVec) { } // file path - string fullPath = path + kPathSeparator + direntPtr->d_name; + string fullPath = path + g_PathSeparator + direntPtr->d_name; // directory need recursion if (IsDirectory(fullPath)) { GetPathFiles(fullPath, fileVec); diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/CropAndPasteHelper.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/CropAndPasteHelper.cpp index f6a40e3ad..265506a79 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/CropAndPasteHelper.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/CropAndPasteHelper.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,8 @@ * 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 AclLiteImageProc.cpp -* Description: handle dvpp process */ + #include #include #include "acl/acl.h" @@ -28,23 +26,23 @@ CropAndPasteHelper::CropAndPasteHelper(aclrtStream& stream, acldvppChannelDesc *dvppChannelDesc, uint32_t ltHorz, uint32_t ltVert, uint32_t rbHorz, uint32_t rbVert): - stream_(stream), - vpcOutBufferDev_(nullptr), - cropArea_(nullptr), - pasteArea_(nullptr), - vpcInputDesc_(nullptr), - vpcOutputDesc_(nullptr), - dvppChannelDesc_(dvppChannelDesc), - vpcOutBufferSize_(0) { + g_stream_(stream), + g_vpcOutBufferDev_(nullptr), + g_cropArea_(nullptr), + g_pasteArea_(nullptr), + g_vpcInputDesc_(nullptr), + g_vpcOutputDesc_(nullptr), + g_dvppChannelDesc_(dvppChannelDesc), + g_vpcOutBufferSize_(0) { // Change the left top coordinate to even numver - ltHorz_ = (ltHorz >> 1) << 1; - ltVert_ = (ltVert >> 1) << 1; + g_ltHorz_ = (ltHorz >> 1) << 1; + g_ltVert_ = (ltVert >> 1) << 1; // Change the right bottom coordinate to odd numver - rbHorz_ = ((rbHorz >> 1) << 1) - 1; - rbVert_ = ((rbVert >> 1) << 1) - 1; - //odd - size_.width = rbHorz_ - ltHorz_; - size_.height = rbVert_ - ltVert_; + g_rbHorz_ = ((rbHorz >> 1) << 1) - 1; + g_rbVert_ = ((rbVert >> 1) << 1) - 1; + // odd + g_size_.width = g_rbHorz_ - g_ltHorz_; + g_size_.height = g_rbVert_ - g_ltVert_; } CropAndPasteHelper::CropAndPasteHelper(aclrtStream& stream, @@ -52,22 +50,22 @@ CropAndPasteHelper::CropAndPasteHelper(aclrtStream& stream, uint32_t width, uint32_t height, uint32_t ltHorz, uint32_t ltVert, uint32_t rbHorz, uint32_t rbVert): - stream_(stream), - vpcOutBufferDev_(nullptr), - cropArea_(nullptr), - pasteArea_(nullptr), - vpcInputDesc_(nullptr), - vpcOutputDesc_(nullptr), - dvppChannelDesc_(dvppChannelDesc), - vpcOutBufferSize_(0) { + g_stream_(stream), + g_vpcOutBufferDev_(nullptr), + g_cropArea_(nullptr), + g_pasteArea_(nullptr), + g_vpcInputDesc_(nullptr), + g_vpcOutputDesc_(nullptr), + g_dvppChannelDesc_(dvppChannelDesc), + g_vpcOutBufferSize_(0) { // Change the left top coordinate to even numver - ltHorz_ = (ltHorz >> 1) << 1; - ltVert_ = (ltVert >> 1) << 1; + g_ltHorz_ = (ltHorz >> 1) << 1; + g_ltVert_ = (ltVert >> 1) << 1; // Change the right bottom coordinate to odd numver - rbHorz_ = ((rbHorz >> 1) << 1) - 1; - rbVert_ = ((rbVert >> 1) << 1) - 1; - size_.width = width; - size_.height = height; + g_rbHorz_ = ((rbHorz >> 1) << 1) - 1; + g_rbVert_ = ((rbVert >> 1) << 1) - 1; + g_size_.width = width; + g_size_.height = height; } CropAndPasteHelper::~CropAndPasteHelper() { @@ -75,8 +73,8 @@ CropAndPasteHelper::~CropAndPasteHelper() { } AclLiteError CropAndPasteHelper::InitCropAndPasteInputDesc(ImageData& inputImage) { - originalImageWidth_ = inputImage.width; - originalImageHeight_ = inputImage.height; + g_originalImageWidth_ = inputImage.width; + g_originalImageHeight_ = inputImage.height; uint32_t alignWidth = inputImage.alignWidth; uint32_t alignHeight = inputImage.alignHeight; if (alignWidth == 0 || alignHeight == 0) { @@ -87,59 +85,59 @@ AclLiteError CropAndPasteHelper::InitCropAndPasteInputDesc(ImageData& inputImage uint32_t inputBufferSize = YUV420SP_SIZE(alignWidth, alignHeight); - vpcInputDesc_ = acldvppCreatePicDesc(); - if (vpcInputDesc_ == nullptr) { + g_vpcInputDesc_ = acldvppCreatePicDesc(); + if (g_vpcInputDesc_ == nullptr) { ACLLITE_LOG_ERROR("Dvpp crop create pic desc failed"); return ACLLITE_ERROR; } - acldvppSetPicDescData(vpcInputDesc_, inputImage.data.get()); - acldvppSetPicDescFormat(vpcInputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); - acldvppSetPicDescWidth(vpcInputDesc_, inputImage.width); - acldvppSetPicDescHeight(vpcInputDesc_, inputImage.height); - acldvppSetPicDescWidthStride(vpcInputDesc_, alignWidth); - acldvppSetPicDescHeightStride(vpcInputDesc_, alignHeight); - acldvppSetPicDescSize(vpcInputDesc_, inputBufferSize); + acldvppSetPicDescData(g_vpcInputDesc_, inputImage.data.get()); + acldvppSetPicDescFormat(g_vpcInputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); + acldvppSetPicDescWidth(g_vpcInputDesc_, inputImage.width); + acldvppSetPicDescHeight(g_vpcInputDesc_, inputImage.height); + acldvppSetPicDescWidthStride(g_vpcInputDesc_, alignWidth); + acldvppSetPicDescHeightStride(g_vpcInputDesc_, alignHeight); + acldvppSetPicDescSize(g_vpcInputDesc_, inputBufferSize); return ACLLITE_OK; } AclLiteError CropAndPasteHelper::InitCropAndPasteOutputDesc() { - int cropOutWidth = size_.width; - int cropOutHeight = size_.height; + int cropOutWidth = g_size_.width; + int cropOutHeight = g_size_.height; int cropOutWidthStride = ALIGN_UP16(cropOutWidth); int cropOutHeightStride = ALIGN_UP2(cropOutHeight); if (cropOutWidthStride == 0 || cropOutHeightStride == 0) { ACLLITE_LOG_ERROR("Crop image align widht(%d) and height(%d) failed", - size_.width, size_.height); + g_size_.width, g_size_.height); return ACLLITE_ERROR; } - vpcOutBufferSize_ = YUV420SP_SIZE(cropOutWidthStride, + g_vpcOutBufferSize_ = YUV420SP_SIZE(cropOutWidthStride, cropOutHeightStride); - aclError aclRet = acldvppMalloc(&vpcOutBufferDev_, vpcOutBufferSize_); + aclError aclRet = acldvppMalloc(&g_vpcOutBufferDev_, g_vpcOutBufferSize_); if (aclRet != ACL_SUCCESS) { ACLLITE_LOG_ERROR("Dvpp crop malloc output memory failed, crop " "width %d, height %d size %d, error %d", - size_.width, size_.height, - vpcOutBufferSize_, aclRet); + g_size_.width, g_size_.height, + g_vpcOutBufferSize_, aclRet); return ACLLITE_ERROR; } - aclrtMemset(vpcOutBufferDev_, vpcOutBufferSize_, 0, vpcOutBufferSize_); - vpcOutputDesc_ = acldvppCreatePicDesc(); - if (vpcOutputDesc_ == nullptr) { + aclrtMemset(g_vpcOutBufferDev_, g_vpcOutBufferSize_, 0, g_vpcOutBufferSize_); + g_vpcOutputDesc_ = acldvppCreatePicDesc(); + if (g_vpcOutputDesc_ == nullptr) { ACLLITE_LOG_ERROR("Dvpp crop create pic desc failed"); return ACLLITE_ERROR; } - acldvppSetPicDescData(vpcOutputDesc_, vpcOutBufferDev_); - acldvppSetPicDescFormat(vpcOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); - acldvppSetPicDescWidth(vpcOutputDesc_, cropOutWidth); - acldvppSetPicDescHeight(vpcOutputDesc_, cropOutHeight); - acldvppSetPicDescWidthStride(vpcOutputDesc_, cropOutWidthStride); - acldvppSetPicDescHeightStride(vpcOutputDesc_, cropOutHeightStride); - acldvppSetPicDescSize(vpcOutputDesc_, vpcOutBufferSize_); + acldvppSetPicDescData(g_vpcOutputDesc_, g_vpcOutBufferDev_); + acldvppSetPicDescFormat(g_vpcOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); + acldvppSetPicDescWidth(g_vpcOutputDesc_, cropOutWidth); + acldvppSetPicDescHeight(g_vpcOutputDesc_, cropOutHeight); + acldvppSetPicDescWidthStride(g_vpcOutputDesc_, cropOutWidthStride); + acldvppSetPicDescHeightStride(g_vpcOutputDesc_, cropOutHeightStride); + acldvppSetPicDescSize(g_vpcOutputDesc_, g_vpcOutBufferSize_); return ACLLITE_OK; } @@ -165,18 +163,18 @@ AclLiteError CropAndPasteHelper::Process(ImageData& cropedImage, ImageData& srcI } // must even - uint32_t cropLeftOffset = ltHorz_; + uint32_t cropLeftOffset = g_ltHorz_; // must even - uint32_t cropTopOffset = ltVert_; + uint32_t cropTopOffset = g_ltVert_; // must odd - uint32_t cropRightOffset = rbHorz_; + uint32_t cropRightOffset = g_rbHorz_; // must odd - uint32_t cropBottomOffset = rbVert_; + uint32_t cropBottomOffset = g_rbVert_; - cropArea_ = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset, + g_cropArea_ = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset); - if (cropArea_ == nullptr) { - ACLLITE_LOG_ERROR("acldvppCreateRoiConfig cropArea_ failed"); + if (g_cropArea_ == nullptr) { + ACLLITE_LOG_ERROR("acldvppCreateRoiConfig g_cropArea_ failed"); return ACLLITE_ERROR; } @@ -185,64 +183,63 @@ AclLiteError CropAndPasteHelper::Process(ImageData& cropedImage, ImageData& srcI // must even uint32_t pasteTopOffset = 0; // must odd - uint32_t pasteRightOffset = size_.width; + uint32_t pasteRightOffset = g_size_.width; // must odd - uint32_t pasteBottomOffset = size_.height; + uint32_t pasteBottomOffset = g_size_.height; - pasteArea_ = acldvppCreateRoiConfig(pasteLeftOffset, pasteRightOffset, + g_pasteArea_ = acldvppCreateRoiConfig(pasteLeftOffset, pasteRightOffset, pasteTopOffset, pasteBottomOffset); - if (pasteArea_ == nullptr) { - ACLLITE_LOG_ERROR("acldvppCreateRoiConfig pasteArea_ failed"); + if (g_pasteArea_ == nullptr) { + ACLLITE_LOG_ERROR("acldvppCreateRoiConfig g_pasteArea_ failed"); return ACLLITE_ERROR; } // crop and patse pic - aclError aclRet = acldvppVpcCropAndPasteAsync(dvppChannelDesc_, vpcInputDesc_, - vpcOutputDesc_, cropArea_, - pasteArea_, stream_); + aclError aclRet = acldvppVpcCropAndPasteAsync(g_dvppChannelDesc_, g_vpcInputDesc_, + g_vpcOutputDesc_, g_cropArea_, + g_pasteArea_, g_stream_); if (aclRet != ACL_SUCCESS) { ACLLITE_LOG_ERROR("acldvppVpcCropAndPasteAsync failed, aclRet = %d", aclRet); return ACLLITE_ERROR; } - aclRet = aclrtSynchronizeStream(stream_); + aclRet = aclrtSynchronizeStream(g_stream_); if (aclRet != ACL_SUCCESS) { ACLLITE_LOG_ERROR("crop and paste aclrtSynchronizeStream failed, aclRet = %d", aclRet); return ACLLITE_ERROR; } cropedImage.format = PIXEL_FORMAT_YUV_SEMIPLANAR_420; - cropedImage.width = size_.width; - cropedImage.height = size_.height; - cropedImage.alignWidth = ALIGN_UP16(size_.width); - cropedImage.alignHeight = ALIGN_UP2(size_.height); - cropedImage.size = vpcOutBufferSize_; - cropedImage.data = SHARED_PTR_DVPP_BUF(vpcOutBufferDev_); + cropedImage.width = g_size_.width; + cropedImage.height = g_size_.height; + cropedImage.alignWidth = ALIGN_UP16(g_size_.width); + cropedImage.alignHeight = ALIGN_UP2(g_size_.height); + cropedImage.size = g_vpcOutBufferSize_; + cropedImage.data = SHARED_PTR_DVPP_BUF(g_vpcOutBufferDev_); DestroyCropAndPasteResource(); return ACLLITE_OK; } -AclLiteError CropAndPasteHelper::ProcessCropPaste(ImageData& cropedImage, ImageData& srcImage) -{ +AclLiteError CropAndPasteHelper::ProcessCropPaste(ImageData& cropedImage, ImageData& srcImage) { if (ACLLITE_OK != InitCropAndPasteResource(srcImage)) { ACLLITE_LOG_ERROR("Dvpp cropandpaste failed for init error"); return ACLLITE_ERROR; } // must even - uint32_t cropLeftOffset = ltHorz_; + uint32_t cropLeftOffset = g_ltHorz_; // must even - uint32_t cropTopOffset = ltVert_; + uint32_t cropTopOffset = g_ltVert_; // must odd - uint32_t cropRightOffset = rbHorz_; + uint32_t cropRightOffset = g_rbHorz_; // must odd - uint32_t cropBottomOffset = rbVert_; + uint32_t cropBottomOffset = g_rbVert_; - cropArea_ = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset, + g_cropArea_ = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset); - if (cropArea_ == nullptr) { - ACLLITE_LOG_ERROR("acldvppCreateRoiConfig cropArea_ failed"); + if (g_cropArea_ == nullptr) { + ACLLITE_LOG_ERROR("acldvppCreateRoiConfig g_cropArea_ failed"); return ACLLITE_ERROR; } @@ -251,38 +248,38 @@ AclLiteError CropAndPasteHelper::ProcessCropPaste(ImageData& cropedImage, ImageD // must even uint32_t pasteTopOffset = 0; // must odd - uint32_t pasteRightOffset = (((pasteLeftOffset + size_.width) >> 1) << 1) -1; + uint32_t pasteRightOffset = (((pasteLeftOffset + g_size_.width) >> 1) << 1) -1; // must odd - uint32_t pasteBottomOffset = (((pasteTopOffset + size_.height) >> 1) << 1) -1; + uint32_t pasteBottomOffset = (((pasteTopOffset + g_size_.height) >> 1) << 1) -1; - pasteArea_ = acldvppCreateRoiConfig(pasteLeftOffset, pasteRightOffset, + g_pasteArea_ = acldvppCreateRoiConfig(pasteLeftOffset, pasteRightOffset, pasteTopOffset, pasteBottomOffset); - if (pasteArea_ == nullptr) { - ACLLITE_LOG_ERROR("acldvppCreateRoiConfig pasteArea_ failed"); + if (g_pasteArea_ == nullptr) { + ACLLITE_LOG_ERROR("acldvppCreateRoiConfig g_pasteArea_ failed"); return ACLLITE_ERROR; } // crop and patse pic - aclError aclRet = acldvppVpcCropAndPasteAsync(dvppChannelDesc_, vpcInputDesc_, - vpcOutputDesc_, cropArea_, - pasteArea_, stream_); + aclError aclRet = acldvppVpcCropAndPasteAsync(g_dvppChannelDesc_, g_vpcInputDesc_, + g_vpcOutputDesc_, g_cropArea_, + g_pasteArea_, g_stream_); if (aclRet != ACL_SUCCESS) { ACLLITE_LOG_ERROR("acldvppVpcCropAndPasteAsync failed, aclRet = %d", aclRet); return ACLLITE_ERROR; } - aclRet = aclrtSynchronizeStream(stream_); + aclRet = aclrtSynchronizeStream(g_stream_); if (aclRet != ACL_SUCCESS) { ACLLITE_LOG_ERROR("crop and paste aclrtSynchronizeStream failed, aclRet = %d", aclRet); return ACLLITE_ERROR; } cropedImage.format = PIXEL_FORMAT_YUV_SEMIPLANAR_420; - cropedImage.width = size_.width; - cropedImage.height = size_.height; - cropedImage.alignWidth = ALIGN_UP16(size_.width); - cropedImage.alignHeight = ALIGN_UP2(size_.height); - cropedImage.size = vpcOutBufferSize_; - cropedImage.data = SHARED_PTR_DVPP_BUF(vpcOutBufferDev_); + cropedImage.width = g_size_.width; + cropedImage.height = g_size_.height; + cropedImage.alignWidth = ALIGN_UP16(g_size_.width); + cropedImage.alignHeight = ALIGN_UP2(g_size_.height); + cropedImage.size = g_vpcOutBufferSize_; + cropedImage.data = SHARED_PTR_DVPP_BUF(g_vpcOutBufferDev_); DestroyCropAndPasteResource(); @@ -290,23 +287,23 @@ AclLiteError CropAndPasteHelper::ProcessCropPaste(ImageData& cropedImage, ImageD } void CropAndPasteHelper::DestroyCropAndPasteResource() { - if (cropArea_ != nullptr) { - (void)acldvppDestroyRoiConfig(cropArea_); - cropArea_ = nullptr; + if (g_cropArea_ != nullptr) { + (void)acldvppDestroyRoiConfig(g_cropArea_); + g_cropArea_ = nullptr; } - if (pasteArea_ != nullptr) { - (void)acldvppDestroyRoiConfig(pasteArea_); - pasteArea_ = nullptr; + if (g_pasteArea_ != nullptr) { + (void)acldvppDestroyRoiConfig(g_pasteArea_); + g_pasteArea_ = nullptr; } - if (vpcInputDesc_ != nullptr) { - (void)acldvppDestroyPicDesc(vpcInputDesc_); - vpcInputDesc_ = nullptr; + if (g_vpcInputDesc_ != nullptr) { + (void)acldvppDestroyPicDesc(g_vpcInputDesc_); + g_vpcInputDesc_ = nullptr; } - if (vpcOutputDesc_ != nullptr) { - (void)acldvppDestroyPicDesc(vpcOutputDesc_); - vpcOutputDesc_ = nullptr; + if (g_vpcOutputDesc_ != nullptr) { + (void)acldvppDestroyPicDesc(g_vpcOutputDesc_); + g_vpcOutputDesc_ = nullptr; } } \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/JpegDHelper.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/JpegDHelper.cpp index 7401e9776..f62504a6e 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/JpegDHelper.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/JpegDHelper.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,9 +12,6 @@ * 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 AclLiteImageProc.cpp -* Description: handle dvpp process */ #include @@ -24,10 +21,10 @@ using namespace std; JpegDHelper::JpegDHelper(aclrtStream& stream, acldvppChannelDesc *dvppChannelDesc): - stream_(stream), - decodeOutBufferDev_(nullptr), - decodeOutputDesc_(nullptr), - dvppChannelDesc_(dvppChannelDesc) { + g_stream_(stream), + g_decodeOutBufferDev_(nullptr), + g_decodeOutputDesc_(nullptr), + g_dvppChannelDesc_(dvppChannelDesc) { } JpegDHelper::~JpegDHelper() { @@ -46,25 +43,25 @@ AclLiteError JpegDHelper::InitDecodeOutputDesc(ImageData& inputImage) { uint32_t decodeOutBufferSize = YUV420SP_SIZE(decodeOutWidthStride, decodeOutHeightStride); - aclError aclRet = acldvppMalloc(&decodeOutBufferDev_, decodeOutBufferSize); + aclError aclRet = acldvppMalloc(&g_decodeOutBufferDev_, decodeOutBufferSize); if (aclRet != ACL_SUCCESS) { ACLLITE_LOG_ERROR("Malloc dvpp memory failed, error:%d", aclRet); return ACLLITE_ERROR_MALLOC_DVPP; } - decodeOutputDesc_ = acldvppCreatePicDesc(); - if (decodeOutputDesc_ == nullptr) { + g_decodeOutputDesc_ = acldvppCreatePicDesc(); + if (g_decodeOutputDesc_ == nullptr) { ACLLITE_LOG_ERROR("Create dvpp pic desc failed"); return ACLLITE_ERROR_CREATE_PIC_DESC; } - acldvppSetPicDescData(decodeOutputDesc_, decodeOutBufferDev_); - acldvppSetPicDescFormat(decodeOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); - acldvppSetPicDescWidth(decodeOutputDesc_, inputImage.width); - acldvppSetPicDescHeight(decodeOutputDesc_, inputImage.height); - acldvppSetPicDescWidthStride(decodeOutputDesc_, decodeOutWidthStride); - acldvppSetPicDescHeightStride(decodeOutputDesc_, decodeOutHeightStride); - acldvppSetPicDescSize(decodeOutputDesc_, decodeOutBufferSize); + acldvppSetPicDescData(g_decodeOutputDesc_, g_decodeOutBufferDev_); + acldvppSetPicDescFormat(g_decodeOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); + acldvppSetPicDescWidth(g_decodeOutputDesc_, inputImage.width); + acldvppSetPicDescHeight(g_decodeOutputDesc_, inputImage.height); + acldvppSetPicDescWidthStride(g_decodeOutputDesc_, decodeOutWidthStride); + acldvppSetPicDescHeightStride(g_decodeOutputDesc_, decodeOutHeightStride); + acldvppSetPicDescSize(g_decodeOutputDesc_, decodeOutBufferSize); return ACLLITE_OK; } @@ -76,15 +73,15 @@ AclLiteError JpegDHelper::Process(ImageData& dest, ImageData& src) { return ret; } - aclError aclRet = acldvppJpegDecodeAsync(dvppChannelDesc_, + aclError aclRet = acldvppJpegDecodeAsync(g_dvppChannelDesc_, reinterpret_cast(src.data.get()), - src.size, decodeOutputDesc_, stream_); + src.size, g_decodeOutputDesc_, g_stream_); if (aclRet != ACL_SUCCESS) { ACLLITE_LOG_ERROR("acldvppJpegDecodeAsync failed, error: %d", aclRet); return ACLLITE_ERROR_JPEGD_ASYNC; } - aclRet = aclrtSynchronizeStream(stream_); + aclRet = aclrtSynchronizeStream(g_stream_); if (aclRet != ACL_SUCCESS) { ACLLITE_LOG_ERROR("Sync stream failed, error: %d", aclRet); return ACLLITE_ERROR_SYNC_STREAM; @@ -95,14 +92,14 @@ AclLiteError JpegDHelper::Process(ImageData& dest, ImageData& src) { dest.alignWidth = ALIGN_UP128(src.width); dest.alignHeight = ALIGN_UP16(src.height); dest.size = YUV420SP_SIZE(dest.alignWidth, dest.alignHeight); - dest.data = SHARED_PTR_DVPP_BUF(decodeOutBufferDev_); + dest.data = SHARED_PTR_DVPP_BUF(g_decodeOutBufferDev_); return ACLLITE_OK; } void JpegDHelper::DestroyDecodeResource() { - if (decodeOutputDesc_ != nullptr) { - acldvppDestroyPicDesc(decodeOutputDesc_); - decodeOutputDesc_ = nullptr; + if (g_decodeOutputDesc_ != nullptr) { + acldvppDestroyPicDesc(g_decodeOutputDesc_); + g_decodeOutputDesc_ = nullptr; } } \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/ResizeHelper.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/ResizeHelper.cpp index fc7126495..035a4617e 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/ResizeHelper.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/ResizeHelper.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,9 +12,6 @@ * 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 AclLiteImageProc.cpp -* Description: handle dvpp process */ #include @@ -26,16 +23,16 @@ using namespace std; ResizeHelper::ResizeHelper(aclrtStream& stream, acldvppChannelDesc *dvppChannelDesc, uint32_t width, uint32_t height): - stream_(stream), - vpcOutBufferDev_(nullptr), - vpcInputDesc_(nullptr), - vpcOutputDesc_(nullptr), - resizeConfig_(nullptr), - dvppChannelDesc_(dvppChannelDesc), - inDevBuffer_(nullptr), - vpcOutBufferSize_(0) { - size_.width = width; - size_.height = height; + g_stream_(stream), + g_vpcOutBufferDev_(nullptr), + g_vpcInputDesc_(nullptr), + g_vpcOutputDesc_(nullptr), + g_resizeConfig_(nullptr), + g_dvppChannelDesc_(dvppChannelDesc), + g_inDevBuffer_(nullptr), + g_vpcOutBufferSize_(0) { + g_size_.width = width; + g_size_.height = height; } ResizeHelper::~ResizeHelper() { @@ -52,62 +49,62 @@ AclLiteError ResizeHelper::InitResizeInputDesc(ImageData& inputImage) { } uint32_t inputBufferSize = YUV420SP_SIZE(alignWidth, alignHeight); - vpcInputDesc_ = acldvppCreatePicDesc(); - if (vpcInputDesc_ == nullptr) { + g_vpcInputDesc_ = acldvppCreatePicDesc(); + if (g_vpcInputDesc_ == nullptr) { ACLLITE_LOG_ERROR("Create dvpp pic desc failed"); return ACLLITE_ERROR_CREATE_PIC_DESC; } - acldvppSetPicDescData(vpcInputDesc_, inputImage.data.get()); - acldvppSetPicDescFormat(vpcInputDesc_, inputImage.format); - acldvppSetPicDescWidth(vpcInputDesc_, inputImage.width); - acldvppSetPicDescHeight(vpcInputDesc_, inputImage.height); - acldvppSetPicDescWidthStride(vpcInputDesc_, alignWidth); - acldvppSetPicDescHeightStride(vpcInputDesc_, alignHeight); - acldvppSetPicDescSize(vpcInputDesc_, inputBufferSize); + acldvppSetPicDescData(g_vpcInputDesc_, inputImage.data.get()); + acldvppSetPicDescFormat(g_vpcInputDesc_, inputImage.format); + acldvppSetPicDescWidth(g_vpcInputDesc_, inputImage.width); + acldvppSetPicDescHeight(g_vpcInputDesc_, inputImage.height); + acldvppSetPicDescWidthStride(g_vpcInputDesc_, alignWidth); + acldvppSetPicDescHeightStride(g_vpcInputDesc_, alignHeight); + acldvppSetPicDescSize(g_vpcInputDesc_, inputBufferSize); return ACLLITE_OK; } AclLiteError ResizeHelper::InitResizeOutputDesc() { - int resizeOutWidth = size_.width; - int resizeOutHeight = size_.height; + int resizeOutWidth = g_size_.width; + int resizeOutHeight = g_size_.height; int resizeOutWidthStride = ALIGN_UP16(resizeOutWidth); int resizeOutHeightStride = ALIGN_UP2(resizeOutHeight); if (resizeOutWidthStride == 0 || resizeOutHeightStride == 0) { ACLLITE_LOG_ERROR("Align resize width(%d) and height(%d) failed", - size_.width, size_.height); + g_size_.width, g_size_.height); return ACLLITE_ERROR_INVALID_ARGS; } - vpcOutBufferSize_ = YUV420SP_SIZE(resizeOutWidthStride, resizeOutHeightStride); - aclError aclRet = acldvppMalloc(&vpcOutBufferDev_, vpcOutBufferSize_); + g_vpcOutBufferSize_ = YUV420SP_SIZE(resizeOutWidthStride, resizeOutHeightStride); + aclError aclRet = acldvppMalloc(&g_vpcOutBufferDev_, g_vpcOutBufferSize_); if (aclRet != ACL_SUCCESS) { ACLLITE_LOG_ERROR("Dvpp resize malloc output buffer failed, " - "size %d, error %d", vpcOutBufferSize_, aclRet); + "size %d, error %d", g_vpcOutBufferSize_, aclRet); return ACLLITE_ERROR_MALLOC_DVPP; } - vpcOutputDesc_ = acldvppCreatePicDesc(); - if (vpcOutputDesc_ == nullptr) { - ACLLITE_LOG_ERROR("acldvppCreatePicDesc vpcOutputDesc_ failed"); + g_vpcOutputDesc_ = acldvppCreatePicDesc(); + if (g_vpcOutputDesc_ == nullptr) { + ACLLITE_LOG_ERROR("acldvppCreatePicDesc g_vpcOutputDesc_ failed"); return ACLLITE_ERROR_CREATE_PIC_DESC; } - acldvppSetPicDescData(vpcOutputDesc_, vpcOutBufferDev_); - acldvppSetPicDescFormat(vpcOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); - acldvppSetPicDescWidth(vpcOutputDesc_, resizeOutWidth); - acldvppSetPicDescHeight(vpcOutputDesc_, resizeOutHeight); - acldvppSetPicDescWidthStride(vpcOutputDesc_, resizeOutWidthStride); - acldvppSetPicDescHeightStride(vpcOutputDesc_, resizeOutHeightStride); - acldvppSetPicDescSize(vpcOutputDesc_, vpcOutBufferSize_); + acldvppSetPicDescData(g_vpcOutputDesc_, g_vpcOutBufferDev_); + acldvppSetPicDescFormat(g_vpcOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); + acldvppSetPicDescWidth(g_vpcOutputDesc_, resizeOutWidth); + acldvppSetPicDescHeight(g_vpcOutputDesc_, resizeOutHeight); + acldvppSetPicDescWidthStride(g_vpcOutputDesc_, resizeOutWidthStride); + acldvppSetPicDescHeightStride(g_vpcOutputDesc_, resizeOutHeightStride); + acldvppSetPicDescSize(g_vpcOutputDesc_, g_vpcOutBufferSize_); return ACLLITE_OK; } AclLiteError ResizeHelper::InitResizeResource(ImageData& inputImage) { - resizeConfig_ = acldvppCreateResizeConfig(); - if (resizeConfig_ == nullptr) { + g_resizeConfig_ = acldvppCreateResizeConfig(); + if (g_resizeConfig_ == nullptr) { ACLLITE_LOG_ERROR("Dvpp resize init failed for create config failed"); return ACLLITE_ERROR_CREATE_RESIZE_CONFIG; } @@ -135,25 +132,25 @@ AclLiteError ResizeHelper::Process(ImageData& resizedImage, ImageData& srcImage) } // resize pic - aclError aclRet = acldvppVpcResizeAsync(dvppChannelDesc_, vpcInputDesc_, - vpcOutputDesc_, resizeConfig_, stream_); + aclError aclRet = acldvppVpcResizeAsync(g_dvppChannelDesc_, g_vpcInputDesc_, + g_vpcOutputDesc_, g_resizeConfig_, g_stream_); if (aclRet != ACL_SUCCESS) { ACLLITE_LOG_ERROR("acldvppVpcResizeAsync failed, error: %d", aclRet); return ACLLITE_ERROR_RESIZE_ASYNC; } - aclRet = aclrtSynchronizeStream(stream_); + aclRet = aclrtSynchronizeStream(g_stream_); if (aclRet != ACL_SUCCESS) { ACLLITE_LOG_ERROR("resize aclrtSynchronizeStream failed, error: %d", aclRet); return ACLLITE_ERROR_SYNC_STREAM; } resizedImage.format = PIXEL_FORMAT_YUV_SEMIPLANAR_420; - resizedImage.width = size_.width; - resizedImage.height = size_.height; - resizedImage.alignWidth = ALIGN_UP16(size_.width); - resizedImage.alignHeight = ALIGN_UP2(size_.height); - resizedImage.size = vpcOutBufferSize_; - resizedImage.data = SHARED_PTR_DVPP_BUF(vpcOutBufferDev_); + resizedImage.width = g_size_.width; + resizedImage.height = g_size_.height; + resizedImage.alignWidth = ALIGN_UP16(g_size_.width); + resizedImage.alignHeight = ALIGN_UP2(g_size_.height); + resizedImage.size = g_vpcOutBufferSize_; + resizedImage.data = SHARED_PTR_DVPP_BUF(g_vpcOutBufferDev_); DestroyResizeResource(); @@ -161,18 +158,18 @@ AclLiteError ResizeHelper::Process(ImageData& resizedImage, ImageData& srcImage) } void ResizeHelper::DestroyResizeResource() { - if (resizeConfig_ != nullptr) { - (void)acldvppDestroyResizeConfig(resizeConfig_); - resizeConfig_ = nullptr; + if (g_resizeConfig_ != nullptr) { + (void)acldvppDestroyResizeConfig(g_resizeConfig_); + g_resizeConfig_ = nullptr; } - if (vpcInputDesc_ != nullptr) { - (void)acldvppDestroyPicDesc(vpcInputDesc_); - vpcInputDesc_ = nullptr; + if (g_vpcInputDesc_ != nullptr) { + (void)acldvppDestroyPicDesc(g_vpcInputDesc_); + g_vpcInputDesc_ = nullptr; } - if (vpcOutputDesc_ != nullptr) { - (void)acldvppDestroyPicDesc(vpcOutputDesc_); - vpcOutputDesc_ = nullptr; + if (g_vpcOutputDesc_ != nullptr) { + (void)acldvppDestroyPicDesc(g_vpcOutputDesc_); + g_vpcOutputDesc_ = nullptr; } } diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/color_classify.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/color_classify.cpp index ae12855c0..697a1c8c7 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/color_classify.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/color_classify.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,8 @@ * 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 color_classify.cpp -* Description: handle acl resource */ + #include "color_classify.h" #include #include @@ -27,29 +25,29 @@ using namespace std; namespace { - const int32_t kBatch = 10; - const int kInvalidSize = -1; - const uint32_t kLineSolid = 2; - const uint32_t kLabelOffset = 11; - const uint32_t kEachResultTensorNum = 9; - const double kFountScale = 0.5; - const cv::Scalar kFontColor(0, 0, 255); - const vector kColors{ + const int32_t g_Batch = 10; + const int g_InvalidSize = -1; + const uint32_t g_LineSolid = 2; + const uint32_t g_LabelOffset = 11; + const uint32_t g_EachResultTensorNum = 9; + const double g_FountScale = 0.5; + const cv::Scalar g_FontColor(0, 0, 255); + const vector g_Colors { cv::Scalar(237, 149, 100), cv::Scalar(0, 215, 255), cv::Scalar(50, 205, 50), cv::Scalar(139, 85, 26) }; - const string kCarColorClass[9] = { "black", "blue", "brown", "green", "pink", "red", + const string g_CarColorClass[9] = { "black", "blue", "brown", "green", "pink", "red", "silver", "white", "yellow"}; - uint32_t kModelWidth = 224; - uint32_t kModelHeight = 224; - const char* kModelPath = "../model/color_dvpp_10batch.om"; + uint32_t g_ModelWidth = 224; + uint32_t g_ModelHeight = 224; + const char* g_ModelPath = "../model/color_dvpp_10batch.om"; } ColorClassify::ColorClassify(): -model_(kModelPath), -batchSize_(kBatch), -isInited_(false), -isReleased_(false){ +g_model_(g_ModelPath), +g_batchSize_(g_Batch), +g_isInited_(false), +g_isReleased_(false) { } ColorClassify::~ColorClassify() { @@ -57,34 +55,33 @@ ColorClassify::~ColorClassify() { } AclLiteError ColorClassify::InitModelInput() { - aclError aclRet = aclrtGetRunMode(&runMode_); - inputSize_ = YUV420SP_SIZE(kModelWidth, kModelHeight) * batchSize_; + aclError aclRet = aclrtGetRunMode(&g_runMode_); + g_inputSize_ = YUV420SP_SIZE(g_ModelWidth, g_ModelHeight) * g_batchSize_; void* buf = nullptr; - aclRet = aclrtMalloc(&buf, inputSize_, ACL_MEM_MALLOC_HUGE_FIRST); + aclRet = aclrtMalloc(&buf, g_inputSize_, ACL_MEM_MALLOC_HUGE_FIRST); if ((buf == nullptr) || (aclRet != ACL_ERROR_NONE)) { ACLLITE_LOG_ERROR("Malloc inference input buffer failed, " "error %d", aclRet); return ACLLITE_ERROR; } - inputBuf_ = (uint8_t *)buf; + g_inputBuf_ = (uint8_t *)buf; return ACLLITE_OK; } - AclLiteError ColorClassify::Init() { - if (isInited_) { + if (g_isInited_) { ACLLITE_LOG_INFO("Object detection is initied already"); return ACLLITE_OK; } - AclLiteError aclRet = dvpp_.Init(); + AclLiteError aclRet = g_dvpp_.Init(); if (aclRet) { ACLLITE_LOG_ERROR("Dvpp init failed, error %d", aclRet); return ACLLITE_ERROR; } - aclRet = model_.Init(); + aclRet = g_model_.Init(); if (aclRet) { ACLLITE_LOG_ERROR("Model init failed, error %d", aclRet); return ACLLITE_ERROR; @@ -96,7 +93,7 @@ AclLiteError ColorClassify::Init() { return ACLLITE_ERROR; } - isInited_ = true; + g_isInited_ = true; return ACLLITE_OK; } @@ -104,7 +101,7 @@ AclLiteError ColorClassify::Crop(vector &carInfo, ImageData &orgImg) { static int cnt = 0; AclLiteError ret = ACLLITE_OK; for (int i = 0; i < carInfo.size(); i++) { - ret = dvpp_.Crop(carInfo[i].cropedImgs, orgImg, + ret = g_dvpp_.Crop(carInfo[i].cropedImgs, orgImg, carInfo[i].rectangle.lt.x, carInfo[i].rectangle.lt.y, carInfo[i].rectangle.rb.x, carInfo[i].rectangle.rb.y); if (ret) { @@ -124,8 +121,8 @@ AclLiteError ColorClassify::Crop(vector &carInfo, ImageData &orgImg) { AclLiteError ColorClassify::Resize(vector &carInfo) { AclLiteError ret = ACLLITE_OK; for (size_t i = 0; i < carInfo.size(); i++) { - AclLiteError ret = dvpp_.Resize(carInfo[i].resizedImgs, carInfo[i].cropedImgs, - kModelWidth, kModelHeight); + AclLiteError ret = g_dvpp_.Resize(carInfo[i].resizedImgs, carInfo[i].cropedImgs, + g_ModelWidth, g_ModelHeight); if (ret) { ACLLITE_LOG_ERROR("ColorClassify Resize image failed"); return ACLLITE_ERROR; @@ -142,26 +139,26 @@ int ColorClassify::CopyOneBatchImages(uint8_t* buffer, uint32_t bufferSize, int dataLen = 0; int totalSize = 0; - for (uint32_t i = batchIdx * batchSize_; - i < carInfo.size() && j < batchSize_ && bufferSize > totalSize; + for (uint32_t i = batchIdx * g_batchSize_; + i < carInfo.size() && j < g_batchSize_ && bufferSize > totalSize; i++, j++) { dataLen = CopyImageData(buffer + totalSize, bufferSize - totalSize, carInfo[i].resizedImgs); - if (dataLen == kInvalidSize) { - return kInvalidSize; + if (dataLen == g_InvalidSize) { + return g_InvalidSize; } totalSize += dataLen; } - if (j < batchSize_) { + if (j < g_batchSize_) { for (uint32_t k = 0; - k < batchSize_ - j && bufferSize > totalSize; + k < g_batchSize_ - j && bufferSize > totalSize; k++) { dataLen = CopyImageData(buffer + totalSize, bufferSize - totalSize, carInfo[carInfo.size() - 1].resizedImgs); - if (dataLen == kInvalidSize) { - return kInvalidSize; + if (dataLen == g_InvalidSize) { + return g_InvalidSize; } totalSize += dataLen; } @@ -173,18 +170,18 @@ int ColorClassify::CopyOneBatchImages(uint8_t* buffer, uint32_t bufferSize, int ColorClassify::CopyImageData(uint8_t *buffer, uint32_t bufferSize, ImageData& image) { AclLiteError ret = ACLLITE_OK; - uint32_t dataSize = YUV420SP_SIZE(kModelWidth, kModelHeight); - ret = CopyDataToDeviceEx(buffer, bufferSize, image.data.get(), dataSize, runMode_); + uint32_t dataSize = YUV420SP_SIZE(g_ModelWidth, g_ModelHeight); + ret = CopyDataToDeviceEx(buffer, bufferSize, image.data.get(), dataSize, g_runMode_); if (ret) { ACLLITE_LOG_ERROR("Copy data to device failed"); - return kInvalidSize; + return g_InvalidSize; } return dataSize; } -AclLiteError ColorClassify::Preprocess(ImageData& srcImage, vector &carInfo, int& flag) { - //No car detected +AclLiteError ColorClassify::PreProcess(ImageData& srcImage, vector &carInfo, int& flag) { + // No car detected if (carInfo.size() == 0) { flag = 1; ACLLITE_LOG_INFO("No car detected"); @@ -192,7 +189,7 @@ AclLiteError ColorClassify::Preprocess(ImageData& srcImage, vector &car } ImageData imageDevice; - AclLiteError ret = CopyImageToDevice(imageDevice, srcImage, runMode_, MEMORY_DVPP); + AclLiteError ret = CopyImageToDevice(imageDevice, srcImage, g_runMode_, MEMORY_DVPP); if (ret == ACLLITE_ERROR) { ACLLITE_LOG_ERROR("Copy image to device failed"); return ACLLITE_ERROR; @@ -214,19 +211,19 @@ AclLiteError ColorClassify::Inference(vector &carInfo, std::vector& inferenceOutput) { int carInfoSize = carInfo.size(); - int batchNum = max(carInfoSize, batchSize_) / batchSize_; + int batchNum = max(carInfoSize, g_batchSize_) / g_batchSize_; for (int i = 0; i < batchNum; i++) { - //Copy one batch preprocessed image data to device - int carNum = CopyOneBatchImages(inputBuf_, inputSize_, + // Copy one batch preprocessed image data to device + int carNum = CopyOneBatchImages(g_inputBuf_, g_inputSize_, carInfo, i); if (carNum < 0) { ACLLITE_LOG_ERROR("Copy the %dth batch images failed", i); break; } - //Inference one batch data - AclLiteError ret = model_.Execute(inferenceOutput, - inputBuf_, inputSize_); + // Inference one batch data + AclLiteError ret = g_model_.Execute(inferenceOutput, + g_inputBuf_, g_inputSize_); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Execute model inference failed\n"); break; @@ -236,7 +233,7 @@ AclLiteError ColorClassify::Inference(vector &carInfo, return ACLLITE_OK; } -AclLiteError ColorClassify::Postprocess(std::vector& inferenceOutput, +AclLiteError ColorClassify::PostProcess(std::vector& inferenceOutput, std::vector& carInfo, const string& origImagePath) { void* data = (void *)inferenceOutput[0].data.get(); @@ -246,16 +243,16 @@ AclLiteError ColorClassify::Postprocess(std::vector& inferenceO float* outData = NULL; outData = reinterpret_cast(data); - for(int i = 0; i < carInfo.size(); i++){ - int maxConfidentIndex = i * kEachResultTensorNum; - for(int j = 0; j < kEachResultTensorNum; j++){ - int index = i * kEachResultTensorNum + j; - if(outData[index] > outData[maxConfidentIndex]){ + for (int i = 0; i < carInfo.size(); i++) { + int maxConfidentIndex = i * g_EachResultTensorNum; + for (int j = 0; j < g_EachResultTensorNum; j++) { + int index = i * g_EachResultTensorNum + j; + if (outData[index] > outData[maxConfidentIndex]) { maxConfidentIndex = index; } } - int colorIndex = maxConfidentIndex - i * kEachResultTensorNum; - carInfo[i].carColor_result = kCarColorClass[colorIndex]; + int colorIndex = maxConfidentIndex - i * g_EachResultTensorNum; + carInfo[i].carColor_result = g_CarColorClass[colorIndex]; } DrawResult(carInfo, origImagePath); } @@ -266,36 +263,36 @@ void ColorClassify::DrawResult(vector& carInfo, cv::Mat origImage = cv::imread(origImagePath, CV_LOAD_IMAGE_UNCHANGED); for (int i = 0; i < carInfo.size(); ++i) { cv::rectangle(origImage, carInfo[i].rectangle.lt, carInfo[i].rectangle.rb, - kColors[i % kColors.size()], kLineSolid); - cv::putText(origImage, carInfo[i].text, cv::Point(carInfo[i].rectangle.lt.x - kLabelOffset, - carInfo[i].rectangle.lt.y - kLabelOffset), - cv::FONT_HERSHEY_COMPLEX, kFountScale, kFontColor); + g_Colors[i % g_Colors.size()], g_LineSolid); + cv::putText(origImage, carInfo[i].text, cv::Point(carInfo[i].rectangle.lt.x - g_LabelOffset, + carInfo[i].rectangle.lt.y - g_LabelOffset), + cv::FONT_HERSHEY_COMPLEX, g_FountScale, g_FontColor); cv::putText(origImage, carInfo[i].carColor_result, cv::Point(carInfo[i].rectangle.lt.x, - carInfo[i].rectangle.lt.y + kLabelOffset), - cv::FONT_HERSHEY_COMPLEX, kFountScale, kFontColor); + carInfo[i].rectangle.lt.y + g_LabelOffset), + cv::FONT_HERSHEY_COMPLEX, g_FountScale, g_FontColor); ACLLITE_LOG_INFO("%d %d %d %d %s", carInfo[i].rectangle.lt.x, carInfo[i].rectangle.lt.y, carInfo[i].rectangle.rb.x, carInfo[i].rectangle.rb.y, carInfo[i].text.c_str()); } int pos = origImagePath.find_last_of("/"); - string filename(origImagePath.substr(pos + 1)); + string fileName(origImagePath.substr(pos + 1)); stringstream sstream; sstream.str(""); - sstream << "./out_" << filename; + sstream << "./out_" << fileName; cv::imwrite(sstream.str(), origImage); } void ColorClassify::DestroyResource() { - if (!isReleased_) { - dvpp_.DestroyResource(); - model_.DestroyResource(); + if (!g_isReleased_) { + g_dvpp_.DestroyResource(); + g_model_.DestroyResource(); - if (inputBuf_) { - aclrtFree(inputBuf_); - inputBuf_ = nullptr; + if (g_inputBuf_) { + aclrtFree(g_inputBuf_); + g_inputBuf_ = nullptr; } - isReleased_ = true; + g_isReleased_ = true; } } diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/main.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/main.cpp index 0fe098318..5893a3445 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/main.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/main.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,8 +12,6 @@ * 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 main.cpp */ #include @@ -29,7 +27,7 @@ using namespace std; int main(int argc, char *argv[]) { - if((argc < 2) || (argv[1] == nullptr)){ + if ((argc < 2) || (argv[1] == nullptr)) { ACLLITE_LOG_ERROR("Please input: ./main "); return ACLLITE_ERROR; } @@ -71,7 +69,7 @@ int main(int argc, char *argv[]) { } ImageData resizedImage, yuvImage; - ret = detect.Preprocess(resizedImage, image, yuvImage); + ret = detect.PreProcess(resizedImage, image, yuvImage); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Read file %s failed, continue to read next", imageFile.c_str()); @@ -84,19 +82,19 @@ int main(int argc, char *argv[]) { return ACLLITE_ERROR; } vector carInfo; - ret = detect.Postprocess(image, detectInferOutput, carInfo); + ret = detect.PostProcess(image, detectInferOutput, carInfo); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Process model inference output data failed"); return ACLLITE_ERROR; } int flag = 0; - ret = classify.Preprocess(yuvImage, carInfo, flag); + ret = classify.PreProcess(yuvImage, carInfo, flag); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Process model inference input data failed"); return ACLLITE_ERROR; } - if(flag ==1) { + if (flag ==1) { continue; } std::vector classifyInferOutput; @@ -106,7 +104,7 @@ int main(int argc, char *argv[]) { return ACLLITE_ERROR; } - ret = classify.Postprocess(classifyInferOutput, carInfo, imageFile); + ret = classify.PostProcess(classifyInferOutput, carInfo, imageFile); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Process model inference output data failed"); return ACLLITE_ERROR; diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/object_detect.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/object_detect.cpp index 22c998367..1bb19c670 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/object_detect.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/object_detect.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,9 +12,8 @@ * 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 object_detect.cpp */ + #include "object_detect.h" #include #include @@ -25,20 +24,20 @@ using namespace std; namespace { -const uint32_t kBBoxDataBufId = 0; -const uint32_t kBoxNumDataBufId = 1; +const uint32_t g_BBoxDataBufId = 0; +const uint32_t g_BoxNumDataBufId = 1; enum BBoxIndex { TOPLEFTX = 0, TOPLEFTY, BOTTOMRIGHTX, BOTTOMRIGHTY, SCORE, LABEL }; -uint32_t kModelWidth = 416; -uint32_t kModelHeight = 416; -const char* kModelPath = "../model/yolov3.om"; +uint32_t g_ModelWidth = 416; +uint32_t g_ModelHeight = 416; +const char* g_ModelPath = "../model/yolov3.om"; } ObjectDetect::ObjectDetect(): -model_(kModelPath), -isInited_(false), -isReleased_(false){ - imageInfoSize_ = 0; - imageInfoBuf_ = nullptr; +g_model_(g_ModelPath), +g_isInited_(false), +g_isReleased_(false) { + g_imageInfoSize_ = 0; + g_imageInfoBuf_ = nullptr; } ObjectDetect::~ObjectDetect() { @@ -46,12 +45,12 @@ ObjectDetect::~ObjectDetect() { } AclLiteError ObjectDetect::InitModelInput() { - const float imageInfo[4] = {(float)kModelWidth, (float)kModelHeight, - (float)kModelWidth, (float)kModelHeight}; - imageInfoSize_ = sizeof(imageInfo); - imageInfoBuf_ = CopyDataToDevice((void *)imageInfo, imageInfoSize_, - runMode_, MEMORY_DEVICE); - if (imageInfoBuf_ == nullptr) { + const float imageInfo[4] = {(float)g_ModelWidth, (float)g_ModelHeight, + (float)g_ModelWidth, (float)g_ModelHeight}; + g_imageInfoSize_ = sizeof(imageInfo); + g_imageInfoBuf_ = CopyDataToDevice((void *)imageInfo, g_imageInfoSize_, + g_runMode_, MEMORY_DEVICE); + if (g_imageInfoBuf_ == nullptr) { ACLLITE_LOG_ERROR("Copy image info to device failed"); return ACLLITE_ERROR; } @@ -60,24 +59,24 @@ AclLiteError ObjectDetect::InitModelInput() { } AclLiteError ObjectDetect::Init() { - if (isInited_) { + if (g_isInited_) { ACLLITE_LOG_INFO("Object detection is initied already"); return ACLLITE_OK; } - AclLiteError atlRet = dvpp_.Init(); + AclLiteError atlRet = g_dvpp_.Init(); if (atlRet) { ACLLITE_LOG_ERROR("Dvpp init failed, error %d", atlRet); return ACLLITE_ERROR; } - atlRet = aclrtGetRunMode(&runMode_); + atlRet = aclrtGetRunMode(&g_runMode_); if (atlRet) { ACLLITE_LOG_WARNING("Get runMode failed, error %d", atlRet); return ACLLITE_ERROR; } - atlRet = model_.Init(); + atlRet = g_model_.Init(); if (atlRet) { ACLLITE_LOG_ERROR("Model init failed, error %d", atlRet); return ACLLITE_ERROR; @@ -88,26 +87,26 @@ AclLiteError ObjectDetect::Init() { ACLLITE_LOG_ERROR("Model Input init failed, error %d", atlRet); return ACLLITE_ERROR; } - isInited_ = true; + g_isInited_ = true; return ACLLITE_OK; } -AclLiteError ObjectDetect::Preprocess(ImageData& resizedImage, ImageData& srcImage, ImageData& yuvImage) { +AclLiteError ObjectDetect::PreProcess(ImageData& resizedImage, ImageData& srcImage, ImageData& yuvImage) { ImageData imageDevice; - AclLiteError ret = CopyImageToDevice(imageDevice, srcImage, runMode_, MEMORY_DVPP); + AclLiteError ret = CopyImageToDevice(imageDevice, srcImage, g_runMode_, MEMORY_DVPP); if (ret == ACLLITE_ERROR) { ACLLITE_LOG_ERROR("Copy image to device failed"); return ACLLITE_ERROR; } - ret = dvpp_.JpegD(yuvImage, imageDevice); + ret = g_dvpp_.JpegD(yuvImage, imageDevice); if (ret == ACLLITE_ERROR) { ACLLITE_LOG_ERROR("Convert jpeg to yuv failed"); return ACLLITE_ERROR; } - ret = dvpp_.CropPaste(resizedImage, yuvImage, kModelWidth, kModelHeight, + ret = g_dvpp_.CropPaste(resizedImage, yuvImage, g_ModelWidth, g_ModelHeight, 0, 0, yuvImage.width, yuvImage.height); if (ret == ACLLITE_ERROR) { ACLLITE_LOG_ERROR("CropResolution image failed"); @@ -119,35 +118,34 @@ AclLiteError ObjectDetect::Preprocess(ImageData& resizedImage, ImageData& srcIma AclLiteError ObjectDetect::Inference(std::vector& inferenceOutput, ImageData& resizedImage) { - AclLiteError ret = model_.CreateInput(resizedImage.data.get(), resizedImage.size, - imageInfoBuf_, imageInfoSize_); + AclLiteError ret = g_model_.CreateInput(resizedImage.data.get(), resizedImage.size, + g_imageInfoBuf_, g_imageInfoSize_); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Create mode input dataset failed"); return ACLLITE_ERROR; } - ret = model_.Execute(inferenceOutput); + ret = g_model_.Execute(inferenceOutput); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Execute model inference failed"); return ACLLITE_ERROR; } - model_.DestroyInput(); + g_model_.DestroyInput(); return ACLLITE_OK; } -AclLiteError ObjectDetect::Postprocess(ImageData& image, std::vector& inferenceOutput, +AclLiteError ObjectDetect::PostProcess(ImageData& image, std::vector& inferenceOutput, std::vector& carData) { - float* detectData = (float *)inferenceOutput[kBBoxDataBufId].data.get(); - if(detectData == nullptr){ + float* detectData = (float *)inferenceOutput[g_BBoxDataBufId].data.get(); + if (detectData == nullptr) { ACLLITE_LOG_ERROR("inferoutput is null\n"); return ACLLITE_ERROR; } - uint32_t* boxNum = (uint32_t *)inferenceOutput[kBoxNumDataBufId].data.get(); + uint32_t* boxNum = (uint32_t *)inferenceOutput[g_BoxNumDataBufId].data.get(); uint32_t totalBox = boxNum[0]; - - float widthScale = (float)(image.width) / kModelWidth; - float heightScale = (float)(image.height) / kModelHeight; + float widthScale = (float)(image.width) / g_ModelWidth; + float heightScale = (float)(image.height) / g_ModelHeight; for (uint32_t i = 0; i < totalBox; i++) { uint32_t score = uint32_t(detectData[totalBox * SCORE + i] * 100); @@ -155,7 +153,7 @@ AclLiteError ObjectDetect::Postprocess(ImageData& image, std::vector @@ -37,16 +38,17 @@ class DvppProcess { void DestroyResource(); protected: - aclrtStream stream_; - aclvencChannelDesc *vencChannelDesc_; - acldvppPicDesc *vpcInputDesc_; - aclvencFrameConfig *vencFrameConfig_; - acldvppStreamDesc *outputStreamDesc; - void *codeInputBufferDev_; - acldvppPixelFormat format_; - int32_t enType_; - aclrtRunMode runMode; - uint32_t inputBufferSize; - pthread_t threadId_; + aclrtStream g_stream_; + aclvencChannelDesc *g_vencChannelDesc_; + acldvppPicDesc *g_vpcInputDesc_; + aclvencFrameConfig *g_vencFrameConfig_; + acldvppStreamDesc *g_outputStreamDesc; + void *g_codeInputBufferDev_; + acldvppPixelFormat g_format_; + int32_t g_enType_; + aclrtRunMode g_runMode; + uint32_t g_inputBufferSize; + pthread_t g_threadId_; }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/inc/model_process.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/inc/model_process.h index e28f9ebab..51216dda0 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/inc/model_process.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/inc/model_process.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 model_process.h -* Description: handle model process */ + +#ifndef YOLOV3_COCO_DETECTION_VENC_INC_MODEL_PROCESS_H +#define YOLOV3_COCO_DETECTION_VENC_INC_MODEL_PROCESS_H + #pragma once #include #include "utils.h" @@ -70,8 +71,8 @@ public: * @param [in] bufferSize: input buffer size * @return result */ - Result CreateInput(void *input1, size_t input1size, - void* input2, size_t input2size); + Result CreateInput(void *input1, size_t input1Size, + void* input2, size_t input2Size); /** * @brief destroy input resource @@ -102,15 +103,16 @@ public: aclmdlDataset *GetModelOutputData(); private: - bool loadFlag_; // model load flag - uint32_t modelId_; - void *modelMemPtr_; - size_t modelMemSize_; - void *modelWeightPtr_; - size_t modelWeightSize_; - aclmdlDesc *modelDesc_; - aclmdlDataset *input_; - aclmdlDataset *output_; - bool isReleased_; + bool g_loadFlag_; // model load flag + uint32_t g_modelId_; + void *g_modelMemPtr_; + size_t g_modelMemSize_; + void *g_modelWeightPtr_; + size_t g_modelWeightSize_; + aclmdlDesc *g_modelDesc_; + aclmdlDataset *g_input_; + aclmdlDataset *g_output_; + bool g_isReleased_; }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/inc/object_detect.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/inc/object_detect.h index 7cdeaa30e..5eee48aee 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/inc/object_detect.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/inc/object_detect.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 YOLOV3_COCO_DETECTION_VENC_INC_OBJECT_DETECT_H +#define YOLOV3_COCO_DETECTION_VENC_INC_OBJECT_DETECT_H + #pragma once #include "utils.h" @@ -34,42 +35,44 @@ public: ObjectDetect(const char* modelPath, uint32_t modelWidth, uint32_t modelHeight); ~ObjectDetect(); - //Inference initialization + // Inference initialization Result Init(int imgWidth, int imgHeight); - //nference frame image preprocessing + // nference frame image preprocessing Result Preprocess(cv::Mat& frame); - //Inference frame picture + // Inference frame picture Result Inference(aclmdlDataset*& inferenceOutput); - //Inference output post-processing + // Inference output post-processing Result Postprocess(cv::Mat& frame, aclmdlDataset* modelOutput); - //void DestroyDvpp(); - //Release the requested resources + // void DestroyDvpp(); + // Release the requested resources void DestroyResource(); private: - //Initializes the ACL resource + // Initializes the ACL resource Result InitResource(); - //Loading reasoning model + // Loading reasoning model Result InitModel(const char* omModelPath); Result CreateModelInputdDataset(); - //Get data from model inference output aclmdlDataset to local + // Get data from model inference output aclmdlDataset to local void* GetInferenceOutputItem(uint32_t& itemDataSize, aclmdlDataset* inferenceOutput, uint32_t idx); Result DrawBoundBoxToImage(vector& detectionResults, cv::Mat& origImg); - int32_t deviceId_; //Device ID, default is 0 - aclrtContext context_; //add - aclrtStream stream_; //add - DvppProcess dvpp_; //add - ModelProcess model_; //Inference model instance + int32_t g_deviceId_; // Device ID, default is 0 + aclrtContext g_context_; // add + aclrtStream g_stream_; // add + DvppProcess g_dvpp_; // add + ModelProcess g_model_; // Inference model instance + + const char* g_modelPath_; // Offline model file path + uint32_t g_modelWidth_; // The input width required by the model + uint32_t g_modelHeight_; // The model requires high input + uint32_t g_imageDataSize_; // Model input data size + void* g_imageDataBuf_; // Model input data cache + uint32_t g_imageInfoSize_; + void* g_imageInfoBuf_; + aclrtRunMode g_runMode_; // Run mode, which is whether the current application is running on atlas200DK or AI1 + bool g_isInited_; // Initializes the tag to prevent inference instances from being initialized multiple times +}; - const char* modelPath_; //Offline model file path - uint32_t modelWidth_; //The input width required by the model - uint32_t modelHeight_; //The model requires high input - uint32_t imageDataSize_; //Model input data size - void* imageDataBuf_; //Model input data cache - uint32_t imageInfoSize_; - void* imageInfoBuf_; - aclrtRunMode runMode_; //Run mode, which is whether the current application is running on atlas200DK or AI1 - bool isInited_; //Initializes the tag to prevent inference instances from being initialized multiple times -}; \ No newline at end of file +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/inc/utils.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/inc/utils.h index 217ea3ad6..bf14b2512 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/inc/utils.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/inc/utils.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 utils.h -* Description: handle file operations */ + +#ifndef YOLOV3_COCO_DETECTION_VENC_INC_UTILS_H +#define YOLOV3_COCO_DETECTION_VENC_INC_UTILS_H + #pragma once #include #include @@ -91,7 +92,8 @@ public: static void* CopyDataDeviceToLocal(void* deviceData, uint32_t dataSize); static void* CopyDataHostToDevice(void* deviceData, uint32_t dataSize); static void* CopyDataDeviceToDevice(void* deviceData, uint32_t dataSize); - //static int ReadImageFile(ImageData& image, std::string fileName); - //static Result CopyImageDataToDevice(ImageData& imageDevice, ImageData srcImage, aclrtRunMode mode); + // static int ReadImageFile(ImageData& image, std::string fileName); + // static Result CopyImageDataToDevice(ImageData& imageDevice, ImageData srcImage, aclrtRunMode mode); }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/dvpp_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/dvpp_process.cpp index e444d82a1..246c70ed8 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/dvpp_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/dvpp_process.cpp @@ -24,22 +24,20 @@ #include "dvpp_process.h" using namespace std; extern FILE *outFileFp; -bool runFlag_= true; -void *ThreadFunc(void *arg) -{ +bool g_runFlag_= true; +void *ThreadFunc(void *arg) { int deviceId = 0; aclrtContext context = nullptr; aclrtCreateContext(&context, deviceId); INFO_LOG("process callback thread start "); - while (runFlag_) { + while (g_runFlag_) { (void)aclrtProcessReport(1000); } aclrtDestroyContext(context); return (void*)0; } -bool WriteToFile(FILE *outFileFp_, const void *dataDev, uint32_t dataSize) -{ +bool WriteToFile(FILE *outFileFp_, const void *dataDev, uint32_t dataSize) { bool ret = true; size_t writeRet = fwrite(dataDev, 1, dataSize, outFileFp_); if (writeRet != dataSize) { @@ -50,23 +48,22 @@ bool WriteToFile(FILE *outFileFp_, const void *dataDev, uint32_t dataSize) return ret; } -void callback(acldvppPicDesc *input, acldvppStreamDesc *outputStreamDesc, void *userdata) -{ +void callback(acldvppPicDesc *input, acldvppStreamDesc *g_outputStreamDesc, void *userdata) { pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); - void *outputDev = acldvppGetStreamDescData(outputStreamDesc); - uint32_t streamDescSize = acldvppGetStreamDescSize(outputStreamDesc); + void *outputDev = acldvppGetStreamDescData(g_outputStreamDesc); + uint32_t streamDescSize = acldvppGetStreamDescSize(g_outputStreamDesc); bool ret; - aclrtRunMode runMode; - aclrtGetRunMode(&runMode); - if (runMode == ACL_HOST) { + aclrtRunMode g_runMode; + aclrtGetRunMode(&g_runMode); + if (g_runMode == ACL_HOST) { void * hostPtr = nullptr; aclrtMallocHost(&hostPtr, streamDescSize); aclrtMemcpy(hostPtr, streamDescSize, outputDev, streamDescSize, ACL_MEMCPY_DEVICE_TO_HOST); ret = WriteToFile(outFileFp, hostPtr, streamDescSize); (void)aclrtFreeHost(hostPtr); } - else{ + else { ret = WriteToFile(outFileFp, outputDev, streamDescSize); } @@ -77,22 +74,22 @@ void callback(acldvppPicDesc *input, acldvppStreamDesc *outputStreamDesc, void * } DvppProcess::DvppProcess() -:vencChannelDesc_(nullptr), enType_(2),vencFrameConfig_(nullptr), -vpcInputDesc_(nullptr), outputStreamDesc(nullptr), codeInputBufferDev_(nullptr){ +:g_vencChannelDesc_(nullptr), g_enType_(2),g_vencFrameConfig_(nullptr), +g_vpcInputDesc_(nullptr), g_outputStreamDesc(nullptr), g_codeInputBufferDev_(nullptr) { } -DvppProcess::~DvppProcess(){ - //DestroyResource(); +DvppProcess::~DvppProcess() { + // DestroyResource(); } -Result DvppProcess::InitResource(aclrtStream& stream, int imgWidth, int imgHeight){ - stream_ = stream; - aclError ret = aclrtGetRunMode(&runMode); +Result DvppProcess::InitResource(aclrtStream& stream, int imgWidth, int imgHeight) { + g_stream_ = stream; + aclError ret = aclrtGetRunMode(&g_runMode); if (ret != ACL_SUCCESS) { ERROR_LOG("acl get run mode failed"); } - int createThreadErr = pthread_create(&threadId_, nullptr, ThreadFunc, nullptr); - (void)aclrtSubscribeReport(static_cast(threadId_), stream_); + int createThreadErr = pthread_create(&g_threadId_, nullptr, ThreadFunc, nullptr); + (void)aclrtSubscribeReport(static_cast(g_threadId_), g_stream_); int width = imgWidth; int height = imgHeight; uint32_t alignWidth = ALIGN_UP128(width); @@ -102,115 +99,115 @@ Result DvppProcess::InitResource(aclrtStream& stream, int imgWidth, int imgHeigh width, height, alignWidth, alignHeight); return FAILED; } - //Allocate a large enough memory - inputBufferSize = YUV420SP_SIZE(alignWidth, alignHeight); - ret = acldvppMalloc(&codeInputBufferDev_, inputBufferSize); + // Allocate a large enough memory + g_inputBufferSize = YUV420SP_SIZE(alignWidth, alignHeight); + ret = acldvppMalloc(&g_codeInputBufferDev_, g_inputBufferSize); if (ret != ACL_SUCCESS) { ERROR_LOG("acldvppMalloc failed"); } - format_ = static_cast(PIXEL_FORMAT_YVU_SEMIPLANAR_420); - vencFrameConfig_ = aclvencCreateFrameConfig(); - aclvencSetFrameConfigForceIFrame(vencFrameConfig_, 0); - if (vencFrameConfig_ == nullptr) { + g_format_ = static_cast(PIXEL_FORMAT_YVU_SEMIPLANAR_420); + g_vencFrameConfig_ = aclvencCreateFrameConfig(); + aclvencSetFrameConfigForceIFrame(g_vencFrameConfig_, 0); + if (g_vencFrameConfig_ == nullptr) { ERROR_LOG("Dvpp init failed for create config failed"); return FAILED; } - vencChannelDesc_ = aclvencCreateChannelDesc(); - if (vencChannelDesc_ == nullptr) { + g_vencChannelDesc_ = aclvencCreateChannelDesc(); + if (g_vencChannelDesc_ == nullptr) { ERROR_LOG("aclvencCreateChannelDesc failed"); return FAILED; } - aclvencSetChannelDescThreadId(vencChannelDesc_, threadId_); - aclvencSetChannelDescCallback(vencChannelDesc_, callback); - aclvencSetChannelDescEnType(vencChannelDesc_, static_cast(enType_)); - aclvencSetChannelDescPicFormat(vencChannelDesc_, format_); - aclvencSetChannelDescKeyFrameInterval(vencChannelDesc_, 1); - aclvencSetChannelDescPicWidth(vencChannelDesc_, width); - aclvencSetChannelDescPicHeight(vencChannelDesc_, height); - aclvencCreateChannel(vencChannelDesc_); - - vpcInputDesc_ = acldvppCreatePicDesc(); - if (vpcInputDesc_ == nullptr) { - ERROR_LOG("acldvppCreatePicDesc vpcInputDesc_ failed"); + aclvencSetChannelDescThreadId(g_vencChannelDesc_, g_threadId_); + aclvencSetChannelDescCallback(g_vencChannelDesc_, callback); + aclvencSetChannelDescEnType(g_vencChannelDesc_, static_cast(g_enType_)); + aclvencSetChannelDescPicFormat(g_vencChannelDesc_, g_format_); + aclvencSetChannelDescKeyFrameInterval(g_vencChannelDesc_, 1); + aclvencSetChannelDescPicWidth(g_vencChannelDesc_, width); + aclvencSetChannelDescPicHeight(g_vencChannelDesc_, height); + aclvencCreateChannel(g_vencChannelDesc_); + + g_vpcInputDesc_ = acldvppCreatePicDesc(); + if (g_vpcInputDesc_ == nullptr) { + ERROR_LOG("acldvppCreatePicDesc g_vpcInputDesc_ failed"); return FAILED; } - acldvppSetPicDescFormat(vpcInputDesc_, format_); - acldvppSetPicDescWidth(vpcInputDesc_, width); - acldvppSetPicDescHeight(vpcInputDesc_, height); - acldvppSetPicDescWidthStride(vpcInputDesc_, alignWidth); - acldvppSetPicDescHeightStride(vpcInputDesc_, alignHeight); + acldvppSetPicDescFormat(g_vpcInputDesc_, g_format_); + acldvppSetPicDescWidth(g_vpcInputDesc_, width); + acldvppSetPicDescHeight(g_vpcInputDesc_, height); + acldvppSetPicDescWidthStride(g_vpcInputDesc_, alignWidth); + acldvppSetPicDescHeightStride(g_vpcInputDesc_, alignHeight); INFO_LOG("dvpp init resource ok"); return SUCCESS; } Result DvppProcess::Venc(cv::Mat& srcImage) { aclError ret; - if(runMode == ACL_HOST) { - ret = aclrtMemcpy(codeInputBufferDev_, inputBufferSize, srcImage.data, inputBufferSize, ACL_MEMCPY_HOST_TO_DEVICE); + if (g_runMode == ACL_HOST) { + ret = aclrtMemcpy(g_codeInputBufferDev_, g_inputBufferSize, srcImage.data, g_inputBufferSize, ACL_MEMCPY_HOST_TO_DEVICE); if (ret != ACL_SUCCESS) { - //ERROR_LOG("aclrtMemcpy failed"); + // ERROR_LOG("aclrtMemcpy failed"); } } else { - ret = aclrtMemcpy(codeInputBufferDev_, inputBufferSize, srcImage.data, inputBufferSize, ACL_MEMCPY_DEVICE_TO_DEVICE); + ret = aclrtMemcpy(g_codeInputBufferDev_, g_inputBufferSize, srcImage.data, g_inputBufferSize, ACL_MEMCPY_DEVICE_TO_DEVICE); if (ret != ACL_SUCCESS) { - //ERROR_LOG("aclrtMemcpy failed"); + // ERROR_LOG("aclrtMemcpy failed"); } } - acldvppSetPicDescData(vpcInputDesc_, codeInputBufferDev_); - acldvppSetPicDescSize(vpcInputDesc_, inputBufferSize); + acldvppSetPicDescData(g_vpcInputDesc_, g_codeInputBufferDev_); + acldvppSetPicDescSize(g_vpcInputDesc_, g_inputBufferSize); - ret = aclvencSendFrame(vencChannelDesc_, vpcInputDesc_, - static_cast(outputStreamDesc), vencFrameConfig_, nullptr); + ret = aclvencSendFrame(g_vencChannelDesc_, g_vpcInputDesc_, + static_cast(g_outputStreamDesc), g_vencFrameConfig_, nullptr); if (ret != ACL_SUCCESS) { ERROR_LOG("aclvencSendFrame failed"); } return SUCCESS; } -void DvppProcess::DestroyResource(){ +void DvppProcess::DestroyResource() { - aclvencSetFrameConfigEos(vencFrameConfig_, 1); - aclvencSetFrameConfigForceIFrame(vencFrameConfig_, 0); - aclvencSendFrame(vencChannelDesc_, nullptr, nullptr, vencFrameConfig_, nullptr); + aclvencSetFrameConfigEos(g_vencFrameConfig_, 1); + aclvencSetFrameConfigForceIFrame(g_vencFrameConfig_, 0); + aclvencSendFrame(g_vencChannelDesc_, nullptr, nullptr, g_vencFrameConfig_, nullptr); - if (vencFrameConfig_ != nullptr) { - (void)aclvencDestroyFrameConfig(vencFrameConfig_); - vencFrameConfig_ = nullptr; + if (g_vencFrameConfig_ != nullptr) { + (void)aclvencDestroyFrameConfig(g_vencFrameConfig_); + g_vencFrameConfig_ = nullptr; } - if (vpcInputDesc_ != nullptr) { - (void)acldvppDestroyPicDesc(vpcInputDesc_); - vpcInputDesc_ = nullptr; + if (g_vpcInputDesc_ != nullptr) { + (void)acldvppDestroyPicDesc(g_vpcInputDesc_); + g_vpcInputDesc_ = nullptr; } - if (codeInputBufferDev_ != nullptr) { - (void)acldvppFree(codeInputBufferDev_); - codeInputBufferDev_ = nullptr; + if (g_codeInputBufferDev_ != nullptr) { + (void)acldvppFree(g_codeInputBufferDev_); + g_codeInputBufferDev_ = nullptr; } - if (outputStreamDesc != nullptr) { - (void)acldvppDestroyStreamDesc(outputStreamDesc); - outputStreamDesc = nullptr; + if (g_outputStreamDesc != nullptr) { + (void)acldvppDestroyStreamDesc(g_outputStreamDesc); + g_outputStreamDesc = nullptr; } aclError aclRet; - if (vencChannelDesc_ != nullptr) { - aclRet = aclvencDestroyChannel(vencChannelDesc_); + if (g_vencChannelDesc_ != nullptr) { + aclRet = aclvencDestroyChannel(g_vencChannelDesc_); if (aclRet != ACL_SUCCESS) { ERROR_LOG("aclvencDestroyChannel failed, aclRet = %d", aclRet); } - (void)aclvencDestroyChannelDesc(vencChannelDesc_); - vencChannelDesc_ = nullptr; + (void)aclvencDestroyChannelDesc(g_vencChannelDesc_); + g_vencChannelDesc_ = nullptr; } - (void)aclrtUnSubscribeReport(static_cast(threadId_), stream_); - runFlag_ = false; + (void)aclrtUnSubscribeReport(static_cast(g_threadId_), g_stream_); + g_runFlag_ = false; void *res = nullptr; - pthread_cancel(threadId_); - int joinThreadErr = pthread_join(threadId_, &res); + pthread_cancel(g_threadId_); + int joinThreadErr = pthread_join(g_threadId_, &res); INFO_LOG("end to destroy DvppProcess"); } \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/main.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/main.cpp index 5c17b7fa8..3239e7e94 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/main.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/main.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,9 +12,6 @@ * 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 main.cpp -* Description: dvpp sample main func */ #include @@ -29,19 +26,19 @@ using namespace std; namespace { - uint32_t kModelWidth = 416; - uint32_t kModelHeight = 416; - const char* kModelPath = "../model/yolov3.om"; + uint32_t g_ModelWidth = 416; + uint32_t g_ModelHeight = 416; + const char* g_ModelPath = "../model/yolov3.om"; } FILE *outFileFp; int main(int argc, char *argv[]) { - //Check the input when the application executes, which takes the path to the input video file - if((argc < 2) || (argv[1] == nullptr)){ + // Check the input when the application executes, which takes the path to the input video file + if ((argc < 2) || (argv[1] == nullptr)) { ERROR_LOG("Please input: ./main "); return FAILED; } - //Use Opencv to open the video file + // Use Opencv to open the video file string videoFile = string(argv[1]); cout << "open" << videoFile.c_str() << endl; cv::VideoCapture capture(videoFile); @@ -54,17 +51,17 @@ int main(int argc, char *argv[]) { int imgHeight = static_cast(capture.get(cv::CAP_PROP_FRAME_HEIGHT)); int imgWidth = static_cast(capture.get(cv::CAP_PROP_FRAME_WIDTH)); - //Instantiate the object detection class - ObjectDetect detect(kModelPath, kModelWidth, kModelHeight); - //Initializes the ACL resource for categorical reasoning, dvpp, loads the model - //and requests the memory used for reasoning input + // Instantiate the object detection class + ObjectDetect detect(g_ModelPath, g_ModelWidth, g_ModelHeight); + // Initializes the ACL resource for categorical reasoning, dvpp, loads the model + // and requests the memory used for reasoning input Result ret = detect.Init(imgWidth, imgHeight); if (ret != SUCCESS) { ERROR_LOG("ObjectDetect Init resource failed"); return FAILED; } - //open the output file + // open the output file string fileName = Utils::getTime(); fileName += ".h264"; stringstream sstream; @@ -73,37 +70,36 @@ int main(int argc, char *argv[]) { string outputPath = sstream.str(); outFileFp = fopen(outputPath.c_str(), "ab"); - if(outFileFp == nullptr) - { + if (outFileFp == nullptr) { ERROR_LOG("Failed to open file %s.", fileName.c_str()); return FAILED; } - //Frame by frame reasoning + // Frame by frame reasoning cv::Mat frame; - while(1) { - //Read a frame of an image + while (1) { + // Read a frame of an image if (!capture.read(frame)) { INFO_LOG("Video capture return false"); - //detect.DestroyDvpp(); + // detect.DestroyDvpp(); detect.DestroyResource(); break; } - //The frame image is preprocessed + // The frame image is preprocessed Result ret = detect.Preprocess(frame); if (ret != SUCCESS) { ERROR_LOG("Read file %s failed, continue to read next", videoFile.c_str()); continue; } - //The preprocessed images are fed into model reasoning and the reasoning results are obtained + // The preprocessed images are fed into model reasoning and the reasoning results are obtained aclmdlDataset* inferenceOutput = nullptr; ret = detect.Inference(inferenceOutput); if ((ret != SUCCESS) || (inferenceOutput == nullptr)) { ERROR_LOG("Inference model inference output data failed"); return FAILED; } - //Parses the inference output + // Parses the inference output ret = detect.Postprocess(frame, inferenceOutput); if (ret != SUCCESS) { ERROR_LOG("Process model inference output data failed"); diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/model_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/model_process.cpp index 545b1ae3c..91698060c 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/model_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/model_process.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,18 +12,16 @@ * 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 model_process.cpp -* Description: handle model process */ + #include "model_process.h" #include #include "utils.h" using namespace std; -ModelProcess::ModelProcess():loadFlag_(false), modelId_(0), modelMemPtr_(nullptr), modelMemSize_(0), -modelWeightPtr_(nullptr),modelWeightSize_(0), modelDesc_(nullptr), input_(nullptr), output_(nullptr), -isReleased_(false){ +ModelProcess::ModelProcess():g_loadFlag_(false), g_modelId_(0), g_modelMemPtr_(nullptr), g_modelMemSize_(0), +g_modelWeightPtr_(nullptr),g_modelWeightSize_(0), g_modelDesc_(nullptr), g_input_(nullptr), g_output_(nullptr), +g_isReleased_(false) { } @@ -32,61 +30,61 @@ ModelProcess::~ModelProcess() { } void ModelProcess::DestroyResource() { - if (isReleased_) + if (g_isReleased_) return; Unload(); DestroyDesc(); DestroyInput(); DestroyOutput(); - isReleased_ = true; + g_isReleased_ = true; } Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath) { - if (loadFlag_) { + if (g_loadFlag_) { ERROR_LOG("has already loaded a model"); return FAILED; } - aclError ret = aclmdlQuerySize(modelPath, &modelMemSize_, &modelWeightSize_); + aclError ret = aclmdlQuerySize(modelPath, &g_modelMemSize_, &g_modelWeightSize_); if (ret != ACL_SUCCESS) { ERROR_LOG("query model failed, model file is %s", modelPath); return FAILED; } - ret = aclrtMalloc(&modelMemPtr_, modelMemSize_, ACL_MEM_MALLOC_HUGE_FIRST); + ret = aclrtMalloc(&g_modelMemPtr_, g_modelMemSize_, ACL_MEM_MALLOC_HUGE_FIRST); if (ret != ACL_SUCCESS) { - ERROR_LOG("malloc buffer for mem failed, require size is %zu", modelMemSize_); + ERROR_LOG("malloc buffer for mem failed, require size is %zu", g_modelMemSize_); return FAILED; } - ret = aclrtMalloc(&modelWeightPtr_, modelWeightSize_, ACL_MEM_MALLOC_HUGE_FIRST); + ret = aclrtMalloc(&g_modelWeightPtr_, g_modelWeightSize_, ACL_MEM_MALLOC_HUGE_FIRST); if (ret != ACL_SUCCESS) { - ERROR_LOG("malloc buffer for weight failed, require size is %zu", modelWeightSize_); + ERROR_LOG("malloc buffer for weight failed, require size is %zu", g_modelWeightSize_); return FAILED; } - ret = aclmdlLoadFromFileWithMem(modelPath, &modelId_, modelMemPtr_, - modelMemSize_, modelWeightPtr_, modelWeightSize_); + ret = aclmdlLoadFromFileWithMem(modelPath, &g_modelId_, g_modelMemPtr_, + g_modelMemSize_, g_modelWeightPtr_, g_modelWeightSize_); if (ret != ACL_SUCCESS) { ERROR_LOG("load model from file failed, model file is %s", modelPath); return FAILED; } - loadFlag_ = true; + g_loadFlag_ = true; INFO_LOG("load model %s success", modelPath); return SUCCESS; } Result ModelProcess::CreateDesc() { - modelDesc_ = aclmdlCreateDesc(); - if (modelDesc_ == nullptr) { + g_modelDesc_ = aclmdlCreateDesc(); + if (g_modelDesc_ == nullptr) { ERROR_LOG("create model description failed"); return FAILED; } - aclError ret = aclmdlGetDesc(modelDesc_, modelId_); + aclError ret = aclmdlGetDesc(g_modelDesc_, g_modelId_); if (ret != ACL_SUCCESS) { ERROR_LOG("get model description failed"); return FAILED; @@ -96,30 +94,28 @@ Result ModelProcess::CreateDesc() return SUCCESS; } -void ModelProcess::DestroyDesc() -{ - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; +void ModelProcess::DestroyDesc() { + if (g_modelDesc_ != nullptr) { + (void)aclmdlDestroyDesc(g_modelDesc_); + g_modelDesc_ = nullptr; } } -Result ModelProcess::CreateInput(void *input1, size_t input1size, - void* input2, size_t input2size) -{ - input_ = aclmdlCreateDataset(); - if (input_ == nullptr) { +Result ModelProcess::CreateInput(void *input1, size_t input1Size, + void* input2, size_t input2Size) { + g_input_ = aclmdlCreateDataset(); + if (g_input_ == nullptr) { ERROR_LOG("can't create dataset, create input failed"); return FAILED; } - aclDataBuffer* inputData = aclCreateDataBuffer(input1, input1size); + aclDataBuffer* inputData = aclCreateDataBuffer(input1, input1Size); if (inputData == nullptr) { ERROR_LOG("can't create data buffer, create input failed"); return FAILED; } - aclError ret = aclmdlAddDatasetBuffer(input_, inputData); + aclError ret = aclmdlAddDatasetBuffer(g_input_, inputData); if (inputData == nullptr) { ERROR_LOG("can't add data buffer, create input failed"); aclDestroyDataBuffer(inputData); @@ -127,13 +123,13 @@ Result ModelProcess::CreateInput(void *input1, size_t input1size, return FAILED; } - aclDataBuffer* inputData2 = aclCreateDataBuffer(input2, input2size); + aclDataBuffer* inputData2 = aclCreateDataBuffer(input2, input2Size); if (inputData == nullptr) { ERROR_LOG("can't create data buffer, create input failed"); return FAILED; } - ret = aclmdlAddDatasetBuffer(input_, inputData2); + ret = aclmdlAddDatasetBuffer(g_input_, inputData2); if (inputData == nullptr) { ERROR_LOG("can't add data buffer, create input failed"); aclDestroyDataBuffer(inputData2); @@ -144,36 +140,34 @@ Result ModelProcess::CreateInput(void *input1, size_t input1size, return SUCCESS; } -void ModelProcess::DestroyInput() -{ - if (input_ == nullptr) { +void ModelProcess::DestroyInput() { + if (g_input_ == nullptr) { return; } - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(input_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(input_, i); + for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(g_input_); ++i) { + aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(g_input_, i); aclDestroyDataBuffer(dataBuffer); } - aclmdlDestroyDataset(input_); - input_ = nullptr; + aclmdlDestroyDataset(g_input_); + g_input_ = nullptr; } -Result ModelProcess::CreateOutput() -{ - if (modelDesc_ == nullptr) { +Result ModelProcess::CreateOutput() { + if (g_modelDesc_ == nullptr) { ERROR_LOG("no model description, create ouput failed"); return FAILED; } - output_ = aclmdlCreateDataset(); - if (output_ == nullptr) { + g_output_ = aclmdlCreateDataset(); + if (g_output_ == nullptr) { ERROR_LOG("can't create dataset, create output failed"); return FAILED; } - size_t outputSize = aclmdlGetNumOutputs(modelDesc_); + size_t outputSize = aclmdlGetNumOutputs(g_modelDesc_); for (size_t i = 0; i < outputSize; ++i) { - size_t buffer_size = aclmdlGetOutputSizeByIndex(modelDesc_, i); + size_t buffer_size = aclmdlGetOutputSizeByIndex(g_modelDesc_, i); void *outputBuffer = nullptr; aclError ret = aclrtMalloc(&outputBuffer, buffer_size, ACL_MEM_MALLOC_NORMAL_ONLY); @@ -189,7 +183,7 @@ Result ModelProcess::CreateOutput() return FAILED; } - ret = aclmdlAddDatasetBuffer(output_, outputData); + ret = aclmdlAddDatasetBuffer(g_output_, outputData); if (ret != ACL_SUCCESS) { ERROR_LOG("can't add data buffer, create output failed"); aclrtFree(outputBuffer); @@ -202,28 +196,26 @@ Result ModelProcess::CreateOutput() return SUCCESS; } -void ModelProcess::DestroyOutput() -{ - if (output_ == nullptr) { +void ModelProcess::DestroyOutput() { + if (g_output_ == nullptr) { return; } - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); + for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(g_output_); ++i) { + aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(g_output_, i); void* data = aclGetDataBufferAddr(dataBuffer); (void)aclrtFree(data); (void)aclDestroyDataBuffer(dataBuffer); } - (void)aclmdlDestroyDataset(output_); - output_ = nullptr; + (void)aclmdlDestroyDataset(g_output_); + g_output_ = nullptr; } -Result ModelProcess::Execute() -{ - aclError ret = aclmdlExecute(modelId_, input_, output_); +Result ModelProcess::Execute() { + aclError ret = aclmdlExecute(g_modelId_, g_input_, g_output_); if (ret != ACL_SUCCESS) { - ERROR_LOG("execute model failed, modelId is %u", modelId_); + ERROR_LOG("execute model failed, modelId is %u", g_modelId_); return FAILED; } @@ -231,42 +223,38 @@ Result ModelProcess::Execute() return SUCCESS; } -void ModelProcess::Unload() -{ - if (!loadFlag_) { +void ModelProcess::Unload() { + if (!g_loadFlag_) { WARN_LOG("no model had been loaded, unload failed"); return; } - aclError ret = aclmdlUnload(modelId_); + aclError ret = aclmdlUnload(g_modelId_); if (ret != ACL_SUCCESS) { - ERROR_LOG("unload model failed, modelId is %u", modelId_); + ERROR_LOG("unload model failed, modelId is %u", g_modelId_); } - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; + if (g_modelDesc_ != nullptr) { + (void)aclmdlDestroyDesc(g_modelDesc_); + g_modelDesc_ = nullptr; } - if (modelMemPtr_ != nullptr) { - aclrtFree(modelMemPtr_); - modelMemPtr_ = nullptr; - modelMemSize_ = 0; + if (g_modelMemPtr_ != nullptr) { + aclrtFree(g_modelMemPtr_); + g_modelMemPtr_ = nullptr; + g_modelMemSize_ = 0; } - if (modelWeightPtr_ != nullptr) { - aclrtFree(modelWeightPtr_); - modelWeightPtr_ = nullptr; - modelWeightSize_ = 0; + if (g_modelWeightPtr_ != nullptr) { + aclrtFree(g_modelWeightPtr_); + g_modelWeightPtr_ = nullptr; + g_modelWeightSize_ = 0; } - loadFlag_ = false; - INFO_LOG("unload model success, modelId is %u", modelId_); -} - -aclmdlDataset *ModelProcess::GetModelOutputData() -{ - return output_; + g_loadFlag_ = false; + INFO_LOG("unload model success, modelId is %u", g_modelId_); } - +aclmdlDataset *ModelProcess::GetModelOutputData() { + return g_output_; +} \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/object_detect.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/object_detect.cpp index c83773c4e..1c3c3f9b6 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/object_detect.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/object_detect.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,8 @@ * 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 "object_detect.h" #include "opencv2/opencv.hpp" @@ -54,7 +52,7 @@ namespace { const cv::Scalar kFontColor(0, 0, 255); const uint32_t kLabelOffset = 11; const string kFileSperator = "/"; - const vector kColors{ + const vector kColors { cv::Scalar(237, 149, 100), cv::Scalar(0, 215, 255), cv::Scalar(50, 205, 50), cv::Scalar(139, 85, 26) }; @@ -62,15 +60,15 @@ namespace { ObjectDetect::ObjectDetect(const char* modelPath, uint32_t modelWidth, uint32_t modelHeight) -:deviceId_(0), context_(nullptr), stream_(nullptr), modelWidth_(modelWidth), -modelHeight_(modelHeight), isInited_(false), imageDataBuf_(nullptr), imageInfoBuf_(nullptr){ - imageInfoSize_ = 0; - modelPath_ = modelPath; - imageDataSize_ = RGBU8_IMAGE_SIZE(modelWidth, modelHeight); +:g_deviceId_(0), g_context_(nullptr), g_stream_(nullptr), g_modelWidth_(modelWidth), +g_modelHeight_(modelHeight), g_isInited_(false), g_imageDataBuf_(nullptr), g_imageInfoBuf_(nullptr) { + g_imageInfoSize_ = 0; + g_modelPath_ = modelPath; + g_imageDataSize_ = RGBU8_IMAGE_SIZE(modelWidth, modelHeight); } ObjectDetect::~ObjectDetect() { - //DestroyResource(); + // DestroyResource(); } Result ObjectDetect::InitResource() { @@ -84,15 +82,15 @@ Result ObjectDetect::InitResource() { INFO_LOG("Acl init success"); // open device - ret = aclrtSetDevice(deviceId_); + ret = aclrtSetDevice(g_deviceId_); if (ret != ACL_SUCCESS) { - ERROR_LOG("Acl open device %d failed", deviceId_); + ERROR_LOG("Acl open device %d failed", g_deviceId_); return FAILED; } - INFO_LOG("Open device %d success", deviceId_); + INFO_LOG("Open device %d success", g_deviceId_); // create context (set current) - ret = aclrtCreateContext(&context_, deviceId_); + ret = aclrtCreateContext(&g_context_, g_deviceId_); if (ret != ACL_SUCCESS) { ERROR_LOG("acl create context failed"); return FAILED; @@ -100,15 +98,15 @@ Result ObjectDetect::InitResource() { INFO_LOG("create context success"); // create stream - ret = aclrtCreateStream(&stream_); + ret = aclrtCreateStream(&g_stream_); if (ret != ACL_SUCCESS) { ERROR_LOG("acl create stream failed"); return FAILED; } INFO_LOG("create stream success"); - //Gets whether the current application is running on host or Device - ret = aclrtGetRunMode(&runMode_); + // Gets whether the current application is running on host or Device + ret = aclrtGetRunMode(&g_runMode_); if (ret != ACL_SUCCESS) { ERROR_LOG("acl get run mode failed"); return FAILED; @@ -118,19 +116,19 @@ Result ObjectDetect::InitResource() { } Result ObjectDetect::InitModel(const char* omModelPath) { - Result ret = model_.LoadModelFromFileWithMem(omModelPath); + Result ret = g_model_.LoadModelFromFileWithMem(omModelPath); if (ret != SUCCESS) { ERROR_LOG("execute LoadModelFromFileWithMem failed"); return FAILED; } - ret = model_.CreateDesc(); + ret = g_model_.CreateDesc(); if (ret != SUCCESS) { ERROR_LOG("execute CreateDesc failed"); return FAILED; } - ret = model_.CreateOutput(); + ret = g_model_.CreateOutput(); if (ret != SUCCESS) { ERROR_LOG("execute CreateOutput failed"); return FAILED; @@ -141,28 +139,28 @@ Result ObjectDetect::InitModel(const char* omModelPath) { Result ObjectDetect::CreateModelInputdDataset() { - //Request image data memory for input model - aclError aclRet = aclrtMalloc(&imageDataBuf_, imageDataSize_, ACL_MEM_MALLOC_HUGE_FIRST); + // Request image data memory for input model + aclError aclRet = aclrtMalloc(&g_imageDataBuf_, g_imageDataSize_, ACL_MEM_MALLOC_HUGE_FIRST); if (aclRet != ACL_SUCCESS) { ERROR_LOG("malloc device data buffer failed, aclRet is %d", aclRet); return FAILED; } - //The second input to Yolov3 is the input image width and height parameter - const float imageInfo[4] = {(float)modelWidth_, (float)modelHeight_, - (float)modelWidth_, (float)modelHeight_}; - imageInfoSize_ = sizeof(imageInfo); - if (runMode_ == ACL_HOST) - imageInfoBuf_ = Utils::CopyDataHostToDevice((void *)imageInfo, imageInfoSize_); - else - imageInfoBuf_ = Utils::CopyDataDeviceToDevice((void *)imageInfo, imageInfoSize_); - if (imageInfoBuf_ == nullptr) { + // The second input to Yolov3 is the input image width and height parameter + const float imageInfo[4] = {(float)g_modelWidth_, (float)g_modelHeight_, + (float)g_modelWidth_, (float)g_modelHeight_}; + g_imageInfoSize_ = sizeof(imageInfo); + if (g_runMode_ == ACL_HOST) + g_imageInfoBuf_ = Utils::CopyDataHostToDevice((void *)imageInfo, g_imageInfoSize_); + else + g_imageInfoBuf_ = Utils::CopyDataDeviceToDevice((void *)imageInfo, g_imageInfoSize_); + if (g_imageInfoBuf_ == nullptr) { ERROR_LOG("Copy image info to device failed"); return FAILED; } - //Use the applied memory to create the model and input dataset. After creation, - //only update the memory data for each frame of inference, instead of creating the input dataset every time - Result ret = model_.CreateInput(imageDataBuf_, imageDataSize_, - imageInfoBuf_, imageInfoSize_); + // Use the applied memory to create the model and input dataset. After creation, + // only update the memory data for each frame of inference, instead of creating the input dataset every time + Result ret = g_model_.CreateInput(g_imageDataBuf_, g_imageDataSize_, + g_imageInfoBuf_, g_imageInfoSize_); if (ret != SUCCESS) { ERROR_LOG("Create mode input dataset failed"); return FAILED; @@ -173,7 +171,7 @@ Result ObjectDetect::CreateModelInputdDataset() Result ObjectDetect::Init(int imgWidth, int imgHeight) { - if (isInited_) { + if (g_isInited_) { INFO_LOG("Object detection instance is initied already!"); return SUCCESS; } @@ -184,13 +182,13 @@ Result ObjectDetect::Init(int imgWidth, int imgHeight) { return FAILED; } - ret = InitModel(modelPath_); + ret = InitModel(g_modelPath_); if (ret != SUCCESS) { ERROR_LOG("Init model failed"); return FAILED; } - ret = dvpp_.InitResource(stream_, imgWidth, imgHeight); + ret = g_dvpp_.InitResource(g_stream_, imgWidth, imgHeight); if (ret != SUCCESS) { ERROR_LOG("Init dvpp failed"); return FAILED; @@ -202,23 +200,23 @@ Result ObjectDetect::Init(int imgWidth, int imgHeight) { return FAILED; } - isInited_ = true; + g_isInited_ = true; return SUCCESS; } Result ObjectDetect::Preprocess(cv::Mat& frame) { - //Scale the frame image to the desired size of the model + // Scale the frame image to the desired size of the model cv::Mat reiszeMat; - cv::resize(frame, reiszeMat, cv::Size(modelWidth_, modelHeight_)); + cv::resize(frame, reiszeMat, cv::Size(g_modelWidth_, g_modelHeight_)); if (reiszeMat.empty()) { ERROR_LOG("Resize image failed"); return FAILED; } - //Copy the data into the cache of the input dataset - aclrtMemcpyKind policy = (runMode_ == ACL_HOST)? + // Copy the data into the cache of the input dataset + aclrtMemcpyKind policy = (g_runMode_ == ACL_HOST)? ACL_MEMCPY_HOST_TO_DEVICE:ACL_MEMCPY_DEVICE_TO_DEVICE; - aclError ret = aclrtMemcpy(imageDataBuf_, imageDataSize_, - reiszeMat.ptr(), imageDataSize_, policy); + aclError ret = aclrtMemcpy(g_imageDataBuf_, g_imageDataSize_, + reiszeMat.ptr(), g_imageDataSize_, policy); if (ret != ACL_SUCCESS) { ERROR_LOG("Copy resized image data to device failed."); return FAILED; @@ -228,17 +226,17 @@ Result ObjectDetect::Preprocess(cv::Mat& frame) { } Result ObjectDetect::Inference(aclmdlDataset*& inferenceOutput) { - Result ret = model_.Execute(); + Result ret = g_model_.Execute(); if (ret != SUCCESS) { ERROR_LOG("Execute model inference failed"); return FAILED; } - inferenceOutput = model_.GetModelOutputData(); + inferenceOutput = g_model_.GetModelOutputData(); return SUCCESS; } -Result ObjectDetect::Postprocess(cv::Mat& frame, aclmdlDataset* modelOutput){ +Result ObjectDetect::Postprocess(cv::Mat& frame, aclmdlDataset* modelOutput) { uint32_t dataSize = 0; float* detectData = (float*)GetInferenceOutputItem(dataSize, modelOutput, kBBoxDataBufId); @@ -250,8 +248,8 @@ Result ObjectDetect::Postprocess(cv::Mat& frame, aclmdlDataset* modelOutput){ uint32_t totalBox = boxNum[0]; vector detectResults; - float widthScale = (float)(frame.cols) / modelWidth_; - float heightScale = (float)(frame.rows) / modelHeight_; + float widthScale = (float)(frame.cols) / g_modelWidth_; + float heightScale = (float)(frame.rows) / g_modelHeight_; for (uint32_t i = 0; i < totalBox; i++) { BBox boundBox; uint32_t score = uint32_t(detectData[totalBox * SCORE + i] * 100); @@ -268,7 +266,7 @@ Result ObjectDetect::Postprocess(cv::Mat& frame, aclmdlDataset* modelOutput){ } DrawBoundBoxToImage(detectResults, frame); - if (runMode_ == ACL_HOST) { + if (g_runMode_ == ACL_HOST) { delete[]((uint8_t*)detectData); delete[]((uint8_t*)boxNum); } @@ -301,7 +299,7 @@ void* ObjectDetect::GetInferenceOutputItem(uint32_t& itemDataSize, } void* data = nullptr; - if (runMode_ == ACL_HOST) { + if (g_runMode_ == ACL_HOST) { data = Utils::CopyDataDeviceToLocal(dataBufferDev, bufferSize); if (data == nullptr) { ERROR_LOG("Copy inference output to host failed"); @@ -331,12 +329,10 @@ Result ObjectDetect::DrawBoundBoxToImage(vector& detectionResults,cv::Mat& int rows = origImg.rows; int Yindex = 0; int UVindex = rows * cols; - cv::Mat NV21(rows+rows/2, cols, CV_8UC1); + cv::Mat NV21(rows + rows / 2, cols, CV_8UC1); int UVRow{ 0 }; - for (int i=0;i(i); int B = origImg.at(i, j)[0]; int G = origImg.at(i, j)[1]; @@ -344,16 +340,15 @@ Result ObjectDetect::DrawBoundBoxToImage(vector& detectionResults,cv::Mat& int Y = (77 * R + 150 * G + 29 * B) >> 8; YPointer[j] = Y; uchar* UVPointer = NV21.ptr(rows+i/2); - if (i%2==0&&(j)%2==0) - { + if (i % 2 == 0 && (j) % 2 == 0) { int U = ((-44 * R - 87 * G + 131 * B) >> 8) + 128; int V = ((131 * R - 110 * G - 21 * B) >> 8) + 128; UVPointer[j] = V; - UVPointer[j+1] = U; + UVPointer[j + 1] = U; } } } - aclError ret = dvpp_.Venc(NV21); + aclError ret = g_dvpp_.Venc(NV21); if (ret == FAILED) { ERROR_LOG("Venc encoding failed"); return FAILED; @@ -361,38 +356,37 @@ Result ObjectDetect::DrawBoundBoxToImage(vector& detectionResults,cv::Mat& return SUCCESS; } -void ObjectDetect::DestroyResource() -{ - aclrtFree(imageDataBuf_); - aclrtFree(imageInfoBuf_); +void ObjectDetect::DestroyResource() { + aclrtFree(g_imageDataBuf_); + aclrtFree(g_imageInfoBuf_); - //The ACL resource held by the model instance must be released - //before the ACL exits or ABORT will be torn down - dvpp_.DestroyResource(); - model_.DestroyResource(); + // The ACL resource held by the model instance must be released + // before the ACL exits or ABORT will be torn down + g_dvpp_.DestroyResource(); + g_model_.DestroyResource(); aclError ret; - if (stream_ != nullptr) { - ret = aclrtDestroyStream(stream_); + if (g_stream_ != nullptr) { + ret = aclrtDestroyStream(g_stream_); if (ret != ACL_SUCCESS) { ERROR_LOG("destroy stream failed"); } - stream_ = nullptr; + g_stream_ = nullptr; } INFO_LOG("end to destroy stream"); - if (context_ != nullptr) { - ret = aclrtDestroyContext(context_); + if (g_context_ != nullptr) { + ret = aclrtDestroyContext(g_context_); if (ret != ACL_SUCCESS) { ERROR_LOG("destroy context failed"); } - context_ = nullptr; + g_context_ = nullptr; } INFO_LOG("end to destroy context"); - ret = aclrtResetDevice(deviceId_); + ret = aclrtResetDevice(g_deviceId_); if (ret != ACL_SUCCESS) { ERROR_LOG("reset device failed"); } - INFO_LOG("end to reset device is %d", deviceId_); + INFO_LOG("end to reset device is %d", g_deviceId_); ret = aclFinalize(); if (ret != ACL_SUCCESS) { diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/utils.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/utils.cpp index 1a9f20025..318d3b5c6 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/utils.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/utils.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,8 @@ * 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 utils.cpp -* Description: handle file operations */ + #include "utils.h" #include #include @@ -33,15 +31,15 @@ using namespace std; namespace { -const std::string kImagePathSeparator = ","; -const int kStatSuccess = 0; -const std::string kFileSperator = "/"; -const std::string kPathSeparator = "/"; -const std::string kOutputFilePrefix = "out_"; +const std::string g_ImagePathSeparator = ","; +const int g_StatSuccess = 0; +const std::string g_FileSperator = "/"; +const std::string g_PathSeparator = "/"; +const std::string g_OutputFilePrefix = "out_"; } -string Utils::getTime(){ +string Utils::getTime() { time_t timep; time (& timep); char tmp[64]; @@ -52,7 +50,7 @@ string Utils::getTime(){ bool Utils::IsDirectory(const string &path) { // get path stat struct stat buf; - if (stat(path.c_str(), &buf) != kStatSuccess) { + if (stat(path.c_str(), &buf) != g_StatSuccess) { return false; } @@ -74,7 +72,7 @@ bool Utils::IsPathExist(const string &path) { void Utils::SplitPath(const string &path, vector &path_vec) { char *char_path = const_cast(path.c_str()); - const char *char_split = kImagePathSeparator.c_str(); + const char *char_split = g_ImagePathSeparator.c_str(); char *tmp_path = strtok(char_path, char_split); while (tmp_path) { path_vec.emplace_back(tmp_path); @@ -111,7 +109,7 @@ void Utils::GetPathFiles(const string &path, vector &file_vec) { } // file path - string full_path = path + kPathSeparator + dirent_ptr->d_name; + string full_path = path + g_PathSeparator + dirent_ptr->d_name; // directory need recursion if (IsDirectory(full_path)) { GetPathFiles(full_path, file_vec); diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/dvpp_jpegd.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/dvpp_jpegd.h index 92f5feead..90482db0c 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/dvpp_jpegd.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/dvpp_jpegd.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 dvpp_process.h -* Description: handle dvpp process */ + +#ifndef YOLOV3_COCO_DETECTION_DYNAMIC_AIPP_INC_DVPP_JPEGD_H +#define YOLOV3_COCO_DETECTION_DYNAMIC_AIPP_INC_DVPP_JPEGD_H + #pragma once #include @@ -79,16 +80,17 @@ private: void DestroyResource(); void DestroyOutputPara(); - aclrtStream stream_; - acldvppChannelDesc *dvppChannelDesc_; + aclrtStream g_stream_; + acldvppChannelDesc *g_dvppChannelDesc_; - void* decodeOutBufferDev_; // decode output buffer - acldvppPicDesc *decodeOutputDesc_; //decode output desc + void* g_decodeOutBufferDev_; // decode output buffer + acldvppPicDesc *g_decodeOutputDesc_; //decode output desc - uint8_t *inDevBuffer_; // input pic dev buffer - uint32_t inDevBufferSizeD_; // input pic size for decode + uint8_t *g_inDevBuffer_; // input pic dev buffer + uint32_t g_inDevBufferSizeD_; // input pic size for decode - void *vpcOutBufferDev_; // vpc output buffer - uint32_t vpcOutBufferSize_; // vpc output size + void *g_vpcOutBufferDev_; // vpc output buffer + uint32_t g_vpcOutBufferSize_; // vpc output size }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/dvpp_process.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/dvpp_process.h index aa9d943cd..1f0595f08 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/dvpp_process.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/dvpp_process.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 dvpp_process.h -* Description: handle dvpp process */ + +#ifndef YOLOV3_COCO_DETECTION_DYNAMIC_AIPP_INC_DVPP_PROCESS_H +#define YOLOV3_COCO_DETECTION_DYNAMIC_AIPP_INC_DVPP_PROCESS_H + #pragma once #include @@ -39,7 +40,8 @@ public: void DestroyResource(); protected: - aclrtStream stream_; - acldvppChannelDesc *dvppChannelDesc_; + aclrtStream g_stream_; + acldvppChannelDesc *g_dvppChannelDesc_; }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/dvpp_resize.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/dvpp_resize.h index 404d9b617..8a0483370 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/dvpp_resize.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/dvpp_resize.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 dvpp_process.h -* Description: handle dvpp process */ + +#ifndef YOLOV3_COCO_DETECTION_DYNAMIC_AIPP_INC_DVPP_RESIZE_H +#define YOLOV3_COCO_DETECTION_DYNAMIC_AIPP_INC_DVPP_RESIZE_H + #pragma once #include @@ -74,17 +75,18 @@ private: void DestroyResource(); void DestroyOutputPara(); - aclrtStream stream_; - acldvppChannelDesc *dvppChannelDesc_; - acldvppResizeConfig *resizeConfig_; + aclrtStream g_stream_; + acldvppChannelDesc *g_dvppChannelDesc_; + acldvppResizeConfig *g_resizeConfig_; - acldvppPicDesc *vpcInputDesc_; // vpc input desc - acldvppPicDesc *vpcOutputDesc_; // vpc output desc + acldvppPicDesc *g_vpcInputDesc_; // vpc input desc + acldvppPicDesc *g_vpcOutputDesc_; // vpc output desc - uint8_t *inDevBuffer_; // input pic dev buffer - void *vpcOutBufferDev_; // vpc output buffer - uint32_t vpcOutBufferSize_; // vpc output size - Resolution size_; - acldvppPixelFormat format_; + uint8_t *g_inDevBuffer_; // input pic dev buffer + void *g_vpcOutBufferDev_; // vpc output buffer + uint32_t g_vpcOutBufferSize_; // vpc output size + Resolution g_size_; + acldvppPixelFormat g_format_; }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/model_process.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/model_process.h index bde4a8cdd..03a47b185 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/model_process.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/model_process.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 model_process.h -* Description: handle model process */ + +#ifndef YOLOV3_COCO_DETECTION_DYNAMIC_AIPP_INC_MODEL_PROCESS_H +#define YOLOV3_COCO_DETECTION_DYNAMIC_AIPP_INC_MODEL_PROCESS_H + #pragma once #include #include "utils.h" @@ -94,8 +95,8 @@ class ModelProcess { * @param [in] bufferSize: input buffer size * @return result */ - Result CreateInput(void *input1, uint32_t input1size, - void* input2, uint32_t input2size); + Result CreateInput(void *input1, uint32_t input1Size, + void* input2, uint32_t input2Size); /** * @brief create model input for opencv pic @@ -103,8 +104,8 @@ class ModelProcess { * @param [in] bufferSize: input buffer size * @return result */ - Result CreateInputOpenCV(void* input1, uint32_t input1size, - void* input2, uint32_t input2size); + Result CreateInputOpenCV(void* input1, uint32_t input1Size, + void* input2, uint32_t input2Size); /** * @brief destroy input resource @@ -135,18 +136,19 @@ class ModelProcess { aclmdlDataset *GetModelOutputData(); private: - bool loadFlag_; // model load flag - uint32_t modelId_; - void *modelMemPtr_; - size_t modelMemSize_; - void *modelWeightPtr_; - size_t modelWeightSize_; - aclmdlDesc *modelDesc_; - aclmdlDataset *input_; - aclmdlDataset *output_; - void * inputAIPP_; - uint32_t inputAIPPSize_; - size_t aipp_index_; - bool isReleased_; + bool g_loadFlag_; // model load flag + uint32_t g_modelId_; + void *g_modelMemPtr_; + size_t g_modelMemSize_; + void *g_modelWeightPtr_; + size_t g_modelWeightSize_; + aclmdlDesc *g_modelDesc_; + aclmdlDataset *g_input_; + aclmdlDataset *g_output_; + void * g_inputAIPP_; + uint32_t g_inputAIPPSize_; + size_t g_aipp_index_; + bool g_isReleased_; }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/object_detect.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/object_detect.h index 0d95549fe..e7b5d3704 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/object_detect.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/object_detect.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 YOLOV3_COCO_DETECTION_DYNAMIC_AIPP_INC_OBJECT_DETECT_H +#define YOLOV3_COCO_DETECTION_DYNAMIC_AIPP_INC_OBJECT_DETECT_H + #pragma once #include "utils.h" #include "acl/acl.h" @@ -63,21 +64,22 @@ private: void DestroyResource(); private: - int32_t deviceId_; - aclrtContext context_; - aclrtStream stream_; - uint32_t imageInfoSize_; - void* imageInfoBuf_; - ModelProcess model_; + int32_t g_deviceId_; + aclrtContext g_context_; + aclrtStream g_stream_; + uint32_t g_imageInfoSize_; + void* g_imageInfoBuf_; + ModelProcess g_model_; - const char* modelPath_; - uint32_t modelWidth_; - uint32_t modelHeight_; - uint32_t inputDataSize_; - DvppProcess dvpp_; - aclrtRunMode runMode_; + const char* g_modelPath_; + uint32_t g_modelWidth_; + uint32_t g_modelHeight_; + uint32_t g_inputDataSize_; + DvppProcess g_dvpp_; + aclrtRunMode g_runMode_; - bool isInited_; - bool isDeviceSet_; + bool g_isInited_; + bool g_isDeviceSet_; }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/utils.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/utils.h index 80d71dd94..93232d711 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/utils.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/utils.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 utils.h -* Description: handle file operations */ + +#ifndef YOLOV3_COCO_DETECTION_DYNAMIC_AIPP_INC_UTILS_H +#define YOLOV3_COCO_DETECTION_DYNAMIC_AIPP_INC_UTILS_H + #pragma once #include #include @@ -125,3 +126,4 @@ public: aclrtRunMode mode); }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_jpegd.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_jpegd.cpp index 66787b7fb..08159fad6 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_jpegd.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_jpegd.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,9 +12,6 @@ * 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 dvpp_process.cpp -* Description: handle dvpp process */ #include @@ -24,16 +21,15 @@ using namespace std; DvppJpegD::DvppJpegD(aclrtStream& stream, acldvppChannelDesc *dvppChannelDesc) - : stream_(stream), dvppChannelDesc_(dvppChannelDesc), - decodeOutBufferDev_(nullptr), decodeOutputDesc_(nullptr){ + : g_stream_(stream), g_dvppChannelDesc_(dvppChannelDesc), + g_decodeOutBufferDev_(nullptr), g_decodeOutputDesc_(nullptr) { } -DvppJpegD::~DvppJpegD(){ +DvppJpegD::~DvppJpegD() { DestroyDecodeResource(); } - -Result DvppJpegD::InitDecodeOutputDesc(ImageData& inputImage){ +Result DvppJpegD::InitDecodeOutputDesc(ImageData& inputImage) { uint32_t decodeOutWidthStride = ALIGN_UP128(inputImage.width); uint32_t decodeOutHeightStride = ALIGN_UP16(inputImage.height); if (decodeOutWidthStride == 0 || decodeOutHeightStride == 0) { @@ -45,43 +41,43 @@ Result DvppJpegD::InitDecodeOutputDesc(ImageData& inputImage){ uint32_t decodeOutBufferSize = YUV420SP_SIZE(decodeOutWidthStride, decodeOutHeightStride) * 4; - aclError aclRet = acldvppMalloc(&decodeOutBufferDev_, decodeOutBufferSize); + aclError aclRet = acldvppMalloc(&g_decodeOutBufferDev_, decodeOutBufferSize); if (aclRet != ACL_SUCCESS) { - ERROR_LOG("acldvppMalloc decodeOutBufferDev_ failed, aclRet = %d", aclRet); + ERROR_LOG("acldvppMalloc g_decodeOutBufferDev_ failed, aclRet = %d", aclRet); return FAILED; } - decodeOutputDesc_ = acldvppCreatePicDesc(); - if (decodeOutputDesc_ == nullptr) { - ERROR_LOG("acldvppCreatePicDesc decodeOutputDesc_ failed"); + g_decodeOutputDesc_ = acldvppCreatePicDesc(); + if (g_decodeOutputDesc_ == nullptr) { + ERROR_LOG("acldvppCreatePicDesc g_decodeOutputDesc_ failed"); return FAILED; } - acldvppSetPicDescData(decodeOutputDesc_, decodeOutBufferDev_); - acldvppSetPicDescFormat(decodeOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); - acldvppSetPicDescWidth(decodeOutputDesc_, inputImage.width); - acldvppSetPicDescHeight(decodeOutputDesc_, inputImage.height); - acldvppSetPicDescWidthStride(decodeOutputDesc_, decodeOutWidthStride); - acldvppSetPicDescHeightStride(decodeOutputDesc_, decodeOutHeightStride); - acldvppSetPicDescSize(decodeOutputDesc_, decodeOutBufferSize); + acldvppSetPicDescData(g_decodeOutputDesc_, g_decodeOutBufferDev_); + acldvppSetPicDescFormat(g_decodeOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); + acldvppSetPicDescWidth(g_decodeOutputDesc_, inputImage.width); + acldvppSetPicDescHeight(g_decodeOutputDesc_, inputImage.height); + acldvppSetPicDescWidthStride(g_decodeOutputDesc_, decodeOutWidthStride); + acldvppSetPicDescHeightStride(g_decodeOutputDesc_, decodeOutHeightStride); + acldvppSetPicDescSize(g_decodeOutputDesc_, decodeOutBufferSize); return SUCCESS; } -Result DvppJpegD::Process(ImageData& dest, ImageData& src){ +Result DvppJpegD::Process(ImageData& dest, ImageData& src) { int ret = InitDecodeOutputDesc(src); if (ret != SUCCESS) { ERROR_LOG("InitDecodeOutputDesc failed"); return FAILED; } - aclError aclRet = acldvppJpegDecodeAsync(dvppChannelDesc_, reinterpret_cast(src.data.get()), - src.size, decodeOutputDesc_, stream_); + aclError aclRet = acldvppJpegDecodeAsync(g_dvppChannelDesc_, reinterpret_cast(src.data.get()), + src.size, g_decodeOutputDesc_, g_stream_); if (aclRet != ACL_SUCCESS) { ERROR_LOG("acldvppJpegDecodeAsync failed, aclRet = %d", aclRet); return FAILED; } - aclRet = aclrtSynchronizeStream(stream_); + aclRet = aclrtSynchronizeStream(g_stream_); if (aclRet != ACL_SUCCESS) { ERROR_LOG("decode aclrtSynchronizeStream failed, aclRet = %d", aclRet); return FAILED; @@ -92,15 +88,15 @@ Result DvppJpegD::Process(ImageData& dest, ImageData& src){ dest.alignWidth = ALIGN_UP128(src.width); dest.alignHeight = ALIGN_UP16(src.height); dest.size = YUV420SP_SIZE(dest.alignWidth, dest.alignHeight); - dest.data = SHARED_PTR_DVPP_BUF(decodeOutBufferDev_); + dest.data = SHARED_PTR_DVPP_BUF(g_decodeOutBufferDev_); INFO_LOG("convert image success"); return SUCCESS; } -void DvppJpegD::DestroyDecodeResource(){ - if (decodeOutputDesc_ != nullptr) { - acldvppDestroyPicDesc(decodeOutputDesc_); - decodeOutputDesc_ = nullptr; +void DvppJpegD::DestroyDecodeResource() { + if (g_decodeOutputDesc_ != nullptr) { + acldvppDestroyPicDesc(g_decodeOutputDesc_); + g_decodeOutputDesc_ = nullptr; } } diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_process.cpp index 9a09e3ea3..103aeed65 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_process.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,9 +12,6 @@ * 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 dvpp_process.cpp -* Description: handle dvpp process */ #include @@ -25,54 +22,53 @@ using namespace std; -DvppProcess::DvppProcess():dvppChannelDesc_(nullptr){ +DvppProcess::DvppProcess():g_dvppChannelDesc_(nullptr) { } -DvppProcess::~DvppProcess(){ +DvppProcess::~DvppProcess() { DestroyResource(); } -void DvppProcess::DestroyResource(){ +void DvppProcess::DestroyResource() { aclError aclRet; - if (dvppChannelDesc_ != nullptr) { - aclRet = acldvppDestroyChannel(dvppChannelDesc_); + if (g_dvppChannelDesc_ != nullptr) { + aclRet = acldvppDestroyChannel(g_dvppChannelDesc_); if (aclRet != ACL_SUCCESS) { ERROR_LOG("acldvppDestroyChannel failed, aclRet = %d", aclRet); } - (void)acldvppDestroyChannelDesc(dvppChannelDesc_); - dvppChannelDesc_ = nullptr; + (void)acldvppDestroyChannelDesc(g_dvppChannelDesc_); + g_dvppChannelDesc_ = nullptr; } } -Result DvppProcess::InitResource(aclrtStream& stream) -{ +Result DvppProcess::InitResource(aclrtStream& stream) { aclError aclRet; - dvppChannelDesc_ = acldvppCreateChannelDesc(); - if (dvppChannelDesc_ == nullptr) { + g_dvppChannelDesc_ = acldvppCreateChannelDesc(); + if (g_dvppChannelDesc_ == nullptr) { ERROR_LOG("acldvppCreateChannelDesc failed"); return FAILED; } - aclRet = acldvppCreateChannel(dvppChannelDesc_); + aclRet = acldvppCreateChannel(g_dvppChannelDesc_); if (aclRet != ACL_SUCCESS) { ERROR_LOG("acldvppCreateChannel failed, aclRet = %d", aclRet); return FAILED; } - stream_ = stream; + g_stream_ = stream; INFO_LOG("dvpp init resource ok"); return SUCCESS; } Result DvppProcess::Resize(ImageData& dest, ImageData& src, uint32_t width, uint32_t height) { - DvppResize resizeOp(stream_, dvppChannelDesc_, width, height); + DvppResize resizeOp(g_stream_, g_dvppChannelDesc_, width, height); return resizeOp.Process(dest, src); } Result DvppProcess::CvtJpegToYuv420sp(ImageData& dest, ImageData& src) { - DvppJpegD jpegD(stream_, dvppChannelDesc_); + DvppJpegD jpegD(g_stream_, g_dvppChannelDesc_); return jpegD.Process(dest, src); } diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_resize.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_resize.cpp index 49e4bfe5a..17f067be8 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_resize.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_resize.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,9 +12,6 @@ * 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 dvpp_process.cpp -* Description: handle dvpp process */ #include @@ -25,18 +22,18 @@ using namespace std; DvppResize::DvppResize(aclrtStream& stream, acldvppChannelDesc *dvppChannelDesc, uint32_t width, uint32_t height) -: stream_(stream), dvppChannelDesc_(dvppChannelDesc), -resizeConfig_(nullptr), vpcInputDesc_(nullptr), vpcOutputDesc_(nullptr), -inDevBuffer_(nullptr),vpcOutBufferDev_(nullptr),vpcOutBufferSize_(0){ - size_.width = width; - size_.height = height; +: g_stream_(stream), g_dvppChannelDesc_(dvppChannelDesc), +g_resizeConfig_(nullptr), g_vpcInputDesc_(nullptr), g_vpcOutputDesc_(nullptr), +g_inDevBuffer_(nullptr),g_vpcOutBufferDev_(nullptr),g_vpcOutBufferSize_(0) { + g_size_.width = width; + g_size_.height = height; } -DvppResize::~DvppResize(){ +DvppResize::~DvppResize() { DestroyResizeResource(); } -Result DvppResize::InitResizeInputDesc(ImageData& inputImage){ +Result DvppResize::InitResizeInputDesc(ImageData& inputImage) { uint32_t alignWidth = ALIGN_UP128(inputImage.width); uint32_t alignHeight = ALIGN_UP16(inputImage.height); if (alignWidth == 0 || alignHeight == 0) { @@ -45,25 +42,25 @@ Result DvppResize::InitResizeInputDesc(ImageData& inputImage){ return FAILED; } uint32_t inputBufferSize = YUV420SP_SIZE(alignWidth, alignHeight); - vpcInputDesc_ = acldvppCreatePicDesc(); - if (vpcInputDesc_ == nullptr) { - ERROR_LOG("acldvppCreatePicDesc vpcInputDesc_ failed"); + g_vpcInputDesc_ = acldvppCreatePicDesc(); + if (g_vpcInputDesc_ == nullptr) { + ERROR_LOG("acldvppCreatePicDesc g_vpcInputDesc_ failed"); return FAILED; } - acldvppSetPicDescData(vpcInputDesc_, inputImage.data.get()); - acldvppSetPicDescFormat(vpcInputDesc_, format_); - acldvppSetPicDescWidth(vpcInputDesc_, inputImage.width); - acldvppSetPicDescHeight(vpcInputDesc_, inputImage.height); - acldvppSetPicDescWidthStride(vpcInputDesc_, alignWidth); - acldvppSetPicDescHeightStride(vpcInputDesc_, alignHeight); - acldvppSetPicDescSize(vpcInputDesc_, inputBufferSize); + acldvppSetPicDescData(g_vpcInputDesc_, inputImage.data.get()); + acldvppSetPicDescFormat(g_vpcInputDesc_, g_format_); + acldvppSetPicDescWidth(g_vpcInputDesc_, inputImage.width); + acldvppSetPicDescHeight(g_vpcInputDesc_, inputImage.height); + acldvppSetPicDescWidthStride(g_vpcInputDesc_, alignWidth); + acldvppSetPicDescHeightStride(g_vpcInputDesc_, alignHeight); + acldvppSetPicDescSize(g_vpcInputDesc_, inputBufferSize); return SUCCESS; } -Result DvppResize::InitResizeOutputDesc(){ - int resizeOutWidth = size_.width; - int resizeOutHeight = size_.height; +Result DvppResize::InitResizeOutputDesc() { + int resizeOutWidth = g_size_.width; + int resizeOutHeight = g_size_.height; int resizeOutWidthStride = ALIGN_UP16(resizeOutWidth); int resizeOutHeightStride = ALIGN_UP2(resizeOutHeight); if (resizeOutWidthStride == 0 || resizeOutHeightStride == 0) { @@ -71,34 +68,34 @@ Result DvppResize::InitResizeOutputDesc(){ return FAILED; } - vpcOutBufferSize_ = YUV420SP_SIZE(resizeOutWidthStride, resizeOutHeightStride); - aclError aclRet = acldvppMalloc(&vpcOutBufferDev_, vpcOutBufferSize_); + g_vpcOutBufferSize_ = YUV420SP_SIZE(resizeOutWidthStride, resizeOutHeightStride); + aclError aclRet = acldvppMalloc(&g_vpcOutBufferDev_, g_vpcOutBufferSize_); if (aclRet != ACL_SUCCESS) { - ERROR_LOG("acldvppMalloc vpcOutBufferDev_ failed, aclRet = %d", aclRet); + ERROR_LOG("acldvppMalloc g_vpcOutBufferDev_ failed, aclRet = %d", aclRet); return FAILED; } - vpcOutputDesc_ = acldvppCreatePicDesc(); - if (vpcOutputDesc_ == nullptr) { - ERROR_LOG("acldvppCreatePicDesc vpcOutputDesc_ failed"); + g_vpcOutputDesc_ = acldvppCreatePicDesc(); + if (g_vpcOutputDesc_ == nullptr) { + ERROR_LOG("acldvppCreatePicDesc g_vpcOutputDesc_ failed"); return FAILED; } - acldvppSetPicDescData(vpcOutputDesc_, vpcOutBufferDev_); - acldvppSetPicDescFormat(vpcOutputDesc_, format_); - acldvppSetPicDescWidth(vpcOutputDesc_, resizeOutWidth); - acldvppSetPicDescHeight(vpcOutputDesc_, resizeOutHeight); - acldvppSetPicDescWidthStride(vpcOutputDesc_, resizeOutWidthStride); - acldvppSetPicDescHeightStride(vpcOutputDesc_, resizeOutHeightStride); - acldvppSetPicDescSize(vpcOutputDesc_, vpcOutBufferSize_); + acldvppSetPicDescData(g_vpcOutputDesc_, g_vpcOutBufferDev_); + acldvppSetPicDescFormat(g_vpcOutputDesc_, g_format_); + acldvppSetPicDescWidth(g_vpcOutputDesc_, resizeOutWidth); + acldvppSetPicDescHeight(g_vpcOutputDesc_, resizeOutHeight); + acldvppSetPicDescWidthStride(g_vpcOutputDesc_, resizeOutWidthStride); + acldvppSetPicDescHeightStride(g_vpcOutputDesc_, resizeOutHeightStride); + acldvppSetPicDescSize(g_vpcOutputDesc_, g_vpcOutBufferSize_); return SUCCESS; } Result DvppResize::InitResizeResource(ImageData& inputImage) { - format_ = static_cast(PIXEL_FORMAT_YUV_SEMIPLANAR_420); - resizeConfig_ = acldvppCreateResizeConfig(); - if (resizeConfig_ == nullptr) { + g_format_ = static_cast(PIXEL_FORMAT_YUV_SEMIPLANAR_420); + g_resizeConfig_ = acldvppCreateResizeConfig(); + if (g_resizeConfig_ == nullptr) { ERROR_LOG("Dvpp resize init failed for create config failed"); return FAILED; } @@ -116,51 +113,51 @@ Result DvppResize::InitResizeResource(ImageData& inputImage) { return SUCCESS; } -Result DvppResize::Process(ImageData& resizedImage, ImageData& srcImage){ +Result DvppResize::Process(ImageData& resizedImage, ImageData& srcImage) { if (SUCCESS != InitResizeResource(srcImage)) { ERROR_LOG("Dvpp resize failed for init error"); return FAILED; } // resize pic - aclError aclRet = acldvppVpcResizeAsync(dvppChannelDesc_, vpcInputDesc_, - vpcOutputDesc_, resizeConfig_, stream_); + aclError aclRet = acldvppVpcResizeAsync(g_dvppChannelDesc_, g_vpcInputDesc_, + g_vpcOutputDesc_, g_resizeConfig_, g_stream_); if (aclRet != ACL_SUCCESS) { ERROR_LOG("acldvppVpcResizeAsync failed, aclRet = %d", aclRet); return FAILED; } - aclRet = aclrtSynchronizeStream(stream_); + aclRet = aclrtSynchronizeStream(g_stream_); if (aclRet != ACL_SUCCESS) { ERROR_LOG("resize aclrtSynchronizeStream failed, aclRet = %d", aclRet); return FAILED; } - resizedImage.width = size_.width; - resizedImage.height = size_.height; - resizedImage.alignWidth = ALIGN_UP16(size_.width); - resizedImage.alignHeight = ALIGN_UP2(size_.height); - resizedImage.size = vpcOutBufferSize_; - resizedImage.data = SHARED_PTR_DVPP_BUF(vpcOutBufferDev_); + resizedImage.width = g_size_.width; + resizedImage.height = g_size_.height; + resizedImage.alignWidth = ALIGN_UP16(g_size_.width); + resizedImage.alignHeight = ALIGN_UP2(g_size_.height); + resizedImage.size = g_vpcOutBufferSize_; + resizedImage.data = SHARED_PTR_DVPP_BUF(g_vpcOutBufferDev_); DestroyResizeResource(); return SUCCESS; } -void DvppResize::DestroyResizeResource(){ - if (resizeConfig_ != nullptr) { - (void)acldvppDestroyResizeConfig(resizeConfig_); - resizeConfig_ = nullptr; +void DvppResize::DestroyResizeResource() { + if (g_resizeConfig_ != nullptr) { + (void)acldvppDestroyResizeConfig(g_resizeConfig_); + g_resizeConfig_ = nullptr; } - if (vpcInputDesc_ != nullptr) { - (void)acldvppDestroyPicDesc(vpcInputDesc_); - vpcInputDesc_ = nullptr; + if (g_vpcInputDesc_ != nullptr) { + (void)acldvppDestroyPicDesc(g_vpcInputDesc_); + g_vpcInputDesc_ = nullptr; } - if (vpcOutputDesc_ != nullptr) { - (void)acldvppDestroyPicDesc(vpcOutputDesc_); - vpcOutputDesc_ = nullptr; + if (g_vpcOutputDesc_ != nullptr) { + (void)acldvppDestroyPicDesc(g_vpcOutputDesc_); + g_vpcOutputDesc_ = nullptr; } } diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/main.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/main.cpp index cf89819e4..5fe8a420e 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/main.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/main.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,9 +12,6 @@ * 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 main.cpp -* Description: dvpp sample main func */ #include @@ -26,28 +23,28 @@ using namespace std; namespace { -uint32_t kModelWidth = 416; -uint32_t kModelHeight = 416; -const char* kModelPath = "../model/yolov3.om"; +uint32_t g_ModelWidth = 416; +uint32_t g_ModelHeight = 416; +const char* g_ModelPath = "../model/yolov3.om"; } int main(int argc, char *argv[]) { - //Check input args: the path of input picture, and ignore hidden dotfile directory - if((argc != 2) || (argv[1] == nullptr)){ + // Check input args: the path of input picture, and ignore hidden dotfile directory + if ((argc != 2) || (argv[1] == nullptr)) { ERROR_LOG("Please input: ./main "); return FAILED; } - //Instantiate the object detection class - ObjectDetect detect(kModelPath, kModelWidth, kModelHeight); - //Initialize the acl resources, dvpp, load model, - //and malloc input memory of input which is const + // Instantiate the object detection class + ObjectDetect detect(g_ModelPath, g_ModelWidth, g_ModelHeight); + // Initialize the acl resources, dvpp, load model, + // and malloc input memory of input which is const Result ret = detect.Init(); if (ret != SUCCESS) { ERROR_LOG("Classification Init resource failed"); return FAILED; } - //Get all the image file path in the image directory + // Get all the image file path in the image directory string inputImageDir = string(argv[1]); vector fileVec; Utils::GetAllFiles(inputImageDir, fileVec); diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/model_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/model_process.cpp index fbc9bf199..ca43196d8 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/model_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/model_process.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,91 +12,89 @@ * 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 model_process.cpp -* Description: handle model process */ + #include "model_process.h" #include #include #include "utils.h" using namespace std; -ModelProcess::ModelProcess():loadFlag_(false), modelId_(0), modelMemPtr_(nullptr), modelMemSize_(0), -modelWeightPtr_(nullptr),modelWeightSize_(0), modelDesc_(nullptr), input_(nullptr), output_(nullptr), -isReleased_(false){ +ModelProcess::ModelProcess():g_loadFlag_(false), g_modelId_(0), g_modelMemPtr_(nullptr), g_modelMemSize_(0), +g_modelWeightPtr_(nullptr),g_modelWeightSize_(0), g_modelDesc_(nullptr), g_input_(nullptr), g_output_(nullptr), +g_isReleased_(false) { } -ModelProcess::~ModelProcess(){ +ModelProcess::~ModelProcess() { DestroyResource(); } -void ModelProcess::DestroyResource(){ - if (isReleased_) {return;} +void ModelProcess::DestroyResource() { + if (g_isReleased_) {return;} Unload(); DestroyDesc(); DestroyInput(); DestroyOutput(); - isReleased_ = true; + g_isReleased_ = true; } Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath){ - if (loadFlag_) { + if (g_loadFlag_) { ERROR_LOG("has already loaded a model"); return FAILED; } - aclError ret = aclmdlQuerySize(modelPath, &modelMemSize_, &modelWeightSize_); + aclError ret = aclmdlQuerySize(modelPath, &g_modelMemSize_, &g_modelWeightSize_); if (ret != ACL_SUCCESS) { ERROR_LOG("query model failed, model file is %s", modelPath); return FAILED; } - ret = aclrtMalloc(&modelMemPtr_, modelMemSize_, ACL_MEM_MALLOC_HUGE_FIRST); + ret = aclrtMalloc(&g_modelMemPtr_, g_modelMemSize_, ACL_MEM_MALLOC_HUGE_FIRST); if (ret != ACL_SUCCESS) { - ERROR_LOG("malloc buffer for mem failed, require size is %zu", modelMemSize_); + ERROR_LOG("malloc buffer for mem failed, require size is %zu", g_modelMemSize_); return FAILED; } - ret = aclrtMalloc(&modelWeightPtr_, modelWeightSize_, ACL_MEM_MALLOC_HUGE_FIRST); + ret = aclrtMalloc(&g_modelWeightPtr_, g_modelWeightSize_, ACL_MEM_MALLOC_HUGE_FIRST); if (ret != ACL_SUCCESS) { - ERROR_LOG("malloc buffer for weight failed, require size is %zu", modelWeightSize_); + ERROR_LOG("malloc buffer for weight failed, require size is %zu", g_modelWeightSize_); return FAILED; } - ret = aclmdlLoadFromFileWithMem(modelPath, &modelId_, modelMemPtr_, - modelMemSize_, modelWeightPtr_, modelWeightSize_); + ret = aclmdlLoadFromFileWithMem(modelPath, &g_modelId_, g_modelMemPtr_, + g_modelMemSize_, g_modelWeightPtr_, g_modelWeightSize_); if (ret != ACL_SUCCESS) { ERROR_LOG("load model from file failed, model file is %s", modelPath); return FAILED; } - loadFlag_ = true; + g_loadFlag_ = true; INFO_LOG("load model %s success", modelPath); return SUCCESS; } -Result ModelProcess::CreateDesc(){ - modelDesc_ = aclmdlCreateDesc(); - if (modelDesc_ == nullptr) { +Result ModelProcess::CreateDesc() { + g_modelDesc_ = aclmdlCreateDesc(); + if (g_modelDesc_ == nullptr) { ERROR_LOG("create model description failed"); return FAILED; } - aclError ret = aclmdlGetDesc(modelDesc_, modelId_); + aclError ret = aclmdlGetDesc(g_modelDesc_, g_modelId_); if (ret != ACL_SUCCESS) { ERROR_LOG("get model description failed"); return FAILED; } - ret = aclmdlGetInputIndexByName(modelDesc_, ACL_DYNAMIC_AIPP_NAME, &aipp_index_); + ret = aclmdlGetInputIndexByName(g_modelDesc_, ACL_DYNAMIC_AIPP_NAME, &g_aipp_index_); if (ret != 0) { ERROR_LOG("call aclmdlGetInputIndexByName failed"); return FAILED; } - inputAIPPSize_ = aclmdlGetInputSizeByIndex(modelDesc_, aipp_index_); - ret = aclrtMalloc(&inputAIPP_, inputAIPPSize_, ACL_MEM_MALLOC_HUGE_FIRST); + g_inputAIPPSize_ = aclmdlGetInputSizeByIndex(g_modelDesc_, g_aipp_index_); + ret = aclrtMalloc(&g_inputAIPP_, g_inputAIPPSize_, ACL_MEM_MALLOC_HUGE_FIRST); if (ret != ACL_SUCCESS) { ERROR_LOG("malloc device data input3 failed, result is %d", ret); return FAILED; @@ -106,10 +104,10 @@ Result ModelProcess::CreateDesc(){ return SUCCESS; } -void ModelProcess::DestroyDesc(){ - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; +void ModelProcess::DestroyDesc() { + if (g_modelDesc_ != nullptr) { + (void)aclmdlDestroyDesc(g_modelDesc_); + g_modelDesc_ = nullptr; } } @@ -131,7 +129,7 @@ Result ModelProcess::AddDatasetBuffer(aclmdlDataset *dataset, return SUCCESS; } -aclmdlAIPP* ModelProcess::SetAIPPTensor(uint64_t batch_size){ +aclmdlAIPP* ModelProcess::SetAIPPTensor(uint64_t batch_size) { aclmdlAIPP* aippParamTensor = aclmdlCreateAIPP(batch_size); if (aippParamTensor == nullptr) { ERROR_LOG("call aclmdlCreateAIPP failed"); @@ -194,7 +192,7 @@ aclmdlAIPP* ModelProcess::SetAIPPTensor(uint64_t batch_size){ return aippParamTensor; } -aclmdlAIPP* ModelProcess::SetAIPPTensorOpenCV(uint64_t batch_size){ +aclmdlAIPP* ModelProcess::SetAIPPTensorOpenCV(uint64_t batch_size) { aclmdlAIPP* aippParamTensor = aclmdlCreateAIPP(batch_size); if (aippParamTensor == nullptr) { ERROR_LOG("call aclmdlCreateAIPP failed"); @@ -257,11 +255,11 @@ aclmdlAIPP* ModelProcess::SetAIPPTensorOpenCV(uint64_t batch_size){ return aippParamTensor; } -Result ModelProcess::CreateInput(void* input1, uint32_t input1size, - void* input2, uint32_t input2size){ - vector inputData = {{input1, input1size},{input2, input2size}, - {inputAIPP_, inputAIPPSize_}}; - uint32_t dataNum = aclmdlGetNumInputs(modelDesc_); +Result ModelProcess::CreateInput(void* input1, uint32_t input1Size, + void* input2, uint32_t input2Size) { + vector inputData = {{input1, input1Size},{input2, input2Size}, + {g_inputAIPP_, g_inputAIPPSize_}}; + uint32_t dataNum = aclmdlGetNumInputs(g_modelDesc_); if (dataNum == 0) { ERROR_LOG("Create input failed for no input data"); return FAILED; @@ -271,20 +269,20 @@ Result ModelProcess::CreateInput(void* input1, uint32_t input1size, return FAILED; } - input_ = aclmdlCreateDataset(); - if (input_ == nullptr) { + g_input_ = aclmdlCreateDataset(); + if (g_input_ == nullptr) { ERROR_LOG("Create input failed for create dataset failed"); return FAILED; } for (uint32_t i = 0; i < inputData.size(); i++) { - size_t modelInputSize = aclmdlGetInputSizeByIndex(modelDesc_, i); + size_t modelInputSize = aclmdlGetInputSizeByIndex(g_modelDesc_, i); if (modelInputSize != inputData[i].size) { WARNNING_LOG("Input size verify failed " "input[%d] size: %d, provide size : %d", i, modelInputSize, inputData[i].size); } - Result ret = AddDatasetBuffer(input_, + Result ret = AddDatasetBuffer(g_input_, inputData[i].data, inputData[i].size); if (ret != SUCCESS) { @@ -295,7 +293,7 @@ Result ModelProcess::CreateInput(void* input1, uint32_t input1size, } uint64_t batch_num = 1 ; aclmdlAIPP* aippParamTensor = SetAIPPTensor(batch_num); - aclError aclret = aclmdlSetInputAIPP(modelId_, input_, aipp_index_, aippParamTensor); + aclError aclret = aclmdlSetInputAIPP(g_modelId_, g_input_, g_aipp_index_, aippParamTensor); if (aclret != ACL_SUCCESS) { ERROR_LOG("call aclmdlSetInputAIPP failed"); return FAILED; @@ -309,11 +307,11 @@ Result ModelProcess::CreateInput(void* input1, uint32_t input1size, return SUCCESS; } -Result ModelProcess::CreateInputOpenCV(void* input1, uint32_t input1size, - void* input2, uint32_t input2size){ - vector inputData = {{input1, input1size},{input2, input2size}, - {inputAIPP_, inputAIPPSize_}}; - uint32_t dataNum = aclmdlGetNumInputs(modelDesc_); +Result ModelProcess::CreateInputOpenCV(void* input1, uint32_t input1Size, + void* input2, uint32_t input2Size) { + vector inputData = {{input1, input1Size},{input2, input2Size}, + {g_inputAIPP_, g_inputAIPPSize_}}; + uint32_t dataNum = aclmdlGetNumInputs(g_modelDesc_); if (dataNum == 0) { ERROR_LOG("Create input failed for no input data"); return FAILED; @@ -323,20 +321,20 @@ Result ModelProcess::CreateInputOpenCV(void* input1, uint32_t input1size, return FAILED; } - input_ = aclmdlCreateDataset(); - if (input_ == nullptr) { + g_input_ = aclmdlCreateDataset(); + if (g_input_ == nullptr) { ERROR_LOG("Create input failed for create dataset failed"); return FAILED; } for (uint32_t i = 0; i < inputData.size(); i++) { - size_t modelInputSize = aclmdlGetInputSizeByIndex(modelDesc_, i); + size_t modelInputSize = aclmdlGetInputSizeByIndex(g_modelDesc_, i); if (modelInputSize != inputData[i].size) { WARNNING_LOG("Input size verify failed " "input[%d] size: %d, provide size : %d", i, modelInputSize, inputData[i].size); } - Result ret = AddDatasetBuffer(input_, + Result ret = AddDatasetBuffer(g_input_, inputData[i].data, inputData[i].size); if (ret != SUCCESS) { @@ -347,7 +345,7 @@ Result ModelProcess::CreateInputOpenCV(void* input1, uint32_t input1size, } uint64_t batch_num = 1 ; aclmdlAIPP* aippParamTensor = SetAIPPTensorOpenCV(batch_num); - aclError aclret = aclmdlSetInputAIPP(modelId_, input_, aipp_index_, aippParamTensor); + aclError aclret = aclmdlSetInputAIPP(g_modelId_, g_input_, g_aipp_index_, aippParamTensor); if (aclret != ACL_SUCCESS) { ERROR_LOG("call aclmdlSetInputAIPP failed"); return FAILED; @@ -361,34 +359,34 @@ Result ModelProcess::CreateInputOpenCV(void* input1, uint32_t input1size, return SUCCESS; } -void ModelProcess::DestroyInput(){ - if (input_ == nullptr) { +void ModelProcess::DestroyInput() { + if (g_input_ == nullptr) { return; } - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(input_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(input_, i); + for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(g_input_); ++i) { + aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(g_input_, i); aclDestroyDataBuffer(dataBuffer); } - aclmdlDestroyDataset(input_); - input_ = nullptr; + aclmdlDestroyDataset(g_input_); + g_input_ = nullptr; } -Result ModelProcess::CreateOutput(){ - if (modelDesc_ == nullptr) { +Result ModelProcess::CreateOutput() { + if (g_modelDesc_ == nullptr) { ERROR_LOG("no model description, create ouput failed"); return FAILED; } - output_ = aclmdlCreateDataset(); - if (output_ == nullptr) { + g_output_ = aclmdlCreateDataset(); + if (g_output_ == nullptr) { ERROR_LOG("can't create dataset, create output failed"); return FAILED; } - size_t outputSize = aclmdlGetNumOutputs(modelDesc_); + size_t outputSize = aclmdlGetNumOutputs(g_modelDesc_); for (size_t i = 0; i < outputSize; ++i) { - size_t buffer_size = aclmdlGetOutputSizeByIndex(modelDesc_, i); + size_t buffer_size = aclmdlGetOutputSizeByIndex(g_modelDesc_, i); void *outputBuffer = nullptr; aclError ret = aclrtMalloc(&outputBuffer, buffer_size, ACL_MEM_MALLOC_NORMAL_ONLY); @@ -404,7 +402,7 @@ Result ModelProcess::CreateOutput(){ return FAILED; } - ret = aclmdlAddDatasetBuffer(output_, outputData); + ret = aclmdlAddDatasetBuffer(g_output_, outputData); if (ret != ACL_SUCCESS) { ERROR_LOG("can't add data buffer, create output failed"); aclrtFree(outputBuffer); @@ -417,26 +415,26 @@ Result ModelProcess::CreateOutput(){ return SUCCESS; } -void ModelProcess::DestroyOutput(){ - if (output_ == nullptr) { +void ModelProcess::DestroyOutput() { + if (g_output_ == nullptr) { return; } - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); + for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(g_output_); ++i) { + aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(g_output_, i); void* data = aclGetDataBufferAddr(dataBuffer); (void)aclrtFree(data); (void)aclDestroyDataBuffer(dataBuffer); } - (void)aclmdlDestroyDataset(output_); - output_ = nullptr; + (void)aclmdlDestroyDataset(g_output_); + g_output_ = nullptr; } -Result ModelProcess::Execute(){ - aclError ret = aclmdlExecute(modelId_, input_, output_); +Result ModelProcess::Execute() { + aclError ret = aclmdlExecute(g_modelId_, g_input_, g_output_); if (ret != ACL_SUCCESS) { - ERROR_LOG("execute model failed, modelId is %u", modelId_); + ERROR_LOG("execute model failed, modelId is %u", g_modelId_); return FAILED; } @@ -446,37 +444,37 @@ Result ModelProcess::Execute(){ void ModelProcess::Unload() { - if (!loadFlag_) { + if (!g_loadFlag_) { WARNNING_LOG("no model had been loaded, unload failed"); return; } - aclError ret = aclmdlUnload(modelId_); + aclError ret = aclmdlUnload(g_modelId_); if (ret != ACL_SUCCESS) { - ERROR_LOG("unload model failed, modelId is %u", modelId_); + ERROR_LOG("unload model failed, modelId is %u", g_modelId_); } - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; + if (g_modelDesc_ != nullptr) { + (void)aclmdlDestroyDesc(g_modelDesc_); + g_modelDesc_ = nullptr; } - if (modelMemPtr_ != nullptr) { - aclrtFree(modelMemPtr_); - modelMemPtr_ = nullptr; - modelMemSize_ = 0; + if (g_modelMemPtr_ != nullptr) { + aclrtFree(g_modelMemPtr_); + g_modelMemPtr_ = nullptr; + g_modelMemSize_ = 0; } - if (modelWeightPtr_ != nullptr) { - aclrtFree(modelWeightPtr_); - modelWeightPtr_ = nullptr; - modelWeightSize_ = 0; + if (g_modelWeightPtr_ != nullptr) { + aclrtFree(g_modelWeightPtr_); + g_modelWeightPtr_ = nullptr; + g_modelWeightSize_ = 0; } - loadFlag_ = false; - INFO_LOG("unload model success, modelId is %u", modelId_); + g_loadFlag_ = false; + INFO_LOG("unload model success, modelId is %u", g_modelId_); } -aclmdlDataset *ModelProcess::GetModelOutputData(){ - return output_; +aclmdlDataset *ModelProcess::GetModelOutputData() { + return g_output_; } diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/object_detect.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/object_detect.cpp index 901e48106..45060c370 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/object_detect.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/object_detect.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,8 @@ * 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 "object_detect.h" #include #include "opencv2/opencv.hpp" @@ -60,7 +58,7 @@ namespace { const string kFileSperator = "/"; // opencv color list for boundingbox - const vector kColors{ + const vector kColors { cv::Scalar(237, 149, 100), cv::Scalar(0, 215, 255), cv::Scalar(50, 205, 50), cv::Scalar(139, 85, 26) }; @@ -71,11 +69,11 @@ namespace { ObjectDetect::ObjectDetect(const char* modelPath, uint32_t modelWidth, uint32_t modelHeight) -:deviceId_(0), context_(nullptr), stream_(nullptr), modelWidth_(modelWidth), -modelHeight_(modelHeight), isInited_(false), isDeviceSet_(false){ - imageInfoSize_ = 0; - imageInfoBuf_ = nullptr; - modelPath_ = modelPath; +:g_deviceId_(0), g_context_(nullptr), g_stream_(nullptr), g_modelWidth_(modelWidth), +g_modelHeight_(modelHeight), g_isInited_(false), g_isDeviceSet_(false) { + g_imageInfoSize_ = 0; + g_imageInfoBuf_ = nullptr; + g_modelPath_ = modelPath; } ObjectDetect::~ObjectDetect() { @@ -93,16 +91,16 @@ Result ObjectDetect::InitResource() { INFO_LOG("acl init success"); // open device - ret = aclrtSetDevice(deviceId_); + ret = aclrtSetDevice(g_deviceId_); if (ret != ACL_SUCCESS) { - ERROR_LOG("acl open device %d failed", deviceId_); + ERROR_LOG("acl open device %d failed", g_deviceId_); return FAILED; } - isDeviceSet_ = true; - INFO_LOG("open device %d success", deviceId_); + g_isDeviceSet_ = true; + INFO_LOG("open device %d success", g_deviceId_); // create context (set current) - ret = aclrtCreateContext(&context_, deviceId_); + ret = aclrtCreateContext(&g_context_, g_deviceId_); if (ret != ACL_SUCCESS) { ERROR_LOG("acl create context failed"); return FAILED; @@ -110,14 +108,14 @@ Result ObjectDetect::InitResource() { INFO_LOG("create context success"); // create stream - ret = aclrtCreateStream(&stream_); + ret = aclrtCreateStream(&g_stream_); if (ret != ACL_SUCCESS) { ERROR_LOG("acl create stream failed"); return FAILED; } INFO_LOG("create stream success"); - ret = aclrtGetRunMode(&runMode_); + ret = aclrtGetRunMode(&g_runMode_); if (ret != ACL_SUCCESS) { ERROR_LOG("acl get run mode failed"); return FAILED; @@ -127,19 +125,19 @@ Result ObjectDetect::InitResource() { } Result ObjectDetect::InitModel(const char* omModelPath) { - Result ret = model_.LoadModelFromFileWithMem(omModelPath); + Result ret = g_model_.LoadModelFromFileWithMem(omModelPath); if (ret != SUCCESS) { ERROR_LOG("execute LoadModelFromFileWithMem failed"); return FAILED; } - ret = model_.CreateDesc(); + ret = g_model_.CreateDesc(); if (ret != SUCCESS) { ERROR_LOG("execute CreateDesc failed"); return FAILED; } - ret = model_.CreateOutput(); + ret = g_model_.CreateOutput(); if (ret != SUCCESS) { ERROR_LOG("execute CreateOutput failed"); return FAILED; @@ -148,15 +146,15 @@ Result ObjectDetect::InitModel(const char* omModelPath) { return SUCCESS; } -Result ObjectDetect::CreateImageInfoBuffer(){ - const float imageInfo[4] = {(float)modelWidth_, (float)modelHeight_, - (float)modelWidth_, (float)modelHeight_}; - imageInfoSize_ = sizeof(imageInfo); - if (runMode_ == ACL_HOST) - imageInfoBuf_ = Utils::CopyDataHostToDevice((void *)imageInfo, imageInfoSize_); - else - imageInfoBuf_ = Utils::CopyDataDeviceToDevice((void *)imageInfo, imageInfoSize_); - if (imageInfoBuf_ == nullptr) { +Result ObjectDetect::CreateImageInfoBuffer() { + const float imageInfo[4] = {(float)g_modelWidth_, (float)g_modelHeight_, + (float)g_modelWidth_, (float)g_modelHeight_}; + g_imageInfoSize_ = sizeof(imageInfo); + if (g_runMode_ == ACL_HOST) + g_imageInfoBuf_ = Utils::CopyDataHostToDevice((void *)imageInfo, g_imageInfoSize_); + else + g_imageInfoBuf_ = Utils::CopyDataDeviceToDevice((void *)imageInfo, g_imageInfoSize_); + if (g_imageInfoBuf_ == nullptr) { ERROR_LOG("Copy image info to device failed"); return FAILED; } @@ -165,7 +163,7 @@ Result ObjectDetect::CreateImageInfoBuffer(){ } Result ObjectDetect::Init() { - if (isInited_) { + if (g_isInited_) { INFO_LOG("Object detection instance is initied already!"); return SUCCESS; } @@ -176,13 +174,13 @@ Result ObjectDetect::Init() { return FAILED; } - ret = InitModel(modelPath_); + ret = InitModel(g_modelPath_); if (ret != SUCCESS) { ERROR_LOG("Init model failed"); return FAILED; } - ret = dvpp_.InitResource(stream_); + ret = g_dvpp_.InitResource(g_stream_); if (ret != SUCCESS) { ERROR_LOG("Init dvpp failed"); return FAILED; @@ -194,7 +192,7 @@ Result ObjectDetect::Init() { return FAILED; } - isInited_ = true; + g_isInited_ = true; return SUCCESS; } @@ -206,16 +204,16 @@ Result ObjectDetect::ProcessForDvpp(ImageData& srcImage, const string& origImage origImagePath.c_str()); return FAILED; } - //Send the resized picture to the model for inference - //and get the inference results + // Send the resized picture to the model for inference + // and get the inference results aclmdlDataset* inferenceOutput = nullptr; ret = Inference(inferenceOutput, resizedImage); if ((ret != SUCCESS) || (inferenceOutput == nullptr)) { ERROR_LOG("Inference model inference output data failed"); return FAILED; } - //Analyze the inference output, mark the object category and - //location by the inference result + // Analyze the inference output, mark the object category and + // location by the inference result ret = Postprocess(srcImage, inferenceOutput, origImagePath); if (ret != SUCCESS) { ERROR_LOG("Process model inference output data failed"); @@ -228,12 +226,12 @@ Result ObjectDetect::ProcessForOpenCV(cv::Mat& srcMat, const string& origImagePa uint32_t reiszeMatLen = 0; void* reiszeMatBuffer = nullptr; Result ret = PreprocessOpencv(srcMat, reiszeMatLen, reiszeMatBuffer); - if (ret != SUCCESS){ + if (ret != SUCCESS) { ERROR_LOG("Preprocess for Opencv mat failed"); } - //Send the resized picture to the model for inference - //and get the inference results + // Send the resized picture to the model for inference + // and get the inference results aclmdlDataset* inferenceOutput = nullptr; ret = InferenceOpenCV(inferenceOutput, reiszeMatLen, reiszeMatBuffer); if ((ret != SUCCESS) || (inferenceOutput == nullptr)) { @@ -252,17 +250,17 @@ Result ObjectDetect::ProcessForOpenCV(cv::Mat& srcMat, const string& origImagePa Result ObjectDetect::Preprocess(ImageData& resizedImage, ImageData& srcImage) { ImageData imageDevice; - Utils::CopyImageDataToDevice(imageDevice, srcImage, runMode_); + Utils::CopyImageDataToDevice(imageDevice, srcImage, g_runMode_); ImageData yuvImage; - Result ret = dvpp_.CvtJpegToYuv420sp(yuvImage, imageDevice); + Result ret = g_dvpp_.CvtJpegToYuv420sp(yuvImage, imageDevice); if (ret == FAILED) { ERROR_LOG("Convert jpeg to yuv failed"); return FAILED; } - //resize - ret = dvpp_.Resize(resizedImage, yuvImage, modelWidth_, modelHeight_); + // resize + ret = g_dvpp_.Resize(resizedImage, yuvImage, g_modelWidth_, g_modelHeight_); if (ret == FAILED) { ERROR_LOG("Resize image failed"); return FAILED; @@ -273,20 +271,20 @@ Result ObjectDetect::Preprocess(ImageData& resizedImage, ImageData& srcImage) { Result ObjectDetect::PreprocessOpencv(cv::Mat& srcMat, uint32_t& reiszeMatLen, void*& reiszeMatBuffer) { cv::Mat reiszeMat,dstMat; - //color convert & resize + // color convert & resize cv::cvtColor(srcMat, dstMat, cv::COLOR_BGR2RGB); if (dstMat.empty()) { ERROR_LOG("Mat cvtColor failed"); return FAILED; } - cv::resize(dstMat, reiszeMat, cv::Size(modelWidth_, modelHeight_)); + cv::resize(dstMat, reiszeMat, cv::Size(g_modelWidth_, g_modelHeight_)); if (reiszeMat.empty()) { ERROR_LOG("Mat cvtColor failed"); return FAILED; } - Result ret = Utils::CopyOpenCVMatToDevice(reiszeMat, reiszeMatLen, reiszeMatBuffer, runMode_); - if (ret != SUCCESS){ + Result ret = Utils::CopyOpenCVMatToDevice(reiszeMat, reiszeMatLen, reiszeMatBuffer, g_runMode_); + if (ret != SUCCESS) { ERROR_LOG("Copy Mat data to device failed"); } return SUCCESS; @@ -294,39 +292,39 @@ Result ObjectDetect::PreprocessOpencv(cv::Mat& srcMat, uint32_t& reiszeMatLen, v Result ObjectDetect::Inference(aclmdlDataset*& inferenceOutput, ImageData& resizedImage) { - Result ret = model_.CreateInput(resizedImage.data.get(), - resizedImage.size, imageInfoBuf_, imageInfoSize_); + Result ret = g_model_.CreateInput(resizedImage.data.get(), + resizedImage.size, g_imageInfoBuf_, g_imageInfoSize_); if (ret != SUCCESS) { ERROR_LOG("Create mode input dataset failed"); return FAILED; } - ret = model_.Execute(); + ret = g_model_.Execute(); if (ret != SUCCESS) { ERROR_LOG("Execute model inference failed"); return FAILED; } - inferenceOutput = model_.GetModelOutputData(); + inferenceOutput = g_model_.GetModelOutputData(); return SUCCESS; } Result ObjectDetect::InferenceOpenCV(aclmdlDataset*& inferenceOutput, uint32_t& reiszeMatLen, void*& reiszeMatBuffer) { - Result ret = model_.CreateInputOpenCV(reiszeMatBuffer, - reiszeMatLen, imageInfoBuf_, imageInfoSize_); + Result ret = g_model_.CreateInputOpenCV(reiszeMatBuffer, + reiszeMatLen, g_imageInfoBuf_, g_imageInfoSize_); if (ret != SUCCESS) { ERROR_LOG("Create mode input dataset failed"); return FAILED; } - ret = model_.Execute(); + ret = g_model_.Execute(); if (ret != SUCCESS) { ERROR_LOG("Execute model inference failed"); return FAILED; } - inferenceOutput = model_.GetModelOutputData(); + inferenceOutput = g_model_.GetModelOutputData(); return SUCCESS; } @@ -342,8 +340,8 @@ const string& origImagePath) { uint32_t totalBox = boxNum[0]; vector detectResults; - float widthScale = (float)(image.width) / modelWidth_; - float heightScale = (float)(image.height) / modelHeight_; + float widthScale = (float)(image.width) / g_modelWidth_; + float heightScale = (float)(image.height) / g_modelHeight_; for (uint32_t i = 0; i < totalBox; i++) { BBox boundBox; uint32_t score = uint32_t(detectData[totalBox * SCORE + i] * 100); @@ -362,7 +360,7 @@ const string& origImagePath) { } DrowBoundBoxToImage(detectResults, origImagePath); - if (runMode_ == ACL_HOST) { + if (g_runMode_ == ACL_HOST) { delete[]((uint8_t *)detectData); delete[]((uint8_t*)boxNum); } @@ -382,8 +380,8 @@ const string& origImagePath) { uint32_t totalBox = boxNum[0]; vector detectResults; - float widthScale = (float)(srcMat.cols) / modelWidth_; - float heightScale = (float)(srcMat.rows) / modelHeight_; + float widthScale = (float)(srcMat.cols) / g_modelWidth_; + float heightScale = (float)(srcMat.rows) / g_modelHeight_; for (uint32_t i = 0; i < totalBox; i++) { BBox boundBox; uint32_t score = uint32_t(detectData[totalBox * SCORE + i] * 100); @@ -402,7 +400,7 @@ const string& origImagePath) { } DrowBoundBoxToOpenCVImage(detectResults, origImagePath); - if (runMode_ == ACL_HOST) { + if (g_runMode_ == ACL_HOST) { delete[]((uint8_t *)detectData); delete[]((uint8_t*)boxNum); } @@ -435,7 +433,7 @@ void* ObjectDetect::GetInferenceOutputItem(uint32_t& itemDataSize, } void* data = nullptr; - if (runMode_ == ACL_HOST) { + if (g_runMode_ == ACL_HOST) { data = Utils::CopyDataDeviceToLocal(dataBufferDev, bufferSize); if (data == nullptr) { ERROR_LOG("Copy inference output to host failed"); @@ -493,35 +491,35 @@ void ObjectDetect::DrowBoundBoxToOpenCVImage(vector& detectionResults, cv::imwrite(sstream.str(), image); } -void ObjectDetect::DestroyResource(){ - aclrtFree(imageInfoBuf_); - model_.DestroyResource(); - dvpp_.DestroyResource(); +void ObjectDetect::DestroyResource() { + aclrtFree(g_imageInfoBuf_); + g_model_.DestroyResource(); + g_dvpp_.DestroyResource(); aclError ret; - if (stream_ != nullptr) { - ret = aclrtDestroyStream(stream_); + if (g_stream_ != nullptr) { + ret = aclrtDestroyStream(g_stream_); if (ret != ACL_SUCCESS) { ERROR_LOG("destroy stream failed"); } - stream_ = nullptr; + g_stream_ = nullptr; } INFO_LOG("end to destroy stream"); - if (context_ != nullptr) { - ret = aclrtDestroyContext(context_); + if (g_context_ != nullptr) { + ret = aclrtDestroyContext(g_context_); if (ret != ACL_SUCCESS) { ERROR_LOG("destroy context failed"); } - context_ = nullptr; + g_context_ = nullptr; } INFO_LOG("end to destroy context"); - if (isDeviceSet_) { - ret = aclrtResetDevice(deviceId_); + if (g_isDeviceSet_) { + ret = aclrtResetDevice(g_deviceId_); if (ret != ACL_SUCCESS) { ERROR_LOG("reset device failed"); } - INFO_LOG("end to reset device is %d", deviceId_); + INFO_LOG("end to reset device is %d", g_deviceId_); } ret = aclFinalize(); diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/utils.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/utils.cpp index 46cfa271a..531c4a5d4 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/utils.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/utils.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,8 @@ * 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 utils.cpp -* Description: handle file operations */ + #include "utils.h" #include #include @@ -33,18 +31,18 @@ using namespace std; namespace { -const std::string kImagePathSeparator = ","; -const int kStatSuccess = 0; -const std::string kFileSperator = "/"; -const std::string kPathSeparator = "/"; +const std::string g_ImagePathSeparator = ","; +const int g_StatSuccess = 0; +const std::string g_FileSperator = "/"; +const std::string g_PathSeparator = "/"; // output image prefix -const std::string kOutputFilePrefix = "out_"; +const std::string g_OutputFilePrefix = "out_"; } bool Utils::IsDirectory(const string &path) { // get path stat struct stat buf; - if (stat(path.c_str(), &buf) != kStatSuccess) { + if (stat(path.c_str(), &buf) != g_StatSuccess) { return false; } @@ -66,7 +64,7 @@ bool Utils::IsPathExist(const string &path) { void Utils::SplitPath(const string &path, vector &path_vec) { char *char_path = const_cast(path.c_str()); - const char *char_split = kImagePathSeparator.c_str(); + const char *char_split = g_ImagePathSeparator.c_str(); char *tmp_path = strtok(char_path, char_split); while (tmp_path) { path_vec.emplace_back(tmp_path); @@ -103,7 +101,7 @@ void Utils::GetPathFiles(const string &path, vector &file_vec) { } // file path - string full_path = path + kPathSeparator + dirent_ptr->d_name; + string full_path = path + g_PathSeparator + dirent_ptr->d_name; // directory need recursion if (IsDirectory(full_path)) { GetPathFiles(full_path, file_vec); @@ -209,8 +207,7 @@ aclrtRunMode runMode) { return SUCCESS; } -int Utils::ReadImageFile(ImageData& image, std::string fileName) -{ +int Utils::ReadImageFile(ImageData& image, std::string fileName) { struct stat sBuf; int fileStatus = stat(fileName.data(), &sBuf); if (fileStatus == -1) { diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/dvpp_jpegd.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/dvpp_jpegd.h index 94de8e4d0..9a7508cea 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/dvpp_jpegd.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/dvpp_jpegd.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 dvpp_process.h -* Description: handle dvpp process */ + +#ifndef YOLOV3_COCO_DETECTION_PICTURE_INC_DVPP_JPEGD_H +#define YOLOV3_COCO_DETECTION_PICTURE_INC_DVPP_JPEGD_H + #pragma once #include #include "acl/acl.h" @@ -78,16 +79,17 @@ private: void DestroyResource(); void DestroyOutputPara(); - aclrtStream stream_; - acldvppChannelDesc *dvppChannelDesc_; + aclrtStream g_stream_; + acldvppChannelDesc *g_dvppChannelDesc_; - void* decodeOutBufferDev_; // decode output buffer - acldvppPicDesc *decodeOutputDesc_; //decode output desc + void* g_decodeOutBufferDev_; // decode output buffer + acldvppPicDesc *g_decodeOutputDesc_; //decode output desc - uint8_t *inDevBuffer_; // input pic dev buffer - uint32_t inDevBufferSizeD_; // input pic size for decode + uint8_t *g_inDevBuffer_; // input pic dev buffer + uint32_t g_inDevBufferSizeD_; // input pic size for decode - void *vpcOutBufferDev_; // vpc output buffer + void *g_vpcOutBufferDev_; // vpc output buffer uint32_t vpcOutBufferSize_; // vpc output size }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/dvpp_process.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/dvpp_process.h index 37f6c145c..c4896a268 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/dvpp_process.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/dvpp_process.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 dvpp_process.h -* Description: handle dvpp process */ + +#ifndef YOLOV3_COCO_DETECTION_PICTURE_INC_DVPP_PROCESS_H +#define YOLOV3_COCO_DETECTION_PICTURE_INC_DVPP_PROCESS_H + #pragma once #include #include "acl/acl.h" @@ -37,7 +38,8 @@ public: void DestroyResource(); protected: - aclrtStream stream_; - acldvppChannelDesc *dvppChannelDesc_; + aclrtStream g_stream_; + acldvppChannelDesc *g_dvppChannelDesc_; }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/dvpp_resize.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/dvpp_resize.h index e11b90e68..140d5f2d4 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/dvpp_resize.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/dvpp_resize.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 dvpp_process.h -* Description: handle dvpp process */ + +#ifndef YOLOV3_COCO_DETECTION_PICTURE_INC_DVPP_RESIZE_H +#define YOLOV3_COCO_DETECTION_PICTURE_INC_DVPP_RESIZE_H + #pragma once #include #include "acl/acl.h" @@ -74,16 +75,17 @@ private: void DestroyOutputPara(); aclrtStream stream_; - acldvppChannelDesc *dvppChannelDesc_; - acldvppResizeConfig *resizeConfig_; + acldvppChannelDesc *g_dvppChannelDesc_; + acldvppResizeConfig *g_resizeConfig_; - acldvppPicDesc *vpcInputDesc_; // vpc input desc - acldvppPicDesc *vpcOutputDesc_; // vpc output desc + acldvppPicDesc *g_vpcInputDesc_; // vpc input desc + acldvppPicDesc *g_vpcOutputDesc_; // vpc output desc - uint8_t *inDevBuffer_; // input pic dev buffer - void *vpcOutBufferDev_; // vpc output buffer - uint32_t vpcOutBufferSize_; // vpc output size - Resolution size_; - acldvppPixelFormat format_; + uint8_t *g_inDevBuffer_; // input pic dev buffer + void *g_vpcOutBufferDev_; // vpc output buffer + uint32_t g_vpcOutBufferSize_; // vpc output size + Resolution g_size_; + acldvppPixelFormat g_format_; }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/model_process.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/model_process.h index 33365bdf0..c533c6795 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/model_process.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/model_process.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 model_process.h -* Description: handle model process */ + +#ifndef YOLOV3_COCO_DETECTION_PICTURE_INC_MODEL_PROCESS_H +#define YOLOV3_COCO_DETECTION_PICTURE_INC_MODEL_PROCESS_H + #pragma once #include #include "utils.h" @@ -102,16 +103,17 @@ class ModelProcess { aclmdlDataset *GetModelOutputData(); private: - bool loadFlag_; // model load flag - uint32_t modelId_; - void *modelMemPtr_; - size_t modelMemSize_; - void *modelWeightPtr_; - size_t modelWeightSize_; - aclmdlDesc *modelDesc_; - aclmdlDataset *input_; - aclmdlDataset *output_; - - bool isReleased_; + bool g_loadFlag_; // model load flag + uint32_t g_modelId_; + void *g_modelMemPtr_; + size_t g_modelMemSize_; + void *g_modelWeightPtr_; + size_t g_modelWeightSize_; + aclmdlDesc *g_modelDesc_; + aclmdlDataset *g_input_; + aclmdlDataset *g_output_; + + bool g_isReleased_; }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/object_detect.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/object_detect.h index dd346618d..4cdf562dc 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/object_detect.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/object_detect.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 YOLOV3_COCO_DETECTION_PICTURE_INC_OBJECT_DETECT_H +#define YOLOV3_COCO_DETECTION_PICTURE_INC_OBJECT_DETECT_H + #pragma once #include "utils.h" #include "acl/acl.h" @@ -51,21 +52,22 @@ private: void DestroyResource(); private: - int32_t deviceId_; - aclrtContext context_; - aclrtStream stream_; - uint32_t imageInfoSize_; - void* imageInfoBuf_; - ModelProcess model_; + int32_t g_deviceId_; + aclrtContext g_context_; + aclrtStream g_stream_; + uint32_t g_imageInfoSize_; + void* g_imageInfoBuf_; + ModelProcess g_model_; - const char* modelPath_; - uint32_t modelWidth_; - uint32_t modelHeight_; - uint32_t inputDataSize_; - DvppProcess dvpp_; - aclrtRunMode runMode_; + const char* g_modelPath_; + uint32_t g_modelWidth_; + uint32_t g_modelHeight_; + uint32_t g_inputDataSize_; + DvppProcess g_dvpp_; + aclrtRunMode g_runMode_; - bool isInited_; - bool isDeviceSet_; + bool g_isInited_; + bool g_isDeviceSet_; }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/utils.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/utils.h index 557bd4929..32e30341a 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/utils.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/utils.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 utils.h -* Description: handle file operations */ + +#ifndef YOLOV3_COCO_DETECTION_PICTURE_INC_UTILS_H +#define YOLOV3_COCO_DETECTION_PICTURE_INC_UTILS_H + #pragma once #include #include @@ -120,3 +121,5 @@ public: static void* CopyDataHostToDvpp(void* data, int size); static void* CopyDataDeviceToDvpp(void* data, int size); }; + +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/dvpp_jpegd.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/dvpp_jpegd.cpp index df98ce0a7..942b8d8ed 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/dvpp_jpegd.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/dvpp_jpegd.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,9 +12,6 @@ * 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 dvpp_process.cpp -* Description: handle dvpp process */ #include @@ -24,15 +21,15 @@ using namespace std; DvppJpegD::DvppJpegD(aclrtStream& stream, acldvppChannelDesc *dvppChannelDesc) - : stream_(stream), dvppChannelDesc_(dvppChannelDesc), - decodeOutBufferDev_(nullptr), decodeOutputDesc_(nullptr){ + : g_stream_(stream), g_dvppChannelDesc_(dvppChannelDesc), + g_decodeOutBufferDev_(nullptr), g_decodeOutputDesc_(nullptr) { } -DvppJpegD::~DvppJpegD(){ +DvppJpegD::~DvppJpegD() { DestroyDecodeResource(); } -Result DvppJpegD::InitDecodeOutputDesc(ImageData& inputImage){ +Result DvppJpegD::InitDecodeOutputDesc(ImageData& inputImage) { uint32_t decodeOutWidthStride = ALIGN_UP128(inputImage.width); uint32_t decodeOutHeightStride = ALIGN_UP16(inputImage.height); if (decodeOutWidthStride == 0 || decodeOutHeightStride == 0) { @@ -44,29 +41,29 @@ Result DvppJpegD::InitDecodeOutputDesc(ImageData& inputImage){ uint32_t decodeOutBufferSize = YUV420SP_SIZE(decodeOutWidthStride, decodeOutHeightStride) * 4; - aclError aclRet = acldvppMalloc(&decodeOutBufferDev_, decodeOutBufferSize); + aclError aclRet = acldvppMalloc(&g_decodeOutBufferDev_, decodeOutBufferSize); if (aclRet != ACL_SUCCESS) { - ERROR_LOG("acldvppMalloc decodeOutBufferDev_ failed, aclRet = %d", aclRet); + ERROR_LOG("acldvppMalloc g_decodeOutBufferDev_ failed, aclRet = %d", aclRet); return FAILED; } - decodeOutputDesc_ = acldvppCreatePicDesc(); - if (decodeOutputDesc_ == nullptr) { - ERROR_LOG("acldvppCreatePicDesc decodeOutputDesc_ failed"); + g_decodeOutputDesc_ = acldvppCreatePicDesc(); + if (g_decodeOutputDesc_ == nullptr) { + ERROR_LOG("acldvppCreatePicDesc g_decodeOutputDesc_ failed"); return FAILED; } - acldvppSetPicDescData(decodeOutputDesc_, decodeOutBufferDev_); - acldvppSetPicDescFormat(decodeOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); - acldvppSetPicDescWidth(decodeOutputDesc_, inputImage.width); - acldvppSetPicDescHeight(decodeOutputDesc_, inputImage.height); - acldvppSetPicDescWidthStride(decodeOutputDesc_, decodeOutWidthStride); - acldvppSetPicDescHeightStride(decodeOutputDesc_, decodeOutHeightStride); - acldvppSetPicDescSize(decodeOutputDesc_, decodeOutBufferSize); + acldvppSetPicDescData(g_decodeOutputDesc_, g_decodeOutBufferDev_); + acldvppSetPicDescFormat(g_decodeOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); + acldvppSetPicDescWidth(g_decodeOutputDesc_, inputImage.width); + acldvppSetPicDescHeight(g_decodeOutputDesc_, inputImage.height); + acldvppSetPicDescWidthStride(g_decodeOutputDesc_, decodeOutWidthStride); + acldvppSetPicDescHeightStride(g_decodeOutputDesc_, decodeOutHeightStride); + acldvppSetPicDescSize(g_decodeOutputDesc_, decodeOutBufferSize); return SUCCESS; } -Result DvppJpegD::Process(ImageData& dest, ImageData& src){ +Result DvppJpegD::Process(ImageData& dest, ImageData& src) { int ret = InitDecodeOutputDesc(src); if (ret != SUCCESS) { ERROR_LOG("InitDecodeOutputDesc failed"); @@ -75,14 +72,14 @@ Result DvppJpegD::Process(ImageData& dest, ImageData& src){ ImageData imageDevice; Utils::CopyImageDataToDvpp(imageDevice, src); - aclError aclRet = acldvppJpegDecodeAsync(dvppChannelDesc_, reinterpret_cast(imageDevice.data.get()), - imageDevice.size, decodeOutputDesc_, stream_); + aclError aclRet = acldvppJpegDecodeAsync(g_dvppChannelDesc_, reinterpret_cast(imageDevice.data.get()), + imageDevice.size, g_decodeOutputDesc_, g_stream_); if (aclRet != ACL_SUCCESS) { ERROR_LOG("acldvppJpegDecodeAsync failed, aclRet = %d", aclRet); return FAILED; } - aclRet = aclrtSynchronizeStream(stream_); + aclRet = aclrtSynchronizeStream(g_stream_); if (aclRet != ACL_SUCCESS) { ERROR_LOG("decode aclrtSynchronizeStream failed, aclRet = %d", aclRet); return FAILED; @@ -93,15 +90,15 @@ Result DvppJpegD::Process(ImageData& dest, ImageData& src){ dest.alignWidth = ALIGN_UP128(imageDevice.width); dest.alignHeight = ALIGN_UP16(imageDevice.height); dest.size = YUV420SP_SIZE(dest.alignWidth, dest.alignHeight); - dest.data = SHARED_PTR_DVPP_BUF(decodeOutBufferDev_); + dest.data = SHARED_PTR_DVPP_BUF(g_decodeOutBufferDev_); INFO_LOG("convert image success"); return SUCCESS; } -void DvppJpegD::DestroyDecodeResource(){ - if (decodeOutputDesc_ != nullptr) { - acldvppDestroyPicDesc(decodeOutputDesc_); - decodeOutputDesc_ = nullptr; +void DvppJpegD::DestroyDecodeResource() { + if (g_decodeOutputDesc_ != nullptr) { + acldvppDestroyPicDesc(g_decodeOutputDesc_); + g_decodeOutputDesc_ = nullptr; } } diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/dvpp_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/dvpp_process.cpp index e8a5fb531..e84315c0d 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/dvpp_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/dvpp_process.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,9 +12,6 @@ * 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 dvpp_process.cpp -* Description: handle dvpp process */ #include @@ -25,53 +22,52 @@ using namespace std; -DvppProcess::DvppProcess():dvppChannelDesc_(nullptr){ +DvppProcess::DvppProcess():g_dvppChannelDesc_(nullptr) { } -DvppProcess::~DvppProcess(){ +DvppProcess::~DvppProcess() { DestroyResource(); } -void DvppProcess::DestroyResource(){ +void DvppProcess::DestroyResource() { aclError aclRet; - if (dvppChannelDesc_ != nullptr) { - aclRet = acldvppDestroyChannel(dvppChannelDesc_); + if (g_dvppChannelDesc_ != nullptr) { + aclRet = acldvppDestroyChannel(g_dvppChannelDesc_); if (aclRet != ACL_SUCCESS) { ERROR_LOG("acldvppDestroyChannel failed, aclRet = %d", aclRet); } - (void)acldvppDestroyChannelDesc(dvppChannelDesc_); - dvppChannelDesc_ = nullptr; + (void)acldvppDestroyChannelDesc(g_dvppChannelDesc_); + g_dvppChannelDesc_ = nullptr; } } -Result DvppProcess::InitResource(aclrtStream& stream) -{ +Result DvppProcess::InitResource(aclrtStream& stream) { aclError aclRet; - dvppChannelDesc_ = acldvppCreateChannelDesc(); - if (dvppChannelDesc_ == nullptr) { + g_dvppChannelDesc_ = acldvppCreateChannelDesc(); + if (g_dvppChannelDesc_ == nullptr) { ERROR_LOG("acldvppCreateChannelDesc failed"); return FAILED; } - aclRet = acldvppCreateChannel(dvppChannelDesc_); + aclRet = acldvppCreateChannel(g_dvppChannelDesc_); if (aclRet != ACL_SUCCESS) { ERROR_LOG("acldvppCreateChannel failed, aclRet = %d", aclRet); return FAILED; } - stream_ = stream; + g_stream_ = stream; INFO_LOG("dvpp init resource ok"); return SUCCESS; } Result DvppProcess::Resize(ImageData& dest, ImageData& src, uint32_t width, uint32_t height) { - DvppResize resizeOp(stream_, dvppChannelDesc_, width, height); + DvppResize resizeOp(g_stream_, g_dvppChannelDesc_, width, height); return resizeOp.Process(dest, src); } Result DvppProcess::CvtJpegToYuv420sp(ImageData& dest, ImageData& src) { - DvppJpegD jpegD(stream_, dvppChannelDesc_); + DvppJpegD jpegD(g_stream_, g_dvppChannelDesc_); return jpegD.Process(dest, src); } diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/dvpp_resize.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/dvpp_resize.cpp index 7c608463b..4747176eb 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/dvpp_resize.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/dvpp_resize.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,9 +12,6 @@ * 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 dvpp_process.cpp -* Description: handle dvpp process */ #include @@ -25,18 +22,18 @@ using namespace std; DvppResize::DvppResize(aclrtStream& stream, acldvppChannelDesc *dvppChannelDesc, uint32_t width, uint32_t height) -: stream_(stream), dvppChannelDesc_(dvppChannelDesc), -resizeConfig_(nullptr), vpcInputDesc_(nullptr), vpcOutputDesc_(nullptr), -inDevBuffer_(nullptr),vpcOutBufferDev_(nullptr),vpcOutBufferSize_(0){ - size_.width = width; - size_.height = height; +: stream_(stream), g_dvppChannelDesc_(dvppChannelDesc), +g_resizeConfig_(nullptr), g_vpcInputDesc_(nullptr), g_vpcOutputDesc_(nullptr), +g_inDevBuffer_(nullptr),g_vpcOutBufferDev_(nullptr),g_vpcOutBufferSize_(0) { + g_size_.width = width; + g_size_.height = height; } -DvppResize::~DvppResize(){ +DvppResize::~DvppResize() { DestroyResizeResource(); } -Result DvppResize::InitResizeInputDesc(ImageData& inputImage){ +Result DvppResize::InitResizeInputDesc(ImageData& inputImage) { uint32_t alignWidth = ALIGN_UP128(inputImage.width); uint32_t alignHeight = ALIGN_UP16(inputImage.height); if (alignWidth == 0 || alignHeight == 0) { @@ -45,31 +42,31 @@ Result DvppResize::InitResizeInputDesc(ImageData& inputImage){ return FAILED; } uint32_t inputBufferSize = YUV420SP_SIZE(alignWidth, alignHeight); - vpcInputDesc_ = acldvppCreatePicDesc(); - if (vpcInputDesc_ == nullptr) { - ERROR_LOG("acldvppCreatePicDesc vpcInputDesc_ failed"); + g_vpcInputDesc_ = acldvppCreatePicDesc(); + if (g_vpcInputDesc_ == nullptr) { + ERROR_LOG("acldvppCreatePicDesc g_vpcInputDesc_ failed"); return FAILED; } uint32_t inputWidth = inputImage.width; uint32_t inputHeight = inputImage.height; - if(inputImage.width % 2 != 0) + if (inputImage.width % 2 != 0) inputWidth--; - if(inputImage.height % 2 != 0) + if (inputImage.height % 2 != 0) inputHeight--; - acldvppSetPicDescData(vpcInputDesc_, inputImage.data.get()); - acldvppSetPicDescFormat(vpcInputDesc_, format_); - acldvppSetPicDescWidth(vpcInputDesc_, inputWidth); - acldvppSetPicDescHeight(vpcInputDesc_, inputHeight); - acldvppSetPicDescWidthStride(vpcInputDesc_, alignWidth); - acldvppSetPicDescHeightStride(vpcInputDesc_, alignHeight); - acldvppSetPicDescSize(vpcInputDesc_, inputBufferSize); + acldvppSetPicDescData(g_vpcInputDesc_, inputImage.data.get()); + acldvppSetPicDescFormat(g_vpcInputDesc_, g_format_); + acldvppSetPicDescWidth(g_vpcInputDesc_, inputWidth); + acldvppSetPicDescHeight(g_vpcInputDesc_, inputHeight); + acldvppSetPicDescWidthStride(g_vpcInputDesc_, alignWidth); + acldvppSetPicDescHeightStride(g_vpcInputDesc_, alignHeight); + acldvppSetPicDescSize(g_vpcInputDesc_, inputBufferSize); return SUCCESS; } -Result DvppResize::InitResizeOutputDesc(){ - int resizeOutWidth = size_.width; - int resizeOutHeight = size_.height; +Result DvppResize::InitResizeOutputDesc() { + int resizeOutWidth = g_size_.width; + int resizeOutHeight = g_size_.height; int resizeOutWidthStride = ALIGN_UP16(resizeOutWidth); int resizeOutHeightStride = ALIGN_UP2(resizeOutHeight); if (resizeOutWidthStride == 0 || resizeOutHeightStride == 0) { @@ -77,34 +74,34 @@ Result DvppResize::InitResizeOutputDesc(){ return FAILED; } - vpcOutBufferSize_ = YUV420SP_SIZE(resizeOutWidthStride, resizeOutHeightStride); - aclError aclRet = acldvppMalloc(&vpcOutBufferDev_, vpcOutBufferSize_); + g_vpcOutBufferSize_ = YUV420SP_SIZE(resizeOutWidthStride, resizeOutHeightStride); + aclError aclRet = acldvppMalloc(&g_vpcOutBufferDev_, g_vpcOutBufferSize_); if (aclRet != ACL_SUCCESS) { - ERROR_LOG("acldvppMalloc vpcOutBufferDev_ failed, aclRet = %d", aclRet); + ERROR_LOG("acldvppMalloc g_vpcOutBufferDev_ failed, aclRet = %d", aclRet); return FAILED; } - vpcOutputDesc_ = acldvppCreatePicDesc(); - if (vpcOutputDesc_ == nullptr) { - ERROR_LOG("acldvppCreatePicDesc vpcOutputDesc_ failed"); + g_vpcOutputDesc_ = acldvppCreatePicDesc(); + if (g_vpcOutputDesc_ == nullptr) { + ERROR_LOG("acldvppCreatePicDesc g_vpcOutputDesc_ failed"); return FAILED; } - acldvppSetPicDescData(vpcOutputDesc_, vpcOutBufferDev_); - acldvppSetPicDescFormat(vpcOutputDesc_, format_); - acldvppSetPicDescWidth(vpcOutputDesc_, resizeOutWidth); - acldvppSetPicDescHeight(vpcOutputDesc_, resizeOutHeight); - acldvppSetPicDescWidthStride(vpcOutputDesc_, resizeOutWidthStride); - acldvppSetPicDescHeightStride(vpcOutputDesc_, resizeOutHeightStride); - acldvppSetPicDescSize(vpcOutputDesc_, vpcOutBufferSize_); + acldvppSetPicDescData(g_vpcOutputDesc_, g_vpcOutBufferDev_); + acldvppSetPicDescFormat(g_vpcOutputDesc_, g_format_); + acldvppSetPicDescWidth(g_vpcOutputDesc_, resizeOutWidth); + acldvppSetPicDescHeight(g_vpcOutputDesc_, resizeOutHeight); + acldvppSetPicDescWidthStride(g_vpcOutputDesc_, resizeOutWidthStride); + acldvppSetPicDescHeightStride(g_vpcOutputDesc_, resizeOutHeightStride); + acldvppSetPicDescSize(g_vpcOutputDesc_, g_vpcOutBufferSize_); return SUCCESS; } Result DvppResize::InitResizeResource(ImageData& inputImage) { - format_ = static_cast(PIXEL_FORMAT_YUV_SEMIPLANAR_420); - resizeConfig_ = acldvppCreateResizeConfig(); - if (resizeConfig_ == nullptr) { + g_format_ = static_cast(PIXEL_FORMAT_YUV_SEMIPLANAR_420); + g_resizeConfig_ = acldvppCreateResizeConfig(); + if (g_resizeConfig_ == nullptr) { ERROR_LOG("Dvpp resize init failed for create config failed"); return FAILED; } @@ -122,15 +119,15 @@ Result DvppResize::InitResizeResource(ImageData& inputImage) { return SUCCESS; } -Result DvppResize::Process(ImageData& resizedImage, ImageData& srcImage){ +Result DvppResize::Process(ImageData& resizedImage, ImageData& srcImage) { if (SUCCESS != InitResizeResource(srcImage)) { ERROR_LOG("Dvpp resize failed for init error"); return FAILED; } // resize pic - aclError aclRet = acldvppVpcResizeAsync(dvppChannelDesc_, vpcInputDesc_, - vpcOutputDesc_, resizeConfig_, stream_); + aclError aclRet = acldvppVpcResizeAsync(g_dvppChannelDesc_, g_vpcInputDesc_, + g_vpcOutputDesc_, g_resizeConfig_, stream_); if (aclRet != ACL_SUCCESS) { ERROR_LOG("acldvppVpcResizeAsync failed, aclRet = %d", aclRet); return FAILED; @@ -142,31 +139,31 @@ Result DvppResize::Process(ImageData& resizedImage, ImageData& srcImage){ return FAILED; } - resizedImage.width = size_.width; - resizedImage.height = size_.height; - resizedImage.alignWidth = ALIGN_UP16(size_.width); - resizedImage.alignHeight = ALIGN_UP2(size_.height); - resizedImage.size = vpcOutBufferSize_; - resizedImage.data = SHARED_PTR_DVPP_BUF(vpcOutBufferDev_); + resizedImage.width = g_size_.width; + resizedImage.height = g_size_.height; + resizedImage.alignWidth = ALIGN_UP16(g_size_.width); + resizedImage.alignHeight = ALIGN_UP2(g_size_.height); + resizedImage.size = g_vpcOutBufferSize_; + resizedImage.data = SHARED_PTR_DVPP_BUF(g_vpcOutBufferDev_); DestroyResizeResource(); return SUCCESS; } -void DvppResize::DestroyResizeResource(){ - if (resizeConfig_ != nullptr) { - (void)acldvppDestroyResizeConfig(resizeConfig_); - resizeConfig_ = nullptr; +void DvppResize::DestroyResizeResource() { + if (g_resizeConfig_ != nullptr) { + (void)acldvppDestroyResizeConfig(g_resizeConfig_); + g_resizeConfig_ = nullptr; } - if (vpcInputDesc_ != nullptr) { - (void)acldvppDestroyPicDesc(vpcInputDesc_); - vpcInputDesc_ = nullptr; + if (g_vpcInputDesc_ != nullptr) { + (void)acldvppDestroyPicDesc(g_vpcInputDesc_); + g_vpcInputDesc_ = nullptr; } - if (vpcOutputDesc_ != nullptr) { - (void)acldvppDestroyPicDesc(vpcOutputDesc_); - vpcOutputDesc_ = nullptr; + if (g_vpcOutputDesc_ != nullptr) { + (void)acldvppDestroyPicDesc(g_vpcOutputDesc_); + g_vpcOutputDesc_ = nullptr; } } diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/main.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/main.cpp index 351d96e3f..80d08fa4e 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/main.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/main.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,9 +12,6 @@ * 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 main.cpp -* Description: dvpp sample main func */ #include @@ -26,28 +23,28 @@ using namespace std; namespace { -uint32_t kModelWidth = 416; -uint32_t kModelHeight = 416; -const char* kModelPath = "../model/yolov3.om"; +uint32_t g_ModelWidth = 416; +uint32_t g_ModelHeight = 416; +const char* g_ModelPath = "../model/yolov3.om"; } int main(int argc, char *argv[]) { - //Check input args: the path of input picture, and ignore hidden dotfile directory - if((argc != 2) || (argv[1] == nullptr)){ + // Check input args: the path of input picture, and ignore hidden dotfile directory + if ((argc != 2) || (argv[1] == nullptr)) { ERROR_LOG("Please input: ./main "); return FAILED; } - //Instantiate the object detection class - ObjectDetect detect(kModelPath, kModelWidth, kModelHeight); - //Initialize the acl resources, dvpp, load model, - //and malloc input memory of input which is const + // Instantiate the object detection class + ObjectDetect detect(g_ModelPath, g_ModelWidth, g_ModelHeight); + // Initialize the acl resources, dvpp, load model, + // and malloc input memory of input which is const Result ret = detect.Init(); if (ret != SUCCESS) { ERROR_LOG("Classification Init resource failed"); return FAILED; } - //Get all the image file path in the image directory + // Get all the image file path in the image directory string inputImageDir = string(argv[1]); vector fileVec; Utils::GetAllFiles(inputImageDir, fileVec); @@ -65,7 +62,7 @@ int main(int argc, char *argv[]) { return FAILED; } - //Preprocess: copy image to device, convert to yuv, and resize + // Preprocess: copy image to device, convert to yuv, and resize ImageData resizedImage; Result ret = detect.Preprocess(resizedImage, image); if (ret != SUCCESS) { @@ -73,16 +70,16 @@ int main(int argc, char *argv[]) { imageFile.c_str()); continue; } - //Send the resized picture to the model for inference - //and get the inference results + // Send the resized picture to the model for inference + // and get the inference results aclmdlDataset* inferenceOutput = nullptr; ret = detect.Inference(inferenceOutput, resizedImage); if ((ret != SUCCESS) || (inferenceOutput == nullptr)) { ERROR_LOG("Inference model inference output data failed"); return FAILED; } - //Analyze the inference output, mark the object category and - //location by the inference result + // Analyze the inference output, mark the object category and + // location by the inference result ret = detect.Postprocess(image, inferenceOutput, imageFile); if (ret != SUCCESS) { ERROR_LOG("Process model inference output data failed"); diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/model_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/model_process.cpp index 66ef4288b..07ddb80a3 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/model_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/model_process.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,79 +12,77 @@ * 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 model_process.cpp -* Description: handle model process */ + #include "model_process.h" #include #include "utils.h" using namespace std; -ModelProcess::ModelProcess():loadFlag_(false), modelId_(0), modelMemPtr_(nullptr), modelMemSize_(0), -modelWeightPtr_(nullptr),modelWeightSize_(0), modelDesc_(nullptr), input_(nullptr), output_(nullptr), -isReleased_(false){ +ModelProcess::ModelProcess():g_loadFlag_(false), g_modelId_(0), g_modelMemPtr_(nullptr), g_modelMemSize_(0), +g_modelWeightPtr_(nullptr),g_modelWeightSize_(0), g_modelDesc_(nullptr), g_input_(nullptr), g_output_(nullptr), +g_isReleased_(false) { } -ModelProcess::~ModelProcess(){ +ModelProcess::~ModelProcess() { DestroyResource(); } -void ModelProcess::DestroyResource(){ - if (isReleased_) +void ModelProcess::DestroyResource() { + if (g_isReleased_) return; Unload(); DestroyDesc(); DestroyInput(); DestroyOutput(); - isReleased_ = true; + g_isReleased_ = true; } -Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath){ - if (loadFlag_) { +Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath) { + if (g_loadFlag_) { ERROR_LOG("has already loaded a model"); return FAILED; } - aclError ret = aclmdlQuerySize(modelPath, &modelMemSize_, &modelWeightSize_); + aclError ret = aclmdlQuerySize(modelPath, &g_modelMemSize_, &g_modelWeightSize_); if (ret != ACL_SUCCESS) { ERROR_LOG("query model failed, model file is %s", modelPath); return FAILED; } - ret = aclrtMalloc(&modelMemPtr_, modelMemSize_, ACL_MEM_MALLOC_HUGE_FIRST); + ret = aclrtMalloc(&g_modelMemPtr_, g_modelMemSize_, ACL_MEM_MALLOC_HUGE_FIRST); if (ret != ACL_SUCCESS) { - ERROR_LOG("malloc buffer for mem failed, require size is %zu", modelMemSize_); + ERROR_LOG("malloc buffer for mem failed, require size is %zu", g_modelMemSize_); return FAILED; } - ret = aclrtMalloc(&modelWeightPtr_, modelWeightSize_, ACL_MEM_MALLOC_HUGE_FIRST); + ret = aclrtMalloc(&g_modelWeightPtr_, g_modelWeightSize_, ACL_MEM_MALLOC_HUGE_FIRST); if (ret != ACL_SUCCESS) { - ERROR_LOG("malloc buffer for weight failed, require size is %zu", modelWeightSize_); + ERROR_LOG("malloc buffer for weight failed, require size is %zu", g_modelWeightSize_); return FAILED; } - ret = aclmdlLoadFromFileWithMem(modelPath, &modelId_, modelMemPtr_, - modelMemSize_, modelWeightPtr_, modelWeightSize_); + ret = aclmdlLoadFromFileWithMem(modelPath, &g_modelId_, g_modelMemPtr_, + g_modelMemSize_, g_modelWeightPtr_, g_modelWeightSize_); if (ret != ACL_SUCCESS) { ERROR_LOG("load model from file failed, model file is %s", modelPath); return FAILED; } - loadFlag_ = true; + g_loadFlag_ = true; INFO_LOG("load model %s success", modelPath); return SUCCESS; } -Result ModelProcess::CreateDesc(){ - modelDesc_ = aclmdlCreateDesc(); - if (modelDesc_ == nullptr) { +Result ModelProcess::CreateDesc() { + g_modelDesc_ = aclmdlCreateDesc(); + if (g_modelDesc_ == nullptr) { ERROR_LOG("create model description failed"); return FAILED; } - aclError ret = aclmdlGetDesc(modelDesc_, modelId_); + aclError ret = aclmdlGetDesc(g_modelDesc_, g_modelId_); if (ret != ACL_SUCCESS) { ERROR_LOG("get model description failed"); return FAILED; @@ -94,17 +92,17 @@ Result ModelProcess::CreateDesc(){ return SUCCESS; } -void ModelProcess::DestroyDesc(){ - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; +void ModelProcess::DestroyDesc() { + if (g_modelDesc_ != nullptr) { + (void)aclmdlDestroyDesc(g_modelDesc_); + g_modelDesc_ = nullptr; } } Result ModelProcess::CreateInput(void *input1, size_t input1size, - void* input2, size_t input2size){ - input_ = aclmdlCreateDataset(); - if (input_ == nullptr) { + void* input2, size_t input2size) { + g_input_ = aclmdlCreateDataset(); + if (g_input_ == nullptr) { ERROR_LOG("can't create dataset, create input failed"); return FAILED; } @@ -115,7 +113,7 @@ Result ModelProcess::CreateInput(void *input1, size_t input1size, return FAILED; } - aclError ret = aclmdlAddDatasetBuffer(input_, inputData); + aclError ret = aclmdlAddDatasetBuffer(g_input_, inputData); if (inputData == nullptr) { ERROR_LOG("can't add data buffer, create input failed"); aclDestroyDataBuffer(inputData); @@ -129,7 +127,7 @@ Result ModelProcess::CreateInput(void *input1, size_t input1size, return FAILED; } - ret = aclmdlAddDatasetBuffer(input_, inputData2); + ret = aclmdlAddDatasetBuffer(g_input_, inputData2); if (inputData == nullptr) { ERROR_LOG("can't add data buffer, create input failed"); aclDestroyDataBuffer(inputData2); @@ -140,34 +138,34 @@ Result ModelProcess::CreateInput(void *input1, size_t input1size, return SUCCESS; } -void ModelProcess::DestroyInput(){ - if (input_ == nullptr) { +void ModelProcess::DestroyInput() { + if (g_input_ == nullptr) { return; } - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(input_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(input_, i); + for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(g_input_); ++i) { + aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(g_input_, i); aclDestroyDataBuffer(dataBuffer); } - aclmdlDestroyDataset(input_); - input_ = nullptr; + aclmdlDestroyDataset(g_input_); + g_input_ = nullptr; } -Result ModelProcess::CreateOutput(){ - if (modelDesc_ == nullptr) { +Result ModelProcess::CreateOutput() { + if (g_modelDesc_ == nullptr) { ERROR_LOG("no model description, create ouput failed"); return FAILED; } - output_ = aclmdlCreateDataset(); - if (output_ == nullptr) { + g_output_ = aclmdlCreateDataset(); + if (g_output_ == nullptr) { ERROR_LOG("can't create dataset, create output failed"); return FAILED; } - size_t outputSize = aclmdlGetNumOutputs(modelDesc_); + size_t outputSize = aclmdlGetNumOutputs(g_modelDesc_); for (size_t i = 0; i < outputSize; ++i) { - size_t buffer_size = aclmdlGetOutputSizeByIndex(modelDesc_, i); + size_t buffer_size = aclmdlGetOutputSizeByIndex(g_modelDesc_, i); void *outputBuffer = nullptr; aclError ret = aclrtMalloc(&outputBuffer, buffer_size, ACL_MEM_MALLOC_NORMAL_ONLY); @@ -183,7 +181,7 @@ Result ModelProcess::CreateOutput(){ return FAILED; } - ret = aclmdlAddDatasetBuffer(output_, outputData); + ret = aclmdlAddDatasetBuffer(g_output_, outputData); if (ret != ACL_SUCCESS) { ERROR_LOG("can't add data buffer, create output failed"); aclrtFree(outputBuffer); @@ -196,26 +194,26 @@ Result ModelProcess::CreateOutput(){ return SUCCESS; } -void ModelProcess::DestroyOutput(){ - if (output_ == nullptr) { +void ModelProcess::DestroyOutput() { + if (g_output_ == nullptr) { return; } - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); + for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(g_output_); ++i) { + aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(g_output_, i); void* data = aclGetDataBufferAddr(dataBuffer); (void)aclrtFree(data); (void)aclDestroyDataBuffer(dataBuffer); } - (void)aclmdlDestroyDataset(output_); - output_ = nullptr; + (void)aclmdlDestroyDataset(g_output_); + g_output_ = nullptr; } -Result ModelProcess::Execute(){ - aclError ret = aclmdlExecute(modelId_, input_, output_); +Result ModelProcess::Execute() { + aclError ret = aclmdlExecute(g_modelId_, g_input_, g_output_); if (ret != ACL_SUCCESS) { - ERROR_LOG("execute model failed, modelId is %u", modelId_); + ERROR_LOG("execute model failed, modelId is %u", g_modelId_); return FAILED; } @@ -223,39 +221,38 @@ Result ModelProcess::Execute(){ return SUCCESS; } -void ModelProcess::Unload() -{ - if (!loadFlag_) { +void ModelProcess::Unload() { + if (!g_loadFlag_) { WARN_LOG("no model had been loaded, unload failed"); return; } - aclError ret = aclmdlUnload(modelId_); + aclError ret = aclmdlUnload(g_modelId_); if (ret != ACL_SUCCESS) { - ERROR_LOG("unload model failed, modelId is %u", modelId_); + ERROR_LOG("unload model failed, modelId is %u", g_modelId_); } - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; + if (g_modelDesc_ != nullptr) { + (void)aclmdlDestroyDesc(g_modelDesc_); + g_modelDesc_ = nullptr; } - if (modelMemPtr_ != nullptr) { - aclrtFree(modelMemPtr_); - modelMemPtr_ = nullptr; - modelMemSize_ = 0; + if (g_modelMemPtr_ != nullptr) { + aclrtFree(g_modelMemPtr_); + g_modelMemPtr_ = nullptr; + g_modelMemSize_ = 0; } - if (modelWeightPtr_ != nullptr) { - aclrtFree(modelWeightPtr_); - modelWeightPtr_ = nullptr; - modelWeightSize_ = 0; + if (g_modelWeightPtr_ != nullptr) { + aclrtFree(g_modelWeightPtr_); + g_modelWeightPtr_ = nullptr; + g_modelWeightSize_ = 0; } - loadFlag_ = false; - INFO_LOG("unload model success, modelId is %u", modelId_); + g_loadFlag_ = false; + INFO_LOG("unload model success, modelId is %u", g_modelId_); } -aclmdlDataset *ModelProcess::GetModelOutputData(){ - return output_; +aclmdlDataset *ModelProcess::GetModelOutputData() { + return g_output_; } diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/object_detect.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/object_detect.cpp index 1d0bb6174..3fdab378a 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/object_detect.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/object_detect.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,8 @@ * 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 "object_detect.h" #include #include @@ -65,7 +63,7 @@ namespace { const string kFileSperator = "/"; // opencv color list for boundingbox - const vector kColors{ + const vector kColors { cv::Scalar(237, 149, 100), cv::Scalar(0, 215, 255), cv::Scalar(50, 205, 50), cv::Scalar(139, 85, 26) }; @@ -73,11 +71,11 @@ namespace { ObjectDetect::ObjectDetect(const char* modelPath, uint32_t modelWidth, uint32_t modelHeight) -:deviceId_(0), context_(nullptr), stream_(nullptr), modelWidth_(modelWidth), -modelHeight_(modelHeight), isInited_(false), isDeviceSet_(false){ - imageInfoSize_ = 0; - imageInfoBuf_ = nullptr; - modelPath_ = modelPath; +:g_deviceId_(0), g_context_(nullptr), g_stream_(nullptr), g_modelWidth_(modelWidth), +g_modelHeight_(modelHeight), g_isInited_(false), g_isDeviceSet_(false) { + g_imageInfoSize_ = 0; + g_imageInfoBuf_ = nullptr; + g_modelPath_ = modelPath; } ObjectDetect::~ObjectDetect() { @@ -95,15 +93,15 @@ Result ObjectDetect::InitResource() { INFO_LOG("acl init success"); // open device - ret = aclrtSetDevice(deviceId_); + ret = aclrtSetDevice(g_deviceId_); if (ret != ACL_SUCCESS) { - ERROR_LOG("acl open device %d failed", deviceId_); + ERROR_LOG("acl open device %d failed", g_deviceId_); return FAILED; } - isDeviceSet_ = true; - INFO_LOG("open device %d success", deviceId_); + g_isDeviceSet_ = true; + INFO_LOG("open device %d success", g_deviceId_); // create context (set current) - ret = aclrtCreateContext(&context_, deviceId_); + ret = aclrtCreateContext(&g_context_, g_deviceId_); if (ret != ACL_SUCCESS) { ERROR_LOG("acl create context failed"); return FAILED; @@ -111,14 +109,14 @@ Result ObjectDetect::InitResource() { INFO_LOG("create context success"); // create stream - ret = aclrtCreateStream(&stream_); + ret = aclrtCreateStream(&g_stream_); if (ret != ACL_SUCCESS) { ERROR_LOG("acl create stream failed"); return FAILED; } INFO_LOG("create stream success"); - ret = aclrtGetRunMode(&runMode_); + ret = aclrtGetRunMode(&g_runMode_); if (ret != ACL_SUCCESS) { ERROR_LOG("acl get run mode failed"); return FAILED; @@ -128,19 +126,19 @@ Result ObjectDetect::InitResource() { } Result ObjectDetect::InitModel(const char* omModelPath) { - Result ret = model_.LoadModelFromFileWithMem(omModelPath); + Result ret = g_model_.LoadModelFromFileWithMem(omModelPath); if (ret != SUCCESS) { ERROR_LOG("execute LoadModelFromFileWithMem failed"); return FAILED; } - ret = model_.CreateDesc(); + ret = g_model_.CreateDesc(); if (ret != SUCCESS) { ERROR_LOG("execute CreateDesc failed"); return FAILED; } - ret = model_.CreateOutput(); + ret = g_model_.CreateOutput(); if (ret != SUCCESS) { ERROR_LOG("execute CreateOutput failed"); return FAILED; @@ -149,15 +147,15 @@ Result ObjectDetect::InitModel(const char* omModelPath) { return SUCCESS; } -Result ObjectDetect::CreateImageInfoBuffer(){ - const float imageInfo[4] = {(float)modelWidth_, (float)modelHeight_, - (float)modelWidth_, (float)modelHeight_}; - imageInfoSize_ = sizeof(imageInfo); - if (runMode_ == ACL_HOST) - imageInfoBuf_ = Utils::CopyDataHostToDevice((void *)imageInfo, imageInfoSize_); +Result ObjectDetect::CreateImageInfoBuffer() { + const float imageInfo[4] = {(float)g_modelWidth_, (float)g_modelHeight_, + (float)g_modelWidth_, (float)g_modelHeight_}; + g_imageInfoSize_ = sizeof(imageInfo); + if (g_runMode_ == ACL_HOST) + g_imageInfoBuf_ = Utils::CopyDataHostToDevice((void *)imageInfo, g_imageInfoSize_); else - imageInfoBuf_ = Utils::CopyDataDeviceToDevice((void *)imageInfo, imageInfoSize_); - if (imageInfoBuf_ == nullptr) { + g_imageInfoBuf_ = Utils::CopyDataDeviceToDevice((void *)imageInfo, g_imageInfoSize_); + if (g_imageInfoBuf_ == nullptr) { ERROR_LOG("Copy image info to device failed"); return FAILED; } @@ -166,7 +164,7 @@ Result ObjectDetect::CreateImageInfoBuffer(){ } Result ObjectDetect::Init() { - if (isInited_) { + if (g_isInited_) { INFO_LOG("Object detection instance is initied already!"); return SUCCESS; } @@ -177,13 +175,13 @@ Result ObjectDetect::Init() { return FAILED; } - ret = InitModel(modelPath_); + ret = InitModel(g_modelPath_); if (ret != SUCCESS) { ERROR_LOG("Init model failed"); return FAILED; } - ret = dvpp_.InitResource(stream_); + ret = g_dvpp_.InitResource(g_stream_); if (ret != SUCCESS) { ERROR_LOG("Init dvpp failed"); return FAILED; @@ -195,23 +193,23 @@ Result ObjectDetect::Init() { return FAILED; } - isInited_ = true; + g_isInited_ = true; return SUCCESS; } Result ObjectDetect::Preprocess(ImageData& resizedImage, ImageData& srcImage) { ImageData imageDevice; - Utils::CopyImageDataToDevice(imageDevice, srcImage, runMode_); + Utils::CopyImageDataToDevice(imageDevice, srcImage, g_runMode_); ImageData yuvImage; - Result ret = dvpp_.CvtJpegToYuv420sp(yuvImage, imageDevice); + Result ret = g_dvpp_.CvtJpegToYuv420sp(yuvImage, imageDevice); if (ret == FAILED) { ERROR_LOG("Convert jpeg to yuv failed"); return FAILED; } - //resize - ret = dvpp_.Resize(resizedImage, yuvImage, modelWidth_, modelHeight_); + // resize + ret = g_dvpp_.Resize(resizedImage, yuvImage, g_modelWidth_, g_modelHeight_); if (ret == FAILED) { ERROR_LOG("Resize image failed"); return FAILED; @@ -222,21 +220,21 @@ Result ObjectDetect::Preprocess(ImageData& resizedImage, ImageData& srcImage) { Result ObjectDetect::Inference(aclmdlDataset*& inferenceOutput, ImageData& resizedImage) { - Result ret = model_.CreateInput(resizedImage.data.get(), + Result ret = g_model_.CreateInput(resizedImage.data.get(), resizedImage.size, - imageInfoBuf_, imageInfoSize_); + g_imageInfoBuf_, g_imageInfoSize_); if (ret != SUCCESS) { ERROR_LOG("Create mode input dataset failed"); return FAILED; } - ret = model_.Execute(); + ret = g_model_.Execute(); if (ret != SUCCESS) { ERROR_LOG("Execute model inference failed"); return FAILED; } - inferenceOutput = model_.GetModelOutputData(); + inferenceOutput = g_model_.GetModelOutputData(); return SUCCESS; } @@ -253,8 +251,8 @@ const string& origImagePath) { uint32_t totalBox = boxNum[0]; vector detectResults; - float widthScale = (float)(image.width) / modelWidth_; - float heightScale = (float)(image.height) / modelHeight_; + float widthScale = (float)(image.width) / g_modelWidth_; + float heightScale = (float)(image.height) / g_modelHeight_; for (uint32_t i = 0; i < totalBox; i++) { BBox boundBox; @@ -273,7 +271,7 @@ const string& origImagePath) { } DrawBoundBoxToImage(detectResults, origImagePath); - if (runMode_ == ACL_HOST) { + if (g_runMode_ == ACL_HOST) { delete[]((uint8_t *)detectData); delete[]((uint8_t*)boxNum); } @@ -306,7 +304,7 @@ uint32_t idx) { } void* data = nullptr; - if (runMode_ == ACL_HOST) { + if (g_runMode_ == ACL_HOST) { data = Utils::CopyDataDeviceToLocal(dataBufferDev, bufferSize); if (data == nullptr) { ERROR_LOG("Copy inference output to host failed"); @@ -346,35 +344,35 @@ const string& origImagePath) { cv::imwrite(sstream.str(), image); } -void ObjectDetect::DestroyResource(){ - aclrtFree(imageInfoBuf_); - model_.DestroyResource(); - dvpp_.DestroyResource(); +void ObjectDetect::DestroyResource() { + aclrtFree(g_imageInfoBuf_); + g_model_.DestroyResource(); + g_dvpp_.DestroyResource(); aclError ret; - if (stream_ != nullptr) { - ret = aclrtDestroyStream(stream_); + if (g_stream_ != nullptr) { + ret = aclrtDestroyStream(g_stream_); if (ret != ACL_SUCCESS) { ERROR_LOG("destroy stream failed"); } - stream_ = nullptr; + g_stream_ = nullptr; } INFO_LOG("end to destroy stream"); - if (context_ != nullptr) { - ret = aclrtDestroyContext(context_); + if (g_context_ != nullptr) { + ret = aclrtDestroyContext(g_context_); if (ret != ACL_SUCCESS) { ERROR_LOG("destroy context failed"); } - context_ = nullptr; + g_context_ = nullptr; } INFO_LOG("end to destroy context"); - if (isDeviceSet_) { - ret = aclrtResetDevice(deviceId_); + if (g_isDeviceSet_) { + ret = aclrtResetDevice(g_deviceId_); if (ret != ACL_SUCCESS) { ERROR_LOG("reset device failed"); } - INFO_LOG("end to reset device is %d", deviceId_); + INFO_LOG("end to reset device is %d", g_deviceId_); } ret = aclFinalize(); diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/utils.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/utils.cpp index f1c06d86f..b79d509d2 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/utils.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/utils.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,8 @@ * 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 utils.cpp -* Description: handle file operations */ + #include "utils.h" #include #include @@ -171,14 +169,14 @@ Result Utils::CopyImageDataToDvpp(ImageData& imageDevice, ImageData srcImage) { } void* buffer = nullptr; - if (runMode_ == ACL_HOST){ + if (runMode_ == ACL_HOST) { buffer = Utils::CopyDataHostToDvpp(srcImage.data.get(), srcImage.size); if (buffer == nullptr) { ERROR_LOG("Copy image to device failed"); return FAILED; } } - else{ + else { buffer = Utils::CopyDataDeviceToDvpp(srcImage.data.get(), srcImage.size); if (buffer == nullptr) { ERROR_LOG("Copy image to device failed"); @@ -257,8 +255,7 @@ Result Utils::CopyImageDataToDevice(ImageData& imageDevice, return SUCCESS; } -int Utils::ReadImageFile(ImageData& image, std::string fileName) -{ +int Utils::ReadImageFile(ImageData& image, std::string fileName) { struct stat sBuf; int fileStatus = stat(fileName.data(), &sBuf); if (fileStatus == -1) { diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/inc/dvpp_process.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/inc/dvpp_process.h index e4f94db3b..b935cb0d5 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/inc/dvpp_process.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/inc/dvpp_process.h @@ -1,12 +1,22 @@ -/** -* @file dvpp_process.h +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. * -* Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +* 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. */ + +#ifndef YOLOV3_COCO_DETECTION_PICTURE_DVPP_WITH_AIPP_INC_DVPP_PROCESS_H +#define YOLOV3_COCO_DETECTION_PICTURE_DVPP_WITH_AIPP_INC_DVPP_PROCESS_H + #pragma once #include #include "utils.h" @@ -75,30 +85,31 @@ private: void DestroyOutputPara(); - acldvppChannelDesc *dvppChannelDesc_; - aclrtStream stream_; - acldvppResizeConfig *resizeConfig_; + acldvppChannelDesc *g_dvppChannelDesc_; + aclrtStream g_stream_; + acldvppResizeConfig *g_resizeConfig_; - void* decodeOutDevBuffer_; // decode output buffer - acldvppPicDesc *decodeOutputDesc_; //decode output desc + void* g_decodeOutDevBuffer_; // decode output buffer + acldvppPicDesc *g_decodeOutputDesc_; // decode output desc - acldvppPicDesc *resizeInputDesc_; // resize input desc - acldvppPicDesc *resizeOutputDesc_; // resize output desc + acldvppPicDesc *g_resizeInputDesc_; // resize input desc + acldvppPicDesc *g_resizeOutputDesc_; // resize output desc - void *inDevBuffer_; // decode input buffer - uint32_t inDevBufferSize_; // dvpp input buffer size - uint32_t jpegDecodeOutputSize_; // jpeg decode output size + void *g_inDevBuffer_; // decode input buffer + uint32_t g_inDevBufferSize_; // dvpp input buffer size + uint32_t g_jpegDecodeOutputSize_; // jpeg decode output size - uint32_t decodeOutputWidth_; // decode output width - uint32_t decodeOutputWidthStride_; // decode output width aligned - uint32_t decodeOutputHeight_; // decode output height + uint32_t g_decodeOutputWidth_; // decode output width + uint32_t g_decodeOutputWidthStride_; // decode output width aligned + uint32_t g_decodeOutputHeight_; // decode output height - void *resizeOutBufferDev_; // resize output buffer - uint32_t resizeOutBufferSize_; // resize output size + void *g_resizeOutBufferDev_; // resize output buffer + uint32_t g_resizeOutBufferSize_; // resize output size - uint32_t modelInputWidth_; // model input width - uint32_t modelInputHeight_; // model input height - uint32_t resizeOutWidthStride_; // resize output width aligned - uint32_t resizeOutHeightStride_; // resize output height aligned + uint32_t g_modelInputWidth_; // model input width + uint32_t g_modelInputHeight_; // model input height + uint32_t g_resizeOutWidthStride_; // resize output width aligned + uint32_t g_resizeOutHeightStride_; // resize output height aligned }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/inc/model_process.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/inc/model_process.h index cc449bc27..36f3143dd 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/inc/model_process.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/inc/model_process.h @@ -1,12 +1,22 @@ -/** -* @file model_process.h +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. * -* Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +* 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. */ + +#ifndef YOLOV3_COCO_DETECTION_PICTURE_DVPP_WITH_AIPP_INC_MODEL_PROCESS_H +#define YOLOV3_COCO_DETECTION_PICTURE_DVPP_WITH_AIPP_INC_MODEL_PROCESS_H + #pragma once #include #include "utils.h" @@ -55,7 +65,7 @@ public: * @param [in] input2size: input buffer size * @return result */ - Result CreateInput(void *input1, size_t input1size, void *input2, size_t input2size); + Result CreateInput(void *input1, size_t input1Size, void *input2, size_t input2Size); /** * @brief destroy input resource @@ -92,13 +102,15 @@ public: Result GetModelInputWH(int &width, int &height); private: - uint32_t modelId_; - size_t modelWorkSize_; // model work memory buffer size - size_t modelWeightSize_; // model weight memory buffer size - void *modelWorkPtr_; // model work memory buffer - void *modelWeightPtr_; // model weight memory buffer - bool loadFlag_; // model load flag - aclmdlDesc *modelDesc_; - aclmdlDataset *input_; - aclmdlDataset *output_; + uint32_t g_modelId_; + size_t g_modelWorkSize_; // model work memory buffer size + size_t g_modelWeightSize_; // model weight memory buffer size + void *g_modelWorkPtr_; // model work memory buffer + void *g_modelWeightPtr_; // model weight memory buffer + bool g_loadFlag_; // model load flag + aclmdlDesc *g_modelDesc_; + aclmdlDataset *g_input_; + aclmdlDataset *g_output_; }; + +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/inc/sample_process.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/inc/sample_process.h index 09b654d87..2b70ace5f 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/inc/sample_process.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/inc/sample_process.h @@ -1,12 +1,22 @@ -/** -* @file sample_process.h +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. * -* Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +* 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. */ + +#ifndef YOLOV3_COCO_DETECTION_PICTURE_DVPP_WITH_AIPP_INC_SAMPLE_PROCESS_H +#define YOLOV3_COCO_DETECTION_PICTURE_DVPP_WITH_AIPP_INC_SAMPLE_PROCESS_H + #pragma once #include "utils.h" #include "acl/acl.h" @@ -43,9 +53,10 @@ public: private: void DestroyResource(); - aclrtRunMode runMode_; - int32_t deviceId_; - aclrtContext context_; - aclrtStream stream_; + aclrtRunMode g_runMode_; + int32_t g_deviceId_; + aclrtContext g_context_; + aclrtStream g_stream_; }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/inc/utils.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/inc/utils.h index d363407cd..97e2ef04b 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/inc/utils.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/inc/utils.h @@ -1,12 +1,22 @@ -/** -* @file utils.h +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. * -* Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +* 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. */ + +#ifndef YOLOV3_COCO_DETECTION_PICTURE_DVPP_WITH_AIPP_INC_UTILS_H +#define YOLOV3_COCO_DETECTION_PICTURE_DVPP_WITH_AIPP_INC_UTILS_H + #pragma once #include #include @@ -55,12 +65,10 @@ struct Resolution { class RunStatus { public: - static void SetDeviceStatus(bool isDevice) - { + static void SetDeviceStatus(bool isDevice) { isDevice_ = isDevice; } - static bool GetDeviceStatus() - { + static bool GetDeviceStatus() { return isDevice_; } private: @@ -105,3 +113,4 @@ public: static void* CopyDataDeviceToLocal(void* deviceData, uint32_t dataSize); }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/dvpp_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/dvpp_process.cpp index cd39ccb15..91b91d51c 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/dvpp_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/dvpp_process.cpp @@ -1,20 +1,26 @@ -/** -* @file dvpp_process.cpp +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. * -* Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at + +* http://www.apache.org/licenses/LICENSE-2.0 + +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. */ + #include "dvpp_process.h" #include "acl/acl.h" #include "utils.h" using namespace std; namespace { - uint32_t AlignSize(uint32_t origSize, uint32_t alignment) - { + uint32_t AlignSize(uint32_t origSize, uint32_t alignment) { if (alignment == 0) { return 0; } @@ -23,37 +29,34 @@ namespace { } } -DvppProcess::DvppProcess(aclrtStream& stream) : stream_(stream), dvppChannelDesc_(nullptr), - resizeConfig_(nullptr), decodeOutDevBuffer_(nullptr),decodeOutputDesc_(nullptr), resizeInputDesc_(nullptr), - resizeOutputDesc_(nullptr), inDevBuffer_(nullptr),inDevBufferSize_(0), jpegDecodeOutputSize_(0), - decodeOutputWidth_(0), decodeOutputWidthStride_(0), decodeOutputHeight_(0), resizeOutBufferDev_(nullptr), - resizeOutBufferSize_(0), modelInputWidth_(0), modelInputHeight_(0), resizeOutWidthStride_(0), - resizeOutHeightStride_(0) -{ +DvppProcess::DvppProcess(aclrtStream& stream) : g_stream_(stream), g_dvppChannelDesc_(nullptr), + g_resizeConfig_(nullptr), g_decodeOutDevBuffer_(nullptr),g_decodeOutputDesc_(nullptr), g_resizeInputDesc_(nullptr), + g_resizeOutputDesc_(nullptr), g_inDevBuffer_(nullptr),g_inDevBufferSize_(0), g_jpegDecodeOutputSize_(0), + g_decodeOutputWidth_(0), g_decodeOutputWidthStride_(0), g_decodeOutputHeight_(0), g_resizeOutBufferDev_(nullptr), + g_resizeOutBufferSize_(0), g_modelInputWidth_(0), g_modelInputHeight_(0), g_resizeOutWidthStride_(0), + g_resizeOutHeightStride_(0) { } -DvppProcess::~DvppProcess() -{ +DvppProcess::~DvppProcess() { DestroyResource(); DestroyOutputPara(); } -Result DvppProcess::InitResource() -{ - dvppChannelDesc_ = acldvppCreateChannelDesc(); - if (dvppChannelDesc_ == nullptr) { +Result DvppProcess::InitResource() { + g_dvppChannelDesc_ = acldvppCreateChannelDesc(); + if (g_dvppChannelDesc_ == nullptr) { ERROR_LOG("acldvppCreateChannelDesc failed"); return FAILED; } - aclError ret = acldvppCreateChannel(dvppChannelDesc_); + aclError ret = acldvppCreateChannel(g_dvppChannelDesc_); if (ret != ACL_SUCCESS) { ERROR_LOG("acldvppCreateChannelAsync failed, errorCode = %d", static_cast(ret)); return FAILED; } - resizeConfig_ = acldvppCreateResizeConfig(); - if (resizeConfig_ == nullptr) { + g_resizeConfig_ = acldvppCreateResizeConfig(); + if (g_resizeConfig_ == nullptr) { ERROR_LOG("acldvppCreateResizeConfig failed"); return FAILED; } @@ -62,41 +65,38 @@ Result DvppProcess::InitResource() return SUCCESS; } -void DvppProcess::SetInput(void *inDevBuffer, uint32_t inDevBufferSize, const PicDesc &picDesc) -{ - inDevBuffer_ = inDevBuffer; - inDevBufferSize_ = inDevBufferSize; - jpegDecodeOutputSize_ = picDesc.jpegDecodeSize; +void DvppProcess::SetInput(void *inDevBuffer, uint32_t inDevBufferSize, const PicDesc &picDesc) { + g_inDevBuffer_ = inDevBuffer; + g_inDevBufferSize_ = inDevBufferSize; + g_jpegDecodeOutputSize_ = picDesc.jpegDecodeSize; } -void DvppProcess::GetDvppOutput(void **outputBuffer, int &outputSize) -{ +void DvppProcess::GetDvppOutput(void **outputBuffer, int &outputSize) { if (outputBuffer == nullptr) { ERROR_LOG("outputBuffer is nullptr"); return; } - *outputBuffer = resizeOutBufferDev_; - outputSize = resizeOutBufferSize_; - resizeOutBufferDev_ = nullptr; - resizeOutBufferSize_ = 0; + *outputBuffer = g_resizeOutBufferDev_; + outputSize = g_resizeOutBufferSize_; + g_resizeOutBufferDev_ = nullptr; + g_resizeOutBufferSize_ = 0; } -Result DvppProcess::InitDvppOutputPara(int modelInputWidth, int modelInputHeight) -{ +Result DvppProcess::InitDvppOutputPara(int modelInputWidth, int modelInputHeight) { if ((modelInputWidth <= 0) || (modelInputHeight <= 0)) { ERROR_LOG("InitInput para invalid, modelInputWidth %d, modelInputHeight %d", modelInputWidth, modelInputHeight); return FAILED; } - modelInputWidth_ = modelInputWidth; - modelInputHeight_ = modelInputHeight; - resizeOutWidthStride_ = AlignSize(modelInputWidth, 16); // 16-byte alignment - resizeOutHeightStride_ = AlignSize(modelInputHeight, 2); // 2-byte alignment + g_modelInputWidth_ = modelInputWidth; + g_modelInputHeight_ = modelInputHeight; + g_resizeOutWidthStride_ = AlignSize(modelInputWidth, 16); // 16-byte alignment + g_resizeOutHeightStride_ = AlignSize(modelInputHeight, 2); // 2-byte alignment // output buffer, adjust the value based on the actual model - resizeOutBufferSize_ = resizeOutWidthStride_ * resizeOutHeightStride_ * 3 / 2; // yuv format size - aclError ret = acldvppMalloc(&resizeOutBufferDev_, resizeOutBufferSize_); + g_resizeOutBufferSize_ = g_resizeOutWidthStride_ * g_resizeOutHeightStride_ * 3 / 2; // yuv format size + aclError ret = acldvppMalloc(&g_resizeOutBufferDev_, g_resizeOutBufferSize_); if (ret != ACL_SUCCESS) { ERROR_LOG("acldvppMalloc resizeOutBuffer failed, errorCode = %d", static_cast(ret)); return FAILED; @@ -105,115 +105,108 @@ Result DvppProcess::InitDvppOutputPara(int modelInputWidth, int modelInputHeight return SUCCESS; } -void DvppProcess::DestroyOutputPara() -{ - if (resizeOutBufferDev_ != nullptr) { - (void)acldvppFree(resizeOutBufferDev_); - resizeOutBufferDev_ = nullptr; +void DvppProcess::DestroyOutputPara() { + if (g_resizeOutBufferDev_ != nullptr) { + (void)acldvppFree(g_resizeOutBufferDev_); + g_resizeOutBufferDev_ = nullptr; } } -Result DvppProcess::InitDecodeOutputDesc() -{ - aclError ret = acldvppMalloc(&decodeOutDevBuffer_, jpegDecodeOutputSize_); +Result DvppProcess::InitDecodeOutputDesc() { + aclError ret = acldvppMalloc(&g_decodeOutDevBuffer_, g_jpegDecodeOutputSize_); if (ret != ACL_SUCCESS) { ERROR_LOG("acldvppMalloc jpegOutBufferDev failed, errorCode = %d", static_cast(ret)); return FAILED; } - decodeOutputDesc_ = acldvppCreatePicDesc(); - if (decodeOutputDesc_ == nullptr) { + g_decodeOutputDesc_ = acldvppCreatePicDesc(); + if (g_decodeOutputDesc_ == nullptr) { ERROR_LOG("acldvppCreatePicDesc decodeOutputDesc failed"); return FAILED; } - (void)acldvppSetPicDescData(decodeOutputDesc_, decodeOutDevBuffer_); - (void)acldvppSetPicDescFormat(decodeOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); - (void)acldvppSetPicDescSize(decodeOutputDesc_, jpegDecodeOutputSize_); + (void)acldvppSetPicDescData(g_decodeOutputDesc_, g_decodeOutDevBuffer_); + (void)acldvppSetPicDescFormat(g_decodeOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); + (void)acldvppSetPicDescSize(g_decodeOutputDesc_, g_jpegDecodeOutputSize_); return SUCCESS; } -Result DvppProcess::ProcessDecode() -{ +Result DvppProcess::ProcessDecode() { // decode to yuv format - aclError ret = acldvppJpegDecodeAsync(dvppChannelDesc_, inDevBuffer_, inDevBufferSize_, - decodeOutputDesc_, stream_); + aclError ret = acldvppJpegDecodeAsync(g_dvppChannelDesc_, g_inDevBuffer_, g_inDevBufferSize_, + g_decodeOutputDesc_, g_stream_); if (ret != ACL_SUCCESS) { ERROR_LOG("acldvppJpegDecodeAsync failed, errorCode = %d", static_cast(ret)); return FAILED; } - ret = aclrtSynchronizeStream(stream_); + ret = aclrtSynchronizeStream(g_stream_); if (ret != ACL_SUCCESS) { ERROR_LOG("aclrtSynchronizeStream failed, errorCode = %d", static_cast(ret)); return FAILED; } // get yuv image width and height - decodeOutputWidth_ = acldvppGetPicDescWidth(decodeOutputDesc_); - decodeOutputHeight_ = acldvppGetPicDescHeight(decodeOutputDesc_); - decodeOutputWidthStride_ = acldvppGetPicDescWidthStride(decodeOutputDesc_); + g_decodeOutputWidth_ = acldvppGetPicDescWidth(g_decodeOutputDesc_); + g_decodeOutputHeight_ = acldvppGetPicDescHeight(g_decodeOutputDesc_); + g_decodeOutputWidthStride_ = acldvppGetPicDescWidthStride(g_decodeOutputDesc_); return SUCCESS; } -void DvppProcess::DestroyDecodeResource() -{ - if (decodeOutputDesc_ != nullptr) { - (void)acldvppDestroyPicDesc(decodeOutputDesc_); - decodeOutputDesc_ = nullptr; +void DvppProcess::DestroyDecodeResource() { + if (g_decodeOutputDesc_ != nullptr) { + (void)acldvppDestroyPicDesc(g_decodeOutputDesc_); + g_decodeOutputDesc_ = nullptr; } } -Result DvppProcess::InitResizeInputDesc() -{ - uint32_t jpegOutWidthStride = decodeOutputWidthStride_; // 128-byte alignment on 310, 64-byte alignment on 310P - uint32_t jpegOutHeightStride = AlignSize(decodeOutputHeight_, 16); // 16-byte alignment +Result DvppProcess::InitResizeInputDesc() { + uint32_t jpegOutWidthStride = g_decodeOutputWidthStride_; // 128-byte alignment on 310, 64-byte alignment on 310P + uint32_t jpegOutHeightStride = AlignSize(g_decodeOutputHeight_, 16); // 16-byte alignment uint32_t jpegOutBufferSize = jpegOutWidthStride * jpegOutHeightStride * 3 / 2; // yuv format size - resizeInputDesc_ = acldvppCreatePicDesc(); - if (resizeInputDesc_ == nullptr) { + g_resizeInputDesc_ = acldvppCreatePicDesc(); + if (g_resizeInputDesc_ == nullptr) { ERROR_LOG("InitResizeInputDesc failed"); return FAILED; } - (void)acldvppSetPicDescData(resizeInputDesc_, decodeOutDevBuffer_); - (void)acldvppSetPicDescFormat(resizeInputDesc_, PIXEL_FORMAT_YVU_SEMIPLANAR_420); - (void)acldvppSetPicDescWidth(resizeInputDesc_, decodeOutputWidth_); - (void)acldvppSetPicDescHeight(resizeInputDesc_, decodeOutputHeight_); - (void)acldvppSetPicDescWidthStride(resizeInputDesc_, jpegOutWidthStride); - (void)acldvppSetPicDescHeightStride(resizeInputDesc_, jpegOutHeightStride); - (void)acldvppSetPicDescSize(resizeInputDesc_, jpegOutBufferSize); + (void)acldvppSetPicDescData(g_resizeInputDesc_, g_decodeOutDevBuffer_); + (void)acldvppSetPicDescFormat(g_resizeInputDesc_, PIXEL_FORMAT_YVU_SEMIPLANAR_420); + (void)acldvppSetPicDescWidth(g_resizeInputDesc_, g_decodeOutputWidth_); + (void)acldvppSetPicDescHeight(g_resizeInputDesc_, g_decodeOutputHeight_); + (void)acldvppSetPicDescWidthStride(g_resizeInputDesc_, jpegOutWidthStride); + (void)acldvppSetPicDescHeightStride(g_resizeInputDesc_, jpegOutHeightStride); + (void)acldvppSetPicDescSize(g_resizeInputDesc_, jpegOutBufferSize); return SUCCESS; } -Result DvppProcess::InitResizeOutputDesc() -{ - resizeOutputDesc_ = acldvppCreatePicDesc(); - if (resizeOutputDesc_ == nullptr) { +Result DvppProcess::InitResizeOutputDesc() { + g_resizeOutputDesc_ = acldvppCreatePicDesc(); + if (g_resizeOutputDesc_ == nullptr) { ERROR_LOG("acldvppCreatePicDesc failed"); return FAILED; } - (void)acldvppSetPicDescData(resizeOutputDesc_, resizeOutBufferDev_); - (void)acldvppSetPicDescFormat(resizeOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); - (void)acldvppSetPicDescWidth(resizeOutputDesc_, modelInputWidth_); - (void)acldvppSetPicDescHeight(resizeOutputDesc_, modelInputHeight_); - (void)acldvppSetPicDescWidthStride(resizeOutputDesc_, resizeOutWidthStride_); - (void)acldvppSetPicDescHeightStride(resizeOutputDesc_, resizeOutHeightStride_); - (void)acldvppSetPicDescSize(resizeOutputDesc_, resizeOutBufferSize_); + (void)acldvppSetPicDescData(g_resizeOutputDesc_, g_resizeOutBufferDev_); + (void)acldvppSetPicDescFormat(g_resizeOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); + (void)acldvppSetPicDescWidth(g_resizeOutputDesc_, g_modelInputWidth_); + (void)acldvppSetPicDescHeight(g_resizeOutputDesc_, g_modelInputHeight_); + (void)acldvppSetPicDescWidthStride(g_resizeOutputDesc_, g_resizeOutWidthStride_); + (void)acldvppSetPicDescHeightStride(g_resizeOutputDesc_, g_resizeOutHeightStride_); + (void)acldvppSetPicDescSize(g_resizeOutputDesc_, g_resizeOutBufferSize_); return SUCCESS; } -Result DvppProcess::ProcessResize() -{ +Result DvppProcess::ProcessResize() { // resize pic size - aclError ret = acldvppSetResizeConfigInterpolation(resizeConfig_, 0); - ret = acldvppVpcResizeAsync(dvppChannelDesc_, resizeInputDesc_, - resizeOutputDesc_, resizeConfig_, stream_); + aclError ret = acldvppSetResizeConfigInterpolation(g_resizeConfig_, 0); + ret = acldvppVpcResizeAsync(g_dvppChannelDesc_, g_resizeInputDesc_, + g_resizeOutputDesc_, g_resizeConfig_, g_stream_); if (ret != ACL_SUCCESS) { ERROR_LOG("acldvppVpcResizeAsync failed, errorCode = %d", static_cast(ret)); return FAILED; } - ret = aclrtSynchronizeStream(stream_); + ret = aclrtSynchronizeStream(g_stream_); if (ret != ACL_SUCCESS) { ERROR_LOG("aclrtSynchronizeStream failed, errorCode = %d", static_cast(ret)); return FAILED; @@ -222,45 +215,42 @@ Result DvppProcess::ProcessResize() return SUCCESS; } -void DvppProcess::DestroyResizeResource() -{ - if (decodeOutDevBuffer_ != nullptr) { - (void)acldvppFree(decodeOutDevBuffer_); - decodeOutDevBuffer_ = nullptr; +void DvppProcess::DestroyResizeResource() { + if (g_decodeOutDevBuffer_ != nullptr) { + (void)acldvppFree(g_decodeOutDevBuffer_); + g_decodeOutDevBuffer_ = nullptr; } - if (resizeInputDesc_ != nullptr) { - acldvppDestroyPicDesc(resizeInputDesc_); - resizeInputDesc_ = nullptr; + if (g_resizeInputDesc_ != nullptr) { + acldvppDestroyPicDesc(g_resizeInputDesc_); + g_resizeInputDesc_ = nullptr; } - if (resizeOutputDesc_ != nullptr) { - acldvppDestroyPicDesc(resizeOutputDesc_); - resizeOutputDesc_ = nullptr; + if (g_resizeOutputDesc_ != nullptr) { + acldvppDestroyPicDesc(g_resizeOutputDesc_); + g_resizeOutputDesc_ = nullptr; } } -void DvppProcess::DestroyResource() -{ - // resizeConfig_ is created in initResource - if (resizeConfig_ != nullptr) { - (void)acldvppDestroyResizeConfig(resizeConfig_); - resizeConfig_ = nullptr; +void DvppProcess::DestroyResource() { + // g_resizeConfig_ is created in initResource + if (g_resizeConfig_ != nullptr) { + (void)acldvppDestroyResizeConfig(g_resizeConfig_); + g_resizeConfig_ = nullptr; } - if (dvppChannelDesc_ != nullptr) { - aclError ret = acldvppDestroyChannel(dvppChannelDesc_); + if (g_dvppChannelDesc_ != nullptr) { + aclError ret = acldvppDestroyChannel(g_dvppChannelDesc_); if (ret != ACL_SUCCESS) { ERROR_LOG("acldvppDestroyChannel failed, errorCode = %d", static_cast(ret)); } - (void)acldvppDestroyChannelDesc(dvppChannelDesc_); - dvppChannelDesc_ = nullptr; + (void)acldvppDestroyChannelDesc(g_dvppChannelDesc_); + g_dvppChannelDesc_ = nullptr; } } -Result DvppProcess::Process() -{ +Result DvppProcess::Process() { // pic decode Result ret = InitDecodeOutputDesc(); if (ret != SUCCESS) { diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/main.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/main.cpp index 8ce595e3a..34d9a506e 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/main.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/main.cpp @@ -1,19 +1,25 @@ -/** -* @file main.cpp +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. * -* Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at + +* http://www.apache.org/licenses/LICENSE-2.0 + +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. */ + #include #include "sample_process.h" #include "utils.h" using namespace std; -int main() -{ +int main() { SampleProcess sampleProcess; Result ret = sampleProcess.InitResource(); if (ret != SUCCESS) { diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/model_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/model_process.cpp index b8ff2537e..06e68d762 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/model_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/model_process.cpp @@ -1,38 +1,42 @@ -/** -* @file model_process.cpp +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. * -* Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at + +* http://www.apache.org/licenses/LICENSE-2.0 + +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. */ + #include "model_process.h" #include #include "utils.h" using namespace std; -ModelProcess::ModelProcess() : loadFlag_(false), modelId_(0), modelWorkPtr_(nullptr), modelWorkSize_(0), - modelWeightPtr_(nullptr),modelWeightSize_(0), modelDesc_(nullptr), input_(nullptr), output_(nullptr) -{ +ModelProcess::ModelProcess() : g_loadFlag_(false), g_modelId_(0), g_modelWorkPtr_(nullptr), g_modelWorkSize_(0), + g_modelWeightPtr_(nullptr),g_modelWeightSize_(0), g_modelDesc_(nullptr), g_input_(nullptr), g_output_(nullptr) { } -ModelProcess::~ModelProcess() -{ +ModelProcess::~ModelProcess() { UnloadModel(); DestroyDesc(); DestroyInput(); DestroyOutput(); } -Result ModelProcess::LoadModel(const char *modelPath) -{ - if (loadFlag_) { +Result ModelProcess::LoadModel(const char *modelPath) { + if (g_loadFlag_) { ERROR_LOG("model has already been loaded"); return FAILED; } - aclError ret = aclmdlQuerySize(modelPath, &modelWorkSize_, &modelWeightSize_); + aclError ret = aclmdlQuerySize(modelPath, &g_modelWorkSize_, &g_modelWeightSize_); if (ret != ACL_SUCCESS) { ERROR_LOG("query model failed, model file is %s, errorCode is %d", modelPath, static_cast(ret)); return FAILED; @@ -40,44 +44,43 @@ Result ModelProcess::LoadModel(const char *modelPath) // using ACL_MEM_MALLOC_HUGE_FIRST to malloc memory, huge memory is preferred to use // and huge memory can improve performance. - ret = aclrtMalloc(&modelWorkPtr_, modelWorkSize_, ACL_MEM_MALLOC_HUGE_FIRST); + ret = aclrtMalloc(&g_modelWorkPtr_, g_modelWorkSize_, ACL_MEM_MALLOC_HUGE_FIRST); if (ret != ACL_SUCCESS) { ERROR_LOG("malloc buffer for work failed, require size is %zu, errorCode is %d", - modelWorkSize_, static_cast(ret)); + g_modelWorkSize_, static_cast(ret)); return FAILED; } // using ACL_MEM_MALLOC_HUGE_FIRST to malloc memory, huge memory is preferred to use // and huge memory can improve performance. - ret = aclrtMalloc(&modelWeightPtr_, modelWeightSize_, ACL_MEM_MALLOC_HUGE_FIRST); + ret = aclrtMalloc(&g_modelWeightPtr_, g_modelWeightSize_, ACL_MEM_MALLOC_HUGE_FIRST); if (ret != ACL_SUCCESS) { ERROR_LOG("malloc buffer for weight failed, require size is %zu, errorCode is %d", - modelWeightSize_, static_cast(ret)); + g_modelWeightSize_, static_cast(ret)); return FAILED; } - ret = aclmdlLoadFromFileWithMem(modelPath, &modelId_, modelWorkPtr_, - modelWorkSize_, modelWeightPtr_, modelWeightSize_); + ret = aclmdlLoadFromFileWithMem(modelPath, &g_modelId_, g_modelWorkPtr_, + g_modelWorkSize_, g_modelWeightPtr_, g_modelWeightSize_); if (ret != ACL_SUCCESS) { ERROR_LOG("load model from file failed, model file is %s, errorCode is %d", modelPath, static_cast(ret)); return FAILED; } - loadFlag_ = true; + g_loadFlag_ = true; INFO_LOG("load model %s success", modelPath); return SUCCESS; } -Result ModelProcess::CreateDesc() -{ - modelDesc_ = aclmdlCreateDesc(); - if (modelDesc_ == nullptr) { +Result ModelProcess::CreateDesc() { + g_modelDesc_ = aclmdlCreateDesc(); + if (g_modelDesc_ == nullptr) { ERROR_LOG("create model description failed"); return FAILED; } - aclError ret = aclmdlGetDesc(modelDesc_, modelId_); + aclError ret = aclmdlGetDesc(g_modelDesc_, g_modelId_); if (ret != ACL_SUCCESS) { ERROR_LOG("get model description failed"); return FAILED; @@ -88,39 +91,37 @@ Result ModelProcess::CreateDesc() return SUCCESS; } -void ModelProcess::DestroyDesc() -{ - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; +void ModelProcess::DestroyDesc() { + if (g_modelDesc_ != nullptr) { + (void)aclmdlDestroyDesc(g_modelDesc_); + g_modelDesc_ = nullptr; } INFO_LOG("destroy model description success"); } -Result ModelProcess::CreateInput(void *input1, size_t input1size, void *input2, size_t input2size) -{ - vector inputData = {{input1, input1size}, {input2, input2size}}; +Result ModelProcess::CreateInput(void *input1, size_t input1Size, void *input2, size_t input2Size) { + vector inputData = {{input1, input1Size}, {input2, input2Size}}; - uint32_t dataNum = aclmdlGetNumInputs(modelDesc_); + uint32_t dataNum = aclmdlGetNumInputs(g_modelDesc_); if (dataNum == 0) { ERROR_LOG("Create input failed for no input data"); return FAILED; } // om used in this sample has only one input - if (modelDesc_ == nullptr) { + if (g_modelDesc_ == nullptr) { ERROR_LOG("no model description, create input failed"); return FAILED; } - input_ = aclmdlCreateDataset(); - if (input_ == nullptr) { + g_input_ = aclmdlCreateDataset(); + if (g_input_ == nullptr) { ERROR_LOG("can't create dataset, create input failed"); return FAILED; } for (uint32_t i = 0; i < inputData.size(); i++) { - size_t modelInputSize = aclmdlGetInputSizeByIndex(modelDesc_, i); + size_t modelInputSize = aclmdlGetInputSizeByIndex(g_modelDesc_, i); if (modelInputSize != inputData[i].size) { ERROR_LOG("Input size verify failed " "input[%d] size: %ld, provide size : %d", @@ -133,7 +134,7 @@ Result ModelProcess::CreateInput(void *input1, size_t input1size, void *input2, return FAILED; } - aclError ret = aclmdlAddDatasetBuffer(input_, dataBuf); + aclError ret = aclmdlAddDatasetBuffer(g_input_, dataBuf); if (ret != ACL_SUCCESS) { ERROR_LOG("add input dataset buffer failed, errorCode is %d", static_cast(ret)); (void)aclDestroyDataBuffer(dataBuf); @@ -145,37 +146,35 @@ Result ModelProcess::CreateInput(void *input1, size_t input1size, void *input2, return SUCCESS; } -void ModelProcess::DestroyInput() -{ - if (input_ == nullptr) { +void ModelProcess::DestroyInput() { + if (g_input_ == nullptr) { return; } - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(input_); ++i) { - aclDataBuffer *dataBuffer = aclmdlGetDatasetBuffer(input_, i); + for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(g_input_); ++i) { + aclDataBuffer *dataBuffer = aclmdlGetDatasetBuffer(g_input_, i); (void)aclDestroyDataBuffer(dataBuffer); } - (void)aclmdlDestroyDataset(input_); - input_ = nullptr; + (void)aclmdlDestroyDataset(g_input_); + g_input_ = nullptr; INFO_LOG("destroy model input success"); } -Result ModelProcess::CreateOutput() -{ - if (modelDesc_ == nullptr) { +Result ModelProcess::CreateOutput() { + if (g_modelDesc_ == nullptr) { ERROR_LOG("no model description, create ouput failed"); return FAILED; } - output_ = aclmdlCreateDataset(); - if (output_ == nullptr) { + g_output_ = aclmdlCreateDataset(); + if (g_output_ == nullptr) { ERROR_LOG("can't create dataset, create output failed"); return FAILED; } - size_t outputSize = aclmdlGetNumOutputs(modelDesc_); + size_t outputSize = aclmdlGetNumOutputs(g_modelDesc_); for (size_t i = 0; i < outputSize; ++i) { - size_t modelOutputSize = aclmdlGetOutputSizeByIndex(modelDesc_, i); + size_t modelOutputSize = aclmdlGetOutputSizeByIndex(g_modelDesc_, i); void *outputBuffer = nullptr; aclError ret = aclrtMalloc(&outputBuffer, modelOutputSize, ACL_MEM_MALLOC_NORMAL_ONLY); @@ -192,7 +191,7 @@ Result ModelProcess::CreateOutput() return FAILED; } - ret = aclmdlAddDatasetBuffer(output_, outputData); + ret = aclmdlAddDatasetBuffer(g_output_, outputData); if (ret != ACL_SUCCESS) { ERROR_LOG("can't add data buffer, create output failed, errorCode is %d", static_cast(ret)); (void)aclrtFree(outputBuffer); @@ -205,77 +204,72 @@ Result ModelProcess::CreateOutput() return SUCCESS; } -void ModelProcess::DestroyOutput() -{ - if (output_ == nullptr) { +void ModelProcess::DestroyOutput() { + if (g_output_ == nullptr) { return; } - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) { - aclDataBuffer *dataBuffer = aclmdlGetDatasetBuffer(output_, i); + for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(g_output_); ++i) { + aclDataBuffer *dataBuffer = aclmdlGetDatasetBuffer(g_output_, i); void* data = aclGetDataBufferAddr(dataBuffer); (void)aclrtFree(data); (void)aclDestroyDataBuffer(dataBuffer); } - (void)aclmdlDestroyDataset(output_); - output_ = nullptr; + (void)aclmdlDestroyDataset(g_output_); + g_output_ = nullptr; INFO_LOG("destroy model output success"); } -Result ModelProcess::Execute() -{ - aclError ret = aclmdlExecute(modelId_, input_, output_); +Result ModelProcess::Execute() { + aclError ret = aclmdlExecute(g_modelId_, g_input_, g_output_); if (ret != ACL_SUCCESS) { - ERROR_LOG("execute model failed, modelId is %u, errorCode is %d", modelId_, static_cast(ret)); + ERROR_LOG("execute model failed, modelId is %u, errorCode is %d", g_modelId_, static_cast(ret)); return FAILED; } - INFO_LOG("model execute success, modelId is %u", modelId_); + INFO_LOG("model execute success, modelId is %u", g_modelId_); return SUCCESS; } -void ModelProcess::UnloadModel() -{ - if (!loadFlag_) { +void ModelProcess::UnloadModel() { + if (!g_loadFlag_) { WARN_LOG("no model had been loaded, unload failed"); return; } - aclError ret = aclmdlUnload(modelId_); + aclError ret = aclmdlUnload(g_modelId_); if (ret != ACL_SUCCESS) { - ERROR_LOG("unload model failed, modelId is %u, errorCode is %d", modelId_, static_cast(ret)); + ERROR_LOG("unload model failed, modelId is %u, errorCode is %d", g_modelId_, static_cast(ret)); } - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; + if (g_modelDesc_ != nullptr) { + (void)aclmdlDestroyDesc(g_modelDesc_); + g_modelDesc_ = nullptr; } - if (modelWorkPtr_ != nullptr) { - aclrtFree(modelWorkPtr_); - modelWorkPtr_ = nullptr; - modelWorkSize_ = 0; + if (g_modelWorkPtr_ != nullptr) { + aclrtFree(g_modelWorkPtr_); + g_modelWorkPtr_ = nullptr; + g_modelWorkSize_ = 0; } - if (modelWeightPtr_ != nullptr) { - aclrtFree(modelWeightPtr_); - modelWeightPtr_ = nullptr; - modelWeightSize_ = 0; + if (g_modelWeightPtr_ != nullptr) { + aclrtFree(g_modelWeightPtr_); + g_modelWeightPtr_ = nullptr; + g_modelWeightSize_ = 0; } - loadFlag_ = false; - INFO_LOG("unload model success, modelId is %u", modelId_); + g_loadFlag_ = false; + INFO_LOG("unload model success, modelId is %u", g_modelId_); } -const aclmdlDataset *ModelProcess::GetModelOutputData() -{ - return output_; +const aclmdlDataset *ModelProcess::GetModelOutputData() { + return g_output_; } -Result ModelProcess::GetModelInputWH(int &width, int &height) -{ - if (modelDesc_ == nullptr) { +Result ModelProcess::GetModelInputWH(int &width, int &height) { + if (g_modelDesc_ == nullptr) { ERROR_LOG("no model description, get input hw failed"); return FAILED; } @@ -283,9 +277,9 @@ Result ModelProcess::GetModelInputWH(int &width, int &height) // dims[0] is N, dims[1] is H, dims[2] is W dims[3] is C aclmdlIODims dims; // om used in this app has only one input - aclError ret = aclmdlGetInputDims(modelDesc_, 0, &dims); + aclError ret = aclmdlGetInputDims(g_modelDesc_, 0, &dims); if (ret != ACL_SUCCESS) { - ERROR_LOG("get model input dims failed, modelId is %u errorCode is %d", modelId_, static_cast(ret)); + ERROR_LOG("get model input dims failed, modelId is %u errorCode is %d", g_modelId_, static_cast(ret)); return FAILED; } if (dims.dimCount != 4) { diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/sample_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/sample_process.cpp index 8c0512c9d..62abfc1ad 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/sample_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/sample_process.cpp @@ -1,12 +1,19 @@ -/** -* @file sample_process.cpp +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. * -* Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at + +* http://www.apache.org/licenses/LICENSE-2.0 + +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. */ + #include "sample_process.h" #include #include "dvpp_process.h" @@ -38,39 +45,36 @@ namespace { "refrigerator", "book", "clock", "vase","scissors", "teddy bear", "hair drier", "toothbrush" }; - const uint32_t kBBoxDataBufId = 0; - const uint32_t kBoxNumDataBufId = 1; + const uint32_t g_BBoxDataBufId = 0; + const uint32_t g_BoxNumDataBufId = 1; enum BBoxIndex { TOPLEFTX = 0, TOPLEFTY, BOTTOMRIGHTX, BOTTOMRIGHTY, SCORE, LABEL }; // bounding box line solid - const uint32_t kLineSolid = 2; + const uint32_t g_LineSolid = 2; // output image prefix - const string kOutputFilePrefix = "out_"; + const string g_OutputFilePrefix = "out_"; // opencv draw label params. - const double kFountScale = 0.5; - const cv::Scalar kFontColor(0, 0, 255); - const uint32_t kLabelOffset = 11; - const string kFileSperator = "/"; + const double g_FountScale = 0.5; + const cv::Scalar g_FontColor(0, 0, 255); + const uint32_t g_LabelOffset = 11; + const string g_FileSperator = "/"; // opencv color list for boundingbox - const vector kColors{ + const vector g_Colors { cv::Scalar(237, 149, 100), cv::Scalar(0, 215, 255), cv::Scalar(50, 205, 50), cv::Scalar(139, 85, 26) }; } -SampleProcess::SampleProcess():deviceId_(0), context_(nullptr), stream_(nullptr) -{ +SampleProcess::SampleProcess():g_deviceId_(0), g_context_(nullptr), g_stream_(nullptr) { } -SampleProcess::~SampleProcess() -{ +SampleProcess::~SampleProcess() { DestroyResource(); } -Result SampleProcess::InitResource() -{ +Result SampleProcess::InitResource() { // ACL init const char *aclConfigPath = "../src/acl.json"; aclError ret = aclInit(aclConfigPath); @@ -81,27 +85,27 @@ Result SampleProcess::InitResource() INFO_LOG("acl init success"); // set device - ret = aclrtSetDevice(deviceId_); + ret = aclrtSetDevice(g_deviceId_); if (ret != ACL_SUCCESS) { - ERROR_LOG("acl set device %d failed, errorCode = %d", deviceId_, static_cast(ret)); + ERROR_LOG("acl set device %d failed, errorCode = %d", g_deviceId_, static_cast(ret)); return FAILED; } - INFO_LOG("set device %d success", deviceId_); + INFO_LOG("set device %d success", g_deviceId_); // create context (set current) - ret = aclrtCreateContext(&context_, deviceId_); + ret = aclrtCreateContext(&g_context_, g_deviceId_); if (ret != ACL_SUCCESS) { ERROR_LOG("acl create context failed, deviceId = %d, errorCode = %d", - deviceId_, static_cast(ret)); + g_deviceId_, static_cast(ret)); return FAILED; } INFO_LOG("create context success"); // create stream - ret = aclrtCreateStream(&stream_); + ret = aclrtCreateStream(&g_stream_); if (ret != ACL_SUCCESS) { ERROR_LOG("acl create stream failed, deviceId = %d, errorCode = %d", - deviceId_, static_cast(ret)); + g_deviceId_, static_cast(ret)); return FAILED; } INFO_LOG("create stream success"); @@ -109,25 +113,24 @@ Result SampleProcess::InitResource() // get run mode // runMode is ACL_HOST which represents app is running in host // runMode is ACL_DEVICE which represents app is running in device - ret = aclrtGetRunMode(&runMode_); + ret = aclrtGetRunMode(&g_runMode_); if (ret != ACL_SUCCESS) { ERROR_LOG("acl get run mode failed, errorCode = %d", static_cast(ret)); return FAILED; } - bool isDivece = (runMode_ == ACL_DEVICE); + bool isDivece = (g_runMode_ == ACL_DEVICE); RunStatus::SetDeviceStatus(isDivece); INFO_LOG("get run mode success"); return SUCCESS; } - Result SampleProcess::Postprocess(const aclmdlDataset* modelOutput, PicDesc &picDesc, int modelWidth, int modelHeight) { uint32_t dataSize = 0; float* detectData = (float *)GetInferenceOutputItem(dataSize, modelOutput, - kBBoxDataBufId); + g_BBoxDataBufId); uint32_t* boxNum = (uint32_t *)GetInferenceOutputItem(dataSize, modelOutput, - kBoxNumDataBufId); + g_BoxNumDataBufId); if (boxNum == nullptr) return FAILED; uint32_t totalBox = boxNum[0]; @@ -152,7 +155,7 @@ Result SampleProcess::Postprocess(const aclmdlDataset* modelOutput, PicDesc &pic } DrawBoundBoxToImage(detectResults, picDesc.picName); - if (runMode_ == ACL_HOST) { + if (g_runMode_ == ACL_HOST) { delete[]((uint8_t *)detectData); delete[]((uint8_t*)boxNum); } @@ -184,7 +187,7 @@ const aclmdlDataset* inferenceOutput, uint32_t idx) { } void* data = nullptr; - if (runMode_ == ACL_HOST) { + if (g_runMode_ == ACL_HOST) { data = Utils::CopyDataDeviceToLocal(dataBufferDev, bufferSize); if (data == nullptr) { ERROR_LOG("Copy inference output to host failed"); @@ -207,9 +210,9 @@ const string& origImagePath) { p1.y = detectionResults[i].rect.ltY; p2.x = detectionResults[i].rect.rbX; p2.y = detectionResults[i].rect.rbY; - cv::rectangle(image, p1, p2, kColors[i % kColors.size()], kLineSolid); - cv::putText(image, detectionResults[i].text, cv::Point(p1.x, p1.y + kLabelOffset), - cv::FONT_HERSHEY_COMPLEX, kFountScale, kFontColor); + cv::rectangle(image, p1, p2, g_Colors[i % g_Colors.size()], g_LineSolid); + cv::putText(image, detectionResults[i].text, cv::Point(p1.x, p1.y + g_LabelOffset), + cv::FONT_HERSHEY_COMPLEX, g_FountScale, g_FontColor); } string folderPath = "./output"; @@ -217,17 +220,16 @@ const string& origImagePath) { mkdir(folderPath.c_str(), 0775); } int pos = origImagePath.find_last_of("/"); - string filename(origImagePath.substr(pos + 1)); + string fileName(origImagePath.substr(pos + 1)); stringstream sstream; sstream.str(""); - sstream << "./output/out_" << filename; + sstream << "./output/out_" << fileName; cv::imwrite(sstream.str(), image); } -Result SampleProcess::Process() -{ +Result SampleProcess::Process() { // dvpp init - DvppProcess dvppProcess(stream_); + DvppProcess dvppProcess(g_stream_); Result ret = dvppProcess.InitResource(); if (ret != SUCCESS) { ERROR_LOG("init dvpp resource failed"); @@ -264,7 +266,7 @@ Result SampleProcess::Process() (float)modelInputWidth, (float)modelInputHeight}; size_t imageInfoSize_ = sizeof(imageInfo); void *imageInfoBuf_; - if (runMode_ == ACL_HOST) + if (g_runMode_ == ACL_HOST) imageInfoBuf_ = Utils::CopyDataHostToDevice((void *)imageInfo, imageInfoSize_); else imageInfoBuf_ = Utils::CopyDataDeviceToDevice((void *)imageInfo, imageInfoSize_); @@ -275,10 +277,9 @@ Result SampleProcess::Process() // input image PicDesc testPic[] = { {"../data/dog1_1024_683.jpg", 0, 0}, - //{"../data/dog2_1024_683.jpg", 0, 0}, + // {"../data/dog2_1024_683.jpg", 0, 0}, }; - for (size_t index = 0; index < sizeof(testPic) / sizeof(testPic[0]); ++index) { INFO_LOG("start to process picture:%s", testPic[index].picName.c_str()); // 1.dvpp process @@ -348,32 +349,31 @@ Result SampleProcess::Process() return SUCCESS; } -void SampleProcess::DestroyResource() -{ +void SampleProcess::DestroyResource() { aclError ret; - if (stream_ != nullptr) { - ret = aclrtDestroyStream(stream_); + if (g_stream_ != nullptr) { + ret = aclrtDestroyStream(g_stream_); if (ret != ACL_SUCCESS) { ERROR_LOG("destroy stream failed, errorCode = %d", static_cast(ret)); } - stream_ = nullptr; + g_stream_ = nullptr; } INFO_LOG("end to destroy stream"); - if (context_ != nullptr) { - ret = aclrtDestroyContext(context_); + if (g_context_ != nullptr) { + ret = aclrtDestroyContext(g_context_); if (ret != ACL_SUCCESS) { ERROR_LOG("destroy context failed, errorCode = %d", static_cast(ret)); } - context_ = nullptr; + g_context_ = nullptr; } INFO_LOG("end to destroy context"); - ret = aclrtResetDevice(deviceId_); + ret = aclrtResetDevice(g_deviceId_); if (ret != ACL_SUCCESS) { - ERROR_LOG("reset device %d failed, errorCode = %d", deviceId_, static_cast(ret)); + ERROR_LOG("reset device %d failed, errorCode = %d", g_deviceId_, static_cast(ret)); } - INFO_LOG("end to reset device %d", deviceId_); + INFO_LOG("end to reset device %d", g_deviceId_); ret = aclFinalize(); if (ret != ACL_SUCCESS) { diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/utils.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/utils.cpp index 8a4425a19..fe569b8c1 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/utils.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/utils.cpp @@ -1,12 +1,19 @@ -/** -* @file utils.cpp +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. * -* Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at + +* http://www.apache.org/licenses/LICENSE-2.0 + +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. */ + #include "utils.h" #include #include @@ -22,8 +29,7 @@ bool RunStatus::isDevice_ = false; -Result Utils::ReadBinFile(const std::string &fileName, void *&inputBuff, uint32_t &fileSize) -{ +Result Utils::ReadBinFile(const std::string &fileName, void *&inputBuff, uint32_t &fileSize) { if (CheckPathIsFile(fileName) == FAILED) { ERROR_LOG("%s is not a file", fileName.c_str()); return FAILED; @@ -69,8 +75,7 @@ Result Utils::ReadBinFile(const std::string &fileName, void *&inputBuff, uint32_ return SUCCESS; } -Result Utils::GetDeviceBufferOfPicture(PicDesc &picDesc, void *&picDevBuffer, uint32_t &devPicBufferSize) -{ +Result Utils::GetDeviceBufferOfPicture(PicDesc &picDesc, void *&picDevBuffer, uint32_t &devPicBufferSize) { if (picDesc.picName.empty()) { ERROR_LOG("picture file name is empty"); return FAILED; @@ -133,8 +138,7 @@ Result Utils::GetDeviceBufferOfPicture(PicDesc &picDesc, void *&picDevBuffer, ui return SUCCESS; } -Result Utils::CheckPathIsFile(const std::string &fileName) -{ +Result Utils::CheckPathIsFile(const std::string &fileName) { #if defined(_MSC_VER) DWORD bRet = GetFileAttributes((LPCSTR)fileName.c_str()); if (bRet == FILE_ATTRIBUTE_DIRECTORY) { diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/drawing.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/drawing.h index e778640c8..4480903a2 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/drawing.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/drawing.h @@ -1,5 +1,21 @@ -#ifndef __DRAWING__H__ -#define __DRAWING__H__ +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. +*/ + +#ifndef YOLOV3_COCO_DETECTION_PICTURE_WITH_FREETYPE_INC_DRAWING_H +#define YOLOV3_COCO_DETECTION_PICTURE_WITH_FREETYPE_INC_DRAWING_H #include #include @@ -11,4 +27,5 @@ void DrawText(ImageData& image, int x, int y, const std::string &text, const YUVColor &color); void DrawRect(ImageData& image, int x1, int y1, int x2, int y2, const YUVColor &color, int lineWidth); -#endif //__DRAWING__H__ + +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/dvpp_cropandpaste.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/dvpp_cropandpaste.h index 62796696b..76371ed35 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/dvpp_cropandpaste.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/dvpp_cropandpaste.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 dvpp_process.h -* Description: handle dvpp process */ + +#ifndef YOLOV3_COCO_DETECTION_PICTURE_WITH_FREETYPE_INC_DVPP_CROPANDPASTE_H +#define YOLOV3_COCO_DETECTION_PICTURE_WITH_FREETYPE_INC_DVPP_CROPANDPASTE_H + #pragma once #include #include "acl/acl.h" @@ -63,25 +64,26 @@ private: void DestroyCropAndPasteResource(); - aclrtStream stream_; - acldvppChannelDesc *dvppChannelDesc_; + aclrtStream g_stream_; + acldvppChannelDesc *g_dvppChannelDesc_; // IN/OUT Desc - acldvppPicDesc *vpcInputDesc_; - acldvppPicDesc *vpcOutputDesc_; + acldvppPicDesc *g_vpcInputDesc_; + acldvppPicDesc *g_vpcOutputDesc_; - uint32_t originalImageWidth_; - uint32_t originalImageHeight_; + uint32_t g_originalImageWidth_; + uint32_t g_originalImageHeight_; - acldvppRoiConfig *cropArea_; - acldvppRoiConfig *pasteArea_; + acldvppRoiConfig *g_cropArea_; + acldvppRoiConfig *g_pasteArea_; // output buffer - void *vpcOutBufferDev_; - uint32_t vpcOutBufferSize_; + void *g_vpcOutBufferDev_; + uint32_t g_vpcOutBufferSize_; - //model [W][H] - Resolution size_; - acldvppPixelFormat format_; + // model [W][H] + Resolution g_size_; + acldvppPixelFormat g_format_; }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/dvpp_jpegd.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/dvpp_jpegd.h index 94de8e4d0..3847a876b 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/dvpp_jpegd.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/dvpp_jpegd.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 dvpp_process.h -* Description: handle dvpp process */ + +#ifndef YOLOV3_COCO_DETECTION_PICTURE_WITH_FREETYPE_INC_DVPP_JPEGD_H +#define YOLOV3_COCO_DETECTION_PICTURE_WITH_FREETYPE_INC_DVPP_JPEGD_H + #pragma once #include #include "acl/acl.h" @@ -78,16 +79,17 @@ private: void DestroyResource(); void DestroyOutputPara(); - aclrtStream stream_; - acldvppChannelDesc *dvppChannelDesc_; + aclrtStream g_stream_; + acldvppChannelDesc *g_dvppChannelDesc_; - void* decodeOutBufferDev_; // decode output buffer - acldvppPicDesc *decodeOutputDesc_; //decode output desc + void* g_decodeOutBufferDev_; // decode output buffer + acldvppPicDesc *g_decodeOutputDesc_; // decode output desc - uint8_t *inDevBuffer_; // input pic dev buffer - uint32_t inDevBufferSizeD_; // input pic size for decode + uint8_t *g_inDevBuffer_; // input pic dev buffer + uint32_t g_inDevBufferSizeD_; // input pic size for decode - void *vpcOutBufferDev_; // vpc output buffer - uint32_t vpcOutBufferSize_; // vpc output size + void *g_vpcOutBufferDev_; // vpc output buffer + uint32_t g_vpcOutBufferSize_; // vpc output size }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/dvpp_process.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/dvpp_process.h index 850d1efcb..d4ff3d693 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/dvpp_process.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/dvpp_process.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 dvpp_process.h -* Description: handle dvpp process */ + +#ifndef YOLOV3_COCO_DETECTION_PICTURE_WITH_FREETYPE_INC_DVPP_PROCESS_H +#define YOLOV3_COCO_DETECTION_PICTURE_WITH_FREETYPE_INC_DVPP_PROCESS_H + #pragma once #include @@ -38,6 +39,8 @@ public: void DestroyResource(); protected: - aclrtStream stream_; - acldvppChannelDesc *dvppChannelDesc_; + aclrtStream g_stream_; + acldvppChannelDesc *g_dvppChannelDesc_; }; + +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/freetype_helper.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/freetype_helper.h index 835303740..a56720dba 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/freetype_helper.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/freetype_helper.h @@ -1,5 +1,21 @@ -#ifndef __FREETYPE_HELPER_H__ -#define __FREETYPE_HELPER_H__ +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. +*/ + +#ifndef YOLOV3_COCO_DETECTION_PICTURE_WITH_FREETYPE_INC_FREETYPE_HELPER_H +#define YOLOV3_COCO_DETECTION_PICTURE_WITH_FREETYPE_INC_FREETYPE_HELPER_H #include #include @@ -10,4 +26,4 @@ void SetPixel(ImageData& image, int x, int y, const YUVColor &color); void RenderText(ImageData& image, int x, int y, const std::string &text, const YUVColor *color); -#endif //__FREETYPE_HELPER_H__ +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/model_process.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/model_process.h index 33365bdf0..b3f918ae4 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/model_process.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/model_process.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 model_process.h -* Description: handle model process */ + +#ifndef YOLOV3_COCO_DETECTION_PICTURE_WITH_FREETYPE_INC_MODEL_PROCESS_H +#define YOLOV3_COCO_DETECTION_PICTURE_WITH_FREETYPE_INC_MODEL_PROCESS_H + #pragma once #include #include "utils.h" @@ -70,8 +71,8 @@ class ModelProcess { * @param [in] bufferSize: input buffer size * @return result */ - Result CreateInput(void *input1, size_t input1size, - void* input2, size_t input2size); + Result CreateInput(void *input1, size_t input1Size, + void* input2, size_t input2Size); /** * @brief destroy input resource @@ -102,16 +103,17 @@ class ModelProcess { aclmdlDataset *GetModelOutputData(); private: - bool loadFlag_; // model load flag - uint32_t modelId_; - void *modelMemPtr_; - size_t modelMemSize_; - void *modelWeightPtr_; - size_t modelWeightSize_; - aclmdlDesc *modelDesc_; - aclmdlDataset *input_; - aclmdlDataset *output_; - - bool isReleased_; + bool g_loadFlag_; // model load flag + uint32_t g_modelId_; + void *g_modelMemPtr_; + size_t g_modelMemSize_; + void *g_modelWeightPtr_; + size_t g_modelWeightSize_; + aclmdlDesc *g_modelDesc_; + aclmdlDataset *g_input_; + aclmdlDataset *g_output_; + + bool g_isReleased_; }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/object_detect.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/object_detect.h index 7eb313666..0c8509d6a 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/object_detect.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/object_detect.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 YOLOV3_COCO_DETECTION_PICTURE_WITH_FREETYPE_INC_OBJECT_DETECT_H +#define YOLOV3_COCO_DETECTION_PICTURE_WITH_FREETYPE_INC_OBJECT_DETECT_H + #pragma once #include "utils.h" #include "acl/acl.h" @@ -51,21 +52,22 @@ private: void DestroyResource(); private: - int32_t deviceId_; - aclrtContext context_; - aclrtStream stream_; - uint32_t imageInfoSize_; - void* imageInfoBuf_; - ModelProcess model_; + int32_t g_deviceId_; + aclrtContext g_context_; + aclrtStream g_stream_; + uint32_t g_imageInfoSize_; + void* g_imageInfoBuf_; + ModelProcess g_model_; - const char* modelPath_; - uint32_t modelWidth_; - uint32_t modelHeight_; + const char* g_modelPath_; + uint32_t g_modelWidth_; + uint32_t g_modelHeight_; uint32_t inputDataSize_; - DvppProcess dvpp_; - aclrtRunMode runMode_; + DvppProcess g_dvpp_; + aclrtRunMode g_runMode_; - bool isInited_; - bool isDeviceSet_; + bool g_isInited_; + bool g_isDeviceSet_; }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/utils.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/utils.h index 166acb610..4e4020806 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/utils.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/utils.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 utils.h -* Description: handle file operations */ + +#ifndef YOLOV3_COCO_DETECTION_PICTURE_WITH_FREETYPE_INC_UTILS_H +#define YOLOV3_COCO_DETECTION_PICTURE_WITH_FREETYPE_INC_UTILS_H + #pragma once #include #include @@ -129,3 +130,4 @@ public: static Result CopyDeviceToLocal(ImageData& imageout, ImageData srcImage, aclrtRunMode mode); }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/drawing.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/drawing.cpp index 6b108281f..c9e87a5ec 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/drawing.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/drawing.cpp @@ -1,3 +1,19 @@ +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at + +* http://www.apache.org/licenses/LICENSE-2.0 + +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + #include #include #include diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/dvpp_cropandpaste.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/dvpp_cropandpaste.cpp index 16c091918..a226ce007 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/dvpp_cropandpaste.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/dvpp_cropandpaste.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,9 +12,6 @@ * 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 dvpp_process.cpp -* Description: handle dvpp process */ #include @@ -25,22 +22,20 @@ using namespace std; DvppCropAndPaste::DvppCropAndPaste(aclrtStream& stream, acldvppChannelDesc *dvppChannelDesc, uint32_t width, uint32_t height) -: stream_(stream), dvppChannelDesc_(dvppChannelDesc), -vpcInputDesc_(nullptr), vpcOutputDesc_(nullptr), -vpcOutBufferDev_(nullptr),vpcOutBufferSize_(0){ - size_.width = width; - size_.height = height; +: g_stream_(stream), g_dvppChannelDesc_(dvppChannelDesc), +g_vpcInputDesc_(nullptr), g_vpcOutputDesc_(nullptr), +g_vpcOutBufferDev_(nullptr),g_vpcOutBufferSize_(0) { + g_size_.width = width; + g_size_.height = height; } -DvppCropAndPaste::~DvppCropAndPaste() -{ +DvppCropAndPaste::~DvppCropAndPaste() { DestroyCropAndPasteResource(); } -Result DvppCropAndPaste::InitCropAndPasteInputDesc(ImageData& inputImage) -{ - originalImageWidth_ = inputImage.width; - originalImageHeight_ = inputImage.height; +Result DvppCropAndPaste::InitCropAndPasteInputDesc(ImageData& inputImage) { + g_originalImageWidth_ = inputImage.width; + g_originalImageHeight_ = inputImage.height; uint32_t alignWidth = ALIGN_UP128(inputImage.width); uint32_t alignHeight = ALIGN_UP16(inputImage.height); if (alignWidth == 0 || alignHeight == 0) { @@ -49,26 +44,25 @@ Result DvppCropAndPaste::InitCropAndPasteInputDesc(ImageData& inputImage) return FAILED; } uint32_t inputBufferSize = YUV420SP_SIZE(alignWidth, alignHeight); - vpcInputDesc_ = acldvppCreatePicDesc(); - if (vpcInputDesc_ == nullptr) { - ERROR_LOG("acldvppCreatePicDesc vpcInputDesc_ failed"); + g_vpcInputDesc_ = acldvppCreatePicDesc(); + if (g_vpcInputDesc_ == nullptr) { + ERROR_LOG("acldvppCreatePicDesc g_vpcInputDesc_ failed"); return FAILED; } - acldvppSetPicDescData(vpcInputDesc_, inputImage.data.get()); // JpegD . vpcResize - acldvppSetPicDescFormat(vpcInputDesc_, format_); - acldvppSetPicDescWidth(vpcInputDesc_, inputImage.width); - acldvppSetPicDescHeight(vpcInputDesc_, inputImage.height); - acldvppSetPicDescWidthStride(vpcInputDesc_, alignWidth); - acldvppSetPicDescHeightStride(vpcInputDesc_, alignHeight); - acldvppSetPicDescSize(vpcInputDesc_, inputBufferSize); + acldvppSetPicDescData(g_vpcInputDesc_, inputImage.data.get()); // JpegD . vpcResize + acldvppSetPicDescFormat(g_vpcInputDesc_, g_format_); + acldvppSetPicDescWidth(g_vpcInputDesc_, inputImage.width); + acldvppSetPicDescHeight(g_vpcInputDesc_, inputImage.height); + acldvppSetPicDescWidthStride(g_vpcInputDesc_, alignWidth); + acldvppSetPicDescHeightStride(g_vpcInputDesc_, alignHeight); + acldvppSetPicDescSize(g_vpcInputDesc_, inputBufferSize); return SUCCESS; } -Result DvppCropAndPaste::InitCropAndPasteOutputDesc() -{ - int resizeOutWidth = size_.width; - int resizeOutHeight = size_.height; +Result DvppCropAndPaste::InitCropAndPasteOutputDesc() { + int resizeOutWidth = g_size_.width; + int resizeOutHeight = g_size_.height; int resizeOutWidthStride = ALIGN_UP16(resizeOutWidth); int resizeOutHeightStride = ALIGN_UP2(resizeOutHeight); if (resizeOutWidthStride == 0 || resizeOutHeightStride == 0) { @@ -76,32 +70,32 @@ Result DvppCropAndPaste::InitCropAndPasteOutputDesc() return FAILED; } - vpcOutBufferSize_ = YUV420SP_SIZE(resizeOutWidthStride, resizeOutHeightStride); - aclError aclRet = acldvppMalloc(&vpcOutBufferDev_, vpcOutBufferSize_); + g_vpcOutBufferSize_ = YUV420SP_SIZE(resizeOutWidthStride, resizeOutHeightStride); + aclError aclRet = acldvppMalloc(&g_vpcOutBufferDev_, g_vpcOutBufferSize_); if (aclRet != ACL_SUCCESS) { - ERROR_LOG("acldvppMalloc vpcOutBufferDev_ failed, aclRet = %d", aclRet); + ERROR_LOG("acldvppMalloc g_vpcOutBufferDev_ failed, aclRet = %d", aclRet); return FAILED; } - vpcOutputDesc_ = acldvppCreatePicDesc(); - if (vpcOutputDesc_ == nullptr) { - ERROR_LOG("acldvppCreatePicDesc vpcOutputDesc_ failed"); + g_vpcOutputDesc_ = acldvppCreatePicDesc(); + if (g_vpcOutputDesc_ == nullptr) { + ERROR_LOG("acldvppCreatePicDesc g_vpcOutputDesc_ failed"); return FAILED; } - acldvppSetPicDescData(vpcOutputDesc_, vpcOutBufferDev_); - acldvppSetPicDescFormat(vpcOutputDesc_, format_); - acldvppSetPicDescWidth(vpcOutputDesc_, resizeOutWidth); - acldvppSetPicDescHeight(vpcOutputDesc_, resizeOutHeight); - acldvppSetPicDescWidthStride(vpcOutputDesc_, resizeOutWidthStride); - acldvppSetPicDescHeightStride(vpcOutputDesc_, resizeOutHeightStride); - acldvppSetPicDescSize(vpcOutputDesc_, vpcOutBufferSize_); + acldvppSetPicDescData(g_vpcOutputDesc_, g_vpcOutBufferDev_); + acldvppSetPicDescFormat(g_vpcOutputDesc_, g_format_); + acldvppSetPicDescWidth(g_vpcOutputDesc_, resizeOutWidth); + acldvppSetPicDescHeight(g_vpcOutputDesc_, resizeOutHeight); + acldvppSetPicDescWidthStride(g_vpcOutputDesc_, resizeOutWidthStride); + acldvppSetPicDescHeightStride(g_vpcOutputDesc_, resizeOutHeightStride); + acldvppSetPicDescSize(g_vpcOutputDesc_, g_vpcOutBufferSize_); return SUCCESS; } // IN/OUT Desc Result DvppCropAndPaste::InitCropAndPasteResource(ImageData& inputImage) { - format_ = static_cast(PIXEL_FORMAT_YUV_SEMIPLANAR_420); + g_format_ = static_cast(PIXEL_FORMAT_YUV_SEMIPLANAR_420); if (SUCCESS != InitCropAndPasteInputDesc(inputImage)) { ERROR_LOG("InitCropAndPasteInputDesc failed"); return FAILED; @@ -115,8 +109,7 @@ Result DvppCropAndPaste::InitCropAndPasteResource(ImageData& inputImage) { return SUCCESS; } -Result DvppCropAndPaste::Process(ImageData& resizedImage, ImageData& srcImage) -{ +Result DvppCropAndPaste::Process(ImageData& resizedImage, ImageData& srcImage) { if (SUCCESS != InitCropAndPasteResource(srcImage)) { ERROR_LOG("Dvpp cropandpaste failed for init error"); return FAILED; @@ -127,14 +120,14 @@ Result DvppCropAndPaste::Process(ImageData& resizedImage, ImageData& srcImage) // must even uint32_t cropTopOffset = 0; // must odd - uint32_t cropRightOffset = (((cropLeftOffset + originalImageWidth_) >> 1) << 1) -1; + uint32_t cropRightOffset = (((cropLeftOffset + g_originalImageWidth_) >> 1) << 1) -1; // must odd - uint32_t cropBottomOffset = (((cropTopOffset + originalImageHeight_) >> 1) << 1) -1; + uint32_t cropBottomOffset = (((cropTopOffset + g_originalImageHeight_) >> 1) << 1) -1; - cropArea_ = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset, + g_cropArea_ = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset, cropTopOffset, cropBottomOffset); - if (cropArea_ == nullptr) { - ERROR_LOG("acldvppCreateRoiConfig cropArea_ failed"); + if (g_cropArea_ == nullptr) { + ERROR_LOG("acldvppCreateRoiConfig g_cropArea_ failed"); return FAILED; } @@ -143,61 +136,60 @@ Result DvppCropAndPaste::Process(ImageData& resizedImage, ImageData& srcImage) // must even uint32_t pasteTopOffset = 0; // must odd - uint32_t pasteRightOffset = (((pasteLeftOffset + size_.width) >> 1) << 1) -1; + uint32_t pasteRightOffset = (((pasteLeftOffset + g_size_.width) >> 1) << 1) -1; // must odd - uint32_t pasteBottomOffset = (((pasteTopOffset + size_.height) >> 1) << 1) -1; + uint32_t pasteBottomOffset = (((pasteTopOffset + g_size_.height) >> 1) << 1) -1; - pasteArea_ = acldvppCreateRoiConfig(pasteLeftOffset, pasteRightOffset, + g_pasteArea_ = acldvppCreateRoiConfig(pasteLeftOffset, pasteRightOffset, pasteTopOffset, pasteBottomOffset); - if (pasteArea_ == nullptr) { - ERROR_LOG("acldvppCreateRoiConfig pasteArea_ failed"); + if (g_pasteArea_ == nullptr) { + ERROR_LOG("acldvppCreateRoiConfig g_pasteArea_ failed"); return FAILED; } // crop and patse pic - aclError aclRet = acldvppVpcCropAndPasteAsync(dvppChannelDesc_, vpcInputDesc_, - vpcOutputDesc_, cropArea_, pasteArea_, stream_); + aclError aclRet = acldvppVpcCropAndPasteAsync(g_dvppChannelDesc_, g_vpcInputDesc_, + g_vpcOutputDesc_, g_cropArea_, g_pasteArea_, g_stream_); if (aclRet != ACL_SUCCESS) { ERROR_LOG("acldvppVpcCropAndPasteAsync failed, aclRet = %d", aclRet); return FAILED; } - aclRet = aclrtSynchronizeStream(stream_); + aclRet = aclrtSynchronizeStream(g_stream_); if (aclRet != ACL_SUCCESS) { ERROR_LOG("crop and paste aclrtSynchronizeStream failed, aclRet = %d", aclRet); return FAILED; } - resizedImage.width = size_.width; - resizedImage.height = size_.height; - resizedImage.alignWidth = ALIGN_UP16(size_.width); - resizedImage.alignHeight = ALIGN_UP2(size_.height); - resizedImage.size = vpcOutBufferSize_; - resizedImage.data = SHARED_PTR_DVPP_BUF(vpcOutBufferDev_); + resizedImage.width = g_size_.width; + resizedImage.height = g_size_.height; + resizedImage.alignWidth = ALIGN_UP16(g_size_.width); + resizedImage.alignHeight = ALIGN_UP2(g_size_.height); + resizedImage.size = g_vpcOutBufferSize_; + resizedImage.data = SHARED_PTR_DVPP_BUF(g_vpcOutBufferDev_); DestroyCropAndPasteResource(); return SUCCESS; } -void DvppCropAndPaste::DestroyCropAndPasteResource() -{ - if (cropArea_ != nullptr) { - (void)acldvppDestroyRoiConfig(cropArea_); - cropArea_ = nullptr; +void DvppCropAndPaste::DestroyCropAndPasteResource() { + if (g_cropArea_ != nullptr) { + (void)acldvppDestroyRoiConfig(g_cropArea_); + g_cropArea_ = nullptr; } - if (pasteArea_ != nullptr) { - (void)acldvppDestroyRoiConfig(pasteArea_); - pasteArea_ = nullptr; + if (g_pasteArea_ != nullptr) { + (void)acldvppDestroyRoiConfig(g_pasteArea_); + g_pasteArea_ = nullptr; } - if (vpcInputDesc_ != nullptr) { - (void)acldvppDestroyPicDesc(vpcInputDesc_); - vpcInputDesc_ = nullptr; + if (g_vpcInputDesc_ != nullptr) { + (void)acldvppDestroyPicDesc(g_vpcInputDesc_); + g_vpcInputDesc_ = nullptr; } - if (vpcOutputDesc_ != nullptr) { - (void)acldvppDestroyPicDesc(vpcOutputDesc_); - vpcOutputDesc_ = nullptr; + if (g_vpcOutputDesc_ != nullptr) { + (void)acldvppDestroyPicDesc(g_vpcOutputDesc_); + g_vpcOutputDesc_ = nullptr; } } diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/dvpp_jpegd.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/dvpp_jpegd.cpp index df98ce0a7..942b8d8ed 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/dvpp_jpegd.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/dvpp_jpegd.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,9 +12,6 @@ * 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 dvpp_process.cpp -* Description: handle dvpp process */ #include @@ -24,15 +21,15 @@ using namespace std; DvppJpegD::DvppJpegD(aclrtStream& stream, acldvppChannelDesc *dvppChannelDesc) - : stream_(stream), dvppChannelDesc_(dvppChannelDesc), - decodeOutBufferDev_(nullptr), decodeOutputDesc_(nullptr){ + : g_stream_(stream), g_dvppChannelDesc_(dvppChannelDesc), + g_decodeOutBufferDev_(nullptr), g_decodeOutputDesc_(nullptr) { } -DvppJpegD::~DvppJpegD(){ +DvppJpegD::~DvppJpegD() { DestroyDecodeResource(); } -Result DvppJpegD::InitDecodeOutputDesc(ImageData& inputImage){ +Result DvppJpegD::InitDecodeOutputDesc(ImageData& inputImage) { uint32_t decodeOutWidthStride = ALIGN_UP128(inputImage.width); uint32_t decodeOutHeightStride = ALIGN_UP16(inputImage.height); if (decodeOutWidthStride == 0 || decodeOutHeightStride == 0) { @@ -44,29 +41,29 @@ Result DvppJpegD::InitDecodeOutputDesc(ImageData& inputImage){ uint32_t decodeOutBufferSize = YUV420SP_SIZE(decodeOutWidthStride, decodeOutHeightStride) * 4; - aclError aclRet = acldvppMalloc(&decodeOutBufferDev_, decodeOutBufferSize); + aclError aclRet = acldvppMalloc(&g_decodeOutBufferDev_, decodeOutBufferSize); if (aclRet != ACL_SUCCESS) { - ERROR_LOG("acldvppMalloc decodeOutBufferDev_ failed, aclRet = %d", aclRet); + ERROR_LOG("acldvppMalloc g_decodeOutBufferDev_ failed, aclRet = %d", aclRet); return FAILED; } - decodeOutputDesc_ = acldvppCreatePicDesc(); - if (decodeOutputDesc_ == nullptr) { - ERROR_LOG("acldvppCreatePicDesc decodeOutputDesc_ failed"); + g_decodeOutputDesc_ = acldvppCreatePicDesc(); + if (g_decodeOutputDesc_ == nullptr) { + ERROR_LOG("acldvppCreatePicDesc g_decodeOutputDesc_ failed"); return FAILED; } - acldvppSetPicDescData(decodeOutputDesc_, decodeOutBufferDev_); - acldvppSetPicDescFormat(decodeOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); - acldvppSetPicDescWidth(decodeOutputDesc_, inputImage.width); - acldvppSetPicDescHeight(decodeOutputDesc_, inputImage.height); - acldvppSetPicDescWidthStride(decodeOutputDesc_, decodeOutWidthStride); - acldvppSetPicDescHeightStride(decodeOutputDesc_, decodeOutHeightStride); - acldvppSetPicDescSize(decodeOutputDesc_, decodeOutBufferSize); + acldvppSetPicDescData(g_decodeOutputDesc_, g_decodeOutBufferDev_); + acldvppSetPicDescFormat(g_decodeOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); + acldvppSetPicDescWidth(g_decodeOutputDesc_, inputImage.width); + acldvppSetPicDescHeight(g_decodeOutputDesc_, inputImage.height); + acldvppSetPicDescWidthStride(g_decodeOutputDesc_, decodeOutWidthStride); + acldvppSetPicDescHeightStride(g_decodeOutputDesc_, decodeOutHeightStride); + acldvppSetPicDescSize(g_decodeOutputDesc_, decodeOutBufferSize); return SUCCESS; } -Result DvppJpegD::Process(ImageData& dest, ImageData& src){ +Result DvppJpegD::Process(ImageData& dest, ImageData& src) { int ret = InitDecodeOutputDesc(src); if (ret != SUCCESS) { ERROR_LOG("InitDecodeOutputDesc failed"); @@ -75,14 +72,14 @@ Result DvppJpegD::Process(ImageData& dest, ImageData& src){ ImageData imageDevice; Utils::CopyImageDataToDvpp(imageDevice, src); - aclError aclRet = acldvppJpegDecodeAsync(dvppChannelDesc_, reinterpret_cast(imageDevice.data.get()), - imageDevice.size, decodeOutputDesc_, stream_); + aclError aclRet = acldvppJpegDecodeAsync(g_dvppChannelDesc_, reinterpret_cast(imageDevice.data.get()), + imageDevice.size, g_decodeOutputDesc_, g_stream_); if (aclRet != ACL_SUCCESS) { ERROR_LOG("acldvppJpegDecodeAsync failed, aclRet = %d", aclRet); return FAILED; } - aclRet = aclrtSynchronizeStream(stream_); + aclRet = aclrtSynchronizeStream(g_stream_); if (aclRet != ACL_SUCCESS) { ERROR_LOG("decode aclrtSynchronizeStream failed, aclRet = %d", aclRet); return FAILED; @@ -93,15 +90,15 @@ Result DvppJpegD::Process(ImageData& dest, ImageData& src){ dest.alignWidth = ALIGN_UP128(imageDevice.width); dest.alignHeight = ALIGN_UP16(imageDevice.height); dest.size = YUV420SP_SIZE(dest.alignWidth, dest.alignHeight); - dest.data = SHARED_PTR_DVPP_BUF(decodeOutBufferDev_); + dest.data = SHARED_PTR_DVPP_BUF(g_decodeOutBufferDev_); INFO_LOG("convert image success"); return SUCCESS; } -void DvppJpegD::DestroyDecodeResource(){ - if (decodeOutputDesc_ != nullptr) { - acldvppDestroyPicDesc(decodeOutputDesc_); - decodeOutputDesc_ = nullptr; +void DvppJpegD::DestroyDecodeResource() { + if (g_decodeOutputDesc_ != nullptr) { + acldvppDestroyPicDesc(g_decodeOutputDesc_); + g_decodeOutputDesc_ = nullptr; } } diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/dvpp_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/dvpp_process.cpp index f848226f4..bba407d8e 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/dvpp_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/dvpp_process.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,9 +12,6 @@ * 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 dvpp_process.cpp -* Description: handle dvpp process */ #include @@ -25,54 +22,53 @@ using namespace std; -DvppProcess::DvppProcess():dvppChannelDesc_(nullptr){ +DvppProcess::DvppProcess():g_dvppChannelDesc_(nullptr) { } -DvppProcess::~DvppProcess(){ +DvppProcess::~DvppProcess() { DestroyResource(); } -void DvppProcess::DestroyResource(){ +void DvppProcess::DestroyResource() { aclError aclRet; - if (dvppChannelDesc_ != nullptr) { - aclRet = acldvppDestroyChannel(dvppChannelDesc_); + if (g_dvppChannelDesc_ != nullptr) { + aclRet = acldvppDestroyChannel(g_dvppChannelDesc_); if (aclRet != ACL_SUCCESS) { ERROR_LOG("acldvppDestroyChannel failed, aclRet = %d", aclRet); } - (void)acldvppDestroyChannelDesc(dvppChannelDesc_); - dvppChannelDesc_ = nullptr; + (void)acldvppDestroyChannelDesc(g_dvppChannelDesc_); + g_dvppChannelDesc_ = nullptr; } } -Result DvppProcess::InitResource(aclrtStream& stream) -{ +Result DvppProcess::InitResource(aclrtStream& stream) { aclError aclRet; - dvppChannelDesc_ = acldvppCreateChannelDesc(); - if (dvppChannelDesc_ == nullptr) { + g_dvppChannelDesc_ = acldvppCreateChannelDesc(); + if (g_dvppChannelDesc_ == nullptr) { ERROR_LOG("acldvppCreateChannelDesc failed"); return FAILED; } - aclRet = acldvppCreateChannel(dvppChannelDesc_); + aclRet = acldvppCreateChannel(g_dvppChannelDesc_); if (aclRet != ACL_SUCCESS) { ERROR_LOG("acldvppCreateChannel failed, aclRet = %d", aclRet); return FAILED; } - stream_ = stream; + g_stream_ = stream; INFO_LOG("dvpp init resource ok"); return SUCCESS; } Result DvppProcess::CropAndPaste(ImageData& dest, ImageData& src, uint32_t width, uint32_t height) { - DvppCropAndPaste cropandpasteOp(stream_, dvppChannelDesc_, width, height); + DvppCropAndPaste cropandpasteOp(g_stream_, g_dvppChannelDesc_, width, height); return cropandpasteOp.Process(dest, src); } Result DvppProcess::CvtJpegToYuv420sp(ImageData& dest, ImageData& src) { - DvppJpegD jpegD(stream_, dvppChannelDesc_); + DvppJpegD jpegD(g_stream_, g_dvppChannelDesc_); return jpegD.Process(dest, src); } diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/freetype_helper.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/freetype_helper.cpp index 1dc5a66b1..99b24ed0c 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/freetype_helper.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/freetype_helper.cpp @@ -1,3 +1,19 @@ +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at + +* http://www.apache.org/licenses/LICENSE-2.0 + +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + #include #include #include @@ -24,8 +40,7 @@ const char *FreeTypeErrorMessage(FT_Error err) { } } -#define CHECK_FREETYPE(x) \ -{ \ +#define CHECK_FREETYPE(x) { \ auto error = x; \ if (error) { \ auto err_msg = FreeTypeErrorMessage(error); \ @@ -81,7 +96,7 @@ int DrawChar(char32_t ch, int x, int y, const YUVColor *color, private: GlpyhContext() { - std::vector font_list{ + std::vector font_list { "/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf", // For CJK "/usr/share/fonts/truetype/noto/NotoMono-Regular.ttf" // For latin and // digits @@ -110,7 +125,7 @@ void SetPixel(ImageData& image, int x, int y, const YUVColor &color) { uv_offset[1] = color.v; } -//cache glyphs +// cache glyphs void RenderText(ImageData& image, int x, int y, const std::string &text, const YUVColor *color) { auto &ctx = GlpyhContext::GetInstance(); std::wstring_convert, char32_t> cvt; diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/main.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/main.cpp index 57a99d23c..ae6461b8d 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/main.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/main.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,9 +12,6 @@ * 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 main.cpp -* Description: dvpp sample main func */ #include @@ -25,28 +22,28 @@ using namespace std; namespace { -uint32_t kModelWidth = 416; -uint32_t kModelHeight = 416; -const char* kModelPath = "../model/yolov3.om"; +uint32_t g_ModelWidth = 416; +uint32_t g_ModelHeight = 416; +const char* g_ModelPath = "../model/yolov3.om"; } int main(int argc, char *argv[]) { - //Check input args: the path of input picture, and ignore hidden dotfile directory - if((argc != 2) || (argv[1] == nullptr)){ + // Check input args: the path of input picture, and ignore hidden dotfile directory + if ((argc != 2) || (argv[1] == nullptr)) { ERROR_LOG("Please input: ./main "); return FAILED; } - //Instantiate the object detection class - ObjectDetect detect(kModelPath, kModelWidth, kModelHeight); - //Initialize the acl resources, dvpp, load model, - //and malloc input memory of input which is const + // Instantiate the object detection class + ObjectDetect detect(g_ModelPath, g_ModelWidth, g_ModelHeight); + // Initialize the acl resources, dvpp, load model, + // and malloc input memory of input which is const Result ret = detect.Init(); if (ret != SUCCESS) { ERROR_LOG("Classification Init resource failed"); return FAILED; } - //Get all the image file path in the image directory + // Get all the image file path in the image directory string inputImageDir = string(argv[1]); vector fileVec; Utils::GetAllFiles(inputImageDir, fileVec); @@ -64,7 +61,7 @@ int main(int argc, char *argv[]) { return FAILED; } - //Preprocess: copy image to device, convert to yuv, and resize + // Preprocess: copy image to device, convert to yuv, and resize ImageData resizedImage; ImageData yuvImage; Result ret = detect.Preprocess(resizedImage, image, yuvImage); @@ -73,16 +70,16 @@ int main(int argc, char *argv[]) { imageFile.c_str()); continue; } - //Send the resized picture to the model for inference - //and get the inference results + // Send the resized picture to the model for inference + // and get the inference results aclmdlDataset* inferenceOutput = nullptr; ret = detect.Inference(inferenceOutput, resizedImage); if ((ret != SUCCESS) || (inferenceOutput == nullptr)) { ERROR_LOG("Inference model inference output data failed"); return FAILED; } - //Analyze the inference output, mark the object category and - //location by the inference result + // Analyze the inference output, mark the object category and + // location by the inference result ret = detect.Postprocess(yuvImage, inferenceOutput, imageFile); if (ret != SUCCESS) { ERROR_LOG("Process model inference output data failed"); diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/model_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/model_process.cpp index 66ef4288b..bd5ebf557 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/model_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/model_process.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,79 +12,77 @@ * 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 model_process.cpp -* Description: handle model process */ + #include "model_process.h" #include #include "utils.h" using namespace std; -ModelProcess::ModelProcess():loadFlag_(false), modelId_(0), modelMemPtr_(nullptr), modelMemSize_(0), -modelWeightPtr_(nullptr),modelWeightSize_(0), modelDesc_(nullptr), input_(nullptr), output_(nullptr), -isReleased_(false){ +ModelProcess::ModelProcess():g_loadFlag_(false), g_modelId_(0), g_modelMemPtr_(nullptr), g_modelMemSize_(0), +g_modelWeightPtr_(nullptr),g_modelWeightSize_(0), g_modelDesc_(nullptr), g_input_(nullptr), g_output_(nullptr), +g_isReleased_(false) { } -ModelProcess::~ModelProcess(){ +ModelProcess::~ModelProcess() { DestroyResource(); } -void ModelProcess::DestroyResource(){ - if (isReleased_) +void ModelProcess::DestroyResource() { + if (g_isReleased_) return; Unload(); DestroyDesc(); DestroyInput(); DestroyOutput(); - isReleased_ = true; + g_isReleased_ = true; } -Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath){ - if (loadFlag_) { +Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath) { + if (g_loadFlag_) { ERROR_LOG("has already loaded a model"); return FAILED; } - aclError ret = aclmdlQuerySize(modelPath, &modelMemSize_, &modelWeightSize_); + aclError ret = aclmdlQuerySize(modelPath, &g_modelMemSize_, &g_modelWeightSize_); if (ret != ACL_SUCCESS) { ERROR_LOG("query model failed, model file is %s", modelPath); return FAILED; } - ret = aclrtMalloc(&modelMemPtr_, modelMemSize_, ACL_MEM_MALLOC_HUGE_FIRST); + ret = aclrtMalloc(&g_modelMemPtr_, g_modelMemSize_, ACL_MEM_MALLOC_HUGE_FIRST); if (ret != ACL_SUCCESS) { - ERROR_LOG("malloc buffer for mem failed, require size is %zu", modelMemSize_); + ERROR_LOG("malloc buffer for mem failed, require size is %zu", g_modelMemSize_); return FAILED; } - ret = aclrtMalloc(&modelWeightPtr_, modelWeightSize_, ACL_MEM_MALLOC_HUGE_FIRST); + ret = aclrtMalloc(&g_modelWeightPtr_, g_modelWeightSize_, ACL_MEM_MALLOC_HUGE_FIRST); if (ret != ACL_SUCCESS) { - ERROR_LOG("malloc buffer for weight failed, require size is %zu", modelWeightSize_); + ERROR_LOG("malloc buffer for weight failed, require size is %zu", g_modelWeightSize_); return FAILED; } - ret = aclmdlLoadFromFileWithMem(modelPath, &modelId_, modelMemPtr_, - modelMemSize_, modelWeightPtr_, modelWeightSize_); + ret = aclmdlLoadFromFileWithMem(modelPath, &g_modelId_, g_modelMemPtr_, + g_modelMemSize_, g_modelWeightPtr_, g_modelWeightSize_); if (ret != ACL_SUCCESS) { ERROR_LOG("load model from file failed, model file is %s", modelPath); return FAILED; } - loadFlag_ = true; + g_loadFlag_ = true; INFO_LOG("load model %s success", modelPath); return SUCCESS; } -Result ModelProcess::CreateDesc(){ - modelDesc_ = aclmdlCreateDesc(); - if (modelDesc_ == nullptr) { +Result ModelProcess::CreateDesc() { + g_modelDesc_ = aclmdlCreateDesc(); + if (g_modelDesc_ == nullptr) { ERROR_LOG("create model description failed"); return FAILED; } - aclError ret = aclmdlGetDesc(modelDesc_, modelId_); + aclError ret = aclmdlGetDesc(g_modelDesc_, g_modelId_); if (ret != ACL_SUCCESS) { ERROR_LOG("get model description failed"); return FAILED; @@ -94,28 +92,28 @@ Result ModelProcess::CreateDesc(){ return SUCCESS; } -void ModelProcess::DestroyDesc(){ - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; +void ModelProcess::DestroyDesc() { + if (g_modelDesc_ != nullptr) { + (void)aclmdlDestroyDesc(g_modelDesc_); + g_modelDesc_ = nullptr; } } -Result ModelProcess::CreateInput(void *input1, size_t input1size, - void* input2, size_t input2size){ - input_ = aclmdlCreateDataset(); - if (input_ == nullptr) { +Result ModelProcess::CreateInput(void *input1, size_t input1Size, + void* input2, size_t input2Size) { + g_input_ = aclmdlCreateDataset(); + if (g_input_ == nullptr) { ERROR_LOG("can't create dataset, create input failed"); return FAILED; } - aclDataBuffer* inputData = aclCreateDataBuffer(input1, input1size); + aclDataBuffer* inputData = aclCreateDataBuffer(input1, input1Size); if (inputData == nullptr) { ERROR_LOG("can't create data buffer, create input failed"); return FAILED; } - aclError ret = aclmdlAddDatasetBuffer(input_, inputData); + aclError ret = aclmdlAddDatasetBuffer(g_input_, inputData); if (inputData == nullptr) { ERROR_LOG("can't add data buffer, create input failed"); aclDestroyDataBuffer(inputData); @@ -123,13 +121,13 @@ Result ModelProcess::CreateInput(void *input1, size_t input1size, return FAILED; } - aclDataBuffer* inputData2 = aclCreateDataBuffer(input2, input2size); + aclDataBuffer* inputData2 = aclCreateDataBuffer(input2, input2Size); if (inputData == nullptr) { ERROR_LOG("can't create data buffer, create input failed"); return FAILED; } - ret = aclmdlAddDatasetBuffer(input_, inputData2); + ret = aclmdlAddDatasetBuffer(g_input_, inputData2); if (inputData == nullptr) { ERROR_LOG("can't add data buffer, create input failed"); aclDestroyDataBuffer(inputData2); @@ -141,33 +139,33 @@ Result ModelProcess::CreateInput(void *input1, size_t input1size, } void ModelProcess::DestroyInput(){ - if (input_ == nullptr) { + if (g_input_ == nullptr) { return; } - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(input_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(input_, i); + for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(g_input_); ++i) { + aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(g_input_, i); aclDestroyDataBuffer(dataBuffer); } - aclmdlDestroyDataset(input_); - input_ = nullptr; + aclmdlDestroyDataset(g_input_); + g_input_ = nullptr; } Result ModelProcess::CreateOutput(){ - if (modelDesc_ == nullptr) { + if (g_modelDesc_ == nullptr) { ERROR_LOG("no model description, create ouput failed"); return FAILED; } - output_ = aclmdlCreateDataset(); - if (output_ == nullptr) { + g_output_ = aclmdlCreateDataset(); + if (g_output_ == nullptr) { ERROR_LOG("can't create dataset, create output failed"); return FAILED; } - size_t outputSize = aclmdlGetNumOutputs(modelDesc_); + size_t outputSize = aclmdlGetNumOutputs(g_modelDesc_); for (size_t i = 0; i < outputSize; ++i) { - size_t buffer_size = aclmdlGetOutputSizeByIndex(modelDesc_, i); + size_t buffer_size = aclmdlGetOutputSizeByIndex(g_modelDesc_, i); void *outputBuffer = nullptr; aclError ret = aclrtMalloc(&outputBuffer, buffer_size, ACL_MEM_MALLOC_NORMAL_ONLY); @@ -183,7 +181,7 @@ Result ModelProcess::CreateOutput(){ return FAILED; } - ret = aclmdlAddDatasetBuffer(output_, outputData); + ret = aclmdlAddDatasetBuffer(g_output_, outputData); if (ret != ACL_SUCCESS) { ERROR_LOG("can't add data buffer, create output failed"); aclrtFree(outputBuffer); @@ -197,25 +195,25 @@ Result ModelProcess::CreateOutput(){ } void ModelProcess::DestroyOutput(){ - if (output_ == nullptr) { + if (g_output_ == nullptr) { return; } - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); + for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(g_output_); ++i) { + aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(g_output_, i); void* data = aclGetDataBufferAddr(dataBuffer); (void)aclrtFree(data); (void)aclDestroyDataBuffer(dataBuffer); } - (void)aclmdlDestroyDataset(output_); - output_ = nullptr; + (void)aclmdlDestroyDataset(g_output_); + g_output_ = nullptr; } Result ModelProcess::Execute(){ - aclError ret = aclmdlExecute(modelId_, input_, output_); + aclError ret = aclmdlExecute(g_modelId_, g_input_, g_output_); if (ret != ACL_SUCCESS) { - ERROR_LOG("execute model failed, modelId is %u", modelId_); + ERROR_LOG("execute model failed, modelId is %u", g_modelId_); return FAILED; } @@ -225,37 +223,37 @@ Result ModelProcess::Execute(){ void ModelProcess::Unload() { - if (!loadFlag_) { + if (!g_loadFlag_) { WARN_LOG("no model had been loaded, unload failed"); return; } - aclError ret = aclmdlUnload(modelId_); + aclError ret = aclmdlUnload(g_modelId_); if (ret != ACL_SUCCESS) { - ERROR_LOG("unload model failed, modelId is %u", modelId_); + ERROR_LOG("unload model failed, modelId is %u", g_modelId_); } - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; + if (g_modelDesc_ != nullptr) { + (void)aclmdlDestroyDesc(g_modelDesc_); + g_modelDesc_ = nullptr; } - if (modelMemPtr_ != nullptr) { - aclrtFree(modelMemPtr_); - modelMemPtr_ = nullptr; - modelMemSize_ = 0; + if (g_modelMemPtr_ != nullptr) { + aclrtFree(g_modelMemPtr_); + g_modelMemPtr_ = nullptr; + g_modelMemSize_ = 0; } - if (modelWeightPtr_ != nullptr) { - aclrtFree(modelWeightPtr_); - modelWeightPtr_ = nullptr; - modelWeightSize_ = 0; + if (g_modelWeightPtr_ != nullptr) { + aclrtFree(g_modelWeightPtr_); + g_modelWeightPtr_ = nullptr; + g_modelWeightSize_ = 0; } - loadFlag_ = false; - INFO_LOG("unload model success, modelId is %u", modelId_); + g_loadFlag_ = false; + INFO_LOG("unload model success, modelId is %u", g_modelId_); } aclmdlDataset *ModelProcess::GetModelOutputData(){ - return output_; + return g_output_; } diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/object_detect.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/object_detect.cpp index 53d8dd3e8..1bdf56254 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/object_detect.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/object_detect.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,8 @@ * 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 "object_detect.h" #include @@ -53,11 +51,11 @@ namespace { ObjectDetect::ObjectDetect(const char* modelPath, uint32_t modelWidth, uint32_t modelHeight) -:deviceId_(0), context_(nullptr), stream_(nullptr), modelWidth_(modelWidth), -modelHeight_(modelHeight), isInited_(false), isDeviceSet_(false){ - imageInfoSize_ = 0; - imageInfoBuf_ = nullptr; - modelPath_ = modelPath; +:g_deviceId_(0), g_context_(nullptr), g_stream_(nullptr), g_modelWidth_(modelWidth), +g_modelHeight_(modelHeight), g_isInited_(false), g_isDeviceSet_(false) { + g_imageInfoSize_ = 0; + g_imageInfoBuf_ = nullptr; + g_modelPath_ = modelPath; } ObjectDetect::~ObjectDetect() { @@ -75,16 +73,16 @@ Result ObjectDetect::InitResource() { INFO_LOG("acl init success"); // open device - ret = aclrtSetDevice(deviceId_); + ret = aclrtSetDevice(g_deviceId_); if (ret != ACL_SUCCESS) { - ERROR_LOG("acl open device %d failed", deviceId_); + ERROR_LOG("acl open device %d failed", g_deviceId_); return FAILED; } - isDeviceSet_ = true; - INFO_LOG("open device %d success", deviceId_); + g_isDeviceSet_ = true; + INFO_LOG("open device %d success", g_deviceId_); // create context (set current) - ret = aclrtCreateContext(&context_, deviceId_); + ret = aclrtCreateContext(&g_context_, g_deviceId_); if (ret != ACL_SUCCESS) { ERROR_LOG("acl create context failed"); return FAILED; @@ -92,14 +90,14 @@ Result ObjectDetect::InitResource() { INFO_LOG("create context success"); // create stream - ret = aclrtCreateStream(&stream_); + ret = aclrtCreateStream(&g_stream_); if (ret != ACL_SUCCESS) { ERROR_LOG("acl create stream failed"); return FAILED; } INFO_LOG("create stream success"); - ret = aclrtGetRunMode(&runMode_); + ret = aclrtGetRunMode(&g_runMode_); if (ret != ACL_SUCCESS) { ERROR_LOG("acl get run mode failed"); return FAILED; @@ -109,19 +107,19 @@ Result ObjectDetect::InitResource() { } Result ObjectDetect::InitModel(const char* omModelPath) { - Result ret = model_.LoadModelFromFileWithMem(omModelPath); + Result ret = g_model_.LoadModelFromFileWithMem(omModelPath); if (ret != SUCCESS) { ERROR_LOG("execute LoadModelFromFileWithMem failed"); return FAILED; } - ret = model_.CreateDesc(); + ret = g_model_.CreateDesc(); if (ret != SUCCESS) { ERROR_LOG("execute CreateDesc failed"); return FAILED; } - ret = model_.CreateOutput(); + ret = g_model_.CreateOutput(); if (ret != SUCCESS) { ERROR_LOG("execute CreateOutput failed"); return FAILED; @@ -130,15 +128,15 @@ Result ObjectDetect::InitModel(const char* omModelPath) { return SUCCESS; } -Result ObjectDetect::CreateImageInfoBuffer(){ - const float imageInfo[4] = {(float)modelWidth_, (float)modelHeight_, - (float)modelWidth_, (float)modelHeight_}; - imageInfoSize_ = sizeof(imageInfo); - if (runMode_ == ACL_HOST) - imageInfoBuf_ = Utils::CopyDataHostToDevice((void *)imageInfo, imageInfoSize_); +Result ObjectDetect::CreateImageInfoBuffer() { + const float imageInfo[4] = {(float)g_modelWidth_, (float)g_modelHeight_, + (float)g_modelWidth_, (float)g_modelHeight_}; + g_imageInfoSize_ = sizeof(imageInfo); + if (g_runMode_ == ACL_HOST) + g_imageInfoBuf_ = Utils::CopyDataHostToDevice((void *)imageInfo, g_imageInfoSize_); else - imageInfoBuf_ = Utils::CopyDataDeviceToDevice((void *)imageInfo, imageInfoSize_); - if (imageInfoBuf_ == nullptr) { + g_imageInfoBuf_ = Utils::CopyDataDeviceToDevice((void *)imageInfo, g_imageInfoSize_); + if (g_imageInfoBuf_ == nullptr) { ERROR_LOG("Copy image info to device failed"); return FAILED; } @@ -147,7 +145,7 @@ Result ObjectDetect::CreateImageInfoBuffer(){ } Result ObjectDetect::Init() { - if (isInited_) { + if (g_isInited_) { INFO_LOG("Object detection instance is initied already!"); return SUCCESS; } @@ -158,13 +156,13 @@ Result ObjectDetect::Init() { return FAILED; } - ret = InitModel(modelPath_); + ret = InitModel(g_modelPath_); if (ret != SUCCESS) { ERROR_LOG("Init model failed"); return FAILED; } - ret = dvpp_.InitResource(stream_); + ret = g_dvpp_.InitResource(g_stream_); if (ret != SUCCESS) { ERROR_LOG("Init dvpp failed"); return FAILED; @@ -176,7 +174,7 @@ Result ObjectDetect::Init() { return FAILED; } - isInited_ = true; + g_isInited_ = true; return SUCCESS; } @@ -184,41 +182,41 @@ Result ObjectDetect::Preprocess(ImageData& resizedImage, ImageData& srcImage, ImageData& oriImage) { ImageData imageDevice, yuvImage; - Utils::CopyImageDataToDevice(imageDevice, srcImage, runMode_); + Utils::CopyImageDataToDevice(imageDevice, srcImage, g_runMode_); - Result ret = dvpp_.CvtJpegToYuv420sp(yuvImage, imageDevice); + Result ret = g_dvpp_.CvtJpegToYuv420sp(yuvImage, imageDevice); if (ret == FAILED) { ERROR_LOG("Convert jpeg to yuv failed"); return FAILED; } - ret = dvpp_.CropAndPaste(resizedImage, yuvImage, modelWidth_, modelHeight_); + ret = g_dvpp_.CropAndPaste(resizedImage, yuvImage, g_modelWidth_, g_modelHeight_); if (ret == FAILED) { ERROR_LOG("Resize image failed"); return FAILED; } - Utils::CopyDeviceToLocal(oriImage, yuvImage, runMode_); + Utils::CopyDeviceToLocal(oriImage, yuvImage, g_runMode_); return SUCCESS; } Result ObjectDetect::Inference(aclmdlDataset*& inferenceOutput, ImageData& resizedImage) { - Result ret = model_.CreateInput(resizedImage.data.get(), + Result ret = g_model_.CreateInput(resizedImage.data.get(), resizedImage.size, - imageInfoBuf_, imageInfoSize_); + g_imageInfoBuf_, g_imageInfoSize_); if (ret != SUCCESS) { ERROR_LOG("Create mode input dataset failed"); return FAILED; } - ret = model_.Execute(); + ret = g_model_.Execute(); if (ret != SUCCESS) { ERROR_LOG("Execute model inference failed"); return FAILED; } - inferenceOutput = model_.GetModelOutputData(); + inferenceOutput = g_model_.GetModelOutputData(); return SUCCESS; } @@ -235,8 +233,8 @@ const string& origImagePath) { uint32_t totalBox = boxNum[0]; vector detectResults; - float widthScale = (float)(image.width) / modelWidth_; - float heightScale = (float)(image.height) / modelHeight_; + float widthScale = (float)(image.width) / g_modelWidth_; + float heightScale = (float)(image.height) / g_modelHeight_; for (uint32_t i = 0; i < totalBox; i++) { BBox boundBox; uint32_t score = uint32_t(detectData[totalBox * SCORE + i] * 100); @@ -255,7 +253,7 @@ const string& origImagePath) { } DrawBoundBoxToImage(image, detectResults, origImagePath); - if (runMode_ == ACL_HOST) { + if (g_runMode_ == ACL_HOST) { delete[]((uint8_t *)detectData); delete[]((uint8_t*)boxNum); } @@ -312,7 +310,7 @@ void* ObjectDetect::GetInferenceOutputItem(uint32_t& itemDataSize, } void* data = nullptr; - if (runMode_ == ACL_HOST) { + if (g_runMode_ == ACL_HOST) { data = Utils::CopyDataDeviceToLocal(dataBufferDev, bufferSize); if (data == nullptr) { ERROR_LOG("Copy inference output to host failed"); @@ -326,35 +324,35 @@ void* ObjectDetect::GetInferenceOutputItem(uint32_t& itemDataSize, return data; } -void ObjectDetect::DestroyResource(){ - aclrtFree(imageInfoBuf_); - model_.DestroyResource(); - dvpp_.DestroyResource(); +void ObjectDetect::DestroyResource() { + aclrtFree(g_imageInfoBuf_); + g_model_.DestroyResource(); + g_dvpp_.DestroyResource(); aclError ret; - if (stream_ != nullptr) { - ret = aclrtDestroyStream(stream_); + if (g_stream_ != nullptr) { + ret = aclrtDestroyStream(g_stream_); if (ret != ACL_SUCCESS) { ERROR_LOG("destroy stream failed"); } - stream_ = nullptr; + g_stream_ = nullptr; } INFO_LOG("end to destroy stream"); - if (context_ != nullptr) { - ret = aclrtDestroyContext(context_); + if (g_context_ != nullptr) { + ret = aclrtDestroyContext(g_context_); if (ret != ACL_SUCCESS) { ERROR_LOG("destroy context failed"); } - context_ = nullptr; + g_context_ = nullptr; } INFO_LOG("end to destroy context"); - if (isDeviceSet_) { - ret = aclrtResetDevice(deviceId_); + if (g_isDeviceSet_) { + ret = aclrtResetDevice(g_deviceId_); if (ret != ACL_SUCCESS) { ERROR_LOG("reset device failed"); } - INFO_LOG("end to reset device is %d", deviceId_); + INFO_LOG("end to reset device is %d", g_deviceId_); } ret = aclFinalize(); diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/utils.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/utils.cpp index cf151bdf9..43b511ed5 100755 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/utils.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/utils.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,8 @@ * 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 utils.cpp -* Description: handle file operations */ + #include "utils.h" #include #include @@ -33,19 +31,19 @@ using namespace std; namespace { -const std::string kImagePathSeparator = ","; -const int kStatSuccess = 0; -const std::string kFileSperator = "/"; -const std::string kPathSeparator = "/"; +const std::string g_ImagePathSeparator = ","; +const int g_StatSuccess = 0; +const std::string g_FileSperator = "/"; +const std::string g_PathSeparator = "/"; // output image prefix -const std::string kOutputFilePrefix = "out_"; +const std::string g_OutputFilePrefix = "out_"; } bool Utils::IsDirectory(const string &path) { // get path stat struct stat buf; - if (stat(path.c_str(), &buf) != kStatSuccess) { + if (stat(path.c_str(), &buf) != g_StatSuccess) { return false; } @@ -67,7 +65,7 @@ bool Utils::IsPathExist(const string &path) { void Utils::SplitPath(const string &path, vector &path_vec) { char *char_path = const_cast(path.c_str()); - const char *char_split = kImagePathSeparator.c_str(); + const char *char_split = g_ImagePathSeparator.c_str(); char *tmp_path = strtok(char_path, char_split); while (tmp_path) { path_vec.emplace_back(tmp_path); @@ -104,7 +102,7 @@ void Utils::GetPathFiles(const string &path, vector &file_vec) { } // file path - string full_path = path + kPathSeparator + dirent_ptr->d_name; + string full_path = path + g_PathSeparator + dirent_ptr->d_name; // directory need recursion if (IsDirectory(full_path)) { GetPathFiles(full_path, file_vec); @@ -171,14 +169,14 @@ Result Utils::CopyImageDataToDvpp(ImageData& imageDevice, ImageData srcImage) { } void* buffer = nullptr; - if (runMode_ == ACL_HOST){ + if (runMode_ == ACL_HOST) { buffer = Utils::CopyDataHostToDvpp(srcImage.data.get(), srcImage.size); if (buffer == nullptr) { ERROR_LOG("Copy image to device failed"); return FAILED; } } - else{ + else { buffer = Utils::CopyDataDeviceToDvpp(srcImage.data.get(), srcImage.size); if (buffer == nullptr) { ERROR_LOG("Copy image to device failed"); @@ -212,10 +210,10 @@ void* Utils::CopyDataDeviceToLocal(void* deviceData, uint32_t dataSize) { Result Utils::CopyDeviceToLocal(ImageData& imageout, ImageData srcImage, aclrtRunMode mode) { void * buffer = nullptr; - if (mode == ACL_HOST){ + if (mode == ACL_HOST) { buffer = Utils::CopyDataDeviceToLocal(srcImage.data.get(), srcImage.size); } - else{ + else { buffer = Utils::CopyDataDeviceToLocal(srcImage.data.get(), srcImage.size); } @@ -281,8 +279,7 @@ Result Utils::CopyImageDataToDevice(ImageData& imageDevice, return SUCCESS; } -int Utils::ReadImageFile(ImageData& image, std::string fileName) -{ +int Utils::ReadImageFile(ImageData& image, std::string fileName) { struct stat sBuf; int fileStatus = stat(fileName.data(), &sBuf); if (fileStatus == -1) { diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/inc/model_process.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/inc/model_process.h index 13f5358e6..f74b0753d 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/inc/model_process.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/inc/model_process.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 model_process.h -* Description: handle model process */ + +#ifndef YOLOV3_COCO_DETECTION_VIDEO_INC_MODEL_PROCESS_H +#define YOLOV3_COCO_DETECTION_VIDEO_INC_MODEL_PROCESS_H + #pragma once #include #include "utils.h" @@ -102,15 +103,16 @@ public: aclmdlDataset *GetModelOutputData(); private: - bool loadFlag_; // model load flag - uint32_t modelId_; - void *modelMemPtr_; - size_t modelMemSize_; - void *modelWeightPtr_; - size_t modelWeightSize_; - aclmdlDesc *modelDesc_; - aclmdlDataset *input_; - aclmdlDataset *output_; - bool isReleased_; + bool g_loadFlag_; // model load flag + uint32_t g_modelId_; + void *g_modelMemPtr_; + size_t g_modelMemSize_; + void *g_modelWeightPtr_; + size_t g_modelWeightSize_; + aclmdlDesc *g_modelDesc_; + aclmdlDataset *g_input_; + aclmdlDataset *g_output_; + bool g_isReleased_; }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/inc/object_detect.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/inc/object_detect.h index 91a334e47..be5ed3101 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/inc/object_detect.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/inc/object_detect.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 YOLOV3_COCO_DETECTION_VIDEO_INC_OBJECT_DETECT_H +#define YOLOV3_COCO_DETECTION_VIDEO_INC_OBJECT_DETECT_H + #pragma once #include @@ -37,48 +38,49 @@ public: ObjectDetect(const char* modelPath, uint32_t modelWidth, uint32_t modelHeight); ~ObjectDetect(); - //Inference initialization + // Inference initialization Result Init(); - //nference frame image preprocessing + // nference frame image preprocessing Result Preprocess(cv::Mat& frame); - //Inference frame picture + // Inference frame picture Result Inference(aclmdlDataset*& inferenceOutput); - //Inference output post-processing + // Inference output post-processing Result Postprocess(cv::Mat& frame, aclmdlDataset* modelOutput); private: - //Initializes the ACL resource + // Initializes the ACL resource Result InitResource(); - //Loading reasoning model + // Loading reasoning model Result InitModel(const char* omModelPath); Result CreateModelInputdDataset(); - //Establish a connection to the Presenter Server + // Establish a connection to the Presenter Server Result OpenPresenterChannel(); - //Get data from model inference output aclmdlDataset to local + // Get data from model inference output aclmdlDataset to local void* GetInferenceOutputItem(uint32_t& itemDataSize, aclmdlDataset* inferenceOutput, uint32_t idx); - //Serializes a frame image into a data stream + // Serializes a frame image into a data stream void EncodeImage(vector& encodeImg, cv::Mat& origImg); Result SendImage(std::vector& detectionResults, cv::Mat& frame); - //Release the requested resources + // Release the requested resources void DestroyResource(); private: - int32_t deviceId_; //Device ID, default is 0 - ModelProcess model_; //Inference model instance + int32_t g_deviceId_; // Device ID, default is 0 + ModelProcess g_model_; // Inference model instance - const char* modelPath_; //Offline model file path - uint32_t modelWidth_; //The input width required by the model - uint32_t modelHeight_; //The model requires high input - uint32_t imageDataSize_; //Model input data size - void* imageDataBuf_; //Model input data cache - uint32_t imageInfoSize_; - void* imageInfoBuf_; - aclrtRunMode runMode_; //Run mode, which is whether the current application is running on atlas200DK or AI1 + const char* g_modelPath_; // Offline model file path + uint32_t g_modelWidth_; // The input width required by the model + uint32_t g_modelHeight_; // The model requires high input + uint32_t g_imageDataSize_; // Model input data size + void* g_imageDataBuf_; // Model input data cache + uint32_t g_imageInfoSize_; + void* g_imageInfoBuf_; + aclrtRunMode g_runMode_; // Run mode, which is whether the current application is running on atlas200DK or AI1 - Channel* channel_; //A channel to connect to presenter Server - bool isInited_; //Initializes the tag to prevent inference instances from being initialized multiple times + Channel* g_channel_; // A channel to connect to presenter Server + bool g_isInited_; // Initializes the tag to prevent inference instances from being initialized multiple times }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/inc/sample_process.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/inc/sample_process.h deleted file mode 100644 index 7906ab5f9..000000000 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/inc/sample_process.h +++ /dev/null @@ -1,71 +0,0 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd -* -* 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 -*/ -#pragma once -#include "utils.h" -#include "acl/acl.h" -#include - -template -std::shared_ptr MakeSharedNoThrow() { - try { - return std::make_shared(); - } catch (...) { - return nullptr; - } -} - -#define MAKE_SHARED_NO_THROW(memory, memory_type) \ - memory = MakeSharedNoThrow(); - -/** -* SampleProcess -*/ -class SampleProcess { -public: - /** - * @brief Constructor - */ - SampleProcess(); - - /** - * @brief Destructor - */ - ~SampleProcess(); - - /** - * @brief init reousce - * @return result - */ - Result InitResource(); - - /** - * @brief encode sample process - * @param [in] input_path: input video path - * @return result - */ - Result VideoProcess(std::string input_path); - -private: - void DestroyResource(); - - int32_t deviceId_; - aclrtContext context_; - aclrtStream stream_; -}; - diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/inc/utils.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/inc/utils.h index 69516dd68..27719252f 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/inc/utils.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/inc/utils.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 utils.h -* Description: handle file operations */ + +#ifndef YOLOV3_COCO_DETECTION_VIDEO_INC_UTILS_H +#define YOLOV3_COCO_DETECTION_VIDEO_INC_UTILS_H + #pragma once #include @@ -116,3 +117,4 @@ public: static Result CopyImageDataToDevice(ImageData& imageDevice, ImageData srcImage, aclrtRunMode mode); }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/main.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/main.cpp index d2219a457..ce735855d 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/main.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/main.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,9 +12,6 @@ * 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 main.cpp -* Description: dvpp sample main func */ #include @@ -25,56 +22,56 @@ using namespace std; namespace { -uint32_t kModelWidth = 416; -uint32_t kModelHeight = 416; -const char* kModelPath = "../model/yolov3.om"; +uint32_t g_ModelWidth = 416; +uint32_t g_ModelHeight = 416; +const char* g_ModelPath = "../model/yolov3.om"; } int main(int argc, char *argv[]) { - //Check the input when the application executes, which takes the path to the input video file - if((argc < 2) || (argv[1] == nullptr)){ + // Check the input when the application executes, which takes the path to the input video file + if ((argc < 2) || (argv[1] == nullptr)) { ERROR_LOG("Please input: ./main "); return FAILED; } - //Instantiate the target detection class with the parameters of the classification model path and the required width and height of the model input - ObjectDetect detect(kModelPath, kModelWidth, kModelHeight); - //Initializes the ACL resource for categorical reasoning, loads the model and requests the memory used for reasoning input + // Instantiate the target detection class with the parameters of the classification model path and the required width and height of the model input + ObjectDetect detect(g_ModelPath, g_ModelWidth, g_ModelHeight); + // Initializes the ACL resource for categorical reasoning, loads the model and requests the memory used for reasoning input Result ret = detect.Init(); if (ret != SUCCESS) { ERROR_LOG("Classification Init resource failed"); return FAILED; } - //Use Opencv to open the video file + // Use Opencv to open the video file string videoFile = string(argv[1]); cv::VideoCapture capture(videoFile); if (!capture.isOpened()) { ERROR_LOG("Movie open Erro"); return FAILED; } - //Frame by frame reasoning + // Frame by frame reasoning while(1) { - //Read a frame of an image + // Read a frame of an image cv::Mat frame; if (!capture.read(frame)) { INFO_LOG("Video capture return false"); break; } - //The frame image is preprocessed + // The frame image is preprocessed Result ret = detect.Preprocess(frame); if (ret != SUCCESS) { ERROR_LOG("Read file %s failed, continue to read next", videoFile.c_str()); continue; } - //The preprocessed images are fed into model reasoning and the reasoning results are obtained + // The preprocessed images are fed into model reasoning and the reasoning results are obtained aclmdlDataset* inferenceOutput = nullptr; ret = detect.Inference(inferenceOutput); if ((ret != SUCCESS) || (inferenceOutput == nullptr)) { ERROR_LOG("Inference model inference output data failed"); return FAILED; } - //Parses the inference output and sends the inference class, location, confidence, and image to the Presenter Server for display + // Parses the inference output and sends the inference class, location, confidence, and image to the Presenter Server for display ret = detect.Postprocess(frame, inferenceOutput); if (ret != SUCCESS) { ERROR_LOG("Process model inference output data failed"); diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/model_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/model_process.cpp index 12ff2d633..66dcaa534 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/model_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/model_process.cpp @@ -21,9 +21,9 @@ #include "utils.h" using namespace std; -ModelProcess::ModelProcess():loadFlag_(false), modelId_(0), modelMemPtr_(nullptr), modelMemSize_(0), -modelWeightPtr_(nullptr),modelWeightSize_(0), modelDesc_(nullptr), input_(nullptr), output_(nullptr), -isReleased_(false){ +ModelProcess::ModelProcess():g_loadFlag_(false), g_modelId_(0), g_modelMemPtr_(nullptr), g_modelMemSize_(0), +g_modelWeightPtr_(nullptr),g_modelWeightSize_(0), g_modelDesc_(nullptr), g_input_(nullptr), g_output_(nullptr), +g_isReleased_(false) { } @@ -32,61 +32,60 @@ ModelProcess::~ModelProcess() { } void ModelProcess::DestroyResource() { - if (isReleased_) + if (g_isReleased_) return; Unload(); DestroyDesc(); DestroyInput(); DestroyOutput(); - isReleased_ = true; + g_isReleased_ = true; } Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath) { - if (loadFlag_) { + if (g_loadFlag_) { ERROR_LOG("has already loaded a model"); return FAILED; } - aclError ret = aclmdlQuerySize(modelPath, &modelMemSize_, &modelWeightSize_); + aclError ret = aclmdlQuerySize(modelPath, &g_modelMemSize_, &g_modelWeightSize_); if (ret != ACL_SUCCESS) { ERROR_LOG("query model failed, model file is %s", modelPath); return FAILED; } - ret = aclrtMalloc(&modelMemPtr_, modelMemSize_, ACL_MEM_MALLOC_HUGE_FIRST); + ret = aclrtMalloc(&g_modelMemPtr_, g_modelMemSize_, ACL_MEM_MALLOC_HUGE_FIRST); if (ret != ACL_SUCCESS) { - ERROR_LOG("malloc buffer for mem failed, require size is %zu", modelMemSize_); + ERROR_LOG("malloc buffer for mem failed, require size is %zu", g_modelMemSize_); return FAILED; } - ret = aclrtMalloc(&modelWeightPtr_, modelWeightSize_, ACL_MEM_MALLOC_HUGE_FIRST); + ret = aclrtMalloc(&g_modelWeightPtr_, g_modelWeightSize_, ACL_MEM_MALLOC_HUGE_FIRST); if (ret != ACL_SUCCESS) { - ERROR_LOG("malloc buffer for weight failed, require size is %zu", modelWeightSize_); + ERROR_LOG("malloc buffer for weight failed, require size is %zu", g_modelWeightSize_); return FAILED; } - ret = aclmdlLoadFromFileWithMem(modelPath, &modelId_, modelMemPtr_, - modelMemSize_, modelWeightPtr_, modelWeightSize_); + ret = aclmdlLoadFromFileWithMem(modelPath, &g_modelId_, g_modelMemPtr_, + g_modelMemSize_, g_modelWeightPtr_, g_modelWeightSize_); if (ret != ACL_SUCCESS) { ERROR_LOG("load model from file failed, model file is %s", modelPath); return FAILED; } - loadFlag_ = true; + g_loadFlag_ = true; INFO_LOG("load model %s success", modelPath); return SUCCESS; } -Result ModelProcess::CreateDesc() -{ - modelDesc_ = aclmdlCreateDesc(); - if (modelDesc_ == nullptr) { +Result ModelProcess::CreateDesc() { + g_modelDesc_ = aclmdlCreateDesc(); + if (g_modelDesc_ == nullptr) { ERROR_LOG("create model description failed"); return FAILED; } - aclError ret = aclmdlGetDesc(modelDesc_, modelId_); + aclError ret = aclmdlGetDesc(g_modelDesc_, g_modelId_); if (ret != ACL_SUCCESS) { ERROR_LOG("get model description failed"); return FAILED; @@ -96,19 +95,17 @@ Result ModelProcess::CreateDesc() return SUCCESS; } -void ModelProcess::DestroyDesc() -{ - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; +void ModelProcess::DestroyDesc() { + if (g_modelDesc_ != nullptr) { + (void)aclmdlDestroyDesc(g_modelDesc_); + g_modelDesc_ = nullptr; } } Result ModelProcess::CreateInput(void *input1, size_t input1size, - void* input2, size_t input2size) -{ - input_ = aclmdlCreateDataset(); - if (input_ == nullptr) { + void* input2, size_t input2size) { + g_input_ = aclmdlCreateDataset(); + if (g_input_ == nullptr) { ERROR_LOG("can't create dataset, create input failed"); return FAILED; } @@ -119,7 +116,7 @@ Result ModelProcess::CreateInput(void *input1, size_t input1size, return FAILED; } - aclError ret = aclmdlAddDatasetBuffer(input_, inputData); + aclError ret = aclmdlAddDatasetBuffer(g_input_, inputData); if (inputData == nullptr) { ERROR_LOG("can't add data buffer, create input failed"); aclDestroyDataBuffer(inputData); @@ -133,7 +130,7 @@ Result ModelProcess::CreateInput(void *input1, size_t input1size, return FAILED; } - ret = aclmdlAddDatasetBuffer(input_, inputData2); + ret = aclmdlAddDatasetBuffer(g_input_, inputData2); if (inputData == nullptr) { ERROR_LOG("can't add data buffer, create input failed"); aclDestroyDataBuffer(inputData2); @@ -144,36 +141,34 @@ Result ModelProcess::CreateInput(void *input1, size_t input1size, return SUCCESS; } -void ModelProcess::DestroyInput() -{ - if (input_ == nullptr) { +void ModelProcess::DestroyInput() { + if (g_input_ == nullptr) { return; } - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(input_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(input_, i); + for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(g_input_); ++i) { + aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(g_input_, i); aclDestroyDataBuffer(dataBuffer); } - aclmdlDestroyDataset(input_); - input_ = nullptr; + aclmdlDestroyDataset(g_input_); + g_input_ = nullptr; } -Result ModelProcess::CreateOutput() -{ - if (modelDesc_ == nullptr) { +Result ModelProcess::CreateOutput() { + if (g_modelDesc_ == nullptr) { ERROR_LOG("no model description, create ouput failed"); return FAILED; } - output_ = aclmdlCreateDataset(); - if (output_ == nullptr) { + g_output_ = aclmdlCreateDataset(); + if (g_output_ == nullptr) { ERROR_LOG("can't create dataset, create output failed"); return FAILED; } - size_t outputSize = aclmdlGetNumOutputs(modelDesc_); + size_t outputSize = aclmdlGetNumOutputs(g_modelDesc_); for (size_t i = 0; i < outputSize; ++i) { - size_t buffer_size = aclmdlGetOutputSizeByIndex(modelDesc_, i); + size_t buffer_size = aclmdlGetOutputSizeByIndex(g_modelDesc_, i); void *outputBuffer = nullptr; aclError ret = aclrtMalloc(&outputBuffer, buffer_size, ACL_MEM_MALLOC_NORMAL_ONLY); @@ -189,7 +184,7 @@ Result ModelProcess::CreateOutput() return FAILED; } - ret = aclmdlAddDatasetBuffer(output_, outputData); + ret = aclmdlAddDatasetBuffer(g_output_, outputData); if (ret != ACL_SUCCESS) { ERROR_LOG("can't add data buffer, create output failed"); aclrtFree(outputBuffer); @@ -202,68 +197,64 @@ Result ModelProcess::CreateOutput() return SUCCESS; } -void ModelProcess::DestroyOutput() -{ - if (output_ == nullptr) { +void ModelProcess::DestroyOutput() { + if (g_output_ == nullptr) { return; } - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); + for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(g_output_); ++i) { + aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(g_output_, i); void* data = aclGetDataBufferAddr(dataBuffer); (void)aclrtFree(data); (void)aclDestroyDataBuffer(dataBuffer); } - (void)aclmdlDestroyDataset(output_); - output_ = nullptr; + (void)aclmdlDestroyDataset(g_output_); + g_output_ = nullptr; } -Result ModelProcess::Execute() -{ - aclError ret = aclmdlExecute(modelId_, input_, output_); +Result ModelProcess::Execute() { + aclError ret = aclmdlExecute(g_modelId_, g_input_, g_output_); if (ret != ACL_SUCCESS) { - ERROR_LOG("execute model failed, modelId is %u", modelId_); + ERROR_LOG("execute model failed, modelId is %u", g_modelId_); return FAILED; } return SUCCESS; } -void ModelProcess::Unload() -{ - if (!loadFlag_) { +void ModelProcess::Unload() { + if (!g_loadFlag_) { WARN_LOG("no model had been loaded, unload failed"); return; } - aclError ret = aclmdlUnload(modelId_); + aclError ret = aclmdlUnload(g_modelId_); if (ret != ACL_SUCCESS) { - ERROR_LOG("unload model failed, modelId is %u", modelId_); + ERROR_LOG("unload model failed, modelId is %u", g_modelId_); } - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; + if (g_modelDesc_ != nullptr) { + (void)aclmdlDestroyDesc(g_modelDesc_); + g_modelDesc_ = nullptr; } - if (modelMemPtr_ != nullptr) { - aclrtFree(modelMemPtr_); - modelMemPtr_ = nullptr; - modelMemSize_ = 0; + if (g_modelMemPtr_ != nullptr) { + aclrtFree(g_modelMemPtr_); + g_modelMemPtr_ = nullptr; + g_modelMemSize_ = 0; } - if (modelWeightPtr_ != nullptr) { - aclrtFree(modelWeightPtr_); - modelWeightPtr_ = nullptr; - modelWeightSize_ = 0; + if (g_modelWeightPtr_ != nullptr) { + aclrtFree(g_modelWeightPtr_); + g_modelWeightPtr_ = nullptr; + g_modelWeightSize_ = 0; } - loadFlag_ = false; - INFO_LOG("unload model success, modelId is %u", modelId_); + g_loadFlag_ = false; + INFO_LOG("unload model success, modelId is %u", g_modelId_); } -aclmdlDataset *ModelProcess::GetModelOutputData() -{ - return output_; +aclmdlDataset *ModelProcess::GetModelOutputData() { + return g_output_; } diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/object_detect.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/object_detect.cpp index 1e7936d02..28bee0718 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/object_detect.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/object_detect.cpp @@ -42,22 +42,22 @@ const static std::vector yolov3Label = { "person", "bicycle", "car" "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase","scissors", "teddy bear", "hair drier", "toothbrush" }; -//Inferential output dataset subscript 0 unit is detection box information data -const uint32_t kBBoxDataBufId = 0; -//The unit with subscript 1 is the number of boxes -const uint32_t kBoxNumDataBufId = 1; -//Each field subscript in the box message +// Inferential output dataset subscript 0 unit is detection box information data +const uint32_t g_BBoxDataBufId = 0; +// The unit with subscript 1 is the number of boxes +const uint32_t g_BoxNumDataBufId = 1; +// Each field subscript in the box message enum BBoxIndex { TOPLEFTX = 0, TOPLEFTY, BOTTOMRIGHTX, BOTTOMRIGHTY, SCORE, LABEL }; } ObjectDetect::ObjectDetect(const char* modelPath, uint32_t modelWidth, uint32_t modelHeight) -:deviceId_(0), imageDataBuf_(nullptr), imageInfoBuf_(nullptr), modelWidth_(modelWidth), -modelHeight_(modelHeight), isInited_(false){ - modelPath_ = modelPath; - imageDataSize_ = RGBU8_IMAGE_SIZE(modelWidth, modelHeight); - channel_ = nullptr; - PresenterErrorCode openChannelret = OpenChannelByConfig(channel_, "./param.conf"); +:g_deviceId_(0), g_imageDataBuf_(nullptr), g_imageInfoBuf_(nullptr), g_modelWidth_(modelWidth), +g_modelHeight_(modelHeight), g_isInited_(false) { + g_modelPath_ = modelPath; + g_imageDataSize_ = RGBU8_IMAGE_SIZE(modelWidth, modelHeight); + g_channel_ = nullptr; + PresenterErrorCode openChannelret = OpenChannelByConfig(g_channel_, "./param.conf"); if (openChannelret != PresenterErrorCode::kNone) { ERROR_LOG("Open channel failed, error %d\n", (int)openChannelret); } @@ -78,14 +78,14 @@ Result ObjectDetect::InitResource() { INFO_LOG("Acl init success"); // open device - ret = aclrtSetDevice(deviceId_); + ret = aclrtSetDevice(g_deviceId_); if (ret != ACL_SUCCESS) { - ERROR_LOG("Acl open device %d failed", deviceId_); + ERROR_LOG("Acl open device %d failed", g_deviceId_); return FAILED; } - INFO_LOG("Open device %d success", deviceId_); - //Gets whether the current application is running on host or Device - ret = aclrtGetRunMode(&runMode_); + INFO_LOG("Open device %d success", g_deviceId_); + // Gets whether the current application is running on host or Device + ret = aclrtGetRunMode(&g_runMode_); if (ret != ACL_SUCCESS) { ERROR_LOG("acl get run mode failed"); return FAILED; @@ -95,51 +95,49 @@ Result ObjectDetect::InitResource() { } Result ObjectDetect::InitModel(const char* omModelPath) { - Result ret = model_.LoadModelFromFileWithMem(omModelPath); + Result ret = g_model_.LoadModelFromFileWithMem(omModelPath); if (ret != SUCCESS) { ERROR_LOG("execute LoadModelFromFileWithMem failed"); return FAILED; } - ret = model_.CreateDesc(); + ret = g_model_.CreateDesc(); if (ret != SUCCESS) { ERROR_LOG("execute CreateDesc failed"); return FAILED; } - ret = model_.CreateOutput(); + ret = g_model_.CreateOutput(); if (ret != SUCCESS) { ERROR_LOG("execute CreateOutput failed"); return FAILED; } - return SUCCESS; } -Result ObjectDetect::CreateModelInputdDataset() -{ - //Request image data memory for input model - aclError aclRet = aclrtMalloc(&imageDataBuf_, imageDataSize_, ACL_MEM_MALLOC_HUGE_FIRST); +Result ObjectDetect::CreateModelInputdDataset() { + // Request image data memory for input model + aclError aclRet = aclrtMalloc(&g_imageDataBuf_, g_imageDataSize_, ACL_MEM_MALLOC_HUGE_FIRST); if (aclRet != ACL_SUCCESS) { ERROR_LOG("malloc device data buffer failed, aclRet is %d", aclRet); return FAILED; } - //The second input to Yolov3 is the input image width and height parameter - const float imageInfo[4] = {(float)modelWidth_, (float)modelHeight_, - (float)modelWidth_, (float)modelHeight_}; - imageInfoSize_ = sizeof(imageInfo); - if (runMode_ == ACL_HOST) - imageInfoBuf_ = Utils::CopyDataHostToDevice((void *)imageInfo, imageInfoSize_); + // The second input to Yolov3 is the input image width and height parameter + const float imageInfo[4] = {(float)g_modelWidth_, (float)g_modelHeight_, + (float)g_modelWidth_, (float)g_modelHeight_}; + g_imageInfoSize_ = sizeof(imageInfo); + if (g_runMode_ == ACL_HOST) + g_imageInfoBuf_ = Utils::CopyDataHostToDevice((void *)imageInfo, g_imageInfoSize_); else - imageInfoBuf_ = Utils::CopyDataDeviceToDevice((void *)imageInfo, imageInfoSize_); - if (imageInfoBuf_ == nullptr) { + g_imageInfoBuf_ = Utils::CopyDataDeviceToDevice((void *)imageInfo, g_imageInfoSize_); + if (g_imageInfoBuf_ == nullptr) { ERROR_LOG("Copy image info to device failed"); return FAILED; } - //Use the applied memory to create the model and input dataset. After creation, only update the memory data for each frame of inference, instead of creating the input dataset every time - Result ret = model_.CreateInput(imageDataBuf_, imageDataSize_, - imageInfoBuf_, imageInfoSize_); + // Use the applied memory to create the model and input dataset. After creation, only update the memory data for each frame of inference, instead of creating the input dataset every time + Result ret = g_model_.CreateInput(g_imageDataBuf_, g_imageDataSize_, + g_imageInfoBuf_, g_imageInfoSize_); if (ret != SUCCESS) { ERROR_LOG("Create mode input dataset failed"); return FAILED; @@ -149,19 +147,19 @@ Result ObjectDetect::CreateModelInputdDataset() } Result ObjectDetect::Init() { - //If it is already initialized, it is returned - if (isInited_) { + // If it is already initialized, it is returned + if (g_isInited_) { INFO_LOG("Classify instance is initied already!"); return SUCCESS; } - //Initializes the ACL resource + // Initializes the ACL resource Result ret = InitResource(); if (ret != SUCCESS) { ERROR_LOG("Init acl resource failed"); return FAILED; } - //Initializes the model management instance - ret = InitModel(modelPath_); + // Initializes the model management instance + ret = InitModel(g_modelPath_); if (ret != SUCCESS) { ERROR_LOG("Init model failed"); return FAILED; @@ -173,23 +171,23 @@ Result ObjectDetect::Init() { return FAILED; } - isInited_ = true; + g_isInited_ = true; return SUCCESS; } Result ObjectDetect::Preprocess(cv::Mat& frame) { - //Scale the frame image to the desired size of the model + // Scale the frame image to the desired size of the model cv::Mat reiszeMat; - cv::resize(frame, reiszeMat, cv::Size(modelWidth_, modelHeight_)); + cv::resize(frame, reiszeMat, cv::Size(g_modelWidth_, g_modelHeight_)); if (reiszeMat.empty()) { ERROR_LOG("Resize image failed"); return FAILED; } - //Copy the data into the cache of the input dataset - aclrtMemcpyKind policy = (runMode_ == ACL_HOST)? + // Copy the data into the cache of the input dataset + aclrtMemcpyKind policy = (g_runMode_ == ACL_HOST)? ACL_MEMCPY_HOST_TO_DEVICE:ACL_MEMCPY_DEVICE_TO_DEVICE; - aclError ret = aclrtMemcpy(imageDataBuf_, imageDataSize_, - reiszeMat.ptr(), imageDataSize_, policy); + aclError ret = aclrtMemcpy(g_imageDataBuf_, g_imageDataSize_, + reiszeMat.ptr(), g_imageDataSize_, policy); if (ret != ACL_SUCCESS) { ERROR_LOG("Copy resized image data to device failed."); return FAILED; @@ -199,61 +197,61 @@ Result ObjectDetect::Preprocess(cv::Mat& frame) { } Result ObjectDetect::Inference(aclmdlDataset*& inferenceOutput) { - //Perform reasoning - Result ret = model_.Execute(); + // Perform reasoning + Result ret = g_model_.Execute(); if (ret != SUCCESS) { ERROR_LOG("Execute model inference failed"); return FAILED; } - //Get inference output - inferenceOutput = model_.GetModelOutputData(); + // Get inference output + inferenceOutput = g_model_.GetModelOutputData(); return SUCCESS; } Result ObjectDetect::Postprocess(cv::Mat& frame, - aclmdlDataset* modelOutput){ - //Get box information data + aclmdlDataset* modelOutput) { + // Get box information data uint32_t dataSize = 0; float* detectData = (float*)GetInferenceOutputItem(dataSize, modelOutput, - kBBoxDataBufId); + g_BBoxDataBufId); if (detectData == nullptr) return FAILED; - //Gets the number of boxes + // Gets the number of boxes uint32_t* boxNum = (uint32_t*)GetInferenceOutputItem(dataSize, modelOutput, - kBoxNumDataBufId); + g_BoxNumDataBufId); if (boxNum == nullptr) return FAILED; - //Number of boxes The first data is valid + // Number of boxes The first data is valid uint32_t totalBox = boxNum[0]; - // - float widthScale = (float)(frame.cols) / modelWidth_; - float heightScale = (float)(frame.rows) / modelHeight_; + // + float widthScale = (float)(frame.cols) / g_modelWidth_; + float heightScale = (float)(frame.rows) / g_modelHeight_; vector detectResults; for (uint32_t i = 0; i < totalBox; i++) { DetectionResult oneResult; Point point_lt, point_rb; - //get the confidence of the detected object. Anything less than 0.8 is considered invalid + // get the confidence of the detected object. Anything less than 0.8 is considered invalid uint32_t score = uint32_t(detectData[totalBox * SCORE + i] * 100); if (score < 80) continue; - //get the frame coordinates and converts them to the coordinates on the original frame + // get the frame coordinates and converts them to the coordinates on the original frame oneResult.lt.x = detectData[totalBox * TOPLEFTX + i] * widthScale; oneResult.lt.y = detectData[totalBox * TOPLEFTY + i] * heightScale; oneResult.rb.x = detectData[totalBox * BOTTOMRIGHTX + i] * widthScale; oneResult.rb.y = detectData[totalBox * BOTTOMRIGHTY + i] * heightScale; - //Construct a string that marks the object: object name + confidence + // Construct a string that marks the object: object name + confidence uint32_t objIndex = (uint32_t)detectData[totalBox * LABEL + i]; oneResult.result_text = yolov3Label[objIndex] + std::to_string(score) + "\%"; detectResults.emplace_back(oneResult); } - //If it is the host side, the data is copied from the device and the memory used by the copy is freed - if (runMode_ == ACL_HOST) { + // If it is the host side, the data is copied from the device and the memory used by the copy is freed + if (g_runMode_ == ACL_HOST) { delete[]((uint8_t*)detectData); delete[]((uint8_t*)boxNum); } - //Sends inference results and images to presenter Server for display + // Sends inference results and images to presenter Server for display SendImage(detectResults, frame); return SUCCESS; @@ -285,7 +283,7 @@ void* ObjectDetect::GetInferenceOutputItem(uint32_t& itemDataSize, } void* data = nullptr; - if (runMode_ == ACL_HOST) { + if (g_runMode_ == ACL_HOST) { data = Utils::CopyDataDeviceToLocal(dataBufferDev, bufferSize); if (data == nullptr) { ERROR_LOG("Copy inference output to host failed"); @@ -304,8 +302,8 @@ void ObjectDetect::EncodeImage(vector& encodeImg, cv::Mat& origImg) { vector param = vector(2); param[0] = CV_IMWRITE_JPEG_QUALITY; - param[1] = 95;//default(95) 0-100 - //Jpeg images must serialize the Proto message before they can be sent + param[1] = 95;// default(95) 0-100 + // Jpeg images must serialize the Proto message before they can be sent cv::imencode(".jpg", origImg, encodeImg, param); } @@ -321,8 +319,8 @@ Result ObjectDetect::SendImage(vector& detectionResults, imageParam.size = encodeImg.size(); imageParam.data = reinterpret_cast(encodeImg.data()); imageParam.detection_results = detectionResults; - //Sends the detected object frame information and frame image to the Presenter Server for display - PresenterErrorCode errorCode = PresentImage(channel_, imageParam); + // Sends the detected object frame information and frame image to the Presenter Server for display + PresenterErrorCode errorCode = PresentImage(g_channel_, imageParam); if (errorCode != PresenterErrorCode::kNone) { ERROR_LOG("PresentImage failed %d", static_cast(errorCode)); return FAILED; @@ -331,22 +329,21 @@ Result ObjectDetect::SendImage(vector& detectionResults, return SUCCESS; } -void ObjectDetect::DestroyResource() -{ - aclrtFree(imageDataBuf_); - aclrtFree(imageInfoBuf_); +void ObjectDetect::DestroyResource() { + aclrtFree(g_imageDataBuf_); + aclrtFree(g_imageInfoBuf_); - delete channel_; + delete g_channel_; - //The ACL resource held by the model instance must be released before the ACL exits or ABORT will be torn down - model_.DestroyResource(); + // The ACL resource held by the model instance must be released before the ACL exits or ABORT will be torn down + g_model_.DestroyResource(); aclError ret; - ret = aclrtResetDevice(deviceId_); + ret = aclrtResetDevice(g_deviceId_); if (ret != ACL_SUCCESS) { ERROR_LOG("reset device failed"); } - INFO_LOG("end to reset device is %d", deviceId_); + INFO_LOG("end to reset device is %d", g_deviceId_); ret = aclFinalize(); if (ret != ACL_SUCCESS) { diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/utils.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/utils.cpp index fcbdb337c..62a58e4f3 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/utils.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/utils.cpp @@ -32,19 +32,19 @@ using namespace std; namespace { -const std::string kImagePathSeparator = ","; -const int kStatSuccess = 0; -const std::string kFileSperator = "/"; -const std::string kPathSeparator = "/"; +const std::string g_ImagePathSeparator = ","; +const int g_StatSuccess = 0; +const std::string g_FileSperator = "/"; +const std::string g_PathSeparator = "/"; // output image prefix -const std::string kOutputFilePrefix = "out_"; +const std::string g_OutputFilePrefix = "out_"; } bool Utils::IsDirectory(const string &path) { // get path stat struct stat buf; - if (stat(path.c_str(), &buf) != kStatSuccess) { + if (stat(path.c_str(), &buf) != g_StatSuccess) { return false; } @@ -66,7 +66,7 @@ bool Utils::IsPathExist(const string &path) { void Utils::SplitPath(const string &path, vector &path_vec) { char *char_path = const_cast(path.c_str()); - const char *char_split = kImagePathSeparator.c_str(); + const char *char_split = g_ImagePathSeparator.c_str(); char *tmp_path = strtok(char_path, char_split); while (tmp_path) { path_vec.emplace_back(tmp_path); @@ -103,7 +103,7 @@ void Utils::GetPathFiles(const string &path, vector &file_vec) { } // file path - string full_path = path + kPathSeparator + dirent_ptr->d_name; + string full_path = path + g_PathSeparator + dirent_ptr->d_name; // directory need recursion if (IsDirectory(full_path)) { GetPathFiles(full_path, file_vec); diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video_DVPP_with_AIPP/inc/sample_process.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video_DVPP_with_AIPP/inc/sample_process.h index 5b04b58c7..5d5ef81fd 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video_DVPP_with_AIPP/inc/sample_process.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video_DVPP_with_AIPP/inc/sample_process.h @@ -1,12 +1,22 @@ -/** -* @file sample_process.h +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. * -* Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +* 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. */ + +#ifndef YOLOV3_COCO_DETECTION_VIDEO_DVPP_WITH_AIPP_INC_SAMPLE_PROCESS_H +#define YOLOV3_COCO_DETECTION_VIDEO_DVPP_WITH_AIPP_INC_SAMPLE_PROCESS_H + #pragma once #include "acl/acl.h" #include "opencv2/opencv.hpp" @@ -32,14 +42,15 @@ public: private: void DestroyResource(); - std::string streamName_; - aclrtRunMode runMode_; - AclLiteResource aclDev_; - AclLiteImageProc dvpp_; - AclLiteModel model_; - uint32_t imageInfoSize_; - void* imageInfoBuf_; - AclLiteVideoProc* cap_; - cv::VideoWriter outputVideo_; + std::string g_streamName_; + aclrtRunMode g_runMode_; + AclLiteResource g_aclDev_; + AclLiteImageProc g_dvpp_; + AclLiteModel g_model_; + uint32_t g_imageInfoSize_; + void* g_imageInfoBuf_; + AclLiteVideoProc* g_cap_; + cv::VideoWriter g_outputVideo_; }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video_DVPP_with_AIPP/src/main.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video_DVPP_with_AIPP/src/main.cpp index 415739706..605c4e0bf 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video_DVPP_with_AIPP/src/main.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video_DVPP_with_AIPP/src/main.cpp @@ -1,20 +1,26 @@ -/** -* @file main.cpp +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. * -* Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at + +* http://www.apache.org/licenses/LICENSE-2.0 + +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. */ + #include #include "sample_process.h" using namespace std; -int main(int argc, char *argv[]) -{ - //Check the input when the application executes, which takes the path to the input video file - if((argc < 2) || (argv[1] == nullptr)){ +int main(int argc, char *argv[]) { + // Check the input when the application executes, which takes the path to the input video file + if ((argc < 2) || (argv[1] == nullptr)) { ACLLITE_LOG_ERROR("Please input: ./main "); return ACLLITE_ERROR; } diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video_DVPP_with_AIPP/src/sample_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video_DVPP_with_AIPP/src/sample_process.cpp index 96173041a..2f8cf64d8 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video_DVPP_with_AIPP/src/sample_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video_DVPP_with_AIPP/src/sample_process.cpp @@ -1,12 +1,19 @@ -/** -* @file sample_process.cpp +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. * -* Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at + +* http://www.apache.org/licenses/LICENSE-2.0 + +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. */ + #include #include "acl/acl.h" #include @@ -35,18 +42,18 @@ namespace { "refrigerator", "book", "clock", "vase","scissors", "teddy bear", "hair drier", "toothbrush" }; - const uint32_t kBBoxDataBufId = 0; - const uint32_t kBoxNumDataBufId = 1; + const uint32_t g_BBoxDataBufId = 0; + const uint32_t g_BoxNumDataBufId = 1; enum BBoxIndex { TOPLEFTX = 0, TOPLEFTY, BOTTOMRIGHTX, BOTTOMRIGHTY, SCORE, LABEL }; // bounding box line solid - const uint32_t kLineSolid = 2; + const uint32_t g_LineSolid = 2; // opencv draw label params. - const double kFountScale = 0.5; - const cv::Scalar kFontColor(0, 0, 255); - const uint32_t kLabelOffset = 11; + const double g_FountScale = 0.5; + const cv::Scalar g_FontColor(0, 0, 255); + const uint32_t g_LabelOffset = 11; // opencv color list for boundingbox - const vector kColors{ + const vector g_Colors { cv::Scalar(237, 149, 100), cv::Scalar(0, 215, 255), cv::Scalar(50, 205, 50), cv::Scalar(139, 85, 26) }; const char* omModelPath = "../model/yolov3.om"; @@ -55,11 +62,11 @@ namespace { } SampleProcess::SampleProcess(string streamName): -cap_(nullptr), -model_(omModelPath), -streamName_(streamName) { - imageInfoSize_ = 0; - imageInfoBuf_ = nullptr; +g_cap_(nullptr), +g_model_(omModelPath), +g_streamName_(streamName) { + g_imageInfoSize_ = 0; + g_imageInfoBuf_ = nullptr; } SampleProcess::~SampleProcess() { @@ -67,7 +74,7 @@ SampleProcess::~SampleProcess() { } AclLiteError SampleProcess::InitResource() { - AclLiteError ret = aclDev_.Init(); + AclLiteError ret = g_aclDev_.Init(); if (ret) { ACLLITE_LOG_ERROR("Init resource failed, error %d", ret); return ACLLITE_ERROR; @@ -77,25 +84,25 @@ AclLiteError SampleProcess::InitResource() { return ACLLITE_ERROR; } - ret = dvpp_.Init(); + ret = g_dvpp_.Init(); if (ret) { ACLLITE_LOG_ERROR("Dvpp init failed, error %d", ret); return ACLLITE_ERROR; } - ret = model_.Init(); + ret = g_model_.Init(); if (ret) { ACLLITE_LOG_ERROR("Model init failed, error %d", ret); return ACLLITE_ERROR; } - runMode_ = aclDev_.GetRunMode(); + g_runMode_ = g_aclDev_.GetRunMode(); const float imageInfo[4] = {(float)modelInputWidth, (float)modelInputHeight, (float)modelInputWidth, (float)modelInputHeight}; - imageInfoSize_ = sizeof(imageInfo); - imageInfoBuf_ = CopyDataToDevice((void *)imageInfo, imageInfoSize_, - runMode_, MEMORY_DEVICE); - if (imageInfoBuf_ == nullptr) { + g_imageInfoSize_ = sizeof(imageInfo); + g_imageInfoBuf_ = CopyDataToDevice((void *)imageInfo, g_imageInfoSize_, + g_runMode_, MEMORY_DEVICE); + if (g_imageInfoBuf_ == nullptr) { ACLLITE_LOG_ERROR("Copy image info to device failed"); return ACLLITE_ERROR; } @@ -104,22 +111,22 @@ AclLiteError SampleProcess::InitResource() { } AclLiteError SampleProcess::OpenVideoCapture() { - if (IsRtspAddr(streamName_)) { - cap_ = new AclLiteVideoProc(streamName_); - } else if (IsVideoFile(streamName_)) { - if (!IsPathExist(streamName_)) { - ACLLITE_LOG_ERROR("The %s is inaccessible", streamName_.c_str()); + if (IsRtspAddr(g_streamName_)) { + g_cap_ = new AclLiteVideoProc(g_streamName_); + } else if (IsVideoFile(g_streamName_)) { + if (!IsPathExist(g_streamName_)) { + ACLLITE_LOG_ERROR("The %s is inaccessible", g_streamName_.c_str()); return ACLLITE_ERROR; } - cap_ = new AclLiteVideoProc(streamName_); + g_cap_ = new AclLiteVideoProc(g_streamName_); } else { ACLLITE_LOG_ERROR("Invalid param. The arg should be accessible rtsp," " video file or camera id"); return ACLLITE_ERROR; } - if(!cap_->IsOpened()) { - delete cap_; + if (!g_cap_->IsOpened()) { + delete g_cap_; ACLLITE_LOG_ERROR("Failed to open video"); return ACLLITE_ERROR; } @@ -129,8 +136,8 @@ AclLiteError SampleProcess::OpenVideoCapture() { AclLiteError SampleProcess::Postprocess(const vector& modelOutput, cv::Mat& srcImg, int modelWidth, int modelHeight) { uint32_t dataSize = 0; - float* detectData = (float *)modelOutput[kBBoxDataBufId].data.get(); - uint32_t* boxNum = (uint32_t *)modelOutput[kBoxNumDataBufId].data.get(); + float* detectData = (float *)modelOutput[g_BBoxDataBufId].data.get(); + uint32_t* boxNum = (uint32_t *)modelOutput[g_BoxNumDataBufId].data.get(); uint32_t totalBox = boxNum[0]; float widthScale = (float)(srcImg.cols) / modelInputWidth; @@ -163,52 +170,52 @@ void SampleProcess::DrawBoundBoxToImage(vector& detectionResults, cv::Mat& p1.y = detectionResults[i].rect.ltY; p2.x = detectionResults[i].rect.rbX; p2.y = detectionResults[i].rect.rbY; - cv::rectangle(origImage, p1, p2, kColors[i % kColors.size()], kLineSolid); - cv::putText(origImage, detectionResults[i].text, cv::Point(p1.x, p1.y + kLabelOffset), - cv::FONT_HERSHEY_COMPLEX, kFountScale, kFontColor); + cv::rectangle(origImage, p1, p2, g_Colors[i % g_Colors.size()], g_LineSolid); + cv::putText(origImage, detectionResults[i].text, cv::Point(p1.x, p1.y + g_LabelOffset), + cv::FONT_HERSHEY_COMPLEX, g_FountScale, g_FontColor); } - outputVideo_ << origImage; + g_outputVideo_ << origImage; } AclLiteError SampleProcess::Process() { string outputVideoPath_ = "./test1.mp4"; - uint32_t videoWidth_ = cap_->Get(FRAME_WIDTH); - uint32_t videoHeight_ = cap_->Get(FRAME_HEIGHT); + uint32_t videoWidth_ = g_cap_->Get(FRAME_WIDTH); + uint32_t videoHeight_ = g_cap_->Get(FRAME_HEIGHT); cout << "videoWidth_ and videoHeight_ is" << " " << videoWidth_ << " " << videoHeight_ << endl; - outputVideo_.open(outputVideoPath_, cv::VideoWriter::fourcc('m', 'p', '4', 'v'), 25.0, cv::Size(videoWidth_,videoHeight_)); + g_outputVideo_.open(outputVideoPath_, cv::VideoWriter::fourcc('m', 'p', '4', 'v'), 25.0, cv::Size(videoWidth_,videoHeight_)); ImageData testPic; bool readflag = true; - while(readflag){ - AclLiteError ret = cap_->Read(testPic); + while (readflag) { + AclLiteError ret = g_cap_->Read(testPic); if (ret != ACLLITE_OK) { break; } ImageData resizedImage; - ret = dvpp_.Resize(resizedImage, testPic, modelInputWidth, modelInputHeight); + ret = g_dvpp_.Resize(resizedImage, testPic, modelInputWidth, modelInputHeight); if (ret == ACLLITE_ERROR) { ACLLITE_LOG_ERROR("Resize image failed"); return ACLLITE_ERROR; } // 2.model process - ret = model_.CreateInput(resizedImage.data.get(), resizedImage.size, - imageInfoBuf_, imageInfoSize_); + ret = g_model_.CreateInput(resizedImage.data.get(), resizedImage.size, + g_imageInfoBuf_, g_imageInfoSize_); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Create mode input dataset failed, error:%d", ret); return ACLLITE_ERROR; } std::vector inferenceOutput; - ret = model_.Execute(inferenceOutput); + ret = g_model_.Execute(inferenceOutput); if (ret != ACLLITE_OK) { - model_.DestroyInput(); + g_model_.DestroyInput(); ACLLITE_LOG_ERROR("Execute model inference failed, error: %d", ret); return ACLLITE_ERROR; } - model_.DestroyInput(); + g_model_.DestroyInput(); ImageData yuvImage; - ret = CopyImageToLocal(yuvImage, testPic, runMode_); + ret = CopyImageToLocal(yuvImage, testPic, g_runMode_); if (ret == ACLLITE_ERROR) { ACLLITE_LOG_ERROR("Copy image to host failed"); return ACLLITE_ERROR; @@ -222,16 +229,15 @@ AclLiteError SampleProcess::Process() { return ACLLITE_ERROR; } } - outputVideo_.release(); + g_outputVideo_.release(); return ACLLITE_OK; } -void SampleProcess::DestroyResource() -{ - if (cap_ != nullptr) { - cap_->Close(); - delete cap_; +void SampleProcess::DestroyResource() { + if (g_cap_ != nullptr) { + g_cap_->Close(); + delete g_cap_; } - dvpp_.DestroyResource(); - model_.DestroyResource(); + g_dvpp_.DestroyResource(); + g_model_.DestroyResource(); } diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/inc/dvpp_process.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/inc/dvpp_process.h index fcc9b5cb4..bc0722648 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/inc/dvpp_process.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/inc/dvpp_process.h @@ -1,12 +1,22 @@ -/** -* @file dvpp_process.h +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. * -* Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +* 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. */ + +#ifndef YOLOV4_COCO_DETECTION_PICTURE_INC_DVPP_PROCESS_H +#define YOLOV4_COCO_DETECTION_PICTURE_INC_DVPP_PROCESS_H + #pragma once #include @@ -81,23 +91,25 @@ private: void DestroyOutputPara(); - acldvppChannelDesc *dvppChannelDesc_; - aclrtStream stream_; - acldvppResizeConfig *resizeConfig_; - void *decodeOutDevBuffer_; // decode output buffer - acldvppPicDesc *decodeOutputDesc_; //decode output desc - acldvppPicDesc *resizeInputDesc_; // resize input desc - acldvppPicDesc *resizeOutputDesc_; // resize output desc - void *inDevBuffer_; // decode input buffer - uint32_t inDevBufferSize_; // dvpp input buffer size - uint32_t jpegDecodeOutputSize_; // jpeg decode output size - uint32_t decodeOutputWidth_; // decode output width - uint32_t decodeOutputWidthStride_; // decode output width aligned - uint32_t decodeOutputHeight_; // decode output height - void *resizeOutBufferDev_; // resize output buffer - uint32_t resizeOutBufferSize_; // resize output size - uint32_t modelInputWidth_; // model input width - uint32_t modelInputHeight_; // model input height - uint32_t resizeOutWidthStride_; // resize output width aligned - uint32_t resizeOutHeightStride_; // resize output height aligned + acldvppChannelDesc *g_dvppChannelDesc_; + aclrtStream g_stream_; + acldvppResizeConfig *g_resizeConfig_; + void *g_decodeOutDevBuffer_; // decode output buffer + acldvppPicDesc *g_decodeOutputDesc_; // decode output desc + acldvppPicDesc *g_resizeInputDesc_; // resize input desc + acldvppPicDesc *g_resizeOutputDesc_; // resize output desc + void *g_inDevBuffer_; // decode input buffer + uint32_t g_inDevBufferSize_; // dvpp input buffer size + uint32_t g_jpegDecodeOutputSize_; // jpeg decode output size + uint32_t g_decodeOutputWidth_; // decode output width + uint32_t g_decodeOutputWidthStride_; // decode output width aligned + uint32_t g_decodeOutputHeight_; // decode output height + void *g_resizeOutBufferDev_; // resize output buffer + uint32_t g_resizeOutBufferSize_; // resize output size + uint32_t g_modelInputWidth_; // model input width + uint32_t g_modelInputHeight_; // model input height + uint32_t g_resizeOutWidthStride_; // resize output width aligned + uint32_t g_resizeOutHeightStride_; // resize output height aligned }; + +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/inc/model_process.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/inc/model_process.h index 1367e5033..5936a5f56 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/inc/model_process.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/inc/model_process.h @@ -1,12 +1,22 @@ -/** -* @file model_process.h +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. * -* Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +* 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. */ + +#ifndef YOLOV4_COCO_DETECTION_PICTURE_INC_MODEL_PROCESS_H +#define YOLOV4_COCO_DETECTION_PICTURE_INC_MODEL_PROCESS_H + #pragma once #include @@ -92,12 +102,14 @@ public: Result GetModelInputWH(int &width, int &height); private: - uint32_t modelId_; - bool loadFlag_; // model load flag - aclmdlDesc *modelDesc_; - aclmdlDataset *input_; - aclmdlDataset *output_; - int deviceId_; - aclrtContext context_; - bool initFlag_; + uint32_t g_modelId_; + bool g_loadFlag_; // model load flag + aclmdlDesc *g_modelDesc_; + aclmdlDataset *g_input_; + aclmdlDataset *g_output_; + int g_deviceId_; + aclrtContext g_context_; + bool g_initFlag_; }; + +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/inc/post_process.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/inc/post_process.h index a8b03e991..c32584994 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/inc/post_process.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/inc/post_process.h @@ -1,12 +1,22 @@ -/** -* @file post_process.h +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. * -* Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +* 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. */ + +#ifndef YOLOV4_COCO_DETECTION_PICTURE_INC_POST_PROCESS_H +#define YOLOV4_COCO_DETECTION_PICTURE_INC_POST_PROCESS_H + #pragma once #include @@ -34,13 +44,13 @@ public: * @param [in] originImage: origin image path */ PostProcess(void *outputClass, void *outputBox, float xScale, float yScale, std::string &originImage) : - outputClass_(outputClass), outputBox_(outputBox), xScale_(xScale), yScale_(yScale), - originImage_(originImage) {} + g_outputClass_(outputClass), g_outputBox_(outputBox), g_xScale_(xScale), g_yScale_(yScale), + g_originImage_(originImage) {} /** * @brief Constructor */ - PostProcess() : outputClass_(nullptr), outputBox_(nullptr), xScale_(0), yScale_(0), originImage_("") {} + PostProcess() : g_outputClass_(nullptr), g_outputBox_(nullptr), g_xScale_(0), g_yScale_(0), g_originImage_("") {} /** * @brief Destructor @@ -76,9 +86,11 @@ private: void DrawBoundBoxToImage(const std::vector &result); - void *outputClass_; - void *outputBox_; - float xScale_; - float yScale_; - std::string originImage_; + void *g_outputClass_; + void *g_outputBox_; + float g_xScale_; + float g_yScale_; + std::string g_originImage_; }; + +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/inc/utils.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/inc/utils.h index 5ccde10ed..ab0d58aa0 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/inc/utils.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/inc/utils.h @@ -1,12 +1,22 @@ -/** -* @file utils.h +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. * -* Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +* 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. */ + +#ifndef YOLOV4_COCO_DETECTION_PICTURE_INC_UTILS_PROCESS_H +#define YOLOV4_COCO_DETECTION_PICTURE_INC_UTILS_PROCESS_H + #pragma once #include @@ -73,3 +83,5 @@ public: */ static Result CheckPathIsFile(const std::string &fileName); }; + +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/dvpp_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/dvpp_process.cpp index c897fbad1..4795bcdb4 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/dvpp_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/dvpp_process.cpp @@ -1,12 +1,19 @@ -/** -* @file dvpp_process.cpp +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. * -* Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at + +* http://www.apache.org/licenses/LICENSE-2.0 + +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. */ + #include "dvpp_process.h" #include "acl/acl.h" #include "utils.h" @@ -23,16 +30,16 @@ namespace { } } -DvppProcess::DvppProcess(aclrtStream &stream) : stream_(stream), dvppChannelDesc_(nullptr), - resizeConfig_(nullptr), decodeOutDevBuffer_(nullptr), - decodeOutputDesc_(nullptr), resizeInputDesc_(nullptr), - resizeOutputDesc_(nullptr), inDevBuffer_(nullptr), inDevBufferSize_(0), - jpegDecodeOutputSize_(0), - decodeOutputWidth_(0), decodeOutputWidthStride_(0), - decodeOutputHeight_(0), resizeOutBufferDev_(nullptr), - resizeOutBufferSize_(0), modelInputWidth_(0), modelInputHeight_(0), - resizeOutWidthStride_(0), - resizeOutHeightStride_(0) { +DvppProcess::DvppProcess(aclrtStream &stream) : g_stream_(stream), g_dvppChannelDesc_(nullptr), + g_resizeConfig_(nullptr), g_decodeOutDevBuffer_(nullptr), + g_decodeOutputDesc_(nullptr), g_resizeInputDesc_(nullptr), + g_resizeOutputDesc_(nullptr), g_inDevBuffer_(nullptr), g_inDevBufferSize_(0), + g_jpegDecodeOutputSize_(0), + g_decodeOutputWidth_(0), g_decodeOutputWidthStride_(0), + g_decodeOutputHeight_(0), g_resizeOutBufferDev_(nullptr), + g_resizeOutBufferSize_(0), g_modelInputWidth_(0), g_modelInputHeight_(0), + g_resizeOutWidthStride_(0), + g_resizeOutHeightStride_(0) { } DvppProcess::~DvppProcess() { @@ -41,20 +48,20 @@ DvppProcess::~DvppProcess() { } Result DvppProcess::InitResource() { - dvppChannelDesc_ = acldvppCreateChannelDesc(); - if (dvppChannelDesc_ == nullptr) { + g_dvppChannelDesc_ = acldvppCreateChannelDesc(); + if (g_dvppChannelDesc_ == nullptr) { ERROR_LOG("acldvppCreateChannelDesc failed"); return FAILED; } - aclError ret = acldvppCreateChannel(dvppChannelDesc_); + aclError ret = acldvppCreateChannel(g_dvppChannelDesc_); if (ret != ACL_SUCCESS) { ERROR_LOG("acldvppCreateChannelAsync failed, errorCode = %d", static_cast(ret)); return FAILED; } - resizeConfig_ = acldvppCreateResizeConfig(); - if (resizeConfig_ == nullptr) { + g_resizeConfig_ = acldvppCreateResizeConfig(); + if (g_resizeConfig_ == nullptr) { ERROR_LOG("acldvppCreateResizeConfig failed"); return FAILED; } @@ -64,9 +71,9 @@ Result DvppProcess::InitResource() { } void DvppProcess::SetInput(void *inDevBuffer, uint32_t inDevBufferSize, const PicDesc &picDesc) { - inDevBuffer_ = inDevBuffer; - inDevBufferSize_ = inDevBufferSize; - jpegDecodeOutputSize_ = picDesc.jpegDecodeSize; + g_inDevBuffer_ = inDevBuffer; + g_inDevBufferSize_ = inDevBufferSize; + g_jpegDecodeOutputSize_ = picDesc.jpegDecodeSize; } void DvppProcess::GetDvppOutput(void **outputBuffer, int &outputSize) { @@ -74,10 +81,10 @@ void DvppProcess::GetDvppOutput(void **outputBuffer, int &outputSize) { ERROR_LOG("outputBuffer is nullptr"); return; } - *outputBuffer = resizeOutBufferDev_; - outputSize = resizeOutBufferSize_; - resizeOutBufferDev_ = nullptr; - resizeOutBufferSize_ = 0; + *outputBuffer = g_resizeOutBufferDev_; + outputSize = g_resizeOutBufferSize_; + g_resizeOutBufferDev_ = nullptr; + g_resizeOutBufferSize_ = 0; } Result DvppProcess::InitDvppOutputPara(int modelInputWidth, int modelInputHeight) { @@ -87,14 +94,14 @@ Result DvppProcess::InitDvppOutputPara(int modelInputWidth, int modelInputHeight return FAILED; } - modelInputWidth_ = modelInputWidth; - modelInputHeight_ = modelInputHeight; - resizeOutWidthStride_ = AlignSize(modelInputWidth, 16); // 16-byte alignment - resizeOutHeightStride_ = AlignSize(modelInputHeight, 2); // 2-byte alignment + g_modelInputWidth_ = modelInputWidth; + g_modelInputHeight_ = modelInputHeight; + g_resizeOutWidthStride_ = AlignSize(modelInputWidth, 16); // 16-byte alignment + g_resizeOutHeightStride_ = AlignSize(modelInputHeight, 2); // 2-byte alignment // output buffer, adjust the value based on the actual model - resizeOutBufferSize_ = resizeOutWidthStride_ * resizeOutHeightStride_ * 3 / 2; // yuv format size - aclError ret = acldvppMalloc(&resizeOutBufferDev_, resizeOutBufferSize_); + g_resizeOutBufferSize_ = g_resizeOutWidthStride_ * g_resizeOutHeightStride_ * 3 / 2; // yuv format size + aclError ret = acldvppMalloc(&g_resizeOutBufferDev_, g_resizeOutBufferSize_); if (ret != ACL_SUCCESS) { ERROR_LOG("acldvppMalloc resizeOutBuffer failed, errorCode = %d", static_cast(ret)); return FAILED; @@ -104,106 +111,106 @@ Result DvppProcess::InitDvppOutputPara(int modelInputWidth, int modelInputHeight } void DvppProcess::DestroyOutputPara() { - if (resizeOutBufferDev_ != nullptr) { - (void) acldvppFree(resizeOutBufferDev_); - resizeOutBufferDev_ = nullptr; + if (g_resizeOutBufferDev_ != nullptr) { + (void) acldvppFree(g_resizeOutBufferDev_); + g_resizeOutBufferDev_ = nullptr; } } Result DvppProcess::InitDecodeOutputDesc() { - aclError ret = acldvppMalloc(&decodeOutDevBuffer_, jpegDecodeOutputSize_); + aclError ret = acldvppMalloc(&g_decodeOutDevBuffer_, g_jpegDecodeOutputSize_); if (ret != ACL_SUCCESS) { ERROR_LOG("acldvppMalloc jpegOutBufferDev failed, errorCode = %d", static_cast(ret)); return FAILED; } - decodeOutputDesc_ = acldvppCreatePicDesc(); - if (decodeOutputDesc_ == nullptr) { + g_decodeOutputDesc_ = acldvppCreatePicDesc(); + if (g_decodeOutputDesc_ == nullptr) { ERROR_LOG("acldvppCreatePicDesc decodeOutputDesc failed"); return FAILED; } - (void) acldvppSetPicDescData(decodeOutputDesc_, decodeOutDevBuffer_); - (void) acldvppSetPicDescFormat(decodeOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); - (void) acldvppSetPicDescSize(decodeOutputDesc_, jpegDecodeOutputSize_); + (void) acldvppSetPicDescData(g_decodeOutputDesc_, g_decodeOutDevBuffer_); + (void) acldvppSetPicDescFormat(g_decodeOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); + (void) acldvppSetPicDescSize(g_decodeOutputDesc_, g_jpegDecodeOutputSize_); return SUCCESS; } Result DvppProcess::ProcessDecode() { // decode to yuv format - aclError ret = acldvppJpegDecodeAsync(dvppChannelDesc_, inDevBuffer_, inDevBufferSize_, - decodeOutputDesc_, stream_); + aclError ret = acldvppJpegDecodeAsync(g_dvppChannelDesc_, g_inDevBuffer_, g_inDevBufferSize_, + g_decodeOutputDesc_, g_stream_); if (ret != ACL_SUCCESS) { ERROR_LOG("acldvppJpegDecodeAsync failed, errorCode = %d", static_cast(ret)); return FAILED; } - ret = aclrtSynchronizeStream(stream_); + ret = aclrtSynchronizeStream(g_stream_); if (ret != ACL_SUCCESS) { ERROR_LOG("aclrtSynchronizeStream failed, errorCode = %d", static_cast(ret)); return FAILED; } // get yuv image width and height - decodeOutputWidth_ = acldvppGetPicDescWidth(decodeOutputDesc_); - decodeOutputHeight_ = acldvppGetPicDescHeight(decodeOutputDesc_); - decodeOutputWidthStride_ = acldvppGetPicDescWidthStride(decodeOutputDesc_); + g_decodeOutputWidth_ = acldvppGetPicDescWidth(g_decodeOutputDesc_); + g_decodeOutputHeight_ = acldvppGetPicDescHeight(g_decodeOutputDesc_); + g_decodeOutputWidthStride_ = acldvppGetPicDescWidthStride(g_decodeOutputDesc_); return SUCCESS; } void DvppProcess::DestroyDecodeResource() { - if (decodeOutputDesc_ != nullptr) { - (void) acldvppDestroyPicDesc(decodeOutputDesc_); - decodeOutputDesc_ = nullptr; + if (g_decodeOutputDesc_ != nullptr) { + (void) acldvppDestroyPicDesc(g_decodeOutputDesc_); + g_decodeOutputDesc_ = nullptr; } } Result DvppProcess::InitResizeInputDesc() { - uint32_t jpegOutWidthStride = decodeOutputWidthStride_; // 128-byte alignment on 310, 64-byte alignment on 310P - uint32_t jpegOutHeightStride = AlignSize(decodeOutputHeight_, 16); // 16-byte alignment + uint32_t jpegOutWidthStride = g_decodeOutputWidthStride_; // 128-byte alignment on 310, 64-byte alignment on 310P + uint32_t jpegOutHeightStride = AlignSize(g_decodeOutputHeight_, 16); // 16-byte alignment uint32_t jpegOutBufferSize = jpegOutWidthStride * jpegOutHeightStride * 3 / 2; // yuv format size - resizeInputDesc_ = acldvppCreatePicDesc(); - if (resizeInputDesc_ == nullptr) { + g_resizeInputDesc_ = acldvppCreatePicDesc(); + if (g_resizeInputDesc_ == nullptr) { ERROR_LOG("InitResizeInputDesc failed"); return FAILED; } - (void) acldvppSetPicDescData(resizeInputDesc_, decodeOutDevBuffer_); - (void) acldvppSetPicDescFormat(resizeInputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); - (void) acldvppSetPicDescWidth(resizeInputDesc_, decodeOutputWidth_); - (void) acldvppSetPicDescHeight(resizeInputDesc_, decodeOutputHeight_); - (void) acldvppSetPicDescWidthStride(resizeInputDesc_, jpegOutWidthStride); - (void) acldvppSetPicDescHeightStride(resizeInputDesc_, jpegOutHeightStride); - (void) acldvppSetPicDescSize(resizeInputDesc_, jpegOutBufferSize); + (void) acldvppSetPicDescData(g_resizeInputDesc_, g_decodeOutDevBuffer_); + (void) acldvppSetPicDescFormat(g_resizeInputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); + (void) acldvppSetPicDescWidth(g_resizeInputDesc_, g_decodeOutputWidth_); + (void) acldvppSetPicDescHeight(g_resizeInputDesc_, g_decodeOutputHeight_); + (void) acldvppSetPicDescWidthStride(g_resizeInputDesc_, jpegOutWidthStride); + (void) acldvppSetPicDescHeightStride(g_resizeInputDesc_, jpegOutHeightStride); + (void) acldvppSetPicDescSize(g_resizeInputDesc_, jpegOutBufferSize); return SUCCESS; } Result DvppProcess::InitResizeOutputDesc() { - resizeOutputDesc_ = acldvppCreatePicDesc(); - if (resizeOutputDesc_ == nullptr) { + g_resizeOutputDesc_ = acldvppCreatePicDesc(); + if (g_resizeOutputDesc_ == nullptr) { ERROR_LOG("acldvppCreatePicDesc failed"); return FAILED; } - (void) acldvppSetPicDescData(resizeOutputDesc_, resizeOutBufferDev_); - (void) acldvppSetPicDescFormat(resizeOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); - (void) acldvppSetPicDescWidth(resizeOutputDesc_, modelInputWidth_); - (void) acldvppSetPicDescHeight(resizeOutputDesc_, modelInputHeight_); - (void) acldvppSetPicDescWidthStride(resizeOutputDesc_, resizeOutWidthStride_); - (void) acldvppSetPicDescHeightStride(resizeOutputDesc_, resizeOutHeightStride_); - (void) acldvppSetPicDescSize(resizeOutputDesc_, resizeOutBufferSize_); + (void) acldvppSetPicDescData(g_resizeOutputDesc_, g_resizeOutBufferDev_); + (void) acldvppSetPicDescFormat(g_resizeOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); + (void) acldvppSetPicDescWidth(g_resizeOutputDesc_, g_modelInputWidth_); + (void) acldvppSetPicDescHeight(g_resizeOutputDesc_, g_modelInputHeight_); + (void) acldvppSetPicDescWidthStride(g_resizeOutputDesc_, g_resizeOutWidthStride_); + (void) acldvppSetPicDescHeightStride(g_resizeOutputDesc_, g_resizeOutHeightStride_); + (void) acldvppSetPicDescSize(g_resizeOutputDesc_, g_resizeOutBufferSize_); return SUCCESS; } Result DvppProcess::ProcessResize() { // resize pic size - aclError ret = acldvppVpcResizeAsync(dvppChannelDesc_, resizeInputDesc_, - resizeOutputDesc_, resizeConfig_, stream_); + aclError ret = acldvppVpcResizeAsync(g_dvppChannelDesc_, g_resizeInputDesc_, + g_resizeOutputDesc_, g_resizeConfig_, g_stream_); if (ret != ACL_SUCCESS) { ERROR_LOG("acldvppVpcResizeAsync failed, errorCode = %d", static_cast(ret)); return FAILED; } - ret = aclrtSynchronizeStream(stream_); + ret = aclrtSynchronizeStream(g_stream_); if (ret != ACL_SUCCESS) { ERROR_LOG("aclrtSynchronizeStream failed, errorCode = %d", static_cast(ret)); return FAILED; @@ -213,37 +220,37 @@ Result DvppProcess::ProcessResize() { } void DvppProcess::DestroyResizeResource() { - if (decodeOutDevBuffer_ != nullptr) { - (void) acldvppFree(decodeOutDevBuffer_); - decodeOutDevBuffer_ = nullptr; + if (g_decodeOutDevBuffer_ != nullptr) { + (void) acldvppFree(g_decodeOutDevBuffer_); + g_decodeOutDevBuffer_ = nullptr; } - if (resizeInputDesc_ != nullptr) { - acldvppDestroyPicDesc(resizeInputDesc_); - resizeInputDesc_ = nullptr; + if (g_resizeInputDesc_ != nullptr) { + acldvppDestroyPicDesc(g_resizeInputDesc_); + g_resizeInputDesc_ = nullptr; } - if (resizeOutputDesc_ != nullptr) { - acldvppDestroyPicDesc(resizeOutputDesc_); - resizeOutputDesc_ = nullptr; + if (g_resizeOutputDesc_ != nullptr) { + acldvppDestroyPicDesc(g_resizeOutputDesc_); + g_resizeOutputDesc_ = nullptr; } } void DvppProcess::DestroyResource() { - // resizeConfig_ is created in initResource - if (resizeConfig_ != nullptr) { - acldvppDestroyResizeConfig(resizeConfig_); - resizeConfig_ = nullptr; + // g_resizeConfig_ is created in initResource + if (g_resizeConfig_ != nullptr) { + acldvppDestroyResizeConfig(g_resizeConfig_); + g_resizeConfig_ = nullptr; } - if (dvppChannelDesc_ != nullptr) { - aclError ret = acldvppDestroyChannel(dvppChannelDesc_); + if (g_dvppChannelDesc_ != nullptr) { + aclError ret = acldvppDestroyChannel(g_dvppChannelDesc_); if (ret != ACL_SUCCESS) { ERROR_LOG("acldvppDestroyChannel failed, errorCode = %d", static_cast(ret)); } - (void) acldvppDestroyChannelDesc(dvppChannelDesc_); - dvppChannelDesc_ = nullptr; + (void) acldvppDestroyChannelDesc(g_dvppChannelDesc_); + g_dvppChannelDesc_ = nullptr; } } diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/main.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/main.cpp index d1654ad73..1bbb9a13a 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/main.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/main.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,8 @@ * 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 main.cpp -* Description: tensorflow yolov4 demo */ + #include #include #include @@ -31,7 +29,7 @@ using namespace std; #define CHECK_FUNCTION_RESULT(name, result) \ { \ - if(result != SUCCESS) \ + if (result != SUCCESS) \ { \ ERROR_LOG(#name" failed"); \ return -1; \ @@ -64,7 +62,7 @@ void ProcessOutput(const aclmdlDataset *output, const char *originImage, float x fclose(outputFile); } outputFile = fopen(name.c_str(), "wb"); - //get model output data + // get model output data aclDataBuffer *dataBuffer = aclmdlGetDatasetBuffer(output, i); void *data = aclGetDataBufferAddr(dataBuffer); size_t len = aclGetDataBufferSize(dataBuffer); diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/model_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/model_process.cpp index 40978ec2f..ab2eaea13 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/model_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/model_process.cpp @@ -1,12 +1,19 @@ -/** -* @file model_process.cpp +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. * -* Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at + +* http://www.apache.org/licenses/LICENSE-2.0 + +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. */ + #include "model_process.h" #include #include "utils.h" @@ -14,8 +21,8 @@ using namespace std; -ModelProcess::ModelProcess() : loadFlag_(false), modelId_(0), modelDesc_(nullptr), input_(nullptr), output_(nullptr), - deviceId_(-1), initFlag_(false) { +ModelProcess::ModelProcess() : g_loadFlag_(false), g_modelId_(0), g_modelDesc_(nullptr), g_input_(nullptr), g_output_(nullptr), + g_deviceId_(-1), g_initFlag_(false) { } ModelProcess::~ModelProcess() { @@ -29,7 +36,7 @@ Result ModelProcess::Init(int deviceId) { ERROR_LOG("aclInit failed, errorCode is %d", static_cast(ret)); return FAILED; } - initFlag_ = true; + g_initFlag_ = true; // open device ret = aclrtSetDevice(deviceId); @@ -38,10 +45,10 @@ Result ModelProcess::Init(int deviceId) { return FAILED; } - deviceId_ = deviceId; + g_deviceId_ = deviceId; // create context - ret = aclrtCreateContext(&context_, deviceId_); + ret = aclrtCreateContext(&g_context_, g_deviceId_); if (ret != ACL_SUCCESS) { ERROR_LOG("aclrtCreateContext failed, errorCode is %d", static_cast(ret)); return FAILED; @@ -66,92 +73,92 @@ void ModelProcess::Finalize() { DestroyInput(); DestroyOutput(); - if (context_ != nullptr) { - aclrtDestroyContext(context_); - context_ = nullptr; + if (g_context_ != nullptr) { + aclrtDestroyContext(g_context_); + g_context_ = nullptr; } - if (deviceId_ >= 0) { - (void) aclrtResetDevice(deviceId_); - deviceId_ = -1; + if (g_deviceId_ >= 0) { + (void) aclrtResetDevice(g_deviceId_); + g_deviceId_ = -1; } - if (initFlag_) { + if (g_initFlag_) { (void) aclFinalize(); - initFlag_ = false; + g_initFlag_ = false; } INFO_LOG("finalize model process success."); } Result ModelProcess::LoadModel(const char *modelPath) { - if (loadFlag_) { + if (g_loadFlag_) { ERROR_LOG("model has already been loaded"); return FAILED; } - aclError ret = aclmdlLoadFromFile(modelPath, &modelId_); + aclError ret = aclmdlLoadFromFile(modelPath, &g_modelId_); if (ret != ACL_SUCCESS) { ERROR_LOG("load model from file failed, model file is %s, errorCode is %d", modelPath, static_cast(ret)); return FAILED; } - modelDesc_ = aclmdlCreateDesc(); - if (modelDesc_ == nullptr) { + g_modelDesc_ = aclmdlCreateDesc(); + if (g_modelDesc_ == nullptr) { ERROR_LOG("create model description failed"); - (void) aclmdlUnload(modelId_); + (void) aclmdlUnload(g_modelId_); return FAILED; } - ret = aclmdlGetDesc(modelDesc_, modelId_); + ret = aclmdlGetDesc(g_modelDesc_, g_modelId_); if (ret != ACL_SUCCESS) { ERROR_LOG("get model description failed"); - (void) aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; - (void) aclmdlUnload(modelId_); + (void) aclmdlDestroyDesc(g_modelDesc_); + g_modelDesc_ = nullptr; + (void) aclmdlUnload(g_modelId_); return FAILED; } - loadFlag_ = true; + g_loadFlag_ = true; INFO_LOG("load model %s success", modelPath); return SUCCESS; } void ModelProcess::UnloadModel() { - if (!loadFlag_) { + if (!g_loadFlag_) { return; } - aclError ret = aclmdlUnload(modelId_); + aclError ret = aclmdlUnload(g_modelId_); if (ret != ACL_SUCCESS) { - ERROR_LOG("unload model failed, modelId is %u, errorCode is %d", modelId_, static_cast(ret)); + ERROR_LOG("unload model failed, modelId is %u, errorCode is %d", g_modelId_, static_cast(ret)); } - if (modelDesc_ != nullptr) { - (void) aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; + if (g_modelDesc_ != nullptr) { + (void) aclmdlDestroyDesc(g_modelDesc_); + g_modelDesc_ = nullptr; } - loadFlag_ = false; - INFO_LOG("unload model success, modelId is %u", modelId_); + g_loadFlag_ = false; + INFO_LOG("unload model success, modelId is %u", g_modelId_); } Result ModelProcess::CreateInput(void *inputDataBuffer, size_t bufferSize) { // om used in this sample has only one input - if (modelDesc_ == nullptr) { + if (g_modelDesc_ == nullptr) { ERROR_LOG("no model description, create input failed"); (void) acldvppFree(inputDataBuffer); return FAILED; } - size_t modelInputSize = aclmdlGetInputSizeByIndex(modelDesc_, 0); + size_t modelInputSize = aclmdlGetInputSizeByIndex(g_modelDesc_, 0); if (bufferSize != modelInputSize) { ERROR_LOG("input image size[%zu] is not equal to model input size[%zu]", bufferSize, modelInputSize); (void) acldvppFree(inputDataBuffer); return FAILED; } - input_ = aclmdlCreateDataset(); - if (input_ == nullptr) { + g_input_ = aclmdlCreateDataset(); + if (g_input_ == nullptr) { ERROR_LOG("can't create dataset, create input failed"); (void) acldvppFree(inputDataBuffer); return FAILED; @@ -164,7 +171,7 @@ Result ModelProcess::CreateInput(void *inputDataBuffer, size_t bufferSize) { return FAILED; } - aclError ret = aclmdlAddDatasetBuffer(input_, inputData); + aclError ret = aclmdlAddDatasetBuffer(g_input_, inputData); if (ret != ACL_SUCCESS) { ERROR_LOG("add input dataset buffer failed, errorCode is %d", static_cast(ret)); (void) aclDestroyDataBuffer(inputData); @@ -177,36 +184,36 @@ Result ModelProcess::CreateInput(void *inputDataBuffer, size_t bufferSize) { } void ModelProcess::DestroyInput() { - if (input_ == nullptr) { + if (g_input_ == nullptr) { return; } - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(input_); ++i) { - aclDataBuffer *dataBuffer = aclmdlGetDatasetBuffer(input_, i); + for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(g_input_); ++i) { + aclDataBuffer *dataBuffer = aclmdlGetDatasetBuffer(g_input_, i); void *data = aclGetDataBufferAddr(dataBuffer); (void) acldvppFree(data); (void) aclDestroyDataBuffer(dataBuffer); } - (void) aclmdlDestroyDataset(input_); - input_ = nullptr; + (void) aclmdlDestroyDataset(g_input_); + g_input_ = nullptr; INFO_LOG("destroy model input success"); } Result ModelProcess::CreateOutput() { - if (modelDesc_ == nullptr) { + if (g_modelDesc_ == nullptr) { ERROR_LOG("no model description, create output failed"); return FAILED; } - output_ = aclmdlCreateDataset(); - if (output_ == nullptr) { + g_output_ = aclmdlCreateDataset(); + if (g_output_ == nullptr) { ERROR_LOG("can't create dataset, create output failed"); return FAILED; } - size_t outputSize = aclmdlGetNumOutputs(modelDesc_); + size_t outputSize = aclmdlGetNumOutputs(g_modelDesc_); for (size_t i = 0; i < outputSize; ++i) { - size_t modelOutputSize = aclmdlGetOutputSizeByIndex(modelDesc_, i); + size_t modelOutputSize = aclmdlGetOutputSizeByIndex(g_modelDesc_, i); void *outputBuffer = nullptr; aclError ret = aclrtMalloc(&outputBuffer, modelOutputSize, ACL_MEM_MALLOC_NORMAL_ONLY); if (ret != ACL_SUCCESS) { @@ -222,7 +229,7 @@ Result ModelProcess::CreateOutput() { return FAILED; } - ret = aclmdlAddDatasetBuffer(output_, outputData); + ret = aclmdlAddDatasetBuffer(g_output_, outputData); if (ret != ACL_SUCCESS) { ERROR_LOG("can't add data buffer, create output failed, errorCode is %d", static_cast(ret)); (void) aclrtFree(outputBuffer); @@ -236,39 +243,39 @@ Result ModelProcess::CreateOutput() { } void ModelProcess::DestroyOutput() { - if (output_ == nullptr) { + if (g_output_ == nullptr) { return; } - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) { - aclDataBuffer *dataBuffer = aclmdlGetDatasetBuffer(output_, i); + for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(g_output_); ++i) { + aclDataBuffer *dataBuffer = aclmdlGetDatasetBuffer(g_output_, i); void *data = aclGetDataBufferAddr(dataBuffer); (void) aclrtFree(data); (void) aclDestroyDataBuffer(dataBuffer); } - (void) aclmdlDestroyDataset(output_); - output_ = nullptr; + (void) aclmdlDestroyDataset(g_output_); + g_output_ = nullptr; INFO_LOG("destroy model output success"); } Result ModelProcess::Execute() { - aclError ret = aclmdlExecute(modelId_, input_, output_); + aclError ret = aclmdlExecute(g_modelId_, g_input_, g_output_); if (ret != ACL_SUCCESS) { - ERROR_LOG("execute model failed, modelId is %u, errorCode is %d", modelId_, static_cast(ret)); + ERROR_LOG("execute model failed, modelId is %u, errorCode is %d", g_modelId_, static_cast(ret)); return FAILED; } - INFO_LOG("model execute success, modelId is %u", modelId_); + INFO_LOG("model execute success, modelId is %u", g_modelId_); return SUCCESS; } const aclmdlDataset *ModelProcess::GetModelOutputData() { - return output_; + return g_output_; } Result ModelProcess::GetModelInputWH(int &width, int &height) { - if (modelDesc_ == nullptr) { + if (g_modelDesc_ == nullptr) { ERROR_LOG("no model description, get input hw failed"); return FAILED; } @@ -276,7 +283,7 @@ Result ModelProcess::GetModelInputWH(int &width, int &height) { // dims[0] is N, dims[1] is H, dims[2] is W dims[3] is C aclmdlIODims dims; // om used in this app has only one input - aclError ret = aclmdlGetInputDims(modelDesc_, 0, &dims); + aclError ret = aclmdlGetInputDims(g_modelDesc_, 0, &dims); if (ret != ACL_SUCCESS) { ERROR_LOG("get model input dims failed, errorCode is %d", static_cast(ret)); return FAILED; diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/post_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/post_process.cpp index 31e17d91b..b476817ee 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/post_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/post_process.cpp @@ -1,12 +1,19 @@ -/** -* @file post_process.cpp +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. * -* Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at + +* http://www.apache.org/licenses/LICENSE-2.0 + +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. */ + #include "post_process.h" #include #include "opencv2/opencv.hpp" @@ -53,12 +60,12 @@ namespace { } void PostProcess::SetBoxInfo(size_t index, BBox &box) { - float *boxBuff = static_cast(outputBox_); + float *boxBuff = static_cast(g_outputBox_); boxBuff += (index * sizeof(float)); - box.x = boxBuff[0] * xScale_; - box.y = boxBuff[1] * yScale_; - box.w = boxBuff[2] * xScale_; - box.h = boxBuff[3] * yScale_; + box.x = boxBuff[0] * g_xScale_; + box.y = boxBuff[1] * g_yScale_; + box.w = boxBuff[2] * g_xScale_; + box.h = boxBuff[3] * g_yScale_; } bool PostProcess::SortScore(BBox box1, BBox box2) { @@ -96,7 +103,7 @@ void PostProcess::NMS(vector &boxes, vector &result) { } void PostProcess::DrawBoundBoxToImage(const vector &result) { - cv::Mat image = cv::imread(originImage_, CV_LOAD_IMAGE_UNCHANGED); + cv::Mat image = cv::imread(g_originImage_, CV_LOAD_IMAGE_UNCHANGED); for (size_t i = 0; i < result.size(); ++i) { cv::Point p1, p2; p1.x = result[i].x - result[i].w / 2; @@ -109,8 +116,8 @@ void PostProcess::DrawBoundBoxToImage(const vector &result) { cv::FONT_HERSHEY_COMPLEX, kFountScale, kFontColor); } - size_t pos = originImage_.find_last_of("/"); - string fileName(originImage_.substr(pos + 1)); + size_t pos = g_originImage_.find_last_of("/"); + string fileName(g_originImage_.substr(pos + 1)); stringstream sstream; sstream.str(""); sstream << "../out/out_" << fileName; @@ -120,7 +127,7 @@ void PostProcess::DrawBoundBoxToImage(const vector &result) { Result PostProcess::Process() { vector boxes; - float *classBuff = static_cast(outputClass_); + float *classBuff = static_cast(g_outputClass_); for (size_t i = 0; i < kModelOutputBoxNum; ++i) { float maxValue = 0; float maxIndex = 0; diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/utils.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/utils.cpp index 9d1739604..e49d7c9c0 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/utils.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/utils.cpp @@ -1,12 +1,19 @@ -/** -* @file utils.cpp +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. * -* Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at + +* http://www.apache.org/licenses/LICENSE-2.0 + +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. */ + #include "utils.h" #include #include -- Gitee From a9be774aa3c0c42cbeed998942da0cbfc29a491d Mon Sep 17 00:00:00 2001 From: yy0131 <1595992831@qq.com> Date: Thu, 29 Sep 2022 16:38:22 +0800 Subject: [PATCH 02/14] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../inc/object_detect.h | 33 ++--- .../inc/queue.h | 125 +++++++++--------- .../src/main.cpp | 78 ++++++----- .../src/object_detect.cpp | 76 +++++------ 4 files changed, 154 insertions(+), 158 deletions(-) diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/inc/object_detect.h b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/inc/object_detect.h index d406dec23..91571bec1 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/inc/object_detect.h +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/inc/object_detect.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 YOLOV3_COCO_DETECTION_4_THREAD_INC_OBJECT_DETECT_H +#define YOLOV3_COCO_DETECTION_4_THREAD_INC_OBJECT_DETECT_H + #pragma once #include #include "acl/acl.h" @@ -43,14 +44,16 @@ public: private: AclLiteError CreateInput(); - AclLiteModel model_; - const char* modelPath_; - uint32_t modelWidth_; - uint32_t modelHeight_; - uint32_t imageDataSize_; - void* imageDataBuf_; - uint32_t imageInfoSize_; - void* imageInfoBuf_; - aclrtRunMode runMode_; - bool isInited_; + AclLiteModel g_model_; + const char* g_modelPath_; + uint32_t g_modelWidth_; + uint32_t g_modelHeight_; + uint32_t g_imageDataSize_; + void* g_imageDataBuf_; + uint32_t g_imageInfoSize_; + void* g_imageInfoBuf_; + aclrtRunMode g_runMode_; + bool g_isInited_; }; + +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/inc/queue.h b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/inc/queue.h index 86480f21c..25a299637 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/inc/queue.h +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/inc/queue.h @@ -1,20 +1,21 @@ /* - * Copyright(C) 2020. Huawei Technologies Co.,Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef BLOCKING_QUEUE_H -#define BLOCKING_QUEUE_H +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. +*/ + +#ifndef YOLOV3_COCO_DETECTION_4_THREAD_INC_QUEUE_H +#define YOLOV3_COCO_DETECTION_4_THREAD_INC_QUEUE_H #include #include @@ -24,16 +25,14 @@ #include "opencv2/opencv.hpp" #include "opencv2/imgproc/types_c.h" -struct message_pre -{ +struct message_pre { int videoIndex; int isLastFrame; cv::Mat frame; cv::Mat reiszeMat; }; -struct message -{ +struct message { int videoIndex; int isLastFrame; cv::Mat frame; @@ -41,8 +40,7 @@ struct message std::shared_ptr boxNum; }; -struct message_video -{ +struct message_video { int number; cv::Mat resultImage; }; @@ -51,7 +49,7 @@ static const int DEFAULT_MAX_QUEUE_SIZE = 128; template class BlockingQueue { public: - BlockingQueue(uint32_t maxSize = DEFAULT_MAX_QUEUE_SIZE) : max_size_(maxSize), is_stoped_(false) {} + BlockingQueue(uint32_t maxSize = DEFAULT_MAX_QUEUE_SIZE) : maxSize_(maxSize), isStoped_(false) {} ~BlockingQueue() {} @@ -59,24 +57,24 @@ template class BlockingQueue { { std::unique_lock lock(mutex_); - while (queue_.empty() && !is_stoped_) { - empty_cond_.wait(lock); + while (queue_.empty() && !isStoped_) { + emptyCond_.wait(lock); } - if (is_stoped_) { - //APP_ERR_QUEUE_STOPED + if (isStoped_) { + // APP_ERR_QUEUE_STOPED return 1; } if (queue_.empty()) { - //APP_ERR_QUEUE_EMPTY + // APP_ERR_QUEUE_EMPTY return 2; } else { item = queue_.front(); queue_.pop_front(); } - full_cond_.notify_one(); + fullCond_.notify_one(); return 0; } @@ -86,24 +84,24 @@ template class BlockingQueue { std::unique_lock lock(mutex_); auto realTime = std::chrono::milliseconds(timeOutMs); - while (queue_.empty() && !is_stoped_) { - empty_cond_.wait_for(lock, realTime); + while (queue_.empty() && !isStoped_) { + emptyCond_.wait_for(lock, realTime); } - if (is_stoped_) { - //APP_ERR_QUEUE_STOPED + if (isStoped_) { + // APP_ERR_QUEUE_STOPED return 1; } if (queue_.empty()) { - //APP_ERR_QUEUE_EMPTY + // APP_ERR_QUEUE_EMPTY return 2; } else { item = queue_.front(); queue_.pop_front(); } - full_cond_.notify_one(); + fullCond_.notify_one(); return 0; } @@ -112,22 +110,22 @@ template class BlockingQueue { { std::unique_lock lock(mutex_); - while (queue_.size() >= max_size_ && isWait && !is_stoped_) { - full_cond_.wait(lock); + while (queue_.size() >= maxSize_ && isWait && !isStoped_) { + fullCond_.wait(lock); } - if (is_stoped_) { - //APP_ERR_QUEUE_STOPED + if (isStoped_) { + // APP_ERR_QUEUE_STOPED return 1; } - if (queue_.size() >= max_size_) { - //APP_ERROR_QUEUE_FULL + if (queue_.size() >= maxSize_) { + // APP_ERROR_QUEUE_FULL return 3; } queue_.push_back(item); - empty_cond_.notify_one(); + emptyCond_.notify_one(); return 0; } @@ -136,23 +134,23 @@ template class BlockingQueue { { std::unique_lock lock(mutex_); - while (queue_.size() >= max_size_ && isWait && !is_stoped_) { - full_cond_.wait(lock); + while (queue_.size() >= maxSize_ && isWait && !isStoped_) { + fullCond_.wait(lock); } - if (is_stoped_) { - //APP_ERR_QUEUE_STOPED + if (isStoped_) { + // APP_ERR_QUEUE_STOPED return 1; } - if (queue_.size() >= max_size_) { - //APP_ERROR_QUEUE_FULL + if (queue_.size() >= maxSize_) { + // APP_ERROR_QUEUE_FULL return 3; } queue_.push_front(item); - empty_cond_.notify_one(); + emptyCond_.notify_one(); return 0; } @@ -161,18 +159,18 @@ template class BlockingQueue { { { std::unique_lock lock(mutex_); - is_stoped_ = true; + isStoped_ = true; } - full_cond_.notify_all(); - empty_cond_.notify_all(); + fullCond_.notify_all(); + emptyCond_.notify_all(); } void Restart() { { std::unique_lock lock(mutex_); - is_stoped_ = false; + isStoped_ = false; } } @@ -181,7 +179,7 @@ template class BlockingQueue { { std::unique_lock lock(mutex_); - if (!is_stoped_) { + if (!isStoped_) { return std::list(); } @@ -190,13 +188,13 @@ template class BlockingQueue { int GetBackItem(T &item) { - if (is_stoped_) { - //APP_ERR_QUEUE_STOPED + if (isStoped_) { + // APP_ERR_QUEUE_STOPED return 1; } if (queue_.empty()) { - //APP_ERR_QUEUE_EMPTY + // APP_ERR_QUEUE_EMPTY return 2; } @@ -212,7 +210,7 @@ template class BlockingQueue { int IsFull() { std::unique_lock lock(mutex_); - return queue_.size() >= max_size_; + return queue_.size() >= maxSize_; } int GetSize() @@ -234,10 +232,11 @@ template class BlockingQueue { private: std::list queue_; std::mutex mutex_; - std::condition_variable empty_cond_; - std::condition_variable full_cond_; - uint32_t max_size_; + std::condition_variable emptyCond_; + std::condition_variable fullCond_; + uint32_t maxSize_; - bool is_stoped_; + bool isStoped_; }; -#endif // __INC_BLOCKING_QUEUE_H__ + +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/src/main.cpp b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/src/main.cpp index e70e99de3..d66710bf1 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/src/main.cpp +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/src/main.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,8 @@ * 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 main.cpp -* Description: dvpp sample main func */ + #include #include #include "acllite/AclLiteUtils.h" @@ -39,10 +37,10 @@ struct timespec g_time1 = {0, 0}; struct timespec g_time2 = {0, 0}; namespace { - uint32_t kModelWidth = 416; - uint32_t kModelHeight = 416; - const char* kModelPath = "../model/yolov3.om"; - const static std::vector yolov3Label = { "person", "bicycle", "car", "motorbike", + uint32_t g_ModelWidth = 416; + uint32_t g_ModelHeight = 416; + const char* g_ModelPath = "../model/yolov3.om"; + const static std::vector g_yolov3Label = { "person", "bicycle", "car", "motorbike", "aeroplane","bus", "train", "truck", "boat", "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", @@ -59,16 +57,16 @@ namespace { "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase","scissors", "teddy bear", "hair drier", "toothbrush" }; - const uint32_t kBBoxDataBufId = 0; - const uint32_t kBoxNumDataBufId = 1; + const uint32_t g_BBoxDataBufId = 0; + const uint32_t g_BoxNumDataBufId = 1; enum BBoxIndex { TOPLEFTX = 0, TOPLEFTY, BOTTOMRIGHTX, BOTTOMRIGHTY, SCORE, LABEL }; - const uint32_t kLineSolid = 2; - const string kOutputFilePrefix = "out_"; - const double kFountScale = 0.5; - const cv::Scalar kFontColor(0, 0, 255); - const uint32_t kLabelOffset = 11; - const string kFileSperator = "/"; - const vector kColors{ + const uint32_t g_LineSolid = 2; + const string g_OutputFilePrefix = "out_"; + const double g_FountScale = 0.5; + const cv::Scalar g_FontColor(0, 0, 255); + const uint32_t g_LabelOffset = 11; + const string g_FileSperator = "/"; + const vector g_Colors { cv::Scalar(237, 149, 100), cv::Scalar(0, 215, 255), cv::Scalar(50, 205, 50), cv::Scalar(139, 85, 26) }; } @@ -78,9 +76,9 @@ void Preprocess(cv::VideoCapture capture, aclrtContext context, message_pre premsg; aclrtSetCurrentContext(context); - while(1){ + while (1) { cv::Mat frame; - if (!capture.read(frame)){ + if (!capture.read(frame)) { ACLLITE_LOG_INFO("Video capture return false"); break; } @@ -88,13 +86,13 @@ void Preprocess(cv::VideoCapture capture, aclrtContext context, premsg.videoIndex = threadNum; premsg.isLastFrame = 1; cv::Mat reiszeMat; - cv::resize(frame, reiszeMat, cv::Size(kModelWidth, kModelHeight)); + cv::resize(frame, reiszeMat, cv::Size(g_ModelWidth, g_ModelHeight)); if (reiszeMat.empty()) { ACLLITE_LOG_ERROR("Resize image failed"); break; } premsg.reiszeMat = reiszeMat; - while(1) { + while (1) { if (queue_pre->Push(premsg) != 0) { usleep(1000); } @@ -105,7 +103,7 @@ void Preprocess(cv::VideoCapture capture, aclrtContext context, premsg.videoIndex = threadNum; premsg.isLastFrame = 0; - while(1) { + while (1) { if (queue_pre->Push(premsg) != 0) { usleep(1000); } @@ -116,38 +114,38 @@ void Preprocess(cv::VideoCapture capture, aclrtContext context, } } -void Postprocess(aclrtContext context, BlockingQueue* queue_post){ +void Postprocess(aclrtContext context, BlockingQueue* queue_post) { message postmsg; uint32_t* boxNum = nullptr; float* detectData = nullptr; aclrtSetCurrentContext(context); - while(1) { + while (1) { if(queue_post->Pop(postmsg) != 0) { usleep(1000); continue; } - if (!postmsg.isLastFrame){ + if (!postmsg.isLastFrame) { break; } cv::Mat frame = postmsg.frame; detectData = (float*)postmsg.detectData.get(); boxNum = (uint32_t*)postmsg.boxNum.get(); - if (detectData == nullptr || boxNum == nullptr){ + if (detectData == nullptr || boxNum == nullptr) { break; } uint32_t totalBox = boxNum[0]; vector detectResults; - float widthScale = (float)(frame.cols) / kModelWidth; - float heightScale = (float)(frame.rows) / kModelHeight; + float widthScale = (float)(frame.cols) / g_ModelWidth; + float heightScale = (float)(frame.rows) / g_ModelHeight; for (uint32_t i = 0; i < totalBox; i++) { BBox boundBox; uint32_t score = uint32_t(detectData[totalBox * SCORE + i] * 100); - if (score < 90){ + if (score < 90) { continue; } boundBox.rect.ltX = detectData[totalBox * TOPLEFTX + i] * widthScale; @@ -156,7 +154,7 @@ void Postprocess(aclrtContext context, BlockingQueue* queue_post){ boundBox.rect.rbY = detectData[totalBox * BOTTOMRIGHTY + i] * heightScale; uint32_t objIndex = (uint32_t)detectData[totalBox * LABEL + i]; - boundBox.text = yolov3Label[objIndex] + std::to_string(score) + "\%"; + boundBox.text = g_yolov3Label[objIndex] + std::to_string(score) + "\%"; detectResults.emplace_back(boundBox); } @@ -172,7 +170,7 @@ void Postprocess(aclrtContext context, BlockingQueue* queue_post){ } int main(int argc, char *argv[]) { - //init acl resource + // init acl resource AclLiteResource aclDev; AclLiteError ret = aclDev.Init(); if (ret) { @@ -180,7 +178,7 @@ int main(int argc, char *argv[]) { return ACLLITE_ERROR; } - ObjectDetect detect(kModelPath, kModelWidth, kModelHeight); + ObjectDetect detect(g_ModelPath, g_ModelWidth, g_ModelHeight); ret = detect.Init(); if (ret != ACLLITE_OK) { @@ -232,15 +230,15 @@ int main(int argc, char *argv[]) { message msg; int video_need_preprocess = VIDEONUM; clock_gettime(CLOCK_REALTIME, &g_time1); - while(1) { - if(queue_pre.Pop(premsg) != 0) { + while (1) { + if (queue_pre.Pop(premsg) != 0) { usleep(1000); continue; } msg.videoIndex = premsg.videoIndex; msg.isLastFrame = premsg.isLastFrame; - if(msg.isLastFrame == 0) + if (msg.isLastFrame == 0) { video_need_preprocess = video_need_preprocess - 1; } @@ -264,8 +262,8 @@ int main(int argc, char *argv[]) { ACLLITE_LOG_ERROR("Inference model inference output data failed"); return 1; } - msg.detectData = inferenceOutput[kBBoxDataBufId].data; - msg.boxNum = inferenceOutput[kBoxNumDataBufId].data; + msg.detectData = inferenceOutput[g_BBoxDataBufId].data; + msg.boxNum = inferenceOutput[g_BoxNumDataBufId].data; int i = msg.videoIndex; while (1) { @@ -281,8 +279,8 @@ int main(int argc, char *argv[]) { cout << "Execute time passed is: " << (g_time2.tv_sec - g_time1.tv_sec) * 1000 + (g_time2.tv_nsec - g_time1.tv_nsec) / 1000000 << "ms" << endl; - while(1){ - if(!g_video_need_postprocess){ + while (1) { + if (!g_video_need_postprocess) { break; } } diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/src/object_detect.cpp b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/src/object_detect.cpp index b38c6dfeb..0e68da078 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/src/object_detect.cpp +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/src/object_detect.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,8 @@ * 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 "acl/acl.h" @@ -24,15 +22,14 @@ #include "object_detect.h" #include "opencv2/opencv.hpp" - using namespace std; ObjectDetect::ObjectDetect(const char* modelPath, uint32_t modelWidth, uint32_t modelHeight) -: modelWidth_(modelWidth), modelHeight_(modelHeight), isInited_(false), - imageDataBuf_(nullptr), imageInfoBuf_(nullptr){ - imageInfoSize_ = 0; - modelPath_ = modelPath; +: g_modelWidth_(modelWidth), g_modelHeight_(modelHeight), g_isInited_(false), + g_imageDataBuf_(nullptr), g_imageInfoBuf_(nullptr) { + g_imageInfoSize_ = 0; + g_modelPath_ = modelPath; } ObjectDetect::~ObjectDetect() { @@ -40,26 +37,26 @@ ObjectDetect::~ObjectDetect() { } AclLiteError ObjectDetect::CreateInput() { - //Request image data memory for input model - aclError aclRet = aclrtMalloc(&imageDataBuf_, (size_t)(imageDataSize_), ACL_MEM_MALLOC_HUGE_FIRST); + // Request image data memory for input model + aclError aclRet = aclrtMalloc(& g_imageDataBuf_, (size_t)( g_imageDataSize_), ACL_MEM_MALLOC_HUGE_FIRST); if (aclRet != ACL_SUCCESS) { ACLLITE_LOG_ERROR("malloc device data buffer failed, aclRet is %d", aclRet); return ACLLITE_ERROR; } - //The second input to Yolov3 is the input image width and height parameter - const float imageInfo[4] = {(float)modelWidth_, (float)modelHeight_, - (float)modelWidth_, (float)modelHeight_}; - imageInfoSize_ = sizeof(imageInfo); - imageInfoBuf_ = CopyDataToDevice((void *)imageInfo, imageInfoSize_, - runMode_, MEMORY_DEVICE); - if (imageInfoBuf_ == nullptr) { + // The second input to Yolov3 is the input image width and height parameter + const float imageInfo[4] = {(float) g_modelWidth_, (float) g_modelHeight_, + (float) g_modelWidth_, (float) g_modelHeight_}; + g_imageInfoSize_ = sizeof(imageInfo); + g_imageInfoBuf_ = CopyDataToDevice((void *)imageInfo, g_imageInfoSize_, + g_runMode_, MEMORY_DEVICE); + if ( g_imageInfoBuf_ == nullptr) { ACLLITE_LOG_ERROR("Copy image info to device failed"); return ACLLITE_ERROR; } - AclLiteError ret = model_.CreateInput(imageDataBuf_, imageDataSize_, - imageInfoBuf_, imageInfoSize_); + AclLiteError ret = g_model_.CreateInput( g_imageDataBuf_, g_imageDataSize_, + g_imageInfoBuf_, g_imageInfoSize_); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Create mode input dataset failed"); return ACLLITE_ERROR; @@ -69,27 +66,27 @@ AclLiteError ObjectDetect::CreateInput() { } AclLiteError ObjectDetect::Init() { - //If it is already initialized, it is returned - if (isInited_) { + // If it is already initialized, it is returned + if ( g_isInited_) { ACLLITE_LOG_INFO("Classify instance is initied already!"); return ACLLITE_OK; } - //Gets whether the current application is running on host or Device - AclLiteError ret = aclrtGetRunMode(&runMode_); + // Gets whether the current application is running on host or Device + AclLiteError ret = aclrtGetRunMode(& g_runMode_); if (ret != ACL_SUCCESS) { ACLLITE_LOG_ERROR("acl get run mode failed"); return ACLLITE_ERROR; } - //Initializes the model management instance - ret = model_.Init(modelPath_); + // Initializes the model management instance + ret = g_model_.Init( g_modelPath_); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Init model failed"); return ACLLITE_ERROR; } - imageDataSize_ = model_.GetModelInputSize(0); + g_imageDataSize_ = g_model_.GetModelInputSize(0); ret = CreateInput(); if (ret != ACLLITE_OK) { @@ -97,21 +94,21 @@ AclLiteError ObjectDetect::Init() { return ACLLITE_ERROR; } - isInited_ = true; + g_isInited_ = true; return ACLLITE_OK; } AclLiteError ObjectDetect::Inference(std::vector& inferenceOutput, cv::Mat& reiszeMat) { - AclLiteError ret = CopyDataToDeviceEx(imageDataBuf_, imageDataSize_, - reiszeMat.ptr(), imageDataSize_, - runMode_); + AclLiteError ret = CopyDataToDeviceEx( g_imageDataBuf_, g_imageDataSize_, + reiszeMat.ptr(), g_imageDataSize_, + g_runMode_); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Copy resized image data to device failed."); return ACLLITE_ERROR; } - //Perform reasoning - ret = model_.Execute(inferenceOutput); + // Perform reasoning + ret = g_model_.Execute(inferenceOutput); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Execute model inference failed"); return ACLLITE_ERROR; @@ -121,12 +118,11 @@ AclLiteError ObjectDetect::Inference(std::vector& inferenceOutp return ACLLITE_OK; } -void ObjectDetect::DestroyResource() -{ - model_.DestroyInput(); - model_.DestroyResource(); +void ObjectDetect::DestroyResource() { + g_model_.DestroyInput(); + g_model_.DestroyResource(); - aclrtFree(imageDataBuf_); - aclrtFree(imageInfoBuf_); + aclrtFree( g_imageDataBuf_); + aclrtFree( g_imageInfoBuf_); } -- Gitee From c6faa73c8ba055b05f84dd76f2f56e4b03d5921a Mon Sep 17 00:00:00 2001 From: maohongchao <1506472071@qq.com> Date: Fri, 30 Sep 2022 15:54:10 +0800 Subject: [PATCH 03/14] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../inc/object_detect.h | 33 +-- .../inc/queue.h | 119 +++++------ .../src/main.cpp | 85 ++++---- .../src/object_detect.cpp | 76 ++++--- .../inc/object_detection.h | 23 ++- .../AnimeGANv2_video_multi_device.conf | 2 +- .../src/inference/inference.cpp | 50 ++--- .../src/inference/inference.h | 24 ++- .../src/main.cpp | 68 +++---- .../src/postprocess/postprocess.cpp | 28 +-- .../src/postprocess/postprocess.h | 16 +- .../src/preprocess/preprocess.cpp | 188 ++++++++---------- .../src/preprocess/preprocess.h | 48 ++--- .../src/videoprocess/videoprocess.cpp | 66 +++--- .../src/videoprocess/videoprocess.h | 26 +-- 15 files changed, 417 insertions(+), 435 deletions(-) diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/inc/object_detect.h b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/inc/object_detect.h index 4ef384ebb..8db19f5b8 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/inc/object_detect.h +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/inc/object_detect.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 YOLOV3_COCO_DETECTION_4_THREAD_INC_OBJECT_DETECT_H +#define YOLOV3_COCO_DETECTION_4_THREAD_INC_OBJECT_DETECT_H + #pragma once #include #include "acl/acl.h" @@ -44,14 +45,16 @@ class ObjectDetect { private: AclLiteError CreateInput(); - AclLiteModel model_; - const char* modelPath_; - uint32_t modelWidth_; - uint32_t modelHeight_; - uint32_t imageDataSize_; - void* imageDataBuf_; - uint32_t imageInfoSize_; - void* imageInfoBuf_; - aclrtRunMode runMode_; - bool isInited_; + AclLiteModel g_model_; + const char* g_modelPath_; + uint32_t g_modelWidth_; + uint32_t g_modelHeight_; + uint32_t g_imageDataSize_; + void* g_imageDataBuf_; + uint32_t g_imageInfoSize_; + void* g_imageInfoBuf_; + aclrtRunMode g_runMode_; + bool g_isInited_; }; + +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/inc/queue.h b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/inc/queue.h index f8d814568..f73ac3fa8 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/inc/queue.h +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/inc/queue.h @@ -1,20 +1,21 @@ /* - * Copyright(C) 2020. Huawei Technologies Co.,Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef BLOCKING_QUEUE_H -#define BLOCKING_QUEUE_H +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. +*/ + +#ifndef YOLOV3_COCO_DETECTION_4_THREAD_INC_QUEUE_H +#define YOLOV3_COCO_DETECTION_4_THREAD_INC_QUEUE_H #include #include @@ -25,8 +26,7 @@ #include "opencv2/imgproc/types_c.h" -struct message -{ +struct message { int videoIndex; int isLastFrame; cv::Mat frame; @@ -39,7 +39,7 @@ static const int DEFAULT_MAX_QUEUE_SIZE = 5; template class BlockingQueue { public: - BlockingQueue(uint32_t maxSize = DEFAULT_MAX_QUEUE_SIZE) : max_size_(maxSize), is_stoped_(false) {} + BlockingQueue(uint32_t maxSize = DEFAULT_MAX_QUEUE_SIZE) : maxSize_(maxSize), isStoped_(false) {} ~BlockingQueue() {} @@ -47,24 +47,24 @@ template class BlockingQueue { { std::unique_lock lock(mutex_); - while (queue_.empty() && !is_stoped_) { - empty_cond_.wait(lock); + while (queue_.empty() && !isStoped_) { + emptyCond_.wait(lock); } - if (is_stoped_) { - //APP_ERR_QUEUE_STOPED + if (isStoped_) { + // APP_ERR_QUEUE_STOPED return 1; } if (queue_.empty()) { - //APP_ERR_QUEUE_EMPTY + // APP_ERR_QUEUE_EMPTY return 2; } else { item = queue_.front(); queue_.pop_front(); } - full_cond_.notify_one(); + fullCond_.notify_one(); return 0; } @@ -74,24 +74,24 @@ template class BlockingQueue { std::unique_lock lock(mutex_); auto realTime = std::chrono::milliseconds(timeOutMs); - while (queue_.empty() && !is_stoped_) { - empty_cond_.wait_for(lock, realTime); + while (queue_.empty() && !isStoped_) { + emptyCond_.wait_for(lock, realTime); } - if (is_stoped_) { - //APP_ERR_QUEUE_STOPED + if (isStoped_) { + // APP_ERR_QUEUE_STOPED return 1; } if (queue_.empty()) { - //APP_ERR_QUEUE_EMPTY + // APP_ERR_QUEUE_EMPTY return 2; } else { item = queue_.front(); queue_.pop_front(); } - full_cond_.notify_one(); + fullCond_.notify_one(); return 0; } @@ -100,22 +100,22 @@ template class BlockingQueue { { std::unique_lock lock(mutex_); - while (queue_.size() >= max_size_ && isWait && !is_stoped_) { - full_cond_.wait(lock); + while (queue_.size() >= maxSize_ && isWait && !isStoped_) { + fullCond_.wait(lock); } - if (is_stoped_) { - //APP_ERR_QUEUE_STOPED + if (isStoped_) { + // APP_ERR_QUEUE_STOPED return 1; } - if (queue_.size() >= max_size_) { - //APP_ERROR_QUEUE_FULL + if (queue_.size() >= maxSize_) { + // APP_ERROR_QUEUE_FULL return 3; } queue_.push_back(item); - empty_cond_.notify_one(); + emptyCond_.notify_one(); return 0; } @@ -124,23 +124,23 @@ template class BlockingQueue { { std::unique_lock lock(mutex_); - while (queue_.size() >= max_size_ && isWait && !is_stoped_) { - full_cond_.wait(lock); + while (queue_.size() >= maxSize_ && isWait && !isStoped_) { + fullCond_.wait(lock); } - if (is_stoped_) { - //APP_ERR_QUEUE_STOPED + if (isStoped_) { + // APP_ERR_QUEUE_STOPED return 1; } - if (queue_.size() >= max_size_) { - //APP_ERROR_QUEUE_FULL + if (queue_.size() >= maxSize_) { + // APP_ERROR_QUEUE_FULL return 3; } queue_.push_front(item); - empty_cond_.notify_one(); + emptyCond_.notify_one(); return 0; } @@ -149,18 +149,18 @@ template class BlockingQueue { { { std::unique_lock lock(mutex_); - is_stoped_ = true; + isStoped_ = true; } - full_cond_.notify_all(); - empty_cond_.notify_all(); + fullCond_.notify_all(); + emptyCond_.notify_all(); } void Restart() { { std::unique_lock lock(mutex_); - is_stoped_ = false; + isStoped_ = false; } } @@ -169,7 +169,7 @@ template class BlockingQueue { { std::unique_lock lock(mutex_); - if (!is_stoped_) { + if (!isStoped_) { return std::list(); } @@ -178,13 +178,13 @@ template class BlockingQueue { int GetBackItem(T &item) { - if (is_stoped_) { - //APP_ERR_QUEUE_STOPED + if (isStoped_) { + // APP_ERR_QUEUE_STOPED return 1; } if (queue_.empty()) { - //APP_ERR_QUEUE_EMPTY + // APP_ERR_QUEUE_EMPTY return 2; } @@ -200,7 +200,7 @@ template class BlockingQueue { int IsFull() { std::unique_lock lock(mutex_); - return queue_.size() >= max_size_; + return queue_.size() >= maxSize_; } int GetSize() @@ -222,10 +222,11 @@ template class BlockingQueue { private: std::list queue_; std::mutex mutex_; - std::condition_variable empty_cond_; - std::condition_variable full_cond_; - uint32_t max_size_; + std::condition_variable emptyCond_; + std::condition_variable fullCond_; + uint32_t maxSize_; - bool is_stoped_; + bool isStoped_; }; -#endif // __INC_BLOCKING_QUEUE_H__ + +#endif diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/src/main.cpp b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/src/main.cpp index d2bece41c..20e203c60 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/src/main.cpp +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/src/main.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,9 +12,6 @@ * 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 main.cpp -* Description: dvpp sample main func */ #include @@ -36,10 +33,10 @@ using namespace std; namespace { - uint32_t kModelWidth = 416; - uint32_t kModelHeight = 416; - const char* kModelPath = "../model/yolov3.om"; - const static std::vector yolov3Label = { "person", "bicycle", "car", "motorbike", + uint32_t g_ModelWidth = 416; + uint32_t g_ModelHeight = 416; + const char* g_ModelPath = "../model/yolov3.om"; + const static std::vector g_yolov3Label = { "person", "bicycle", "car", "motorbike", "aeroplane","bus", "train", "truck", "boat", "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", @@ -56,16 +53,16 @@ namespace { "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase","scissors", "teddy bear", "hair drier", "toothbrush" }; - const uint32_t kBBoxDataBufId = 0; - const uint32_t kBoxNumDataBufId = 1; + const uint32_t g_BBoxDataBufId = 0; + const uint32_t g_BoxNumDataBufId = 1; enum BBoxIndex { TOPLEFTX = 0, TOPLEFTY, BOTTOMRIGHTX, BOTTOMRIGHTY, SCORE, LABEL }; - const uint32_t kLineSolid = 2; - const string kOutputFilePrefix = "out_"; - const double kFountScale = 0.5; - const cv::Scalar kFontColor(0, 0, 255); - const uint32_t kLabelOffset = 11; - const string kFileSperator = "/"; - const vector kColors{ + const uint32_t g_LineSolid = 2; + const string g_OutputFilePrefix = "out_"; + const double g_FountScale = 0.5; + const cv::Scalar g_FontColor(0, 0, 255); + const uint32_t g_LabelOffset = 11; + const string g_FileSperator = "/"; + const vector g_Colors { cv::Scalar(237, 149, 100), cv::Scalar(0, 215, 255), cv::Scalar(50, 205, 50), cv::Scalar(139, 85, 26) }; } @@ -75,9 +72,9 @@ void Preprocess(cv::VideoCapture capture, aclrtContext context, message premsg; aclrtSetCurrentContext(context); - while(1){ + while (1) { cv::Mat frame; - if (!capture.read(frame)){ + if (!capture.read(frame)) { ACLLITE_LOG_INFO("Video capture return false"); break; } @@ -85,13 +82,13 @@ void Preprocess(cv::VideoCapture capture, aclrtContext context, premsg.videoIndex = threadNum; premsg.isLastFrame = 1; cv::Mat reiszeMat; - cv::resize(frame, reiszeMat, cv::Size(kModelWidth, kModelHeight)); + cv::resize(frame, reiszeMat, cv::Size(g_ModelWidth, g_ModelHeight)); if (reiszeMat.empty()) { ACLLITE_LOG_ERROR("Resize image failed"); break; } premsg.reiszeMat = reiszeMat; - while(1) { + while (1) { if (queue_pre->Push(premsg) != 0) { usleep(1000); } @@ -102,7 +99,7 @@ void Preprocess(cv::VideoCapture capture, aclrtContext context, premsg.videoIndex = threadNum; premsg.isLastFrame = 0; - while(1) { + while (1) { if (queue_pre->Push(premsg) != 0) { usleep(1000); } @@ -113,38 +110,38 @@ void Preprocess(cv::VideoCapture capture, aclrtContext context, } } -void Postprocess(cv::VideoWriter& outputVideo, aclrtContext context, BlockingQueue* queue_post){ +void Postprocess(cv::VideoWriter& outputVideo, aclrtContext context, BlockingQueue* queue_post) { message postmsg; uint32_t* boxNum = nullptr; float* detectData = nullptr; aclrtSetCurrentContext(context); - while(1) + while (1) { - if(queue_post->Pop(postmsg) != 0) + if (queue_post->Pop(postmsg) != 0) { usleep(1000); continue; } - if (!postmsg.isLastFrame){ + if (!postmsg.isLastFrame) { break; } cv::Mat frame = postmsg.frame; detectData = (float*)postmsg.detectData.get(); boxNum = (uint32_t*)postmsg.boxNum.get(); - if (detectData == nullptr || boxNum == nullptr){ + if (detectData == nullptr || boxNum == nullptr) { break; } uint32_t totalBox = boxNum[0]; vector detectResults; - float widthScale = (float)(frame.cols) / kModelWidth; - float heightScale = (float)(frame.rows) / kModelHeight; + float widthScale = (float)(frame.cols) / g_ModelWidth; + float heightScale = (float)(frame.rows) / g_ModelHeight; for (uint32_t i = 0; i < totalBox; i++) { BBox boundBox; uint32_t score = uint32_t(detectData[totalBox * SCORE + i] * 100); - if (score < 90){ + if (score < 90) { continue; } boundBox.rect.ltX = detectData[totalBox * TOPLEFTX + i] * widthScale; @@ -153,7 +150,7 @@ void Postprocess(cv::VideoWriter& outputVideo, aclrtContext context, BlockingQue boundBox.rect.rbY = detectData[totalBox * BOTTOMRIGHTY + i] * heightScale; uint32_t objIndex = (uint32_t)detectData[totalBox * LABEL + i]; - boundBox.text = yolov3Label[objIndex] + std::to_string(score) + "\%"; + boundBox.text = g_yolov3Label[objIndex] + std::to_string(score) + "\%"; detectResults.emplace_back(boundBox); } for (int i = 0; i < detectResults.size(); ++i) { @@ -162,9 +159,9 @@ void Postprocess(cv::VideoWriter& outputVideo, aclrtContext context, BlockingQue p1.y = detectResults[i].rect.ltY; p2.x = detectResults[i].rect.rbX; p2.y = detectResults[i].rect.rbY; - cv::rectangle(frame, p1, p2, kColors[i % kColors.size()], kLineSolid); - cv::putText(frame, detectResults[i].text, cv::Point(p1.x, p1.y + kLabelOffset), - cv::FONT_HERSHEY_COMPLEX, kFountScale, kFontColor); + cv::rectangle(frame, p1, p2, g_Colors[i % g_Colors.size()], g_LineSolid); + cv::putText(frame, detectResults[i].text, cv::Point(p1.x, p1.y + g_LabelOffset), + cv::FONT_HERSHEY_COMPLEX, g_FountScale, g_FontColor); } outputVideo << frame; @@ -176,14 +173,14 @@ void Postprocess(cv::VideoWriter& outputVideo, aclrtContext context, BlockingQue } int main(int argc, char *argv[]) { - //init acl resource + // init acl resource AclLiteResource aclDev; AclLiteError ret = aclDev.Init(); if (ret) { ACLLITE_LOG_ERROR("Init resource failed, error %d", ret); return ACLLITE_ERROR; } - ObjectDetect detect(kModelPath, kModelWidth, kModelHeight); + ObjectDetect detect(g_ModelPath, g_ModelWidth, g_ModelHeight); ret = detect.Init(); if (ret != ACLLITE_OK) { @@ -226,13 +223,13 @@ int main(int argc, char *argv[]) { task4.detach(); message msg; int video_need_preprocess = VIDEONUM; - while(1) { - if(queue_pre.Pop(msg) != 0) + while (1) { + if (queue_pre.Pop(msg) != 0) { usleep(1000); continue; } - if(msg.isLastFrame == 0) + if (msg.isLastFrame == 0) { video_need_preprocess = video_need_preprocess - 1; } @@ -255,8 +252,8 @@ int main(int argc, char *argv[]) { ACLLITE_LOG_ERROR("Inference model inference output data failed"); return 1; } - msg.detectData = inferenceOutput[kBBoxDataBufId].data; - msg.boxNum = inferenceOutput[kBoxNumDataBufId].data; + msg.detectData = inferenceOutput[g_BBoxDataBufId].data; + msg.boxNum = inferenceOutput[g_BoxNumDataBufId].data; int i = msg.videoIndex; while (1) { @@ -268,8 +265,8 @@ int main(int argc, char *argv[]) { } } } - while(1){ - if(!g_video_need_postprocess){ + while (1) { + if (!g_video_need_postprocess) { break; } } diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/src/object_detect.cpp b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/src/object_detect.cpp index 95be3b116..b82f7ffa1 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/src/object_detect.cpp +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/src/object_detect.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,8 @@ * 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 "acl/acl.h" @@ -23,15 +21,14 @@ #include "object_detect.h" #include "opencv2/opencv.hpp" - using namespace std; ObjectDetect::ObjectDetect(const char* modelPath, uint32_t modelWidth, uint32_t modelHeight) -: modelWidth_(modelWidth), modelHeight_(modelHeight), isInited_(false), - imageDataBuf_(nullptr), imageInfoBuf_(nullptr){ - imageInfoSize_ = 0; - modelPath_ = modelPath; +: g_modelWidth_(modelWidth), g_modelHeight_(modelHeight), g_isInited_(false), + g_imageDataBuf_(nullptr), g_imageInfoBuf_(nullptr) { + g_imageInfoSize_ = 0; + g_modelPath_ = modelPath; } ObjectDetect::~ObjectDetect() { @@ -39,26 +36,26 @@ ObjectDetect::~ObjectDetect() { } AclLiteError ObjectDetect::CreateInput() { - //Request image data memory for input model - aclError aclRet = aclrtMalloc(&imageDataBuf_, (size_t)(imageDataSize_), ACL_MEM_MALLOC_HUGE_FIRST); + // Request image data memory for input model + aclError aclRet = aclrtMalloc(&g_imageDataBuf_, (size_t)(g_imageDataSize_), ACL_MEM_MALLOC_HUGE_FIRST); if (aclRet != ACL_SUCCESS) { ACLLITE_LOG_ERROR("malloc device data buffer failed, aclRet is %d", aclRet); return ACLLITE_ERROR; } - //The second input to Yolov3 is the input image width and height parameter - const float imageInfo[4] = {(float)modelWidth_, (float)modelHeight_, - (float)modelWidth_, (float)modelHeight_}; - imageInfoSize_ = sizeof(imageInfo); - imageInfoBuf_ = CopyDataToDevice((void *)imageInfo, imageInfoSize_, - runMode_, MEMORY_DEVICE); - if (imageInfoBuf_ == nullptr) { + // The second input to Yolov3 is the input image width and height parameter + const float imageInfo[4] = {(float)g_modelWidth_, (float)g_modelHeight_, + (float)g_modelWidth_, (float)g_modelHeight_}; + g_imageInfoSize_ = sizeof(imageInfo); + g_imageInfoBuf_ = CopyDataToDevice((void *)imageInfo, g_imageInfoSize_, + g_runMode_, MEMORY_DEVICE); + if (g_imageInfoBuf_ == nullptr) { ACLLITE_LOG_ERROR("Copy image info to device failed"); return ACLLITE_ERROR; } - AclLiteError ret = model_.CreateInput(imageDataBuf_, imageDataSize_, - imageInfoBuf_, imageInfoSize_); + AclLiteError ret = g_model_.CreateInput(g_imageDataBuf_, g_imageDataSize_, + g_imageInfoBuf_, g_imageInfoSize_); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Create mode input dataset failed"); return ACLLITE_ERROR; @@ -68,27 +65,27 @@ AclLiteError ObjectDetect::CreateInput() { } AclLiteError ObjectDetect::Init() { - //If it is already initialized, it is returned - if (isInited_) { + // If it is already initialized, it is returned + if (g_isInited_) { ACLLITE_LOG_INFO("Classify instance is initied already!"); return ACLLITE_OK; } - //Gets whether the current application is running on host or Device - AclLiteError ret = aclrtGetRunMode(&runMode_); + // Gets whether the current application is running on host or Device + AclLiteError ret = aclrtGetRunMode(&g_runMode_); if (ret != ACL_SUCCESS) { ACLLITE_LOG_ERROR("acl get run mode failed"); return ACLLITE_ERROR; } - //Initializes the model management instance - ret = model_.Init(modelPath_); + // Initializes the model management instance + ret = g_model_.Init(g_modelPath_); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Init model failed"); return ACLLITE_ERROR; } - imageDataSize_ = model_.GetModelInputSize(0); + g_imageDataSize_ = g_model_.GetModelInputSize(0); ret = CreateInput(); if (ret != ACLLITE_OK) { @@ -96,22 +93,22 @@ AclLiteError ObjectDetect::Init() { return ACLLITE_ERROR; } - isInited_ = true; + g_isInited_ = true; return ACLLITE_OK; } AclLiteError ObjectDetect::Inference(std::vector& inferenceOutput, cv::Mat& reiszeMat) { - AclLiteError ret = CopyDataToDeviceEx(imageDataBuf_, imageDataSize_, - reiszeMat.ptr(), imageDataSize_, - runMode_); + AclLiteError ret = CopyDataToDeviceEx(g_imageDataBuf_, g_imageDataSize_, + reiszeMat.ptr(), g_imageDataSize_, + g_runMode_); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Copy resized image data to device failed."); return ACLLITE_ERROR; } - //Perform reasoning - ret = model_.Execute(inferenceOutput); + // Perform reasoning + ret = g_model_.Execute(inferenceOutput); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Execute model inference failed"); return ACLLITE_ERROR; @@ -121,11 +118,10 @@ AclLiteError ObjectDetect::Inference(std::vector& inferenceOutp return ACLLITE_OK; } -void ObjectDetect::DestroyResource() -{ - model_.DestroyInput(); - model_.DestroyResource(); +void ObjectDetect::DestroyResource() { + g_model_.DestroyInput(); + g_model_.DestroyResource(); - aclrtFree(imageDataBuf_); - aclrtFree(imageInfoBuf_); + aclrtFree(g_imageDataBuf_); + aclrtFree(g_imageInfoBuf_); } diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/inc/object_detection.h b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/inc/object_detection.h index d36ad22bc..d2524dee5 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/inc/object_detection.h +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/inc/object_detection.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 ANIMEGAN_MULTI_DEVICE_ONE_VIDEO_INC_OBJECT_DETECTION_H +#define ANIMEGAN_MULTI_DEVICE_ONE_VIDEO_INC_OBJECT_DETECTION_H + #pragma once #include @@ -45,14 +46,14 @@ const int MSG_VIDEO_ENCODE = 6; const int MSG_ENCODE_FINISH = 7; const int MSG_PREPROC_END = 8; const int MSG_APP_EXIT = 10; -const std::vector kInferName = {"inference_0", "inference_1", +const std::vector g_inferName = {"inference_0", "inference_1", "inference_2", "inference_3"}; -const std::vector kPostprocName = {"postprocess_0","postprocess_1", +const std::vector g_postprocName = {"postprocess_0","postprocess_1", "postprocess_2","postprocess_3","postprocess_4","postprocess_5", "postprocess_6","postprocess_7","postprocess_8","postprocess_9", "postprocess_10","postprocess_11"}; } -const string kVideoprocName = "videoprocess_0"; +const string g_videoprocName = "videoprocess_0"; struct PreprocDataMsg { int inferThreadId; @@ -63,7 +64,7 @@ struct PreprocDataMsg { int frameNum; ImageData resizedMat; ImageData imageFrame; - cv::Mat frame; //原图 + cv::Mat frame; // 原图 }; struct InferOutputMsg { @@ -81,4 +82,6 @@ struct PostOutputMsg { cv::Mat resultImage; int isLastFrame; int frameNum; -}; \ No newline at end of file +}; + +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/scripts/AnimeGANv2_video_multi_device.conf b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/scripts/AnimeGANv2_video_multi_device.conf index b06b0479b..902014da9 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/scripts/AnimeGANv2_video_multi_device.conf +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/scripts/AnimeGANv2_video_multi_device.conf @@ -1,6 +1,6 @@ [deeplab] data_addr=../data/test_video.mp4 postprocess_num=1 -device_num=2 +device_num=1 video_height=720 video_width=1280 diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/inference/inference.cpp b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/inference/inference.cpp index f9ebb3b55..049f3a9c1 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/inference/inference.cpp +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/inference/inference.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,8 @@ * 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 "acl/acl.h" @@ -29,36 +27,36 @@ using namespace std; InferenceThread::InferenceThread(const string& modelPath, uint32_t modelWidth, uint32_t modelHeight, aclrtContext& context) : -stream_(nullptr), -context_(context), -model_(modelPath), -modelWidth_(modelWidth), -modelHeight_(modelHeight){ +g_stream_(nullptr), +g_context_(context), +g_model_(modelPath), +g_modelWidth_(modelWidth), +g_modelHeight_(modelHeight) { } InferenceThread::~InferenceThread() { - aclError ret = aclrtSetCurrentContext(context_); + aclError ret = aclrtSetCurrentContext(g_context_); if (ret != ACL_SUCCESS) { ACLLITE_LOG_ERROR("InferenceThread destructor set context failed, error: %d", ret); } - model_.DestroyResource(); + g_model_.DestroyResource(); } AclLiteError InferenceThread::Init() { - AclLiteError ret = aclrtCreateStream(&stream_); + AclLiteError ret = aclrtCreateStream(&g_stream_); if (ret != ACL_ERROR_NONE) { ACLLITE_LOG_ERROR("Create acl stream failed, error %d", ret); return ACLLITE_ERROR_CREATE_STREAM; } - ret = model_.Init(); + ret = g_model_.Init(); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Model init failed, error:%d", ret); return ret; } - ret = aclrtGetRunMode(&runMode_); + ret = aclrtGetRunMode(&g_runMode_); if (ret != ACL_ERROR_NONE) { ACLLITE_LOG_ERROR("acl get run mode failed"); return ACLLITE_ERROR; @@ -82,44 +80,40 @@ AclLiteError InferenceThread::ModelExecute(shared_ptr preprocDat inferOutputMsg->resizedMat = preprocDataMsg->resizedMat; ImageData imageDevice; - AclLiteError ret = CopyImageToDevice(imageDevice, preprocDataMsg->resizedMat, runMode_, MEMORY_DEVICE); + AclLiteError ret = CopyImageToDevice(imageDevice, preprocDataMsg->resizedMat, g_runMode_, MEMORY_DEVICE); if (ret == ACLLITE_ERROR) { ACLLITE_LOG_ERROR("Copy image to device failed"); return ACLLITE_ERROR; } - ret = model_.CreateInput(imageDevice.data.get(), + ret = g_model_.CreateInput(imageDevice.data.get(), imageDevice.size); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Create mode input dataset failed"); return ACLLITE_ERROR; } - ret = model_.Execute(inferOutputMsg->inferData); + ret = g_model_.Execute(inferOutputMsg->inferData); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Execute model inference failed, error: %d", ret); return ACLLITE_ERROR; } - model_.DestroyInput(); + g_model_.DestroyInput(); return ACLLITE_OK; } AclLiteError InferenceThread::MsgSend(shared_ptr preprocDataMsg, shared_ptr &inferOutputMsg) { - while(1) - { + while (1) { AclLiteError ret = SendMessage(preprocDataMsg->postprocThreadId,MSG_INFER_OUTPUT, inferOutputMsg); - if(ret == ACLLITE_ERROR_ENQUEUE) - { + if (ret == ACLLITE_ERROR_ENQUEUE) { usleep(500); continue; } - else if(ret == ACLLITE_OK) - { + else if (ret == ACLLITE_OK) { break; } - else - { + else { ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); return ret; } @@ -138,7 +132,7 @@ AclLiteError InferenceThread::Process(int msgId, shared_ptr data) { ModelExecute(static_pointer_cast(data), inferOutputMsg); MsgSend(static_pointer_cast(data), inferOutputMsg); clock_gettime(CLOCK_REALTIME, &time4); - //cout << "inference time is: " << (time4.tv_sec - time3.tv_sec)*1000 + (time4.tv_nsec - time3.tv_nsec)/1000000 << "ms" << endl; + // cout << "inference time is: " << (time4.tv_sec - time3.tv_sec)*1000 + (time4.tv_nsec - time3.tv_nsec)/1000000 << "ms" << endl; break; default: ACLLITE_LOG_INFO("Inference thread ignore msg %d", msgId); diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/inference/inference.h b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/inference/inference.h index d02916b28..444c4a588 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/inference/inference.h +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/inference/inference.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 ANIMEGAN_MULTI_DEVICE_ONE_VIDEO_INFERENCE_INFERENCE_H +#define ANIMEGAN_MULTI_DEVICE_ONE_VIDEO_INFERENCE_INFERENCE_H + #pragma once #include @@ -51,11 +52,12 @@ private: void DestroyResource(); private: - AclLiteModel model_; - uint32_t modelWidth_; - uint32_t modelHeight_; - aclrtRunMode runMode_; - aclrtStream stream_; - aclrtContext context_; + AclLiteModel g_model_; + uint32_t g_modelWidth_; + uint32_t g_modelHeight_; + aclrtRunMode g_runMode_; + aclrtStream g_stream_; + aclrtContext g_context_; }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/main.cpp b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/main.cpp index 4ebb6107a..efee4a13e 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/main.cpp +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/main.cpp @@ -41,19 +41,19 @@ struct timespec time9 = {0, 0}; struct timespec time10 = {0, 0}; namespace { -uint32_t kExitCount = 0; -uint32_t kModelWidth; -uint32_t kModelHeight; -const char* kModelPath; -const char* kConfigFile = "../scripts/AnimeGANv2_video_multi_device.conf"; -const string kRegexData = "^data_addr+$"; -const string kRegexPostNum = "^postprocess_num+$"; -const string kRegexVideoHeight = "^video_height+$"; -const string kRegexVideoWidth = "^video_width+$"; -const string kRegexDeviceNum = "^device_num+$"; +uint32_t g_exitCount = 0; +uint32_t g_modelWidth; +uint32_t g_modelHeight; +const char* g_modelPath; +const char* g_configFile = "../scripts/AnimeGANv2_video_multi_device.conf"; +const string g_regexData = "^data_addr+$"; +const string g_regexPostNum = "^postprocess_num+$"; +const string g_regexVideoHeight = "^video_height+$"; +const string g_regexVideoWidth = "^video_width+$"; +const string g_regexDeviceNum = "^device_num+$"; aclrtContext context; aclrtRunMode runMode; -vector kContext; +vector g_context; } int MainThreadProcess(uint32_t msgId, @@ -69,15 +69,15 @@ int MainThreadProcess(uint32_t msgId, AclLiteError ParseConfig(string& dataAddr, uint32_t& postNum, uint32_t& deviceNum, uint32_t& videoHeight, uint32_t& videoWidth) { map config; - if(!ReadConfig(config, kConfigFile)) { + if(!ReadConfig(config, g_configFile)) { return ACLLITE_ERROR; } - regex dataAddrRegex(kRegexData.c_str()); - regex postNumRegex(kRegexPostNum.c_str()); - regex videoHeightRegex(kRegexVideoHeight.c_str()); - regex videoWidthRegex(kRegexVideoWidth.c_str()); - regex deviceNumRegex(kRegexDeviceNum.c_str()); + regex dataAddrRegex(g_regexData.c_str()); + regex postNumRegex(g_regexPostNum.c_str()); + regex videoHeightRegex(g_regexVideoHeight.c_str()); + regex videoWidthRegex(g_regexVideoWidth.c_str()); + regex deviceNumRegex(g_regexDeviceNum.c_str()); map::const_iterator mIter = config.begin(); for (; mIter != config.end(); ++mIter) { printf("config item: %s=%s\n", mIter->first.c_str(), mIter->second.c_str()); @@ -115,8 +115,8 @@ void CreatePreprocessInstances(vector& threadTbl, string dataAddr, uint32_t postNum, uint32_t deviceCount, AclLiteResource& aclDev) { aclrtContext context = aclDev.GetContext(); AclLiteThreadParam param; - param.threadInst = new PreprocessThread(dataAddr, kModelWidth, - kModelHeight, postNum, deviceCount, context); + param.threadInst = new PreprocessThread(dataAddr, g_modelWidth, + g_modelHeight, postNum, deviceCount, context); param.context = aclDev.GetContext(); param.runMode = aclDev.GetRunMode(); threadTbl.push_back(param); @@ -126,9 +126,9 @@ void CreatePreprocessInstances(vector& threadTbl, void CreateInferenceInstance(vector& threadTbl, int32_t i, aclrtContext& context, aclrtRunMode& runMode) { AclLiteThreadParam param; - param.threadInst = new InferenceThread(kModelPath, kModelWidth, - kModelHeight, context); - param.threadInstName.assign(kInferName[i].c_str()); + param.threadInst = new InferenceThread(g_modelPath, g_modelWidth, + g_modelHeight, context); + param.threadInstName.assign(g_inferName[i].c_str()); param.context = context; param.runMode = runMode; threadTbl.push_back(param); @@ -141,7 +141,7 @@ void CreatePostprocessInstances(vector& threadTbl, AclLiteThreadParam param; for (int j = 0; j < postNum; j++) { param.threadInst = new PostprocessThread(videoWidth, videoHeight); - param.threadInstName.assign(kPostprocName[i*postNum+j].c_str()); + param.threadInstName.assign(g_postprocName[i*postNum+j].c_str()); param.context = context; param.runMode = runMode; threadTbl.push_back(param); @@ -154,7 +154,7 @@ void CreateVideoprocessInstance(vector& threadTbl, AclLiteThreadParam param; param.threadInst = new VideoprocessThread(videoHeight, videoWidth, postNum); - param.threadInstName.assign(kVideoprocName.c_str()); + param.threadInstName.assign(g_videoprocName.c_str()); param.context = aclDev.GetContext(); param.runMode = aclDev.GetRunMode(); threadTbl.push_back(param); @@ -173,10 +173,10 @@ void CreateThreadInstance(vector& threadTbl, AclLiteResource if (ret != ACLLITE_OK) { return; } - kExitCount = postNum * deviceNum; + g_exitCount = postNum * deviceNum; CreatePreprocessInstances(threadTbl, dataAddr, postNum, deviceNum, aclDev); - CreateVideoprocessInstance(threadTbl, videoHeight, videoWidth, kExitCount, aclDev); + CreateVideoprocessInstance(threadTbl, videoHeight, videoWidth, g_exitCount, aclDev); for(int32_t i=0; i < deviceNum; i++){ ret = aclrtSetDevice(i); @@ -191,7 +191,7 @@ void CreateThreadInstance(vector& threadTbl, AclLiteResource return; } - kContext.push_back(context); + g_context.push_back(context); CreateInferenceInstance(threadTbl, i, context, runMode); CreatePostprocessInstances(threadTbl, postNum, i, context, runMode, videoHeight, videoWidth); @@ -204,8 +204,8 @@ void ExitApp(AclLiteApp& app, vector& threadTbl) { } app.Exit(); - for(int i = 0; i < kContext.size(); i++) { - aclrtDestroyContext(kContext[i]); + for(int i = 0; i < g_context.size(); i++) { + aclrtDestroyContext(g_context[i]); if(i){ aclrtResetDevice(i); } @@ -254,14 +254,14 @@ int main(int argc, char *argv[]) { } } - kModelWidth = atoi(argv[1]); - kModelHeight = atoi(argv[1]); + g_modelWidth = atoi(argv[1]); + g_modelHeight = atoi(argv[1]); if(256 == atoi(argv[1])){ - kModelPath = "../model/AnimeGANv2_256.om"; + g_modelPath = "../model/AnimeGANv2_256.om"; }else if(512 == atoi(argv[1])){ - kModelPath = "../model/AnimeGANv2_512.om"; + g_modelPath = "../model/AnimeGANv2_512.om"; }else{ - kModelPath = "../model/AnimeGANv2_1024.om"; + g_modelPath = "../model/AnimeGANv2_1024.om"; } AclLiteResource aclDev = AclLiteResource(); diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/postprocess/postprocess.cpp b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/postprocess/postprocess.cpp index 93a3750f2..00be5a79b 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/postprocess/postprocess.cpp +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/postprocess/postprocess.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,8 @@ * 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 "acl/acl.h" #include "object_detection.h" @@ -26,7 +24,7 @@ using namespace std; PostprocessThread::PostprocessThread(uint32_t outputWidth, uint32_t outputHeight) : -outputWidth_(outputWidth), outputHeight_(outputHeight){ +g_outputWidth_(outputWidth), g_outputHeight_(outputHeight) { } PostprocessThread::~PostprocessThread() { @@ -47,7 +45,7 @@ AclLiteError PostprocessThread::Process(int msgId, shared_ptr data) { InferOutputProcess(static_pointer_cast(data), postOutputMsg); MsgSend(static_pointer_cast(data), postOutputMsg); clock_gettime(CLOCK_REALTIME, &time6); - //cout << "postprocess time is: " << (time6.tv_sec - time5.tv_sec)*1000 + (time6.tv_nsec - time5.tv_nsec)/1000000 << "ms" << endl; + // cout << "postprocess time is: " << (time6.tv_sec - time5.tv_sec)*1000 + (time6.tv_nsec - time5.tv_nsec)/1000000 << "ms" << endl; break; default: ACLLITE_LOG_INFO("PostprocessThread thread ignore msg %d", msgId); @@ -60,23 +58,19 @@ AclLiteError PostprocessThread::Process(int msgId, shared_ptr data) { AclLiteError PostprocessThread::MsgSend(shared_ptr inferMsg, shared_ptr &postOutputMsg) { int sendFlag = MSG_VIDEO_ENCODE; - if(inferMsg->isLastFrame == 1){ + if (inferMsg->isLastFrame == 1) { sendFlag = MSG_ENCODE_FINISH; } - while(1) - { + while (1) { AclLiteError ret = SendMessage(inferMsg->videoProcessThreadId, sendFlag, postOutputMsg); - if(ret == ACLLITE_ERROR_ENQUEUE) - { + if (ret == ACLLITE_ERROR_ENQUEUE) { usleep(500); continue; } - else if(ret == ACLLITE_OK) - { + else if (ret == ACLLITE_OK) { break; } - else - { + else { ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); return ret; } @@ -94,7 +88,7 @@ AclLiteError PostprocessThread::InferOutputProcess(shared_ptr in return ACLLITE_OK; void* data = inferMsg->inferData[0].data.get(); - if(data == nullptr){ + if (data == nullptr) { ACLLITE_LOG_ERROR("inferoutput is null\n"); return ACLLITE_ERROR; } diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/postprocess/postprocess.h b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/postprocess/postprocess.h index 0a45f5be6..baf900bbc 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/postprocess/postprocess.h +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/postprocess/postprocess.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 ANIMEGAN_MULTI_DEVICE_ONE_VIDEO_POSTPROCESS_POSTPROCESS_H +#define ANIMEGAN_MULTI_DEVICE_ONE_VIDEO_POSTPROCESS_POSTPROCESS_H + #pragma once #include @@ -43,7 +44,8 @@ private: shared_ptr &postOutputMsg); private: - uint32_t outputWidth_; - uint32_t outputHeight_; + uint32_t g_outputWidth_; + uint32_t g_outputHeight_; }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/preprocess/preprocess.cpp b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/preprocess/preprocess.cpp index ec713e5a4..5019da8c3 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/preprocess/preprocess.cpp +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/preprocess/preprocess.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,8 @@ * 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 "object_detection.h" @@ -30,65 +28,65 @@ struct timespec time2 = {0, 0}; PreprocessThread::PreprocessThread(string& videoPath, uint32_t modelWidth, uint32_t modelHeight, uint32_t postThreadNum, uint32_t inferThreadNum, aclrtContext& context) : -videoPath_(videoPath), -context_(context), -cap_(nullptr), -stream_(nullptr), -modelWidth_(modelWidth), -modelHeight_(modelHeight), -selfThreadId_(INVALID_INSTANCE_ID), -videoThreadId_(INVALID_INSTANCE_ID), -inferThreadNum_(inferThreadNum), -postThreadNum_(postThreadNum), -inferChannel_(0), -postChannel_(0), -indexCount_(postThreadNum - 1), -frameCnt_(0) { - for(int i = 0; i < inferThreadNum; i++){ - nextThreadId_.push_back(INVALID_INSTANCE_ID); - for(int j = 0; j < postThreadNum; j++){ - postprocThreadId_.push_back(INVALID_INSTANCE_ID); +g_videoPath_(videoPath), +g_context_(context), +g_cap_(nullptr), +g_stream_(nullptr), +g_modelWidth_(modelWidth), +g_modelHeight_(modelHeight), +g_selfThreadId_(INVALID_INSTANCE_ID), +g_videoThreadId_(INVALID_INSTANCE_ID), +g_inferThreadNum_(inferThreadNum), +g_postThreadNum_(postThreadNum), +g_inferChannel_(0), +g_postChannel_(0), +g_indexCount_(postThreadNum - 1), +g_frameCnt_(0) { + for (int i = 0; i < inferThreadNum; i++) { + g_nextThreadId_.push_back(INVALID_INSTANCE_ID); + for (int j = 0; j < postThreadNum; j++) { + g_postprocThreadId_.push_back(INVALID_INSTANCE_ID); } } } PreprocessThread::~PreprocessThread() { - aclError ret = aclrtSetCurrentContext(context_); + aclError ret = aclrtSetCurrentContext(g_context_); if (ret != ACL_SUCCESS) { ACLLITE_LOG_ERROR("PreprocessThread destructor set context failed, error: %d", ret); } - if (cap_ != nullptr) { - cap_->Close(); - delete cap_; + if (g_cap_ != nullptr) { + g_cap_->Close(); + delete g_cap_; } - dvpp_.DestroyResource(); + g_dvpp_.DestroyResource(); } AclLiteError PreprocessThread::OpenVideoCapture() { - if (IsDigitStr(videoPath_)) { - int cameraId = atoi(videoPath_.c_str()); + if (IsDigitStr(g_videoPath_)) { + int cameraId = atoi(g_videoPath_.c_str()); if ((cameraId < 0) || (cameraId >= CAMERA_ID_INVALID)) { ACLLITE_LOG_ERROR("Invalid camera id arg %s, only allow %d and %d", - videoPath_.c_str(), CAMERA_ID_0, CAMERA_ID_1); + g_videoPath_.c_str(), CAMERA_ID_0, CAMERA_ID_1); return ACLLITE_ERROR; } - cap_ = new AclLiteVideoProc(cameraId); - } else if (IsRtspAddr(videoPath_)) { - cap_ = new AclLiteVideoProc(videoPath_); - } else if (IsVideoFile(videoPath_)) { - if (!IsPathExist(videoPath_)) { - ACLLITE_LOG_ERROR("The %s is inaccessible", videoPath_.c_str()); + g_cap_ = new AclLiteVideoProc(cameraId); + } else if (IsRtspAddr(g_videoPath_)) { + g_cap_ = new AclLiteVideoProc(g_videoPath_); + } else if (IsVideoFile(g_videoPath_)) { + if (!IsPathExist(g_videoPath_)) { + ACLLITE_LOG_ERROR("The %s is inaccessible", g_videoPath_.c_str()); return ACLLITE_ERROR; } - cap_ = new AclLiteVideoProc(videoPath_); + g_cap_ = new AclLiteVideoProc(g_videoPath_); } else { ACLLITE_LOG_ERROR("Invalid param. The arg should be accessible rtsp," " video file or camera id"); return ACLLITE_ERROR; } - if(!cap_->IsOpened()) { - delete cap_; + if (!g_cap_->IsOpened()) { + delete g_cap_; ACLLITE_LOG_ERROR("Failed to open video"); return ACLLITE_ERROR; } @@ -102,41 +100,39 @@ AclLiteError PreprocessThread::Init() { return ACLLITE_ERROR; } - aclError aclRet = aclrtGetRunMode(&runMode_); + aclError aclRet = aclrtGetRunMode(&g_runMode_); if (aclRet != ACL_ERROR_NONE) { ACLLITE_LOG_ERROR("acl get run mode failed"); return ACLLITE_ERROR_GET_RUM_MODE; } - aclRet = dvpp_.Init(); + aclRet = g_dvpp_.Init(); if (aclRet) { ACLLITE_LOG_ERROR("Dvpp init failed, error %d", aclRet); return ACLLITE_ERROR; } - //Get the relevant thread instance id - selfThreadId_ = SelfInstanceId(); - videoThreadId_ = GetAclLiteThreadIdByName(kVideoprocName); - if ((selfThreadId_ == INVALID_INSTANCE_ID) || - (videoThreadId_ == INVALID_INSTANCE_ID)) { - ACLLITE_LOG_ERROR("Self instance id %d, video process instance id %d", selfThreadId_, videoThreadId_); + // Get the relevant thread instance id + g_selfThreadId_ = SelfInstanceId(); + g_videoThreadId_ = GetAclLiteThreadIdByName(g_videoprocName); + if ((g_selfThreadId_ == INVALID_INSTANCE_ID) || + (g_videoThreadId_ == INVALID_INSTANCE_ID)) { + ACLLITE_LOG_ERROR("Self instance id %d, video process instance id %d", g_selfThreadId_, g_videoThreadId_); return ACLLITE_ERROR; } - for(int i = 0; i < inferThreadNum_; i++) - { - nextThreadId_[i] = GetAclLiteThreadIdByName(kInferName[i]); - if (nextThreadId_[i] == INVALID_INSTANCE_ID){ + for (int i = 0; i < g_inferThreadNum_; i++) { + g_nextThreadId_[i] = GetAclLiteThreadIdByName(g_inferName[i]); + if (g_nextThreadId_[i] == INVALID_INSTANCE_ID) { ACLLITE_LOG_ERROR("%d inference instance id %d", - i, nextThreadId_[i]); + i, g_nextThreadId_[i]); return ACLLITE_ERROR; } - for(int j = 0; j < postThreadNum_; j++) - { - postprocThreadId_[i*postThreadNum_+j] = GetAclLiteThreadIdByName(kPostprocName[i*postThreadNum_+j]); - if (postprocThreadId_[i*postThreadNum_+j] == INVALID_INSTANCE_ID){ + for (int j = 0; j < g_postThreadNum_; j++) { + g_postprocThreadId_[i*g_postThreadNum_+j] = GetAclLiteThreadIdByName(g_postprocName[i*g_postThreadNum_+j]); + if (g_postprocThreadId_[i*g_postThreadNum_+j] == INVALID_INSTANCE_ID) { ACLLITE_LOG_ERROR("%d postprocess instance id %d", - i*postThreadNum_+j, postprocThreadId_[i*postThreadNum_+j]); + i*g_postThreadNum_+j, g_postprocThreadId_[i*g_postThreadNum_+j]); return ACLLITE_ERROR; } } @@ -159,7 +155,7 @@ AclLiteError PreprocessThread::Process(int msgId, ret = MsgProcess(preprocDataMsg); ret = MsgSend(preprocDataMsg); clock_gettime(CLOCK_REALTIME, &time2); - //cout << "preprocess time is: " << (time2.tv_sec - time1.tv_sec)*1000 + (time2.tv_nsec - time1.tv_nsec)/1000000 << "ms" << endl; + // cout << "preprocess time is: " << (time2.tv_sec - time1.tv_sec)*1000 + (time2.tv_nsec - time1.tv_nsec)/1000000 << "ms" << endl; break; default: ACLLITE_LOG_ERROR("Preprocess thread receive unknow msg %d", msgId); @@ -170,7 +166,7 @@ AclLiteError PreprocessThread::Process(int msgId, } AclLiteError PreprocessThread::AppStart() { - AclLiteError ret = SendMessage(selfThreadId_, MSG_READ_FRAME, nullptr); + AclLiteError ret = SendMessage(g_selfThreadId_, MSG_READ_FRAME, nullptr); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Process app start message failed, error %d", ret); } @@ -179,52 +175,52 @@ AclLiteError PreprocessThread::AppStart() { } AclLiteError PreprocessThread::ReadFrame(shared_ptr &preprocDataMsg) { - inferChannel_ = frameCnt_ % inferThreadNum_; - if(inferChannel_ == 0){ - indexCount_ = (++indexCount_) % postThreadNum_; + g_inferChannel_ = g_frameCnt_ % g_inferThreadNum_; + if (g_inferChannel_ == 0) { + g_indexCount_ = (++g_indexCount_) % g_postThreadNum_; } - postChannel_ = inferChannel_ * postThreadNum_ + indexCount_; + g_postChannel_ = g_inferChannel_ * g_postThreadNum_ + g_indexCount_; - preprocDataMsg->inferThreadId = nextThreadId_[inferChannel_]; - preprocDataMsg->postprocThreadId = postprocThreadId_[postChannel_]; - preprocDataMsg->videoProcessThreadId = videoThreadId_; - preprocDataMsg->channelId = postChannel_; - preprocDataMsg->frameNum = frameCnt_; + preprocDataMsg->inferThreadId = g_nextThreadId_[g_inferChannel_]; + preprocDataMsg->postprocThreadId = g_postprocThreadId_[g_postChannel_]; + preprocDataMsg->videoProcessThreadId = g_videoThreadId_; + preprocDataMsg->channelId = g_postChannel_; + preprocDataMsg->frameNum = g_frameCnt_; preprocDataMsg->isLastFrame = 0; - AclLiteError ret = cap_->Read(preprocDataMsg->imageFrame); + AclLiteError ret = g_cap_->Read(preprocDataMsg->imageFrame); if (ret != ACLLITE_OK) { preprocDataMsg->isLastFrame = 1; return ret; } - frameCnt_++; + g_frameCnt_++; return ACLLITE_OK; } AclLiteError PreprocessThread::MsgProcess(shared_ptr &preprocDataMsg) { - if(preprocDataMsg->isLastFrame == 1) + if (preprocDataMsg->isLastFrame == 1) return ACLLITE_OK; ImageData imageDevice; - AclLiteError ret = CopyImageToDevice(imageDevice, preprocDataMsg->imageFrame, runMode_, MEMORY_DVPP); + AclLiteError ret = CopyImageToDevice(imageDevice, preprocDataMsg->imageFrame, g_runMode_, MEMORY_DVPP); if (ret == ACLLITE_ERROR) { ACLLITE_LOG_ERROR("Copy image to device failed"); return ACLLITE_ERROR; } ImageData resizedImage; - ret = dvpp_.Resize(resizedImage, imageDevice, modelWidth_, modelHeight_); + ret = g_dvpp_.Resize(resizedImage, imageDevice, g_modelWidth_, g_modelHeight_); if (ret == ACLLITE_ERROR) { - ACLLITE_LOG_ERROR("dvpp_cropandpaste image failed"); + ACLLITE_LOG_ERROR("g_dvpp_cropandpaste image failed"); return ACLLITE_ERROR; } - ret = CopyImageToLocal(preprocDataMsg->resizedMat, resizedImage, runMode_); + ret = CopyImageToLocal(preprocDataMsg->resizedMat, resizedImage, g_runMode_); if (ret == ACLLITE_ERROR) { ACLLITE_LOG_ERROR("Copy image to host failed"); return ACLLITE_ERROR; } ImageData yuvImage; - ret = CopyImageToLocal(yuvImage, preprocDataMsg->imageFrame, runMode_); + ret = CopyImageToLocal(yuvImage, preprocDataMsg->imageFrame, g_runMode_); if (ret == ACLLITE_ERROR) { ACLLITE_LOG_ERROR("Copy image to host failed"); return ACLLITE_ERROR; @@ -237,56 +233,48 @@ AclLiteError PreprocessThread::MsgProcess(shared_ptr &preprocDat AclLiteError PreprocessThread::MsgSend(shared_ptr &preprocDataMsg) { AclLiteError ret; - if(preprocDataMsg->isLastFrame == 0){ - while(1){ + if (preprocDataMsg->isLastFrame == 0) { + while (1) { ret = SendMessage(preprocDataMsg->inferThreadId, MSG_PREPROC_DATA, preprocDataMsg); - if(ret == ACLLITE_ERROR_ENQUEUE) - { + if (ret == ACLLITE_ERROR_ENQUEUE) { usleep(500); continue; } - else if(ret == ACLLITE_OK) - { + else if (ret == ACLLITE_OK) { break; } - else - { + else { ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); return ret; } } - ret = SendMessage(selfThreadId_, MSG_READ_FRAME, nullptr); + ret = SendMessage(g_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 < inferThreadNum_; i++) - { - for(int j = 0; j < postThreadNum_; j++){ + else { + for (int i = 0; i < g_inferThreadNum_; i++) { + for (int j = 0; j < g_postThreadNum_; j++) { shared_ptr preprocDataMsgEnd = make_shared(); preprocDataMsgEnd->videoProcessThreadId = preprocDataMsg->videoProcessThreadId; preprocDataMsgEnd->channelId = preprocDataMsg->channelId; preprocDataMsgEnd->frameNum = preprocDataMsg->frameNum; preprocDataMsgEnd->isLastFrame = preprocDataMsg->isLastFrame; - preprocDataMsgEnd->inferThreadId = nextThreadId_[i]; - preprocDataMsgEnd->postprocThreadId = postprocThreadId_[i*postThreadNum_+j]; - while(1) - { + preprocDataMsgEnd->inferThreadId = g_nextThreadId_[i]; + preprocDataMsgEnd->postprocThreadId = g_postprocThreadId_[i*g_postThreadNum_+j]; + while (1) { ret = SendMessage(preprocDataMsgEnd->inferThreadId, MSG_PREPROC_DATA, preprocDataMsgEnd); - if(ret == ACLLITE_ERROR_ENQUEUE) - { + if (ret == ACLLITE_ERROR_ENQUEUE) { usleep(500); continue; } - else if(ret == ACLLITE_OK) - { + else if (ret == ACLLITE_OK) { break; } - else - { + else { ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); return ret; } diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/preprocess/preprocess.h b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/preprocess/preprocess.h index a8ab258e2..0cc68d195 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/preprocess/preprocess.h +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/preprocess/preprocess.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 ANIMEGAN_MULTI_DEVICE_ONE_VIDEO_PREPROCESS_PREPROCESS_H +#define ANIMEGAN_MULTI_DEVICE_ONE_VIDEO_PREPROCESS_PREPROCESS_H + #pragma once #include #include @@ -44,24 +45,25 @@ private: AclLiteError ReadFrame(shared_ptr &preprocDataMsg); private: - string videoPath_; - AclLiteVideoProc* cap_; - aclrtStream stream_; - aclrtRunMode runMode_; - uint32_t modelWidth_; - uint32_t modelHeight_; - int selfThreadId_; - int videoThreadId_; - vector nextThreadId_; - vector postprocThreadId_; - uint32_t inferThreadNum_; - uint32_t postThreadNum_; - int frameCnt_; - AclLiteImageProc dvpp_; + string g_videoPath_; + AclLiteVideoProc* g_cap_; + aclrtStream g_stream_; + aclrtRunMode g_runMode_; + uint32_t g_modelWidth_; + uint32_t g_modelHeight_; + int g_selfThreadId_; + int g_videoThreadId_; + vector g_nextThreadId_; + vector g_postprocThreadId_; + uint32_t g_inferThreadNum_; + uint32_t g_postThreadNum_; + int g_frameCnt_; + AclLiteImageProc g_dvpp_; - int inferChannel_; - int postChannel_; - int indexCount_; - aclrtContext context_; + int g_inferChannel_; + int g_postChannel_; + int g_indexCount_; + aclrtContext g_context_; }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/videoprocess/videoprocess.cpp b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/videoprocess/videoprocess.cpp index c90738759..bd446aec3 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/videoprocess/videoprocess.cpp +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/videoprocess/videoprocess.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,8 @@ * 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 @@ -30,19 +28,19 @@ struct timespec time7 = {0, 0}; struct timespec time8 = {0, 0}; VideoprocessThread::VideoprocessThread(int videoHeight, int videoWidth, int postNum) : -videoWidth_(videoWidth), -videoHeight_(videoHeight), -postNum_(postNum), -shutdown_(0){ +g_videoWidth_(videoWidth), +g_videoHeight_(videoHeight), +g_postNum_(postNum), +g_shutdown_(0){ } VideoprocessThread::~VideoprocessThread() { } AclLiteError VideoprocessThread::Init() { - outputVideoPath_ = "./out_test_video.mp4"; - cout << "videoWidth_ and videoHeight_ is" << " " << videoWidth_ << " " << videoHeight_ << endl; - outputVideo_.open(outputVideoPath_, cv::VideoWriter::fourcc('m', 'p', '4', 'v'), 29.0, cv::Size(videoWidth_,videoHeight_)); + g_outputVideoPath_ = "./output/out_test_video.mp4"; + cout << "g_videoWidth_ and g_videoHeight_ is" << " " << g_videoWidth_ << " " << g_videoHeight_ << endl; + g_outputVideo_.open(g_outputVideoPath_, cv::VideoWriter::fourcc('m', 'p', '4', 'v'), 29.0, cv::Size(g_videoWidth_,g_videoHeight_)); return ACLLITE_OK; } @@ -59,9 +57,9 @@ AclLiteError VideoprocessThread::Process(int msgId, shared_ptr data) { break; case MSG_ENCODE_FINISH: clock_gettime(CLOCK_REALTIME, &time7); - shutdown_++; - cout << "shutdown_ is " << shutdown_ << endl; - if(shutdown_ == postNum_){ + g_shutdown_++; + cout << "g_shutdown_ is " << g_shutdown_ << endl; + if(g_shutdown_ == g_postNum_){ ShutDownProcess(); SendMessage(g_MainThreadId, MSG_APP_EXIT, nullptr); } @@ -77,11 +75,11 @@ AclLiteError VideoprocessThread::Process(int msgId, shared_ptr data) { AclLiteError VideoprocessThread::ShutDownProcess() { int flag = 0; - shared_ptr outputData[postNum_]; - int noEmptyQueue[postNum_]; - for(int i = 0; i < postNum_; i++) + shared_ptr outputData[g_postNum_]; + int noEmptyQueue[g_postNum_]; + for(int i = 0; i < g_postNum_; i++) { - if(!postqueue_[i].empty()){ + if(!g_postQueue_[i].empty()){ noEmptyQueue[flag] = i; flag++; } @@ -90,17 +88,17 @@ AclLiteError VideoprocessThread::ShutDownProcess() { { for(int i = 0; i < flag; i++) { - outputData[i] = postqueue_[noEmptyQueue[i]].front(); - postqueue_[i].pop(); + outputData[i] = g_postQueue_[noEmptyQueue[i]].front(); + g_postQueue_[i].pop(); } quick_sort(outputData, 0, flag-1); for(int i = 0; i < flag; i++) { cout << "Current frame num is: " << outputData[i]->frameNum << endl; - outputVideo_ << outputData[i]->resultImage; + g_outputVideo_ << outputData[i]->resultImage; } } - outputVideo_.release(); + g_outputVideo_.release(); return ACLLITE_OK; } @@ -110,7 +108,7 @@ AclLiteError VideoprocessThread::RecordQueue(shared_ptr postOutpu ACLLITE_LOG_ERROR("Support up to 16 channels of post-processing."); return ACLLITE_ERROR; } - postqueue_[postOutputMsg->channelId].push(postOutputMsg); + g_postQueue_[postOutputMsg->channelId].push(postOutputMsg); return ACLLITE_OK; } @@ -139,24 +137,24 @@ AclLiteError VideoprocessThread::quick_sort(shared_ptr outputData AclLiteError VideoprocessThread::DataProcess() { int flag = 0; - shared_ptr outputData[postNum_]; - for(int i = 0; i < postNum_; i++) + shared_ptr outputData[g_postNum_]; + for(int i = 0; i < g_postNum_; i++) { - if(!postqueue_[i].empty()) + if(!g_postQueue_[i].empty()) flag++; } - if(flag == postNum_) + if(flag == g_postNum_) { - for(int i = 0; i < postNum_; i++) + for(int i = 0; i < g_postNum_; i++) { - outputData[i] = postqueue_[i].front(); - postqueue_[i].pop(); + outputData[i] = g_postQueue_[i].front(); + g_postQueue_[i].pop(); } - quick_sort(outputData, 0, postNum_-1); - for(int i = 0; i < postNum_; i++) + quick_sort(outputData, 0, g_postNum_-1); + for(int i = 0; i < g_postNum_; i++) { cout <<"Current frame num is: " << outputData[i]->frameNum << endl; - outputVideo_ << outputData[i]->resultImage; + g_outputVideo_ << outputData[i]->resultImage; } } return ACLLITE_OK; diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/videoprocess/videoprocess.h b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/videoprocess/videoprocess.h index e217a7b8d..0d41c9a9f 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/videoprocess/videoprocess.h +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/videoprocess/videoprocess.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 ANIMEGAN_MULTI_DEVICE_ONE_VIDEO_VIDEOPROCESS_VIDEOPROCESS_H +#define ANIMEGAN_MULTI_DEVICE_ONE_VIDEO_VIDEOPROCESS_VIDEOPROCESS_H + #pragma once #include @@ -46,12 +47,13 @@ private: AclLiteError DataProcess(); private: - int videoWidth_; - int videoHeight_; - int postNum_; - int shutdown_; - string outputVideoPath_; - cv::VideoWriter outputVideo_; - queue> postqueue_[16]; + int g_videoWidth_; + int g_videoHeight_; + int g_postNum_; + int g_shutdown_; + string g_outputVideoPath_; + cv::VideoWriter g_outputVideo_; + queue> g_postQueue_[16]; }; +#endif \ No newline at end of file -- Gitee From 5a57347417e1ee921750fdc5e2142a3578bed789 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E8=82=96=E9=98=B3?= <1971168308@qq.com> Date: Fri, 30 Sep 2022 07:57:30 +0000 Subject: [PATCH 04/14] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20model?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../animeGAN_multi_device_one_video/model/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/model/.keep diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/model/.keep b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/model/.keep new file mode 100644 index 000000000..e69de29bb -- Gitee From d6675cd1baadd5f309ed10257f4b1512b10e6f99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E8=82=96=E9=98=B3?= <1971168308@qq.com> Date: Fri, 30 Sep 2022 07:57:46 +0000 Subject: [PATCH 05/14] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20data?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../animeGAN_multi_device_one_video/data/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/data/.keep diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/data/.keep b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/data/.keep new file mode 100644 index 000000000..e69de29bb -- Gitee From 2c469d20f07907a2a0e2c8d8d526d0201699ba2e Mon Sep 17 00:00:00 2001 From: yy0131 <1595992831@qq.com> Date: Fri, 30 Sep 2022 17:23:41 +0800 Subject: [PATCH 06/14] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../inc/object_detection.h | 16 ++- .../src/inference/inference.cpp | 60 ++++----- .../src/inference/inference.h | 24 ++-- .../src/main.cpp | 76 ++++++----- .../src/postprocess/postprocess.cpp | 37 +++--- .../src/postprocess/postprocess.h | 18 +-- .../src/preprocess/preprocess.cpp | 121 +++++++++--------- .../src/preprocess/preprocess.h | 42 +++--- 8 files changed, 190 insertions(+), 204 deletions(-) diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/inc/object_detection.h b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/inc/object_detection.h index b5e219ecb..8677bd23b 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/inc/object_detection.h +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/inc/object_detection.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 DEEPLABV3_MULTI_THREAD_MULTI_DEVICE_INC_OBJECT_DETECTION_H +#define DEEPLABV3_MULTI_THREAD_MULTI_DEVICE_INC_OBJECT_DETECTION_H + #pragma once #include @@ -45,9 +46,9 @@ const int MSG_VIDEO_ENCODE = 6; const int MSG_ENCODE_FINISH = 7; const int MSG_PREPROC_END = 8; const int MSG_APP_EXIT = 10; -const std::vector kInferName = {"inference_0", "inference_1", +const std::vector g_inferName = {"inference_0", "inference_1", "inference_2", "inference_3"}; -const std::vector kPostprocName = {"postprocess_0","postprocess_1", +const std::vector g_postprocName = {"postprocess_0","postprocess_1", "postprocess_2","postprocess_3","postprocess_4","postprocess_5", "postprocess_6","postprocess_7","postprocess_8","postprocess_9", "postprocess_10","postprocess_11"}; @@ -69,3 +70,4 @@ struct InferOutputMsg { string imageFileName; }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/inference/inference.cpp b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/inference/inference.cpp index ec2e19b8b..7f2328fb2 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/inference/inference.cpp +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/inference/inference.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,8 @@ * 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 "acl/acl.h" @@ -29,36 +27,36 @@ using namespace std; InferenceThread::InferenceThread(const string& modelPath, uint32_t modelWidth, uint32_t modelHeight, aclrtContext& context) : -stream_(nullptr), -context_(context), -model_(modelPath), -modelWidth_(modelWidth), -modelHeight_(modelHeight){ +g_stream_(nullptr), +g_context_(context), +g_model_(modelPath), +g_modelWidth_(modelWidth), +g_modelHeight_(modelHeight) { } InferenceThread::~InferenceThread() { - aclError ret = aclrtSetCurrentContext(context_); + aclError ret = aclrtSetCurrentContext(g_context_); if (ret != ACL_SUCCESS) { ACLLITE_LOG_ERROR("InferenceThread destructor set context failed, error: %d", ret); } - model_.DestroyResource(); + g_model_.DestroyResource(); } AclLiteError InferenceThread::Init() { - AclLiteError ret = aclrtCreateStream(&stream_); + AclLiteError ret = aclrtCreateStream(&g_stream_); if (ret != ACL_ERROR_NONE) { ACLLITE_LOG_ERROR("Create acl stream failed, error %d", ret); return ACLLITE_ERROR_CREATE_STREAM; } - ret = model_.Init(); + ret = g_model_.Init(); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Model init failed, error:%d", ret); return ret; } - ret = aclrtGetRunMode(&runMode_); + ret = aclrtGetRunMode(&g_runMode_); if (ret != ACL_ERROR_NONE) { ACLLITE_LOG_ERROR("acl get run mode failed"); return ACLLITE_ERROR; @@ -74,44 +72,40 @@ AclLiteError InferenceThread::ModelExecute(shared_ptr preprocDat inferOutputMsg->frameNum = preprocDataMsg->frameNum; inferOutputMsg->imageFileName = preprocDataMsg->imageFileName; ImageData imageDevice; - AclLiteError ret = CopyImageToDevice(imageDevice, preprocDataMsg->resizedMat, runMode_, MEMORY_DEVICE); + AclLiteError ret = CopyImageToDevice(imageDevice, preprocDataMsg->resizedMat, g_runMode_, MEMORY_DEVICE); if (ret == ACLLITE_ERROR) { ACLLITE_LOG_ERROR("Copy image to device failed"); return ACLLITE_ERROR; } - ret = model_.CreateInput(imageDevice.data.get(), + ret = g_model_.CreateInput(imageDevice.data.get(), imageDevice.size); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Create mode input dataset failed"); return ACLLITE_ERROR; } - ret = model_.Execute(inferOutputMsg->inferData); + ret = g_model_.Execute(inferOutputMsg->inferData); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Execute model inference failed, error: %d", ret); return ACLLITE_ERROR; } - model_.DestroyInput(); + g_model_.DestroyInput(); return ACLLITE_OK; } AclLiteError InferenceThread::MsgSend(shared_ptr preprocDataMsg, shared_ptr &inferOutputMsg) { - while(1) - { + while (1) { AclLiteError ret = SendMessage(preprocDataMsg->postprocThreadId,MSG_INFER_OUTPUT, inferOutputMsg); - if(ret == ACLLITE_ERROR_ENQUEUE) - { + if (ret == ACLLITE_ERROR_ENQUEUE) { usleep(500); continue; } - else if(ret == ACLLITE_OK) - { + else if (ret == ACLLITE_OK) { break; } - else - { + else { ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); return ret; } @@ -121,20 +115,16 @@ AclLiteError InferenceThread::MsgSend(shared_ptr preprocDataMsg, } AclLiteError InferenceThread::MsgSendEnd(shared_ptr preprocDataMsg) { - while(1) - { + while (1) { AclLiteError ret = SendMessage(preprocDataMsg->postprocThreadId, MSG_ENCODE_FINISH, nullptr); - if(ret == ACLLITE_ERROR_ENQUEUE) - { + if (ret == ACLLITE_ERROR_ENQUEUE) { usleep(500); continue; } - else if(ret == ACLLITE_OK) - { + else if (ret == ACLLITE_OK) { break; } - else - { + else { ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); return ret; } diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/inference/inference.h b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/inference/inference.h index 7a794f21d..032825c90 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/inference/inference.h +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/inference/inference.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 DEEPLABV3_MULTI_THREAD_MULTI_DEVICE_INFERENCE_INFERENCE_H +#define DEEPLABV3_MULTI_THREAD_MULTI_DEVICE_INFERENCE_INFERENCE_H + #pragma once #include @@ -51,11 +52,12 @@ private: void DestroyResource(); private: - AclLiteModel model_; - uint32_t modelWidth_; - uint32_t modelHeight_; - aclrtRunMode runMode_; - aclrtStream stream_; - aclrtContext context_; + AclLiteModel g_model_; + uint32_t g_modelWidth_; + uint32_t g_modelHeight_; + aclrtRunMode g_runMode_; + aclrtStream g_stream_; + aclrtContext g_context_; }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/main.cpp b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/main.cpp index 36c69413e..6e0ce2bbd 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/main.cpp +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/main.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,9 +12,6 @@ * 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 main.cpp -* Description: dvpp sample main func */ #include @@ -40,31 +37,31 @@ struct timespec time9 = {0, 0}; struct timespec time10 = {0, 0}; namespace { -uint32_t kExitCount = 0; -uint32_t kModelWidth = 514; -uint32_t kModelHeight = 514; -const char* kModelPath = "../model/deeplab_quant.om"; -const char* kConfigFile = "../scripts/deeplab_multi_thread.conf"; -const string kRegexData = "^data_addr+$"; -const string kRegexPostNum = "^postprocess_num+$"; -const string kRegexOutputHeight = "^output_height+$"; -const string kRegexOutputWidth = "^output_width+$"; -const string kRegexDeviceNum = "^device_num+$"; +uint32_t g_exitCount = 0; +uint32_t g_modelWidth = 514; +uint32_t g_modelHeight = 514; +const char* g_modelPath = "../model/deeplab_quant.om"; +const char* g_configFile = "../scripts/deeplab_multi_thread.conf"; +const string g_regexData = "^data_addr+$"; +const string g_regexPostNum = "^postprocess_num+$"; +const string g_regexOutputHeight = "^output_height+$"; +const string g_regexOutputWidth = "^output_width+$"; +const string g_regexDeviceNum = "^device_num+$"; aclrtContext context; aclrtRunMode runMode; -vector kContext; +vector g_context; } int MainThreadProcess(uint32_t msgId, shared_ptr msgData, void* userData) { if (msgId == MSG_APP_EXIT) { - kExitCount--; + g_exitCount--; } - else + else { return ACLLITE_ERROR; - ACLLITE_LOG_INFO("kExitCount is %d", kExitCount); - if(kExitCount == 0) - { + } + ACLLITE_LOG_INFO("g_exitCount is %d", g_exitCount); + if (g_exitCount == 0) { AclLiteApp& app = GetAclLiteAppInstance(); app.WaitEnd(); ACLLITE_LOG_INFO("Receive exit message, exit now"); @@ -76,15 +73,15 @@ int MainThreadProcess(uint32_t msgId, AclLiteError ParseConfig(string& dataAddr, uint32_t& postNum, uint32_t& deviceNum, uint32_t& outputHeight, uint32_t& outputWidth) { map config; - if(!ReadConfig(config, kConfigFile)) { + if (!ReadConfig(config, g_configFile)) { return ACLLITE_ERROR; } - regex dataAddrRegex(kRegexData.c_str()); - regex postNumRegex(kRegexPostNum.c_str()); - regex outputHeightRegex(kRegexOutputHeight.c_str()); - regex outputWidthRegex(kRegexOutputWidth.c_str()); - regex deviceNumRegex(kRegexDeviceNum.c_str()); + regex dataAddrRegex(g_regexData.c_str()); + regex postNumRegex(g_regexPostNum.c_str()); + regex outputHeightRegex(g_regexOutputHeight.c_str()); + regex outputWidthRegex(g_regexOutputWidth.c_str()); + regex deviceNumRegex(g_regexDeviceNum.c_str()); map::const_iterator mIter = config.begin(); for (; mIter != config.end(); ++mIter) { printf("config item: %s=%s\n", mIter->first.c_str(), mIter->second.c_str()); @@ -122,8 +119,8 @@ void CreatePreprocessInstances(vector& threadTbl, string dataAddr, uint32_t postNum, uint32_t deviceCount, AclLiteResource& aclDev) { aclrtContext context = aclDev.GetContext(); AclLiteThreadParam param; - param.threadInst = new PreprocessThread(dataAddr, kModelWidth, - kModelHeight, postNum, deviceCount, context); + param.threadInst = new PreprocessThread(dataAddr, g_modelWidth, + g_modelHeight, postNum, deviceCount, context); param.context = aclDev.GetContext(); param.runMode = aclDev.GetRunMode(); ACLLITE_LOG_INFO("Preprocess context is :%p", param.context); @@ -134,9 +131,9 @@ void CreatePreprocessInstances(vector& threadTbl, void CreateInferenceInstance(vector& threadTbl, int32_t i, aclrtContext& context, aclrtRunMode& runMode) { AclLiteThreadParam param; - param.threadInst = new InferenceThread(kModelPath, kModelWidth, - kModelHeight, context); - param.threadInstName.assign(kInferName[i].c_str()); + param.threadInst = new InferenceThread(g_modelPath, g_modelWidth, + g_modelHeight, context); + param.threadInstName.assign(g_inferName[i].c_str()); param.context = context; param.runMode = runMode; ACLLITE_LOG_INFO("Inference context is :%p", param.context); @@ -150,7 +147,7 @@ void CreatePostprocessInstances(vector& threadTbl, AclLiteThreadParam param; for (int j = 0; j < postNum; j++) { param.threadInst = new PostprocessThread(outputWidth, outputHeight); - param.threadInstName.assign(kPostprocName[i*postNum+j].c_str()); + param.threadInstName.assign(g_postprocName[i*postNum+j].c_str()); param.context = context; param.runMode = runMode; ACLLITE_LOG_INFO("Postprocess context is :%p", param.context); @@ -170,7 +167,7 @@ void CreateThreadInstance(vector& threadTbl, AclLiteResource return; } - kExitCount = postNum * deviceNum; + g_exitCount = postNum * deviceNum; ret = aclrtGetRunMode(&runMode); if (ret != ACL_ERROR_NONE) { ACLLITE_LOG_ERROR("acl get run mode failed"); @@ -178,7 +175,7 @@ void CreateThreadInstance(vector& threadTbl, AclLiteResource } CreatePreprocessInstances(threadTbl, dataAddr, postNum, deviceNum, aclDev); - for(int32_t i=0; i < deviceNum; i++){ + for (int32_t i=0; i < deviceNum; i++) { ret = aclrtSetDevice(i); if (ret != ACL_ERROR_NONE) { ACLLITE_LOG_ERROR("Acl open device %d failed", i); @@ -190,7 +187,7 @@ void CreateThreadInstance(vector& threadTbl, AclLiteResource ACLLITE_LOG_ERROR("Create acl context failed, error:%d", ret); return; } - kContext.push_back(context); + g_context.push_back(context); CreateInferenceInstance(threadTbl, i, context, runMode); CreatePostprocessInstances(threadTbl, postNum, i, context, runMode, outputHeight, outputWidth); @@ -202,9 +199,9 @@ void ExitApp(AclLiteApp& app, vector& threadTbl) { delete threadTbl[i].threadInst; } app.Exit(); - for(int i = 0; i < kContext.size(); i++) { - aclrtDestroyContext(kContext[i]); - if(i){ + for (int i = 0; i < g_context.size(); i++) { + aclrtDestroyContext(g_context[i]); + if (i) { aclrtResetDevice(i); } } @@ -235,7 +232,6 @@ void StartApp(AclLiteResource& aclDev) { return; } - int main(int argc, char *argv[]) { AclLiteResource aclDev = AclLiteResource(0, "", false); AclLiteError ret = aclDev.Init(); diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/postprocess/postprocess.cpp b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/postprocess/postprocess.cpp index 6cea5e2f9..c54725acb 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/postprocess/postprocess.cpp +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/postprocess/postprocess.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,8 @@ * 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 "acl/acl.h" #include "object_detection.h" @@ -38,7 +36,7 @@ namespace { } PostprocessThread::PostprocessThread(uint32_t outputWidth, uint32_t outputHeight) : -outputWidth_(outputWidth), outputHeight_(outputHeight){ +g_outputWidth_(outputWidth), g_outputHeight_(outputHeight) { } PostprocessThread::~PostprocessThread() { @@ -74,39 +72,39 @@ AclLiteError PostprocessThread::Process(int msgId, shared_ptr data) { AclLiteError PostprocessThread::InferOutputProcess(shared_ptr inferMsg) { float* data = (float *)inferMsg->inferData[0].data.get(); - if(data == nullptr){ + if (data == nullptr) { ACLLITE_LOG_ERROR("inferoutput is null\n"); return ACLLITE_ERROR; } uint32_t dataSize = inferMsg->inferData[0].size; uint32_t size = static_cast(dataSize) / sizeof(float); - //读原图宽高 + // 读原图宽高 cv::Mat originImage = cv::imread(inferMsg->imageFileName, CV_LOAD_IMAGE_COLOR); cv::Mat resizeImage; - cv::resize(originImage, resizeImage, cv::Size(outputWidth_, outputHeight_)); + cv::resize(originImage, resizeImage, cv::Size(g_outputWidth_, g_outputHeight_)); int pos = inferMsg->imageFileName.find_last_of("/"); string filename(inferMsg->imageFileName.substr(pos + 1)); cv::Mat mat_b_up, mat_g_up, mat_r_up; - //对每一张单通道图 - for(int i = 1; i <21; i++){ - //保存为单通道图 - for(int j=i * (size / 21); j < (i + 1) * (size / 21); j++){ - if(data[j] > 0.5){ - //data[j] = kColors[i % kColors.size()]; + // 对每一张单通道图 + for (int i = 1; i < 21; i++) { + // 保存为单通道图 + for (int j = i * (size / 21); j < (i + 1) * (size / 21); j++) { + if (data[j] > 0.5) { + // data[j] = kColors[i % kColors.size()]; data[j] = 1; } - else{ + else { data[j] = 0; } } cv::Mat mat_a(513, 513, CV_32FC1, const_cast((float*)data) + (size / 21)*i); int iVal255 = cv::countNonZero(mat_a); - if (iVal255){ - cv::Mat mat_a_up(outputHeight_, outputWidth_, CV_32FC1); - cv::resize(mat_a, mat_a_up, cv::Size(outputWidth_, outputHeight_)); //现在拿到一个原图掩码 + if (iVal255) { + cv::Mat mat_a_up(g_outputHeight_, g_outputWidth_, CV_32FC1); + cv::resize(mat_a, mat_a_up, cv::Size(g_outputWidth_, g_outputHeight_)); // 现在拿到一个原图掩码 cv::multiply(mat_a_up, mat_a_up, mat_b_up, kColors_1[i % kColors_1.size()]); cv::multiply(mat_a_up, mat_a_up, mat_g_up, kColors_2[i % kColors_2.size()]); @@ -129,4 +127,3 @@ AclLiteError PostprocessThread::InferOutputProcess(shared_ptr in return ACLLITE_OK; } - diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/postprocess/postprocess.h b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/postprocess/postprocess.h index 73a011c6f..61e146e23 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/postprocess/postprocess.h +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/postprocess/postprocess.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 DEEPLABV3_MULTI_THREAD_MULTI_DEVICE_POSTPROCESS_POSTPROCESS_H +#define DEEPLABV3_MULTI_THREAD_MULTI_DEVICE_POSTPROCESS_POSTPROCESS_H + #pragma once #include @@ -38,11 +39,12 @@ public: private: AclLiteError InferOutputProcess(shared_ptr inferMsg); - //AclLiteError MsgSend(shared_ptr inferMsg, + // AclLiteError MsgSend(shared_ptr inferMsg, // shared_ptr &postOutputMsg); private: - uint32_t outputWidth_; - uint32_t outputHeight_; + uint32_t g_outputWidth_; + uint32_t g_outputHeight_; }; +#endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/preprocess/preprocess.cpp b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/preprocess/preprocess.cpp index 2fd55837b..fa678f75f 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/preprocess/preprocess.cpp +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/preprocess/preprocess.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,8 @@ * 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 "object_detection.h" @@ -34,73 +32,71 @@ struct timespec time2 = {0, 0}; PreprocessThread::PreprocessThread(string& filePath, uint32_t modelWidth, uint32_t modelHeight, uint32_t postThreadNum, uint32_t inferThreadNum, aclrtContext& context) : -filePath_(filePath), -context_(context), -modelWidth_(modelWidth), -modelHeight_(modelHeight), -selfThreadId_(INVALID_INSTANCE_ID), -inferThreadNum_(inferThreadNum), -postThreadNum_(postThreadNum), -inferChannel_(0), -postChannel_(0), -indexCount_(postThreadNum - 1), -frameCnt_(0) { - for(int i = 0; i < inferThreadNum; i++){ - nextThreadId_.push_back(INVALID_INSTANCE_ID); - for(int j = 0; j < postThreadNum; j++){ - postprocThreadId_.push_back(INVALID_INSTANCE_ID); +g_filePath_(filePath), +g_context_(context), +g_modelWidth_(modelWidth), +g_modelHeight_(modelHeight), +g_selfThreadId_(INVALID_INSTANCE_ID), +g_inferThreadNum_(inferThreadNum), +g_postThreadNum_(postThreadNum), +g_inferChannel_(0), +g_postChannel_(0), +g_indexCount_(postThreadNum - 1), +g_frameCnt_(0) { + for (int i = 0; i < inferThreadNum; i++) { + g_nextThreadId_.push_back(INVALID_INSTANCE_ID); + for (int j = 0; j < postThreadNum; j++) { + g_postprocThreadId_.push_back(INVALID_INSTANCE_ID); } } } PreprocessThread::~PreprocessThread() { - aclError ret = aclrtSetCurrentContext(context_); + aclError ret = aclrtSetCurrentContext(g_context_); if (ret != ACL_SUCCESS) { ACLLITE_LOG_ERROR("PreprocessThread destructor set context failed, error: %d", ret); } - dvpp_.DestroyResource(); + g_dvpp_.DestroyResource(); } AclLiteError PreprocessThread::Init() { - GetAllFiles(filePath_, fileVec); + GetAllFiles(g_filePath_, fileVec); if (fileVec.empty()) { - ACLLITE_LOG_ERROR("Failed to deal all empty path=%s.", filePath_.c_str()); + ACLLITE_LOG_ERROR("Failed to deal all empty path=%s.", g_filePath_.c_str()); } imageFile = fileVec.begin(); - aclError aclRet = aclrtGetRunMode(&runMode_); + aclError aclRet = aclrtGetRunMode(&g_runMode_); if (aclRet != ACL_ERROR_NONE) { ACLLITE_LOG_ERROR("acl get run mode failed"); return ACLLITE_ERROR_GET_RUM_MODE; } - aclRet = dvpp_.Init(); + aclRet = g_dvpp_.Init(); if (aclRet) { ACLLITE_LOG_ERROR("Dvpp init failed, error %d", aclRet); return ACLLITE_ERROR; } - //Get the relevant thread instance id - selfThreadId_ = SelfInstanceId(); - if (selfThreadId_ == INVALID_INSTANCE_ID) { - ACLLITE_LOG_ERROR("Self instance id %d", selfThreadId_); + // Get the relevant thread instance id + g_selfThreadId_ = SelfInstanceId(); + if (g_selfThreadId_ == INVALID_INSTANCE_ID) { + ACLLITE_LOG_ERROR("Self instance id %d", g_selfThreadId_); return ACLLITE_ERROR; } - for(int i = 0; i < inferThreadNum_; i++) - { - nextThreadId_[i] = GetAclLiteThreadIdByName(kInferName[i]); - if (nextThreadId_[i] == INVALID_INSTANCE_ID){ + for (int i = 0; i < g_inferThreadNum_; i++) { + g_nextThreadId_[i] = GetAclLiteThreadIdByName(g_inferName[i]); + if (g_nextThreadId_[i] == INVALID_INSTANCE_ID) { ACLLITE_LOG_ERROR("%d inference instance id %d", - i, nextThreadId_[i]); + i, g_nextThreadId_[i]); return ACLLITE_ERROR; } - for(int j = 0; j < postThreadNum_; j++) - { - postprocThreadId_[i*postThreadNum_+j] = GetAclLiteThreadIdByName(kPostprocName[i*postThreadNum_+j]); - if (postprocThreadId_[i*postThreadNum_+j] == INVALID_INSTANCE_ID){ + for (int j = 0; j < g_postThreadNum_; j++) { + g_postprocThreadId_[i*g_postThreadNum_+j] = GetAclLiteThreadIdByName(g_postprocName[i*g_postThreadNum_+j]); + if (g_postprocThreadId_[i*g_postThreadNum_+j] == INVALID_INSTANCE_ID) { ACLLITE_LOG_ERROR("%d postprocess instance id %d", - i*postThreadNum_+j, postprocThreadId_[i*postThreadNum_+j]); + i*g_postThreadNum_+j, g_postprocThreadId_[i*g_postThreadNum_+j]); return ACLLITE_ERROR; } } @@ -134,7 +130,7 @@ AclLiteError PreprocessThread::Process(int msgId, } AclLiteError PreprocessThread::AppStart() { - AclLiteError ret = SendMessage(selfThreadId_, MSG_READ_FRAME, nullptr); + AclLiteError ret = SendMessage(g_selfThreadId_, MSG_READ_FRAME, nullptr); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Process app start message failed, error %d", ret); } @@ -143,16 +139,16 @@ AclLiteError PreprocessThread::AppStart() { } AclLiteError PreprocessThread::ReadFrame(shared_ptr &preprocDataMsg) { - inferChannel_ = frameCnt_ % inferThreadNum_; - if(inferChannel_ == 0){ - indexCount_ = (++indexCount_) % postThreadNum_; //postcount初始值为postnum-1; + g_inferChannel_ = g_frameCnt_ % g_inferThreadNum_; + if (g_inferChannel_ == 0) { + g_indexCount_ = (++g_indexCount_) % g_postThreadNum_; // postcount初始值为postnum-1; } - postChannel_ = inferChannel_ * postThreadNum_ + indexCount_; + g_postChannel_ = g_inferChannel_ * g_postThreadNum_ + g_indexCount_; - preprocDataMsg->inferThreadId = nextThreadId_[inferChannel_]; - preprocDataMsg->postprocThreadId = postprocThreadId_[postChannel_]; + preprocDataMsg->inferThreadId = g_nextThreadId_[g_inferChannel_]; + preprocDataMsg->postprocThreadId = g_postprocThreadId_[g_postChannel_]; - preprocDataMsg->frameNum = frameCnt_; + preprocDataMsg->frameNum = g_frameCnt_; preprocDataMsg->isLastFrame = 0; if (imageFile == fileVec.end()) { preprocDataMsg->isLastFrame = 1; @@ -165,35 +161,35 @@ AclLiteError PreprocessThread::ReadFrame(shared_ptr &preprocData } preprocDataMsg->imageFileName = *imageFile; imageFile++; - frameCnt_++; + g_frameCnt_++; return ACLLITE_OK; } AclLiteError PreprocessThread::MsgProcess(ImageData& imageFrame, shared_ptr &preprocDataMsg) { - if(preprocDataMsg->isLastFrame == 1) + if (preprocDataMsg->isLastFrame == 1) return ACLLITE_OK; ImageData imageDevice; - AclLiteError ret = CopyImageToDevice(imageDevice, imageFrame, runMode_, MEMORY_DVPP); + AclLiteError ret = CopyImageToDevice(imageDevice, imageFrame, g_runMode_, MEMORY_DVPP); if (ret == ACLLITE_ERROR) { ACLLITE_LOG_ERROR("Copy image to device failed"); return ACLLITE_ERROR; } ImageData yuvImage; - ret = dvpp_.JpegD(yuvImage, imageDevice); + ret = g_dvpp_.JpegD(yuvImage, imageDevice); if (ret == ACLLITE_ERROR) { ACLLITE_LOG_ERROR("Convert jpeg to yuv failed"); return ACLLITE_ERROR; } ImageData resizedImage; - ret = dvpp_.Resize(resizedImage, yuvImage, modelWidth_, modelHeight_); + ret = g_dvpp_.Resize(resizedImage, yuvImage, g_modelWidth_, g_modelHeight_); if (ret == ACLLITE_ERROR) { - ACLLITE_LOG_ERROR("dvpp_resize image failed"); + ACLLITE_LOG_ERROR("g_dvpp_resize image failed"); return ACLLITE_ERROR; } - ret = CopyImageToLocal(preprocDataMsg->resizedMat, resizedImage, runMode_); + ret = CopyImageToLocal(preprocDataMsg->resizedMat, resizedImage, g_runMode_); if (ret == ACLLITE_ERROR) { ACLLITE_LOG_ERROR("Copy image to host failed"); return ACLLITE_ERROR; @@ -204,24 +200,23 @@ AclLiteError PreprocessThread::MsgProcess(ImageData& imageFrame, AclLiteError PreprocessThread::MsgSend(shared_ptr &preprocDataMsg) { AclLiteError ret; - if(preprocDataMsg->isLastFrame == 0){ + if (preprocDataMsg->isLastFrame == 0) { ret = SendMessage(preprocDataMsg->inferThreadId, MSG_PREPROC_DATA, preprocDataMsg); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Send preprocess data failed, error %d", ret); } - ret = SendMessage(selfThreadId_, MSG_READ_FRAME, nullptr); + ret = SendMessage(g_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 < inferThreadNum_; i++) - { - for(int j = 0; j < postThreadNum_; j++){ + else { + for (int i = 0; i < g_inferThreadNum_; i++) { + for (int j = 0; j < g_postThreadNum_; j++) { shared_ptr preprocDataMsgEnd = make_shared(); - preprocDataMsgEnd->inferThreadId = nextThreadId_[i]; - preprocDataMsgEnd->postprocThreadId = postprocThreadId_[i*postThreadNum_+j]; + preprocDataMsgEnd->inferThreadId = g_nextThreadId_[i]; + preprocDataMsgEnd->postprocThreadId = g_postprocThreadId_[i*g_postThreadNum_+j]; ret = SendMessage(preprocDataMsgEnd->inferThreadId, MSG_PREPROC_END, preprocDataMsgEnd); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Send preprocess data failed, error %d", ret); diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/preprocess/preprocess.h b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/preprocess/preprocess.h index 5d0a9703e..7d6bdfab6 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/preprocess/preprocess.h +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/preprocess/preprocess.h @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,11 @@ * 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 DEEPLABV3_MULTI_THREAD_MULTI_DEVICE_PREPROCESS_PREPROCESS_H +#define DEEPLABV3_MULTI_THREAD_MULTI_DEVICE_PREPROCESS_PREPROCESS_H + #pragma once #include #include @@ -42,20 +43,21 @@ private: AclLiteError ReadFrame(shared_ptr &preprocDataMsg); private: - string filePath_; - aclrtRunMode runMode_; - uint32_t modelWidth_; - uint32_t modelHeight_; - int selfThreadId_; - vector nextThreadId_; - vector postprocThreadId_; - uint32_t inferThreadNum_; - uint32_t postThreadNum_; - int frameCnt_; - AclLiteImageProc dvpp_; - int inferChannel_; - int postChannel_; - int indexCount_; - aclrtContext context_; + string g_filePath_; + aclrtRunMode g_runMode_; + uint32_t g_modelWidth_; + uint32_t g_modelHeight_; + int g_selfThreadId_; + vector g_nextThreadId_; + vector g_postprocThreadId_; + uint32_t g_inferThreadNum_; + uint32_t g_postThreadNum_; + int g_frameCnt_; + AclLiteImageProc g_dvpp_; + int g_inferChannel_; + int g_postChannel_; + int g_indexCount_; + aclrtContext g_context_; }; +#endif \ No newline at end of file -- Gitee From c33c2487d51a55b0f4dc21f58153835207decd52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E8=82=96=E9=98=B3?= <1971168308@qq.com> Date: Sun, 9 Oct 2022 06:31:04 +0000 Subject: [PATCH 07/14] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20data?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../YOLOV3_coco_detection_picture_DVPP_with_AIPP/data/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/data/.keep diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/data/.keep b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/data/.keep new file mode 100644 index 000000000..e69de29bb -- Gitee From ca9d69891f2b0d2e01f747811e8b89d59c553fbd Mon Sep 17 00:00:00 2001 From: yy0131 <1595992831@qq.com> Date: Tue, 11 Oct 2022 15:25:02 +0800 Subject: [PATCH 08/14] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=A7=84=E8=8C=83?= =?UTF-8?q?=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../objectdetection_yolov3pb.iml | 0 .../YOLOV3_VOC_detection_picture/src/acl.json | 0 .../YOLOV3_carColor_sample/inc/AclLiteError.h | 16 +- .../inc/AclLiteImageProc.h | 4 +- .../YOLOV3_carColor_sample/inc/AclLiteModel.h | 14 +- .../inc/AclLiteResource.h | 2 +- .../YOLOV3_carColor_sample/inc/AclLiteType.h | 2 +- .../YOLOV3_carColor_sample/inc/AclLiteUtils.h | 64 +++--- .../inc/CropAndPasteHelper.h | 4 +- .../YOLOV3_carColor_sample/inc/JpegDHelper.h | 4 +- .../YOLOV3_carColor_sample/inc/ResizeHelper.h | 0 .../inc/color_classify.h | 4 +- .../inc/object_detect.h | 6 +- .../YOLOV3_carColor_sample/inc/params.h | 6 +- .../src/AclLiteImageProc.cpp | 38 ++-- .../src/AclLiteModel.cpp | 68 ++++--- .../src/AclLiteResource.cpp | 24 ++- .../src/AclLiteUtils.cpp | 88 +++++---- .../src/CropAndPasteHelper.cpp | 99 +++++----- .../src/JpegDHelper.cpp | 27 +-- .../src/ResizeHelper.cpp | 47 +++-- .../YOLOV3_carColor_sample/src/acl.json | 0 .../src/color_classify.cpp | 170 ++++++++-------- .../YOLOV3_carColor_sample/src/main.cpp | 12 +- .../src/object_detect.cpp | 64 +++--- .../YOLOV3_coco_detection_VENC/.build_project | 0 .../YOLOV3_coco_detection_VENC/.project | 0 .../YOLOV3_coco_detection_VENC/CMakeLists.txt | 0 .../inc/dvpp_process.h | 4 +- .../inc/model_process.h | 1 + .../inc/object_detect.h | 7 +- .../YOLOV3_coco_detection_VENC/inc/utils.h | 2 - .../src/CMakeLists.txt | 0 .../src/dvpp_process.cpp | 73 +++---- .../YOLOV3_coco_detection_VENC/src/main.cpp | 23 ++- .../src/model_process.cpp | 53 +++-- .../src/object_detect.cpp | 131 ++++++------ .../YOLOV3_coco_detection_VENC/src/utils.cpp | 52 ++--- .../inc/dvpp_jpegd.h | 2 +- .../inc/dvpp_process.h | 0 .../inc/dvpp_resize.h | 1 - .../inc/model_process.h | 10 +- .../inc/object_detect.h | 10 +- .../inc/utils.h | 7 +- .../src/dvpp_jpegd.cpp | 21 +- .../src/dvpp_process.cpp | 21 +- .../src/dvpp_resize.cpp | 33 ++-- .../src/main.cpp | 24 +-- .../src/model_process.cpp | 71 ++++--- .../src/object_detect.cpp | 161 ++++++++------- .../src/utils.cpp | 70 ++++--- .../inc/dvpp_jpegd.h | 2 +- .../inc/dvpp_process.h | 0 .../inc/dvpp_resize.h | 1 - .../inc/model_process.h | 4 +- .../inc/object_detect.h | 4 +- .../YOLOV3_coco_detection_picture/inc/utils.h | 9 +- .../src/dvpp_jpegd.cpp | 19 +- .../src/dvpp_process.cpp | 18 +- .../src/dvpp_resize.cpp | 38 ++-- .../src/main.cpp | 22 ++- .../src/model_process.cpp | 49 +++-- .../src/object_detect.cpp | 123 ++++++------ .../src/utils.cpp | 83 ++++---- .../inc/utils.h | 6 +- .../src/dvpp_process.cpp | 66 ++++--- .../src/main.cpp | 3 +- .../src/model_process.cpp | 51 +++-- .../src/sample_process.cpp | 113 ++++++----- .../src/utils.cpp | 39 ++-- .../inc/drawing.h | 2 +- .../inc/dvpp_jpegd.h | 0 .../inc/dvpp_process.h | 0 .../inc/model_process.h | 4 +- .../inc/object_detect.h | 8 +- .../inc/utils.h | 7 +- .../src/drawing.cpp | 54 ++--- .../src/dvpp_cropandpaste.cpp | 35 ++-- .../src/dvpp_jpegd.cpp | 19 +- .../src/dvpp_process.cpp | 21 +- .../src/freetype_helper.cpp | 53 ++--- .../src/main.cpp | 22 ++- .../src/model_process.cpp | 42 ++-- .../src/object_detect.cpp | 86 ++++---- .../src/utils.cpp | 90 +++++---- .../inc/model_process.h | 4 +- .../YOLOV3_coco_detection_video/inc/utils.h | 7 +- .../YOLOV3_coco_detection_video/src/main.cpp | 21 +- .../src/model_process.cpp | 58 +++--- .../src/object_detect.cpp | 119 +++++------ .../YOLOV3_coco_detection_video/src/utils.cpp | 66 ++++--- .../inc/sample_process.h | 3 +- .../src/main.cpp | 6 +- .../src/sample_process.cpp | 122 ++++++------ .../inc/post_process.h | 12 +- .../YOLOV4_coco_detection_picture/inc/utils.h | 6 +- .../src/dvpp_process.cpp | 79 +++++--- .../src/main.cpp | 21 +- .../src/model_process.cpp | 43 ++-- .../src/post_process.cpp | 65 +++--- .../src/utils.cpp | 11 +- .../CMakeLists.txt | 0 .../inc/object_detect.h | 3 +- .../inc/queue.h | 41 ++-- .../src/acl.json | 0 .../src/main.cpp | 149 +++++++------- .../src/object_detect.cpp | 66 ++++--- .../CMakeLists.txt | 0 .../inc/object_detect.h | 7 +- .../inc/queue.h | 6 +- .../src/acl.json | 0 .../src/main.cpp | 120 +++++------ .../src/object_detect.cpp | 49 ++--- .../inc/object_detection.h | 11 +- .../scripts/sample_build.sh | 1 - .../src/inference/inference.cpp | 43 ++-- .../src/inference/inference.h | 4 +- .../src/main.cpp | 116 ++++++----- .../src/postprocess/postprocess.cpp | 37 ++-- .../src/postprocess/postprocess.h | 4 +- .../src/preprocess/preprocess.cpp | 187 +++++++++--------- .../src/preprocess/preprocess.h | 40 ++-- .../src/videoprocess/videoprocess.cpp | 117 ++++++----- .../src/videoprocess/videoprocess.h | 18 +- .../inc/object_detection.h | 10 +- .../src/inference/inference.cpp | 78 ++++---- .../src/inference/inference.h | 18 +- .../src/main.cpp | 87 ++++---- .../src/postprocess/postprocess.cpp | 25 +-- .../src/postprocess/postprocess.h | 6 +- .../src/preprocess/preprocess.cpp | 133 ++++++------- .../src/preprocess/preprocess.h | 36 ++-- 132 files changed, 2555 insertions(+), 2133 deletions(-) mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_VOC_detection_picture/objectdetection_yolov3pb.iml mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_VOC_detection_picture/src/acl.json mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteError.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteImageProc.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteModel.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteResource.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteType.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteUtils.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/CropAndPasteHelper.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/JpegDHelper.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/ResizeHelper.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteImageProc.cpp mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteModel.cpp mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteResource.cpp mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteUtils.cpp mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/CropAndPasteHelper.cpp mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/JpegDHelper.cpp mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/ResizeHelper.cpp mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/acl.json mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/.build_project mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/.project mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/CMakeLists.txt mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/inc/dvpp_process.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/inc/model_process.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/inc/object_detect.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/inc/utils.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/CMakeLists.txt mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/dvpp_process.cpp mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/main.cpp mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/model_process.cpp mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/object_detect.cpp mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/utils.cpp mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/dvpp_jpegd.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/dvpp_process.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/dvpp_resize.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/model_process.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/object_detect.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/utils.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_jpegd.cpp mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_process.cpp mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_resize.cpp mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/model_process.cpp mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/object_detect.cpp mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/utils.cpp mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/dvpp_jpegd.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/dvpp_process.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/dvpp_resize.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/model_process.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/object_detect.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/utils.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/dvpp_jpegd.cpp mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/dvpp_process.cpp mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/dvpp_resize.cpp mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/model_process.cpp mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/object_detect.cpp mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/utils.cpp mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/dvpp_jpegd.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/dvpp_process.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/model_process.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/object_detect.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/utils.h mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/dvpp_jpegd.cpp mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/dvpp_process.cpp mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/model_process.cpp mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/object_detect.cpp mode change 100755 => 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/utils.cpp mode change 100755 => 100644 cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/CMakeLists.txt mode change 100755 => 100644 cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/src/acl.json mode change 100755 => 100644 cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/CMakeLists.txt mode change 100755 => 100644 cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/src/acl.json diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_VOC_detection_picture/objectdetection_yolov3pb.iml b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_VOC_detection_picture/objectdetection_yolov3pb.iml old mode 100755 new mode 100644 diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_VOC_detection_picture/src/acl.json b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_VOC_detection_picture/src/acl.json old mode 100755 new mode 100644 diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteError.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteError.h old mode 100755 new mode 100644 index 3d385cc32..888087ba3 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteError.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteError.h @@ -40,24 +40,24 @@ const int ACLLITE_ERROR_THREAD_ABNORMAL = 14; const int ACLLITE_ERROR_START_THREAD = 15; const int ACLLITE_ERROR_ADD_THREAD = 16; -//malloc or new memory failed +// malloc or new memory failed const int ACLLITE_ERROR_MALLOC = 101; -//aclrtMalloc failed +// aclrtMalloc failed const int ACLLITE_ERROR_MALLOC_DEVICE = 102; const int ACLLITE_ERROR_MALLOC_DVPP = 103; -//access file failed +// access file failed const int ACLLITE_ERROR_ACCESS_FILE = 201; -//the file is invalid +// the file is invalid const int ACLLITE_ERROR_INVALID_FILE = 202; -//open file failed +// open file failed const int ACLLITE_ERROR_OPEN_FILE = 203; -//load model repeated +// load model repeated const int ACLLITE_ERROR_LOAD_MODEL_REPEATED = 301; const int ACLLITE_ERROR_NO_MODEL_DESC = 302; -//load mode by acl failed +// load mode by acl failed const int ACLLITE_ERROR_LOAD_MODEL = 303; const int ACLLITE_ERROR_CREATE_MODEL_DESC = 304; @@ -92,7 +92,7 @@ const int ACLLITE_ERROR_UNSURPPORT_PROPERTY = 404; const int ACLLITE_ERROR_INVALID_PROPERTY_VALUE = 405; -const int ACLLITE_ERROR_UNSURPPORT_VIDEO_CAPTURE =406; +const int ACLLITE_ERROR_UNSURPPORT_VIDEO_CAPTURE = 406; const int ACLLITE_ERROR_CREATE_DVPP_CHANNEL_DESC = 501; diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteImageProc.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteImageProc.h old mode 100755 new mode 100644 index e608b0ed6..aefcb1eff --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteImageProc.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteImageProc.h @@ -35,7 +35,7 @@ public: ~AclLiteImageProc(); AclLiteError Init(); - AclLiteError Resize(ImageData& dest,ImageData& src, + AclLiteError Resize(ImageData& dest, ImageData& src, uint32_t width, uint32_t height); AclLiteError JpegD(ImageData& destYuv, ImageData& srcJpeg); AclLiteError Crop(ImageData& dest, ImageData& src, @@ -44,7 +44,7 @@ public: AclLiteError CropPaste(ImageData& dest, ImageData& src, uint32_t width, uint32_t height, uint32_t ltHorz, uint32_t ltVert, - uint32_t rbHorz, uint32_t rbVert); + uint32_t rbHorz, uint32_t rbVert); void DestroyResource(); protected: diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteModel.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteModel.h old mode 100755 new mode 100644 index bd2a74b5a..3a2033a9e --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteModel.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteModel.h @@ -34,7 +34,7 @@ public: AclLiteError CreateInput(void *input1, uint32_t input1Size, void* input2, uint32_t input2Size); AclLiteError CreateInput(std::vector& inputData); - AclLiteError Execute(std::vector& inferOutputs, + AclLiteError Execute(std::vector& inferOutputs, void *data, uint32_t size); AclLiteError Execute(std::vector& inferOutputs); void DestroyInput(); @@ -43,18 +43,18 @@ private: AclLiteError LoadModelFromFile(const std::string& modelPath); AclLiteError SetDesc(); AclLiteError CreateOutput(); - AclLiteError AddDatasetBuffer(aclmdlDataset* dataset, + AclLiteError AddDatasetBuffer(aclmdlDataset* dataset, void* buffer, uint32_t bufferSize); AclLiteError GetOutputItem(InferenceOutput& out, uint32_t idx); - void Unload(); + void Unload(); void DestroyDesc(); void DestroyOutput(); private: bool g_loadFlag_; // model load flag bool g_isReleased_; // model release flag - uint32_t g_modelId_; //modelid + uint32_t g_modelId_; // modelid size_t g_outputsNum_; size_t g_modelMemSize_; size_t g_modelWorkSize_; @@ -64,9 +64,9 @@ private: void *g_modelWorkPtr_; void *g_modelWeightPtr_; aclmdlDesc *g_modelDesc_; - aclmdlDataset *g_input_; //input dataset - aclmdlDataset *g_output_; //output dataset - std::string g_modelPath_; //model path + aclmdlDataset *g_input_; // input dataset + aclmdlDataset *g_output_; // output dataset + std::string g_modelPath_; // model path }; #endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteResource.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteResource.h old mode 100755 new mode 100644 index 50ca0b5b4..6e7cdbbd8 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteResource.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteResource.h @@ -26,7 +26,7 @@ class AclLiteResource { public: - AclLiteResource(); + AclLiteResource(); ~AclLiteResource(); AclLiteError Init(); diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteType.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteType.h old mode 100755 new mode 100644 index 598fddf39..30b885f68 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteType.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteType.h @@ -28,7 +28,7 @@ enum MemoryType { MEMORY_NORMAL = 0, MEMORY_HOST, MEMORY_DEVICE, - MEMORY_DVPP, + MEMORY_DVPP, MEMORY_INVALID_TYPE }; diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteUtils.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteUtils.h old mode 100755 new mode 100644 index 9441c4202..8a10e7ae9 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteUtils.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteUtils.h @@ -38,7 +38,7 @@ * @param [in]: height: image height * @return bytes size of image */ -#define YUV420SP_SIZE(width, height) ((width) * (height) * 3 / 2) +#define RGBU8_IMAGE_SIZE(width, height) ((width) * (height) * 3) /** * @brief generate shared pointer of dvpp memory @@ -64,7 +64,7 @@ /** * @brief calculate aligned number * @param [in]: num: the original number that to aligned - * @param [in]: align: the align factor + * @param [in]: align: the align factor * @return the number after aligned */ #define ALIGN_UP(num, align) (((num) + (align) - 1) & ~((align) - 1)) @@ -96,8 +96,8 @@ * @return none */ #define ACLLITE_LOG_ERROR(fmt, ...) \ - do{aclAppLog(ACL_ERROR, __FUNCTION__, __FILE__, __LINE__, fmt, ##__VA_ARGS__); \ - fprintf(stdout, "[ERROR] " fmt "\n", ##__VA_ARGS__);}while(0) + do {aclAppLog(ACL_ERROR, __FUNCTION__, __FILE__, __LINE__, fmt, ##__VA_ARGS__); \ + fprintf(stdout, "[ERROR] " fmt "\n", ##__VA_ARGS__);}while (0) /** * @brief Write acl info level log to host log @@ -131,10 +131,10 @@ bool IsDirectory(const std::string &path); * @param [in]: curRunMode: The run mode, get by aclrtGetRunMode, * Atlas200DK is ACL_DEVICE, Atlas300 is ACL_HOST * @param [in]: memType: The dest memory type:MEMORY_NORMAL(in Atlas200DK), - * MEMORY_DEVICE, MEMORY_DVPP + * MEMORY_DEVICE, MEMORY_DVPP * @return void* The dest memory pointer */ -void* CopyDataToDevice(const void* data, uint32_t size, +void* CopyDataToDevice(const void* data, uint32_t size, aclrtRunMode curRunMode, MemoryType memType); /** @@ -145,11 +145,11 @@ void* CopyDataToDevice(const void* data, uint32_t size, * @param [in]: srcSize: The data bytes size * @param [in]: curRunMode: The run mode, get by aclrtGetRunMode, * Atlas200DK is ACL_DEVICE, Atlas300 is ACL_HOST - * @return AclLiteError ACLLITE_OK: copy success + * @return AclLiteError ACLLITE_OK: copy success * others: copy failed */ -AclLiteError CopyDataToDeviceEx(void* dest, uint32_t destSize, - const void* src, uint32_t srcSize, +AclLiteError CopyDataToDeviceEx(void* dest, uint32_t destSize, + const void* src, uint32_t srcSize, aclrtRunMode runMode); /** @@ -161,7 +161,7 @@ AclLiteError CopyDataToDeviceEx(void* dest, uint32_t destSize, * @param [in]: memType: The dest memory type:MEMORY_NORMAL, MEMORY_HOST * @return void* The dest memory pointer */ -void* CopyDataToHost(const void* data, uint32_t size, +void* CopyDataToHost(const void* data, uint32_t size, aclrtRunMode curRunMode, MemoryType memType); /** @@ -170,68 +170,68 @@ void* CopyDataToHost(const void* data, uint32_t size, * @param [in]: size: The data bytes size * @param [in]: policy: the kind of sync, * typedef enum aclrtMemcpyKind { - * ACL_MEMCPY_HOST_TO_HOST, // Memory copy from Host to Host - * ACL_MEMCPY_HOST_TO_DEVICE, // Memory copy from Host to Device - * ACL_MEMCPY_DEVICE_TO_HOST, // Memory copy from Device to Host - * ACL_MEMCPY_DEVICE_TO_DEVICE, // Memory copy from Device to Device + * ACL_MEMCPY_HOST_TO_HOST, // Memory copy from Host to Host + * ACL_MEMCPY_HOST_TO_DEVICE, // Memory copy from Host to Device + * ACL_MEMCPY_DEVICE_TO_HOST, // Memory copy from Device to Host + * ACL_MEMCPY_DEVICE_TO_DEVICE, // Memory copy from Device to Device * } aclrtMemcpyKind; * @param [in]: memType: The dest memory type * @return void* The dest memory pointer */ -void* CopyData(const void* data, uint32_t size, +void* CopyData(const void* data, uint32_t size, aclrtMemcpyKind policy, MemoryType memType); /** * @brief Read jpeg image file. Only support baseline, not support progressive - * @param [out]: image: image data read from file. + * @param [out]: image: image data read from file. * @param [in]: fileName: The data bytes size - * @return AclLiteError ACLLITE_OK: read success + * @return AclLiteError ACLLITE_OK: read success * others: read failed */ AclLiteError ReadJpeg(ImageData& image, const std::string& fileName); /** * @brief Get all files from file list string - * @param [in]: pathList: files list string, seperate by ',', - * the element could be file path or directory + * @param [in]: pathList: files list string, seperate by ',', + * the element could be file path or directory * @param [in]: fileVec: The data bytes size - * @return AclLiteError ACLLITE_OK: read success + * @return AclLiteError ACLLITE_OK: read success * others: read failed */ -void GetAllFiles(const std::string &pathList, +void GetAllFiles(const std::string &pathList, std::vector &fileVec); /** * @brief Read binary file to buffer * @param [in]: filename: binary file name with path - * @param [in]: data: buffer + * @param [in]: data: buffer * @param [in]: size: buffer size - * @return AclLiteError ACLLITE_OK: read success + * @return AclLiteError ACLLITE_OK: read success * others: read failed */ -AclLiteError ReadBinFile(const std::string& fileName, +AclLiteError ReadBinFile(const std::string& fileName, void*& data, uint32_t& size); /** * @brief Copy image to acl device * @param [out]: destImage: The image after copy - * @param [in]: srcImage: The image to copy + * @param [in]: srcImage: The image to copy * @param [in]: curRunMode: The run mode, get by aclrtGetRunMode, * Atlas200DK is ACL_DEVICE, Atlas300 is ACL_HOST - * @param [in]: memType: memory type, dvpp is MEMORY_DVPP, - * device is MEMPRY_DEVICE - * @return AclLiteError ACLLITE_OK: read success + * @param [in]: memType: memory type, dvpp is MEMORY_DVPP, + * device is MEMPRY_DEVICE + * @return AclLiteError ACLLITE_OK: read success * others: read failed - */ -AclLiteError CopyImageToDevice(ImageData& destImage, ImageData& srcImage, + */ +AclLiteError CopyImageToDevice(ImageData& destImage, ImageData& srcImage, aclrtRunMode curRunMode, MemoryType memType); /** * @brief Test file path is exist or not - * @param [in]: path: file path + * @param [in]: path: file path * @return bool true: file path is exist * false: is not exist - */ + */ bool IsPathExist(const std::string &path); #endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/CropAndPasteHelper.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/CropAndPasteHelper.h old mode 100755 new mode 100644 index 644ddc977..feb896fa5 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/CropAndPasteHelper.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/CropAndPasteHelper.h @@ -24,7 +24,7 @@ #include "acl/ops/acl_dvpp.h" class CropAndPasteHelper { - public: +public: /** * @brief Constructor * @param [in] stream: stream @@ -39,7 +39,7 @@ class CropAndPasteHelper { */ CropAndPasteHelper(aclrtStream &stream, acldvppChannelDesc *dvppChannelDesc, uint32_t width, uint32_t height, - uint32_t lt_horz, uint32_t lt_vert, + uint32_t lt_horz, uint32_t lt_vert, uint32_t rb_horz, uint32_t rb_vert); /** diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/JpegDHelper.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/JpegDHelper.h old mode 100755 new mode 100644 index e50fee64b..5179c6d23 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/JpegDHelper.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/JpegDHelper.h @@ -58,7 +58,7 @@ public: * @param [in] inputWidth:width of pic * @param [in] inputHeight:height of pic */ - void SetInput4JpegD(uint8_t* inDevBuffer, int inDevBufferSize, + void SetInput4JpegD(uint8_t* inDevBuffer, int inDevBufferSize, int inputWidth, int inputHeight); AclLiteError InitDecodeOutputDesc(ImageData& inputImage); /** @@ -81,7 +81,7 @@ private: private: aclrtStream g_stream_; void* g_decodeOutBufferDev_; // decode output buffer - acldvppPicDesc *g_decodeOutputDesc_; //decode output desc + acldvppPicDesc *g_decodeOutputDesc_; // decode output desc acldvppChannelDesc *g_dvppChannelDesc_; }; diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/ResizeHelper.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/ResizeHelper.h old mode 100755 new mode 100644 diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/color_classify.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/color_classify.h index 2828deb37..ade135a5f 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/color_classify.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/color_classify.h @@ -18,8 +18,8 @@ #define YOLOV3_CARCOLOR_SAMPLE_INC_COLOR_CLASSIFY_H #pragma once -#include "acl/acl.h" #include +#include "acl/acl.h" #include "AclLiteModel.h" #include "AclLiteImageProc.h" #include "object_detect.h" @@ -40,7 +40,7 @@ private: AclLiteError InitModelInput(); AclLiteError Crop(std::vector &carImgs, ImageData &orgImg); AclLiteError Resize(std::vector &carImgs); - int CopyOneBatchImages(uint8_t* buffer, uint32_t bufferSize, + int CopyOneBatchImages(uint8_t* buffer, uint32_t bufferSize, std::vector &carImgs, int batchIdx); int CopyImageData(uint8_t *buffer, uint32_t bufferSize, ImageData& image); void DrawResult(std::vector& carInfo, const std::string& origImagePath); diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/object_detect.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/object_detect.h index fce5b238d..cf282021e 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/object_detect.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/object_detect.h @@ -18,15 +18,15 @@ #define YOLOV3_CARCOLOR_SAMPLE_INC_OBJECT_DETECT_H #pragma once -#include "acl/acl.h" #include +#include +#include "acl/acl.h" #include "AclLiteModel.h" #include "AclLiteImageProc.h" #include "opencv2/opencv.hpp" #include "opencv2/imgproc.hpp" #include "opencv2/core/types_c.h" #include "opencv2/imgproc/types_c.h" -#include #include "params.h" /** @@ -41,8 +41,6 @@ public: AclLiteError Inference(std::vector& inferenceOutput, ImageData& resizedImage); AclLiteError PostProcess(ImageData& image, std::vector& inferenceOutput, std::vector& carData); - // void DrawCarBoxToImage(std::vector& detectionResults, - // const std::string& origImagePath); private: void DestroyResource(); AclLiteError InitModelInput(); diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/params.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/params.h index f860415b0..1ea5208ef 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/params.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/params.h @@ -17,15 +17,15 @@ #ifndef YOLOV3_CARCOLOR_SAMPLE_INC_PARAMS_H #define YOLOV3_CARCOLOR_SAMPLE_INC_PARAMS_H -#include "acl/acl.h" #include +#include +#include "acl/acl.h" #include "AclLiteModel.h" #include "AclLiteImageProc.h" #include "opencv2/opencv.hpp" #include "opencv2/imgproc.hpp" #include "opencv2/core/types_c.h" #include "opencv2/imgproc/types_c.h" -#include struct Rectangle { cv::Point lt; // left top @@ -35,7 +35,7 @@ struct Rectangle { struct CarInfo { ImageData cropedImgs; // cropped image from original image ImageData resizedImgs; // resized image for inference - Rectangle rectangle; + Rectangle rectangle; std::string text; // 类别 std::string carColor_result; // 颜色 float confidence; // 颜色 diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteImageProc.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteImageProc.cpp old mode 100755 new mode 100644 index a137a221e..a12508f78 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteImageProc.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteImageProc.cpp @@ -23,18 +23,21 @@ using namespace std; -AclLiteImageProc::AclLiteImageProc(): - g_isReleased_(false), - g_stream_(nullptr), - g_dvppChannelDesc_(nullptr), - g_isInitOk_(false) { +AclLiteImageProc::AclLiteImageProc() + : g_isReleased_(false), + g_stream_(nullptr), + g_dvppChannelDesc_(nullptr), + g_isInitOk_(false) +{ } -AclLiteImageProc::~AclLiteImageProc() { +AclLiteImageProc::~AclLiteImageProc() +{ DestroyResource(); } -void AclLiteImageProc::DestroyResource() { +void AclLiteImageProc::DestroyResource() +{ if (g_isReleased_) { return; } @@ -62,7 +65,8 @@ void AclLiteImageProc::DestroyResource() { g_isReleased_ = true; } -AclLiteError AclLiteImageProc::Init() { +AclLiteError AclLiteImageProc::Init() +{ aclError aclRet = aclrtCreateStream(&g_stream_); if (aclRet != ACL_SUCCESS) { ACLLITE_LOG_ERROR("Create venc stream failed, error %d", aclRet); @@ -88,20 +92,23 @@ AclLiteError AclLiteImageProc::Init() { } AclLiteError AclLiteImageProc::Resize(ImageData& dest, ImageData& src, - uint32_t width, uint32_t height) { + uint32_t width, uint32_t height) +{ ResizeHelper resizeOp(g_stream_, g_dvppChannelDesc_, width, height); return resizeOp.Process(dest, src); } -AclLiteError AclLiteImageProc::JpegD(ImageData& dest, ImageData& src) { +AclLiteError AclLiteImageProc::JpegD(ImageData& dest, ImageData& src) +{ JpegDHelper jpegD(g_stream_, g_dvppChannelDesc_); return jpegD.Process(dest, src); } AclLiteError AclLiteImageProc::Crop(ImageData& dest, ImageData& src, uint32_t ltHorz, uint32_t ltVert, - uint32_t rbHorz, uint32_t rbVert) { - CropAndPasteHelper crop(g_stream_, g_dvppChannelDesc_, + uint32_t rbHorz, uint32_t rbVert) +{ + CropAndPasteHelper crop(g_stream_, g_dvppChannelDesc_, ltHorz, ltVert, rbHorz, rbVert); return crop.Process(dest, src); } @@ -109,9 +116,10 @@ AclLiteError AclLiteImageProc::Crop(ImageData& dest, ImageData& src, AclLiteError AclLiteImageProc::CropPaste(ImageData& dest, ImageData& src, uint32_t width, uint32_t height, uint32_t ltHorz, uint32_t ltVert, - uint32_t rbHorz, uint32_t rbVert) { - CropAndPasteHelper crop(g_stream_, g_dvppChannelDesc_, - width, height, ltHorz, + uint32_t rbHorz, uint32_t rbVert) +{ + CropAndPasteHelper crop(g_stream_, g_dvppChannelDesc_, + width, height, ltHorz, ltVert, rbHorz, rbVert); return crop.ProcessCropPaste(dest, src); } \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteModel.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteModel.cpp old mode 100755 new mode 100644 index c959b9940..53b02addb --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteModel.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteModel.cpp @@ -14,33 +14,36 @@ * limitations under the License. */ -#include "AclLiteModel.h" #include +#include "AclLiteModel.h" #include "AclLiteUtils.h" using namespace std; -AclLiteModel::AclLiteModel(const string& modelPath): - g_loadFlag_(false), - g_isReleased_(false), - g_modelId_(0), - g_outputsNum_(0), - g_modelMemSize_(0), - g_modelWorkSize_(0), - g_modelWeightSize_(0), - g_modelMemPtr_(nullptr), - g_modelWorkPtr_(nullptr), - g_modelWeightPtr_(nullptr), - g_modelDesc_(nullptr), - g_input_(nullptr), - g_output_(nullptr), - g_modelPath_(modelPath) { +AclLiteModel::AclLiteModel(const string& modelPath) + : g_loadFlag_(false), + g_isReleased_(false), + g_modelId_(0), + g_outputsNum_(0), + g_modelMemSize_(0), + g_modelWorkSize_(0), + g_modelWeightSize_(0), + g_modelMemPtr_(nullptr), + g_modelWorkPtr_(nullptr), + g_modelWeightPtr_(nullptr), + g_modelDesc_(nullptr), + g_input_(nullptr), + g_output_(nullptr), + g_modelPath_(modelPath) +{ } -AclLiteModel::~AclLiteModel() { +AclLiteModel::~AclLiteModel() +{ DestroyResource(); } -void AclLiteModel::DestroyResource() { +void AclLiteModel::DestroyResource() +{ if (g_isReleased_) { return; } @@ -52,7 +55,8 @@ void AclLiteModel::DestroyResource() { g_isReleased_ = true; } -AclLiteError AclLiteModel::Init() { +AclLiteError AclLiteModel::Init() +{ aclError aclRet = aclrtGetRunMode(&g_runMode_); if (aclRet != ACL_SUCCESS) { ACLLITE_LOG_ERROR("acl get run mode failed"); @@ -82,7 +86,8 @@ AclLiteError AclLiteModel::Init() { return ACLLITE_OK; } -AclLiteError AclLiteModel::LoadModelFromFile(const string& modelPath) { +AclLiteError AclLiteModel::LoadModelFromFile(const string& modelPath) +{ if (g_loadFlag_) { ACLLITE_LOG_ERROR("%s is loaded already", modelPath.c_str()); return ACLLITE_ERROR_LOAD_MODEL_REPEATED; @@ -90,7 +95,7 @@ AclLiteError AclLiteModel::LoadModelFromFile(const string& modelPath) { aclError ret = aclmdlLoadFromFile(modelPath.c_str(), &g_modelId_); if (ret != ACL_SUCCESS) { - ACLLITE_LOG_ERROR("Load model(%s) from file return %d", + ACLLITE_LOG_ERROR("Load model(%s) from file return %d", modelPath.c_str(), ret); return ACLLITE_ERROR_LOAD_MODEL; } @@ -101,17 +106,18 @@ AclLiteError AclLiteModel::LoadModelFromFile(const string& modelPath) { return ACLLITE_OK; } -AclLiteError AclLiteModel::SetDesc() { +AclLiteError AclLiteModel::SetDesc() +{ g_modelDesc_ = aclmdlCreateDesc(); if (g_modelDesc_ == nullptr) { - ACLLITE_LOG_ERROR("Create model(%s) description failed", + ACLLITE_LOG_ERROR("Create model(%s) description failed", g_modelPath_.c_str()); return ACLLITE_ERROR_CREATE_MODEL_DESC; } aclError ret = aclmdlGetDesc(g_modelDesc_, g_modelId_); if (ret != ACL_SUCCESS) { - ACLLITE_LOG_ERROR("Get model(%s) description failed", + ACLLITE_LOG_ERROR("Get model(%s) description failed", g_modelPath_.c_str()); return ACLLITE_ERROR_GET_MODEL_DESC; } @@ -128,13 +134,15 @@ void AclLiteModel::DestroyDesc() } } -AclLiteError AclLiteModel::CreateInput(void *input, uint32_t size) { +AclLiteError AclLiteModel::CreateInput(void *input, uint32_t size) +{ vector inputData = {{input, size}}; return CreateInput(inputData); } -AclLiteError AclLiteModel::CreateInput(void *input1, uint32_t input1Size, - void* input2, uint32_t input2Size) { +AclLiteError AclLiteModel::CreateInput(void *input1, uint32_t input1Size, + void* input2, uint32_t input2Size) +{ vector inputData = {{input1, input1Size}, {input2, input2Size}}; return CreateInput(inputData); } @@ -161,7 +169,7 @@ AclLiteError AclLiteModel::CreateInput(vector& inputData) { size_t modelInputSize = aclmdlGetInputSizeByIndex(g_modelDesc_, i); if (modelInputSize != inputData[i].size) { ACLLITE_LOG_WARNING("Input size verify failed " - "input[%d] size: %ld, provide size : %d", + "input[%d] size: %ld, provide size : %d", i, modelInputSize, inputData[i].size); } AclLiteError atlRet = AddDatasetBuffer(g_input_, @@ -267,7 +275,7 @@ AclLiteError AclLiteModel::Execute(vector& inferOutputs) { for (uint32_t i = 0; i < g_outputsNum_; i++) { InferenceOutput out; - AclLiteError ret = GetOutputItem(out, i); + AclLiteError ret = GetOutputItem(out, i); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Get the %dth interference output failed, " "error: %d", i, ret); @@ -302,7 +310,7 @@ AclLiteError AclLiteModel::GetOutputItem(InferenceOutput& out, return ACLLITE_ERROR_GET_DATA_BUFFER_SIZE; } - void* data = CopyDataToHost(dataBufferDev, bufferSize, + void* data = CopyDataToHost(dataBufferDev, bufferSize, g_runMode_, MEMORY_NORMAL); if (data == nullptr) { ACLLITE_LOG_ERROR("Copy inference output to host failed"); diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteResource.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteResource.cpp old mode 100755 new mode 100644 index 82c4132a7..f43033b53 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteResource.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteResource.cpp @@ -19,20 +19,23 @@ using namespace std; -AclLiteResource::AclLiteResource(): - g_isReleased_(false), - g_useDefaultCtx_(true), - g_deviceId_(0), - g_runMode_(ACL_HOST), - g_context_(nullptr), - g_aclConfig_("") { +AclLiteResource::AclLiteResource() + : g_isReleased_(false), + g_useDefaultCtx_(true), + g_deviceId_(0), + g_runMode_(ACL_HOST), + g_context_(nullptr), + g_aclConfig_("") +{ } -AclLiteResource::~AclLiteResource() { +AclLiteResource::~AclLiteResource() +{ Release(); } -AclLiteError AclLiteResource::Init() { +AclLiteError AclLiteResource::Init() +{ // ACL init aclError ret = aclInit(g_aclConfig_.c_str()); if (ret != ACL_SUCCESS) { @@ -72,7 +75,8 @@ AclLiteError AclLiteResource::Init() { return ACLLITE_OK; } -void AclLiteResource::Release() { +void AclLiteResource::Release() +{ if (g_isReleased_) { return; } diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteUtils.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteUtils.cpp old mode 100755 new mode 100644 index dcda15061..e60c9a359 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteUtils.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/AclLiteUtils.cpp @@ -14,7 +14,6 @@ * limitations under the License. */ -#include "AclLiteUtils.h" #include #include #include @@ -26,6 +25,7 @@ #include #include #include +#include "AclLiteUtils.h" #include "acl/acl.h" #include "acl/ops/acl_dvpp.h" @@ -43,7 +43,8 @@ const std::string g_FileSperator = "/"; const std::string g_PathSeparator = "/"; } -bool IsPathExist(const string &path) { +bool IsPathExist(const string &path) +{ ifstream file(path); if (!file) { return false; @@ -51,7 +52,8 @@ bool IsPathExist(const string &path) { return true; } -bool IsDirectory(const string &path) { +bool IsDirectory(const string &path) +{ // get path stat struct stat buf; if (stat(path.c_str(), &buf) != g_StatSuccess) { @@ -62,7 +64,8 @@ bool IsDirectory(const string &path) { return S_ISDIR(buf.st_mode); } -void SplitPath(const string &path, vector &pathVec) { +void SplitPath(const string &path, vector &pathVec) +{ char *charPath = const_cast(path.c_str()); const char *charSplit = g_ImagePathSeparator.c_str(); char *imageFile = strtok(charPath, charSplit); @@ -72,7 +75,8 @@ void SplitPath(const string &path, vector &pathVec) { } } -void GetPathFiles(const string &path, vector &fileVec) { +void GetPathFiles(const string &path, vector &fileVec) +{ struct dirent *direntPtr = nullptr; DIR *dir = nullptr; if (IsDirectory(path)) { @@ -93,13 +97,13 @@ void GetPathFiles(const string &path, vector &fileVec) { fileVec.emplace_back(fullPath); } } - } - else { + } else { fileVec.emplace_back(path); } } -void GetAllFiles(const string &pathList, vector &fileVec) { +void GetAllFiles(const string &pathList, vector &fileVec) +{ // split file path vector pathVec; SplitPath(pathList, pathVec); @@ -116,13 +120,14 @@ void GetAllFiles(const string &pathList, vector &fileVec) { } } -void* MallocMemory(uint32_t dataSize, MemoryType memType) { +void* MallocMemory(uint32_t dataSize, MemoryType memType) +{ void* buffer = nullptr; aclError aclRet = ACL_SUCCESS; - switch(memType){ + switch (memType) { case MEMORY_NORMAL: - buffer = new uint8_t[dataSize]; + buffer = new uint8_t[dataSize]; break; case MEMORY_HOST: aclRet = aclrtMallocHost(&buffer, dataSize); @@ -145,10 +150,11 @@ void* MallocMemory(uint32_t dataSize, MemoryType memType) { return nullptr; } - return buffer; + return buffer; } -void FreeMemory(void* mem, MemoryType memType) { +void FreeMemory(void* mem, MemoryType memType) +{ switch(memType){ case MEMORY_NORMAL: delete[]((uint8_t *)mem); @@ -204,25 +210,27 @@ void* CopyDataToDevice(const void* data, uint32_t size, } AclLiteError CopyDataToDeviceEx(void* dest, uint32_t destSize, - const void* src, uint32_t srcSize, - aclrtRunMode runMode) { + const void* src, uint32_t srcSize, + aclrtRunMode runMode) +{ aclrtMemcpyKind policy = ACL_MEMCPY_HOST_TO_DEVICE; if (runMode == ACL_DEVICE) { policy = ACL_MEMCPY_DEVICE_TO_DEVICE; - } + } aclError aclRet = aclrtMemcpy(dest, destSize, src, srcSize, policy); if (aclRet != ACL_SUCCESS) { ACLLITE_LOG_ERROR("Copy data to device failed, aclRet is %d", aclRet); return ACLLITE_ERROR; - } + } - return ACLLITE_OK; + return ACLLITE_OK; } -void* CopyDataToHost(const void* data, uint32_t size, - aclrtRunMode curRunMode, MemoryType memType) { - if ((data == nullptr) || (size == 0) || +void* CopyDataToHost(const void* data, uint32_t size, + aclrtRunMode curRunMode, MemoryType memType) +{ + if ((data == nullptr) || (size == 0) || ((curRunMode != ACL_HOST) && (curRunMode != ACL_DEVICE)) || ((memType != MEMORY_HOST) && (memType != MEMORY_NORMAL))) { ACLLITE_LOG_ERROR("Copy data args invalid, data %p, " @@ -236,8 +244,9 @@ void* CopyDataToHost(const void* data, uint32_t size, return CopyData(data, size, policy, memType); } -void* CopyData(const void* data, uint32_t size, - aclrtMemcpyKind policy, MemoryType memType) { +void* CopyData(const void* data, uint32_t size, + aclrtMemcpyKind policy, MemoryType memType) +{ void* buffer = MallocMemory(size, memType); if (buffer == nullptr) { return nullptr; @@ -253,8 +262,9 @@ void* CopyData(const void* data, uint32_t size, return buffer; } -AclLiteError CopyImageToDevice(ImageData& destImage, ImageData& srcImage, - aclrtRunMode curRunMode, MemoryType memType) { +AclLiteError CopyImageToDevice(ImageData& destImage, ImageData& srcImage, + aclrtRunMode curRunMode, MemoryType memType) +{ void* data = CopyDataToDevice(srcImage.data.get(), srcImage.size, curRunMode, memType); if (data == nullptr) { @@ -268,17 +278,17 @@ AclLiteError CopyImageToDevice(ImageData& destImage, ImageData& srcImage, destImage.alignWidth = srcImage.alignWidth; destImage.alignHeight = srcImage.alignHeight; - if(memType == MEMORY_DEVICE) { + if (memType == MEMORY_DEVICE) { destImage.data = SHARED_PTR_DEV_BUF(data); - } - else { + } else { destImage.data = SHARED_PTR_DVPP_BUF(data); } return ACLLITE_OK; } -AclLiteError ReadBinFile(const string& fileName, void*& data, uint32_t& size) { +AclLiteError ReadBinFile(const string& fileName, void*& data, uint32_t& size) +{ struct stat sBuf; int fileStatus = stat(fileName.data(), &sBuf); if (fileStatus == -1) { @@ -286,15 +296,15 @@ AclLiteError ReadBinFile(const string& fileName, void*& data, uint32_t& size) { return ACLLITE_ERROR_ACCESS_FILE; } if (S_ISREG(sBuf.st_mode) == 0) { - ACLLITE_LOG_ERROR("%s is not a file, please enter a file", - fileName.c_str()); + ACLLITE_LOG_ERROR("%s is not a file, please enter a file", + fileName.c_str()); return ACLLITE_ERROR_INVALID_FILE; - } + } std::ifstream binFile(fileName, std::ifstream::binary); if (binFile.is_open() == false) { ACLLITE_LOG_ERROR("open file %s failed", fileName.c_str()); return ACLLITE_ERROR_OPEN_FILE; - } + } binFile.seekg(0, binFile.end); uint32_t binFileBufferLen = binFile.tellg(); @@ -321,7 +331,8 @@ AclLiteError ReadBinFile(const string& fileName, void*& data, uint32_t& size) { return ACLLITE_OK; } -AclLiteError ReadJpeg(ImageData& image, const std::string& fileName) { +AclLiteError ReadJpeg(ImageData& image, const std::string& fileName) +{ uint32_t size = 0; void* buf = nullptr; @@ -329,13 +340,14 @@ AclLiteError ReadJpeg(ImageData& image, const std::string& fileName) { int32_t ch = 0; acldvppJpegGetImageInfo(buf, size, - &(image.width), &(image.height), &ch); - if(image.width == 0 || image.height == 0){ + &(image.width), &(image.height), &ch); + if (image.width == 0 || image.height == 0) { ACLLITE_LOG_ERROR("unsupported format, only Baseline JPEG"); return ACLLITE_ERROR; } - image.data.reset((uint8_t *)buf, [](uint8_t* p) - { delete[](p); } + image.data.reset((uint8_t *)buf, [](uint8_t* p) { + delete[](p); + } ); image.size = size; diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/CropAndPasteHelper.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/CropAndPasteHelper.cpp old mode 100755 new mode 100644 index 265506a79..6376f6542 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/CropAndPasteHelper.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/CropAndPasteHelper.cpp @@ -22,18 +22,19 @@ using namespace std; -CropAndPasteHelper::CropAndPasteHelper(aclrtStream& stream, +CropAndPasteHelper::CropAndPasteHelper(aclrtStream& stream, acldvppChannelDesc *dvppChannelDesc, uint32_t ltHorz, uint32_t ltVert, - uint32_t rbHorz, uint32_t rbVert): - g_stream_(stream), - g_vpcOutBufferDev_(nullptr), - g_cropArea_(nullptr), - g_pasteArea_(nullptr), - g_vpcInputDesc_(nullptr), - g_vpcOutputDesc_(nullptr), - g_dvppChannelDesc_(dvppChannelDesc), - g_vpcOutBufferSize_(0) { + uint32_t rbHorz, uint32_t rbVert) + : g_stream_(stream), + g_vpcOutBufferDev_(nullptr), + g_cropArea_(nullptr), + g_pasteArea_(nullptr), + g_vpcInputDesc_(nullptr), + g_vpcOutputDesc_(nullptr), + g_dvppChannelDesc_(dvppChannelDesc), + g_vpcOutBufferSize_(0) +{ // Change the left top coordinate to even numver g_ltHorz_ = (ltHorz >> 1) << 1; g_ltVert_ = (ltVert >> 1) << 1; @@ -45,34 +46,37 @@ CropAndPasteHelper::CropAndPasteHelper(aclrtStream& stream, g_size_.height = g_rbVert_ - g_ltVert_; } -CropAndPasteHelper::CropAndPasteHelper(aclrtStream& stream, +CropAndPasteHelper::CropAndPasteHelper(aclrtStream& stream, acldvppChannelDesc *dvppChannelDesc, uint32_t width, uint32_t height, uint32_t ltHorz, uint32_t ltVert, - uint32_t rbHorz, uint32_t rbVert): - g_stream_(stream), - g_vpcOutBufferDev_(nullptr), - g_cropArea_(nullptr), - g_pasteArea_(nullptr), - g_vpcInputDesc_(nullptr), - g_vpcOutputDesc_(nullptr), - g_dvppChannelDesc_(dvppChannelDesc), - g_vpcOutBufferSize_(0) { - // Change the left top coordinate to even numver - g_ltHorz_ = (ltHorz >> 1) << 1; - g_ltVert_ = (ltVert >> 1) << 1; - // Change the right bottom coordinate to odd numver - g_rbHorz_ = ((rbHorz >> 1) << 1) - 1; - g_rbVert_ = ((rbVert >> 1) << 1) - 1; - g_size_.width = width; - g_size_.height = height; + uint32_t rbHorz, uint32_t rbVert) + : g_stream_(stream), + g_vpcOutBufferDev_(nullptr), + g_cropArea_(nullptr), + g_pasteArea_(nullptr), + g_vpcInputDesc_(nullptr), + g_vpcOutputDesc_(nullptr), + g_dvppChannelDesc_(dvppChannelDesc), + g_vpcOutBufferSize_(0) +{ + // Change the left top coordinate to even numver + g_ltHorz_ = (ltHorz >> 1) << 1; + g_ltVert_ = (ltVert >> 1) << 1; + // Change the right bottom coordinate to odd numver + g_rbHorz_ = ((rbHorz >> 1) << 1) - 1; + g_rbVert_ = ((rbVert >> 1) << 1) - 1; + g_size_.width = width; + g_size_.height = height; } -CropAndPasteHelper::~CropAndPasteHelper() { +CropAndPasteHelper::~CropAndPasteHelper() +{ DestroyCropAndPasteResource(); } -AclLiteError CropAndPasteHelper::InitCropAndPasteInputDesc(ImageData& inputImage) { +AclLiteError CropAndPasteHelper::InitCropAndPasteInputDesc(ImageData& inputImage) +{ g_originalImageWidth_ = inputImage.width; g_originalImageHeight_ = inputImage.height; uint32_t alignWidth = inputImage.alignWidth; @@ -102,7 +106,8 @@ AclLiteError CropAndPasteHelper::InitCropAndPasteInputDesc(ImageData& inputImage return ACLLITE_OK; } -AclLiteError CropAndPasteHelper::InitCropAndPasteOutputDesc() { +AclLiteError CropAndPasteHelper::InitCropAndPasteOutputDesc() +{ int cropOutWidth = g_size_.width; int cropOutHeight = g_size_.height; int cropOutWidthStride = ALIGN_UP16(cropOutWidth); @@ -114,13 +119,13 @@ AclLiteError CropAndPasteHelper::InitCropAndPasteOutputDesc() { return ACLLITE_ERROR; } - g_vpcOutBufferSize_ = YUV420SP_SIZE(cropOutWidthStride, - cropOutHeightStride); + g_vpcOutBufferSize_ = YUV420SP_SIZE(cropOutWidthStride, + cropOutHeightStride); aclError aclRet = acldvppMalloc(&g_vpcOutBufferDev_, g_vpcOutBufferSize_); if (aclRet != ACL_SUCCESS) { ACLLITE_LOG_ERROR("Dvpp crop malloc output memory failed, crop " - "width %d, height %d size %d, error %d", - g_size_.width, g_size_.height, + "width %d, height %d size %d, error %d", + g_size_.width, g_size_.height, g_vpcOutBufferSize_, aclRet); return ACLLITE_ERROR; } @@ -142,7 +147,8 @@ AclLiteError CropAndPasteHelper::InitCropAndPasteOutputDesc() { return ACLLITE_OK; } -AclLiteError CropAndPasteHelper::InitCropAndPasteResource(ImageData& inputImage) { +AclLiteError CropAndPasteHelper::InitCropAndPasteResource(ImageData& inputImage) +{ if (ACLLITE_OK != InitCropAndPasteInputDesc(inputImage)) { ACLLITE_LOG_ERROR("Dvpp crop init input failed"); return ACLLITE_ERROR; @@ -156,7 +162,8 @@ AclLiteError CropAndPasteHelper::InitCropAndPasteResource(ImageData& inputImage) return ACLLITE_OK; } -AclLiteError CropAndPasteHelper::Process(ImageData& cropedImage, ImageData& srcImage) { +AclLiteError CropAndPasteHelper::Process(ImageData& cropedImage, ImageData& srcImage) +{ if (ACLLITE_OK != InitCropAndPasteResource(srcImage)) { ACLLITE_LOG_ERROR("Dvpp cropandpaste failed for init error"); return ACLLITE_ERROR; @@ -172,7 +179,7 @@ AclLiteError CropAndPasteHelper::Process(ImageData& cropedImage, ImageData& srcI uint32_t cropBottomOffset = g_rbVert_; g_cropArea_ = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset, - cropTopOffset, cropBottomOffset); + cropTopOffset, cropBottomOffset); if (g_cropArea_ == nullptr) { ACLLITE_LOG_ERROR("acldvppCreateRoiConfig g_cropArea_ failed"); return ACLLITE_ERROR; @@ -188,7 +195,7 @@ AclLiteError CropAndPasteHelper::Process(ImageData& cropedImage, ImageData& srcI uint32_t pasteBottomOffset = g_size_.height; g_pasteArea_ = acldvppCreateRoiConfig(pasteLeftOffset, pasteRightOffset, - pasteTopOffset, pasteBottomOffset); + pasteTopOffset, pasteBottomOffset); if (g_pasteArea_ == nullptr) { ACLLITE_LOG_ERROR("acldvppCreateRoiConfig g_pasteArea_ failed"); return ACLLITE_ERROR; @@ -196,7 +203,7 @@ AclLiteError CropAndPasteHelper::Process(ImageData& cropedImage, ImageData& srcI // crop and patse pic aclError aclRet = acldvppVpcCropAndPasteAsync(g_dvppChannelDesc_, g_vpcInputDesc_, - g_vpcOutputDesc_, g_cropArea_, + g_vpcOutputDesc_, g_cropArea_, g_pasteArea_, g_stream_); if (aclRet != ACL_SUCCESS) { ACLLITE_LOG_ERROR("acldvppVpcCropAndPasteAsync failed, aclRet = %d", aclRet); @@ -221,7 +228,8 @@ AclLiteError CropAndPasteHelper::Process(ImageData& cropedImage, ImageData& srcI return ACLLITE_OK; } -AclLiteError CropAndPasteHelper::ProcessCropPaste(ImageData& cropedImage, ImageData& srcImage) { +AclLiteError CropAndPasteHelper::ProcessCropPaste(ImageData& cropedImage, ImageData& srcImage) +{ if (ACLLITE_OK != InitCropAndPasteResource(srcImage)) { ACLLITE_LOG_ERROR("Dvpp cropandpaste failed for init error"); return ACLLITE_ERROR; @@ -237,7 +245,7 @@ AclLiteError CropAndPasteHelper::ProcessCropPaste(ImageData& cropedImage, ImageD uint32_t cropBottomOffset = g_rbVert_; g_cropArea_ = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset, - cropTopOffset, cropBottomOffset); + cropTopOffset, cropBottomOffset); if (g_cropArea_ == nullptr) { ACLLITE_LOG_ERROR("acldvppCreateRoiConfig g_cropArea_ failed"); return ACLLITE_ERROR; @@ -253,7 +261,7 @@ AclLiteError CropAndPasteHelper::ProcessCropPaste(ImageData& cropedImage, ImageD uint32_t pasteBottomOffset = (((pasteTopOffset + g_size_.height) >> 1) << 1) -1; g_pasteArea_ = acldvppCreateRoiConfig(pasteLeftOffset, pasteRightOffset, - pasteTopOffset, pasteBottomOffset); + pasteTopOffset, pasteBottomOffset); if (g_pasteArea_ == nullptr) { ACLLITE_LOG_ERROR("acldvppCreateRoiConfig g_pasteArea_ failed"); return ACLLITE_ERROR; @@ -261,7 +269,7 @@ AclLiteError CropAndPasteHelper::ProcessCropPaste(ImageData& cropedImage, ImageD // crop and patse pic aclError aclRet = acldvppVpcCropAndPasteAsync(g_dvppChannelDesc_, g_vpcInputDesc_, - g_vpcOutputDesc_, g_cropArea_, + g_vpcOutputDesc_, g_cropArea_, g_pasteArea_, g_stream_); if (aclRet != ACL_SUCCESS) { ACLLITE_LOG_ERROR("acldvppVpcCropAndPasteAsync failed, aclRet = %d", aclRet); @@ -286,7 +294,8 @@ AclLiteError CropAndPasteHelper::ProcessCropPaste(ImageData& cropedImage, ImageD return ACLLITE_OK; } -void CropAndPasteHelper::DestroyCropAndPasteResource() { +void CropAndPasteHelper::DestroyCropAndPasteResource() +{ if (g_cropArea_ != nullptr) { (void)acldvppDestroyRoiConfig(g_cropArea_); g_cropArea_ = nullptr; diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/JpegDHelper.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/JpegDHelper.cpp old mode 100755 new mode 100644 index f62504a6e..4c63a8e76 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/JpegDHelper.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/JpegDHelper.cpp @@ -20,18 +20,21 @@ using namespace std; -JpegDHelper::JpegDHelper(aclrtStream& stream, acldvppChannelDesc *dvppChannelDesc): - g_stream_(stream), - g_decodeOutBufferDev_(nullptr), - g_decodeOutputDesc_(nullptr), - g_dvppChannelDesc_(dvppChannelDesc) { +JpegDHelper::JpegDHelper(aclrtStream& stream, acldvppChannelDesc *dvppChannelDesc) + : g_stream_(stream), + g_decodeOutBufferDev_(nullptr), + g_decodeOutputDesc_(nullptr), + g_dvppChannelDesc_(dvppChannelDesc) +{ } -JpegDHelper::~JpegDHelper() { +JpegDHelper::~JpegDHelper() +{ DestroyDecodeResource(); } -AclLiteError JpegDHelper::InitDecodeOutputDesc(ImageData& inputImage) { +AclLiteError JpegDHelper::InitDecodeOutputDesc(ImageData& inputImage) +{ uint32_t decodeOutWidthStride = ALIGN_UP128(inputImage.width); uint32_t decodeOutHeightStride = ALIGN_UP16(inputImage.height); if (decodeOutWidthStride == 0 || decodeOutHeightStride == 0) { @@ -40,7 +43,7 @@ AclLiteError JpegDHelper::InitDecodeOutputDesc(ImageData& inputImage) { return ACLLITE_ERROR_INVALID_ARGS; } - uint32_t decodeOutBufferSize = + uint32_t decodeOutBufferSize = YUV420SP_SIZE(decodeOutWidthStride, decodeOutHeightStride); aclError aclRet = acldvppMalloc(&g_decodeOutBufferDev_, decodeOutBufferSize); @@ -66,14 +69,15 @@ AclLiteError JpegDHelper::InitDecodeOutputDesc(ImageData& inputImage) { return ACLLITE_OK; } -AclLiteError JpegDHelper::Process(ImageData& dest, ImageData& src) { +AclLiteError JpegDHelper::Process(ImageData& dest, ImageData& src) +{ int ret = InitDecodeOutputDesc(src); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("InitDecodeOutputDesc failed"); return ret; } - aclError aclRet = acldvppJpegDecodeAsync(g_dvppChannelDesc_, + aclError aclRet = acldvppJpegDecodeAsync(g_dvppChannelDesc_, reinterpret_cast(src.data.get()), src.size, g_decodeOutputDesc_, g_stream_); if (aclRet != ACL_SUCCESS) { @@ -97,7 +101,8 @@ AclLiteError JpegDHelper::Process(ImageData& dest, ImageData& src) { return ACLLITE_OK; } -void JpegDHelper::DestroyDecodeResource() { +void JpegDHelper::DestroyDecodeResource() +{ if (g_decodeOutputDesc_ != nullptr) { acldvppDestroyPicDesc(g_decodeOutputDesc_); g_decodeOutputDesc_ = nullptr; diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/ResizeHelper.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/ResizeHelper.cpp old mode 100755 new mode 100644 index 035a4617e..b6bab7004 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/ResizeHelper.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/ResizeHelper.cpp @@ -22,24 +22,27 @@ using namespace std; ResizeHelper::ResizeHelper(aclrtStream& stream, acldvppChannelDesc *dvppChannelDesc, - uint32_t width, uint32_t height): - g_stream_(stream), - g_vpcOutBufferDev_(nullptr), - g_vpcInputDesc_(nullptr), - g_vpcOutputDesc_(nullptr), - g_resizeConfig_(nullptr), - g_dvppChannelDesc_(dvppChannelDesc), - g_inDevBuffer_(nullptr), - g_vpcOutBufferSize_(0) { - g_size_.width = width; - g_size_.height = height; + uint32_t width, uint32_t height) + : g_stream_(stream), + g_vpcOutBufferDev_(nullptr), + g_vpcInputDesc_(nullptr), + g_vpcOutputDesc_(nullptr), + g_resizeConfig_(nullptr), + g_dvppChannelDesc_(dvppChannelDesc), + g_inDevBuffer_(nullptr), + g_vpcOutBufferSize_(0) +{ + g_size_.width = width; + g_size_.height = height; } -ResizeHelper::~ResizeHelper() { +ResizeHelper::~ResizeHelper() +{ DestroyResizeResource(); } -AclLiteError ResizeHelper::InitResizeInputDesc(ImageData& inputImage) { +AclLiteError ResizeHelper::InitResizeInputDesc(ImageData& inputImage) +{ uint32_t alignWidth = inputImage.alignWidth; uint32_t alignHeight = inputImage.alignHeight; if (alignWidth == 0 || alignHeight == 0) { @@ -66,7 +69,8 @@ AclLiteError ResizeHelper::InitResizeInputDesc(ImageData& inputImage) { return ACLLITE_OK; } -AclLiteError ResizeHelper::InitResizeOutputDesc() { +AclLiteError ResizeHelper::InitResizeOutputDesc() +{ int resizeOutWidth = g_size_.width; int resizeOutHeight = g_size_.height; int resizeOutWidthStride = ALIGN_UP16(resizeOutWidth); @@ -102,7 +106,8 @@ AclLiteError ResizeHelper::InitResizeOutputDesc() { return ACLLITE_OK; } -AclLiteError ResizeHelper::InitResizeResource(ImageData& inputImage) { +AclLiteError ResizeHelper::InitResizeResource(ImageData& inputImage) +{ g_resizeConfig_ = acldvppCreateResizeConfig(); if (g_resizeConfig_ == nullptr) { ACLLITE_LOG_ERROR("Dvpp resize init failed for create config failed"); @@ -113,18 +118,19 @@ AclLiteError ResizeHelper::InitResizeResource(ImageData& inputImage) { if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("InitResizeInputDesc failed"); return ret; - } + } ret = InitResizeOutputDesc(); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("InitResizeOutputDesc failed"); return ret; - } + } - return ACLLITE_OK; + return ACLLITE_OK; } -AclLiteError ResizeHelper::Process(ImageData& resizedImage, ImageData& srcImage) { +AclLiteError ResizeHelper::Process(ImageData& resizedImage, ImageData& srcImage) +{ AclLiteError atlRet = InitResizeResource(srcImage); if (atlRet != ACLLITE_OK) { ACLLITE_LOG_ERROR("Dvpp resize failed for init error"); @@ -157,7 +163,8 @@ AclLiteError ResizeHelper::Process(ImageData& resizedImage, ImageData& srcImage) return ACLLITE_OK; } -void ResizeHelper::DestroyResizeResource() { +void ResizeHelper::DestroyResizeResource() +{ if (g_resizeConfig_ != nullptr) { (void)acldvppDestroyResizeConfig(g_resizeConfig_); g_resizeConfig_ = nullptr; diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/acl.json b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/acl.json old mode 100755 new mode 100644 diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/color_classify.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/color_classify.cpp index 697a1c8c7..194a85c8f 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/color_classify.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/color_classify.cpp @@ -14,10 +14,10 @@ * limitations under the License. */ -#include "color_classify.h" #include #include #include +#include "color_classify.h" #include "acl/acl.h" #include "opencv2/opencv.hpp" #include "opencv2/imgproc/types_c.h" @@ -25,43 +25,46 @@ using namespace std; namespace { - const int32_t g_Batch = 10; - const int g_InvalidSize = -1; - const uint32_t g_LineSolid = 2; - const uint32_t g_LabelOffset = 11; - const uint32_t g_EachResultTensorNum = 9; - const double g_FountScale = 0.5; - const cv::Scalar g_FontColor(0, 0, 255); - const vector g_Colors { + const int32_t g_batch = 10; + const int g_invalidSize = -1; + const uint32_t g_lineSolid = 2; + const uint32_t g_labelOffset = 11; + const uint32_t g_eachResultTensorNum = 9; + const double g_fountScale = 0.5; + const cv::Scalar g_fontColor(0, 0, 255); + const vector g_colors { cv::Scalar(237, 149, 100), cv::Scalar(0, 215, 255), cv::Scalar(50, 205, 50), cv::Scalar(139, 85, 26) }; - const string g_CarColorClass[9] = { "black", "blue", "brown", "green", "pink", "red", + const string g_carColorClass[9] = { "black", "blue", "brown", "green", "pink", "red", "silver", "white", "yellow"}; - uint32_t g_ModelWidth = 224; - uint32_t g_ModelHeight = 224; - const char* g_ModelPath = "../model/color_dvpp_10batch.om"; + uint32_t g_modelWidth = 224; + uint32_t g_modelHeight = 224; + const char* g_modelPath = "../model/color_dvpp_10batch.om"; } -ColorClassify::ColorClassify(): -g_model_(g_ModelPath), -g_batchSize_(g_Batch), -g_isInited_(false), -g_isReleased_(false) { +ColorClassify::ColorClassify() + : g_model_(g_modelPath), + g_batchSize_(g_batch), + g_isInited_(false), + g_isReleased_(false) +{ } -ColorClassify::~ColorClassify() { +ColorClassify::~ColorClassify() +{ DestroyResource(); } -AclLiteError ColorClassify::InitModelInput() { +AclLiteError ColorClassify::InitModelInput() +{ aclError aclRet = aclrtGetRunMode(&g_runMode_); - g_inputSize_ = YUV420SP_SIZE(g_ModelWidth, g_ModelHeight) * g_batchSize_; + g_inputSize_ = YUV420SP_SIZE(g_modelWidth, g_modelHeight) * g_batchSize_; void* buf = nullptr; aclRet = aclrtMalloc(&buf, g_inputSize_, ACL_MEM_MALLOC_HUGE_FIRST); if ((buf == nullptr) || (aclRet != ACL_ERROR_NONE)) { ACLLITE_LOG_ERROR("Malloc inference input buffer failed, " - "error %d", aclRet); + "error %d", aclRet); return ACLLITE_ERROR; } g_inputBuf_ = (uint8_t *)buf; @@ -69,7 +72,8 @@ AclLiteError ColorClassify::InitModelInput() { return ACLLITE_OK; } -AclLiteError ColorClassify::Init() { +AclLiteError ColorClassify::Init() +{ if (g_isInited_) { ACLLITE_LOG_INFO("Object detection is initied already"); return ACLLITE_OK; @@ -97,19 +101,20 @@ AclLiteError ColorClassify::Init() { return ACLLITE_OK; } -AclLiteError ColorClassify::Crop(vector &carInfo, ImageData &orgImg) { +AclLiteError ColorClassify::Crop(vector &carInfo, ImageData &orgImg) +{ static int cnt = 0; AclLiteError ret = ACLLITE_OK; for (int i = 0; i < carInfo.size(); i++) { ret = g_dvpp_.Crop(carInfo[i].cropedImgs, orgImg, - carInfo[i].rectangle.lt.x, carInfo[i].rectangle.lt.y, - carInfo[i].rectangle.rb.x, carInfo[i].rectangle.rb.y); + carInfo[i].rectangle.lt.x, carInfo[i].rectangle.lt.y, + carInfo[i].rectangle.rb.x, carInfo[i].rectangle.rb.y); if (ret) { ACLLITE_LOG_ERROR("Crop image failed, error: %d, image width %d, " - "height %d, size %d, crop area (%d, %d) (%d, %d)", - ret, carInfo[i].cropedImgs.width, carInfo[i].cropedImgs.height, - carInfo[i].cropedImgs.size, carInfo[i].rectangle.lt.x, - carInfo[i].rectangle.lt.y, carInfo[i].rectangle.rb.x, + "height %d, size %d, crop area (%d, %d) (%d, %d)", + ret, carInfo[i].cropedImgs.width, carInfo[i].cropedImgs.height, + carInfo[i].cropedImgs.size, carInfo[i].rectangle.lt.x, + carInfo[i].rectangle.lt.y, carInfo[i].rectangle.rb.x, carInfo[i].rectangle.rb.y); return ACLLITE_ERROR; } @@ -118,11 +123,12 @@ AclLiteError ColorClassify::Crop(vector &carInfo, ImageData &orgImg) { return ret; } -AclLiteError ColorClassify::Resize(vector &carInfo) { +AclLiteError ColorClassify::Resize(vector &carInfo) +{ AclLiteError ret = ACLLITE_OK; for (size_t i = 0; i < carInfo.size(); i++) { - AclLiteError ret = g_dvpp_.Resize(carInfo[i].resizedImgs, carInfo[i].cropedImgs, - g_ModelWidth, g_ModelHeight); + AclLiteError ret = g_dvpp_.Resize(carInfo[i].resizedImgs, carInfo[i].cropedImgs, + g_modelWidth, g_modelHeight); if (ret) { ACLLITE_LOG_ERROR("ColorClassify Resize image failed"); return ACLLITE_ERROR; @@ -132,33 +138,33 @@ AclLiteError ColorClassify::Resize(vector &carInfo) { return ret; } -int ColorClassify::CopyOneBatchImages(uint8_t* buffer, uint32_t bufferSize, - vector &carInfo, int batchIdx) { - +int ColorClassify::CopyOneBatchImages(uint8_t* buffer, uint32_t bufferSize, + vector &carInfo, int batchIdx) +{ uint32_t j = 0; int dataLen = 0; int totalSize = 0; - for (uint32_t i = batchIdx * g_batchSize_; - i < carInfo.size() && j < g_batchSize_ && bufferSize > totalSize; + for (uint32_t i = batchIdx * g_batchSize_; + i < carInfo.size() && j < g_batchSize_ && bufferSize > totalSize; i++, j++) { - dataLen = CopyImageData(buffer + totalSize, + dataLen = CopyImageData(buffer + totalSize, bufferSize - totalSize, carInfo[i].resizedImgs); - if (dataLen == g_InvalidSize) { - return g_InvalidSize; + if (dataLen == g_invalidSize) { + return g_invalidSize; } totalSize += dataLen; } if (j < g_batchSize_) { - for (uint32_t k = 0; - k < g_batchSize_ - j && bufferSize > totalSize; + for (uint32_t k = 0; + k < g_batchSize_ - j && bufferSize > totalSize; k++) { - dataLen = CopyImageData(buffer + totalSize, - bufferSize - totalSize, - carInfo[carInfo.size() - 1].resizedImgs); - if (dataLen == g_InvalidSize) { - return g_InvalidSize; + dataLen = CopyImageData(buffer + totalSize, + bufferSize - totalSize, + carInfo[carInfo.size() - 1].resizedImgs); + if (dataLen == g_invalidSize) { + return g_invalidSize; } totalSize += dataLen; } @@ -167,20 +173,21 @@ int ColorClassify::CopyOneBatchImages(uint8_t* buffer, uint32_t bufferSize, return j; } -int ColorClassify::CopyImageData(uint8_t *buffer, uint32_t bufferSize, ImageData& image) { - +int ColorClassify::CopyImageData(uint8_t *buffer, uint32_t bufferSize, ImageData& image) +{ AclLiteError ret = ACLLITE_OK; - uint32_t dataSize = YUV420SP_SIZE(g_ModelWidth, g_ModelHeight); + uint32_t dataSize = YUV420SP_SIZE(g_modelWidth, g_modelHeight); ret = CopyDataToDeviceEx(buffer, bufferSize, image.data.get(), dataSize, g_runMode_); if (ret) { ACLLITE_LOG_ERROR("Copy data to device failed"); - return g_InvalidSize; + return g_invalidSize; } return dataSize; } -AclLiteError ColorClassify::PreProcess(ImageData& srcImage, vector &carInfo, int& flag) { +AclLiteError ColorClassify::PreProcess(ImageData& srcImage, vector &carInfo, int& flag) +{ // No car detected if (carInfo.size() == 0) { flag = 1; @@ -202,28 +209,28 @@ AclLiteError ColorClassify::PreProcess(ImageData& srcImage, vector &car ret = Resize(carInfo); if (ret) { ACLLITE_LOG_ERROR("Resize all the data failed, all the data failed"); - return ACLLITE_ERROR; + return ACLLITE_ERROR; } return ACLLITE_OK; } -AclLiteError ColorClassify::Inference(vector &carInfo, - std::vector& inferenceOutput) { - +AclLiteError ColorClassify::Inference(vector &carInfo, + std::vector& inferenceOutput) +{ int carInfoSize = carInfo.size(); int batchNum = max(carInfoSize, g_batchSize_) / g_batchSize_; for (int i = 0; i < batchNum; i++) { - // Copy one batch preprocessed image data to device - int carNum = CopyOneBatchImages(g_inputBuf_, g_inputSize_, - carInfo, i); + // Copy one batch preprocessed image data to device + int carNum = CopyOneBatchImages(g_inputBuf_, g_inputSize_, + carInfo, i); if (carNum < 0) { ACLLITE_LOG_ERROR("Copy the %dth batch images failed", i); break; } // Inference one batch data - AclLiteError ret = g_model_.Execute(inferenceOutput, - g_inputBuf_, g_inputSize_); + AclLiteError ret = g_model_.Execute(inferenceOutput, + g_inputBuf_, g_inputSize_); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Execute model inference failed\n"); break; @@ -234,8 +241,8 @@ AclLiteError ColorClassify::Inference(vector &carInfo, } AclLiteError ColorClassify::PostProcess(std::vector& inferenceOutput, - std::vector& carInfo, const string& origImagePath) { - + std::vector& carInfo, const string& origImagePath) +{ void* data = (void *)inferenceOutput[0].data.get(); if (data == nullptr) { return ACLLITE_ERROR; @@ -244,34 +251,36 @@ AclLiteError ColorClassify::PostProcess(std::vector& inferenceO outData = reinterpret_cast(data); for (int i = 0; i < carInfo.size(); i++) { - int maxConfidentIndex = i * g_EachResultTensorNum; - for (int j = 0; j < g_EachResultTensorNum; j++) { - int index = i * g_EachResultTensorNum + j; + int maxConfidentIndex = i * g_eachResultTensorNum; + for (int j = 0; j < g_eachResultTensorNum; j++) { + int index = i * g_eachResultTensorNum + j; if (outData[index] > outData[maxConfidentIndex]) { maxConfidentIndex = index; } } - int colorIndex = maxConfidentIndex - i * g_EachResultTensorNum; - carInfo[i].carColor_result = g_CarColorClass[colorIndex]; + int colorIndex = maxConfidentIndex - i * g_eachResultTensorNum; + carInfo[i].carColor_result = g_carColorClass[colorIndex]; } DrawResult(carInfo, origImagePath); } void ColorClassify::DrawResult(vector& carInfo, - const string& origImagePath) { - + const string& origImagePath) +{ cv::Mat origImage = cv::imread(origImagePath, CV_LOAD_IMAGE_UNCHANGED); for (int i = 0; i < carInfo.size(); ++i) { cv::rectangle(origImage, carInfo[i].rectangle.lt, carInfo[i].rectangle.rb, - g_Colors[i % g_Colors.size()], g_LineSolid); - cv::putText(origImage, carInfo[i].text, cv::Point(carInfo[i].rectangle.lt.x - g_LabelOffset, - carInfo[i].rectangle.lt.y - g_LabelOffset), - cv::FONT_HERSHEY_COMPLEX, g_FountScale, g_FontColor); - cv::putText(origImage, carInfo[i].carColor_result, cv::Point(carInfo[i].rectangle.lt.x, - carInfo[i].rectangle.lt.y + g_LabelOffset), - cv::FONT_HERSHEY_COMPLEX, g_FountScale, g_FontColor); + g_colors[i % g_colors.size()], g_lineSolid); + cv::putText(origImage, carInfo[i].text, + cv::Point(carInfo[i].rectangle.lt.x - g_labelOffset, + carInfo[i].rectangle.lt.y - g_labelOffset), + cv::FONT_HERSHEY_COMPLEX, g_fountScale, g_fontColor); + cv::putText(origImage, carInfo[i].carColor_result, + cv::Point(carInfo[i].rectangle.lt.x, + carInfo[i].rectangle.lt.y + g_labelOffset), + cv::FONT_HERSHEY_COMPLEX, g_fountScale, g_fontColor); ACLLITE_LOG_INFO("%d %d %d %d %s", carInfo[i].rectangle.lt.x, carInfo[i].rectangle.lt.y, - carInfo[i].rectangle.rb.x, carInfo[i].rectangle.rb.y, + carInfo[i].rectangle.rb.x, carInfo[i].rectangle.rb.y, carInfo[i].text.c_str()); } int pos = origImagePath.find_last_of("/"); @@ -294,5 +303,4 @@ void ColorClassify::DestroyResource() } g_isReleased_ = true; } - -} +} \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/main.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/main.cpp index 5893a3445..91a91fb16 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/main.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/main.cpp @@ -25,9 +25,10 @@ #include "AclLiteResource.h" using namespace std; -int main(int argc, char *argv[]) { - - if ((argc < 2) || (argv[1] == nullptr)) { +int main(int argc, char *argv[]) +{ + int argNum = 2; + if ((argc < argNum) || (argv[1] == nullptr)) { ACLLITE_LOG_ERROR("Please input: ./main "); return ACLLITE_ERROR; } @@ -36,7 +37,7 @@ int main(int argc, char *argv[]) { if (ret) { ACLLITE_LOG_ERROR("Init resource failed, error %d", ret); return ACLLITE_ERROR; - } + } ObjectDetect detect; ret = detect.Init(); @@ -72,7 +73,7 @@ int main(int argc, char *argv[]) { ret = detect.PreProcess(resizedImage, image, yuvImage); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Read file %s failed, continue to read next", - imageFile.c_str()); + imageFile.c_str()); continue; } std::vector detectInferOutput; @@ -109,7 +110,6 @@ int main(int argc, char *argv[]) { ACLLITE_LOG_ERROR("Process model inference output data failed"); return ACLLITE_ERROR; } - } ACLLITE_LOG_INFO("Execute sample success"); return ACLLITE_OK; diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/object_detect.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/object_detect.cpp index 1bb19c670..8240b88b4 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/object_detect.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/object_detect.cpp @@ -14,9 +14,9 @@ * limitations under the License. */ -#include "object_detect.h" #include #include +#include "object_detect.h" #include "acl/acl.h" #include "opencv2/opencv.hpp" #include "opencv2/imgproc/types_c.h" @@ -26,30 +26,33 @@ using namespace std; namespace { const uint32_t g_BBoxDataBufId = 0; const uint32_t g_BoxNumDataBufId = 1; -enum BBoxIndex { TOPLEFTX = 0, TOPLEFTY, BOTTOMRIGHTX, BOTTOMRIGHTY, SCORE, LABEL }; -uint32_t g_ModelWidth = 416; -uint32_t g_ModelHeight = 416; -const char* g_ModelPath = "../model/yolov3.om"; +enum BBoxIndex { TOPLEFTX = 0, TOPLEFTY, BOTTOMRIGHTX, BOTTOMRIGHTY, SCORE, LABEL }; +uint32_t g_modelWidth = 416; +uint32_t g_modelHeight = 416; +const char* g_modelPath = "../model/yolov3.om"; } -ObjectDetect::ObjectDetect(): -g_model_(g_ModelPath), -g_isInited_(false), -g_isReleased_(false) { +ObjectDetect::ObjectDetect() + : g_model_(g_modelPath), + g_isInited_(false), + g_isReleased_(false) +{ g_imageInfoSize_ = 0; g_imageInfoBuf_ = nullptr; } -ObjectDetect::~ObjectDetect() { +ObjectDetect::~ObjectDetect() +{ DestroyResource(); } -AclLiteError ObjectDetect::InitModelInput() { - const float imageInfo[4] = {(float)g_ModelWidth, (float)g_ModelHeight, - (float)g_ModelWidth, (float)g_ModelHeight}; +AclLiteError ObjectDetect::InitModelInput() +{ + const float imageInfo[4] = {(float)g_modelWidth, (float)g_modelHeight, + (float)g_modelWidth, (float)g_modelHeight}; g_imageInfoSize_ = sizeof(imageInfo); g_imageInfoBuf_ = CopyDataToDevice((void *)imageInfo, g_imageInfoSize_, - g_runMode_, MEMORY_DEVICE); + g_runMode_, MEMORY_DEVICE); if (g_imageInfoBuf_ == nullptr) { ACLLITE_LOG_ERROR("Copy image info to device failed"); return ACLLITE_ERROR; @@ -58,7 +61,8 @@ AclLiteError ObjectDetect::InitModelInput() { return ACLLITE_OK; } -AclLiteError ObjectDetect::Init() { +AclLiteError ObjectDetect::Init() +{ if (g_isInited_) { ACLLITE_LOG_INFO("Object detection is initied already"); return ACLLITE_OK; @@ -91,7 +95,8 @@ AclLiteError ObjectDetect::Init() { return ACLLITE_OK; } -AclLiteError ObjectDetect::PreProcess(ImageData& resizedImage, ImageData& srcImage, ImageData& yuvImage) { +AclLiteError ObjectDetect::PreProcess(ImageData& resizedImage, ImageData& srcImage, ImageData& yuvImage) +{ ImageData imageDevice; AclLiteError ret = CopyImageToDevice(imageDevice, srcImage, g_runMode_, MEMORY_DVPP); @@ -106,8 +111,8 @@ AclLiteError ObjectDetect::PreProcess(ImageData& resizedImage, ImageData& srcIma return ACLLITE_ERROR; } - ret = g_dvpp_.CropPaste(resizedImage, yuvImage, g_ModelWidth, g_ModelHeight, - 0, 0, yuvImage.width, yuvImage.height); + ret = g_dvpp_.CropPaste(resizedImage, yuvImage, g_modelWidth, g_modelHeight, + 0, 0, yuvImage.width, yuvImage.height); if (ret == ACLLITE_ERROR) { ACLLITE_LOG_ERROR("CropResolution image failed"); return ACLLITE_ERROR; @@ -117,9 +122,10 @@ AclLiteError ObjectDetect::PreProcess(ImageData& resizedImage, ImageData& srcIma } AclLiteError ObjectDetect::Inference(std::vector& inferenceOutput, - ImageData& resizedImage) { - AclLiteError ret = g_model_.CreateInput(resizedImage.data.get(), resizedImage.size, - g_imageInfoBuf_, g_imageInfoSize_); + ImageData& resizedImage) +{ + AclLiteError ret = g_model_.CreateInput(resizedImage.data.get(), resizedImage.size, + g_imageInfoBuf_, g_imageInfoSize_); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Create mode input dataset failed"); return ACLLITE_ERROR; @@ -135,7 +141,8 @@ AclLiteError ObjectDetect::Inference(std::vector& inferenceOutp } AclLiteError ObjectDetect::PostProcess(ImageData& image, std::vector& inferenceOutput, - std::vector& carData) { + std::vector& carData) +{ float* detectData = (float *)inferenceOutput[g_BBoxDataBufId].data.get(); if (detectData == nullptr) { ACLLITE_LOG_ERROR("inferoutput is null\n"); @@ -143,17 +150,19 @@ AclLiteError ObjectDetect::PostProcess(ImageData& image, std::vector #include "utils.h" #include "acl/acl.h" diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/inc/object_detect.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/inc/object_detect.h old mode 100755 new mode 100644 index 5eee48aee..4787b70f5 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/inc/object_detect.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/inc/object_detect.h @@ -19,11 +19,11 @@ #pragma once +#include #include "utils.h" #include "acl/acl.h" #include "model_process.h" #include "dvpp_process.h" -#include using namespace std; @@ -37,13 +37,12 @@ public: ~ObjectDetect(); // Inference initialization Result Init(int imgWidth, int imgHeight); - // nference frame image preprocessing + // Inference frame image preprocessing Result Preprocess(cv::Mat& frame); // Inference frame picture Result Inference(aclmdlDataset*& inferenceOutput); // Inference output post-processing Result Postprocess(cv::Mat& frame, aclmdlDataset* modelOutput); - // void DestroyDvpp(); // Release the requested resources void DestroyResource(); @@ -72,7 +71,7 @@ private: uint32_t g_imageInfoSize_; void* g_imageInfoBuf_; aclrtRunMode g_runMode_; // Run mode, which is whether the current application is running on atlas200DK or AI1 - bool g_isInited_; // Initializes the tag to prevent inference instances from being initialized multiple times + bool g_isInited_; }; #endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/inc/utils.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/inc/utils.h old mode 100755 new mode 100644 index bf14b2512..d5ac524cc --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/inc/utils.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/inc/utils.h @@ -92,8 +92,6 @@ public: static void* CopyDataDeviceToLocal(void* deviceData, uint32_t dataSize); static void* CopyDataHostToDevice(void* deviceData, uint32_t dataSize); static void* CopyDataDeviceToDevice(void* deviceData, uint32_t dataSize); - // static int ReadImageFile(ImageData& image, std::string fileName); - // static Result CopyImageDataToDevice(ImageData& imageDevice, ImageData srcImage, aclrtRunMode mode); }; #endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/CMakeLists.txt b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/dvpp_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/dvpp_process.cpp old mode 100755 new mode 100644 index 246c70ed8..e66eed293 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/dvpp_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/dvpp_process.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,32 +12,32 @@ * 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 dvpp_process.cpp -* Description: handle dvpp process */ #include +#include #include "acl/acl.h" #include "utils.h" -#include #include "dvpp_process.h" using namespace std; extern FILE *outFileFp; -bool g_runFlag_= true; -void *ThreadFunc(void *arg) { +bool g_runFlag = true; +void *ThreadFunc(void *arg) +{ int deviceId = 0; + int32_t timeout = 1000; aclrtContext context = nullptr; aclrtCreateContext(&context, deviceId); INFO_LOG("process callback thread start "); - while (g_runFlag_) { - (void)aclrtProcessReport(1000); + while (g_runFlag) { + (void)aclrtProcessReport(timeout); } aclrtDestroyContext(context); - return (void*)0; + return static_cast(nullptr); } -bool WriteToFile(FILE *outFileFp_, const void *dataDev, uint32_t dataSize) { +bool WriteToFile(FILE *outFileFp_, const void *dataDev, uint32_t dataSize) +{ bool ret = true; size_t writeRet = fwrite(dataDev, 1, dataSize, outFileFp_); if (writeRet != dataSize) { @@ -48,7 +48,8 @@ bool WriteToFile(FILE *outFileFp_, const void *dataDev, uint32_t dataSize) { return ret; } -void callback(acldvppPicDesc *input, acldvppStreamDesc *g_outputStreamDesc, void *userdata) { +void callback(acldvppPicDesc *input, acldvppStreamDesc *g_outputStreamDesc, void *userdata) +{ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); void *outputDev = acldvppGetStreamDescData(g_outputStreamDesc); uint32_t streamDescSize = acldvppGetStreamDescSize(g_outputStreamDesc); @@ -62,8 +63,7 @@ void callback(acldvppPicDesc *input, acldvppStreamDesc *g_outputStreamDesc, void aclrtMemcpy(hostPtr, streamDescSize, outputDev, streamDescSize, ACL_MEMCPY_DEVICE_TO_HOST); ret = WriteToFile(outFileFp, hostPtr, streamDescSize); (void)aclrtFreeHost(hostPtr); - } - else { + } else { ret = WriteToFile(outFileFp, outputDev, streamDescSize); } @@ -74,15 +74,21 @@ void callback(acldvppPicDesc *input, acldvppStreamDesc *g_outputStreamDesc, void } DvppProcess::DvppProcess() -:g_vencChannelDesc_(nullptr), g_enType_(2),g_vencFrameConfig_(nullptr), -g_vpcInputDesc_(nullptr), g_outputStreamDesc(nullptr), g_codeInputBufferDev_(nullptr) { + : g_vencChannelDesc_(nullptr), + g_enType_(2), + g_vencFrameConfig_(nullptr), + g_vpcInputDesc_(nullptr), + g_outputStreamDesc(nullptr), + g_codeInputBufferDev_(nullptr) +{ } -DvppProcess::~DvppProcess() { - // DestroyResource(); +DvppProcess::~DvppProcess() +{ } -Result DvppProcess::InitResource(aclrtStream& stream, int imgWidth, int imgHeight) { +Result DvppProcess::InitResource(aclrtStream& stream, int imgWidth, int imgHeight) +{ g_stream_ = stream; aclError ret = aclrtGetRunMode(&g_runMode); if (ret != ACL_SUCCESS) { @@ -96,7 +102,7 @@ Result DvppProcess::InitResource(aclrtStream& stream, int imgWidth, int imgHeigh uint32_t alignHeight = ALIGN_UP16(height); if (alignWidth == 0 || alignHeight == 0) { ERROR_LOG("InitCodeInputDesc AlignmentHelper failed. image w %d, h %d, align w%d, h%d", - width, height, alignWidth, alignHeight); + width, height, alignWidth, alignHeight); return FAILED; } // Allocate a large enough memory @@ -105,7 +111,6 @@ Result DvppProcess::InitResource(aclrtStream& stream, int imgWidth, int imgHeigh if (ret != ACL_SUCCESS) { ERROR_LOG("acldvppMalloc failed"); } - g_format_ = static_cast(PIXEL_FORMAT_YVU_SEMIPLANAR_420); g_vencFrameConfig_ = aclvencCreateFrameConfig(); aclvencSetFrameConfigForceIFrame(g_vencFrameConfig_, 0); @@ -113,7 +118,6 @@ Result DvppProcess::InitResource(aclrtStream& stream, int imgWidth, int imgHeigh ERROR_LOG("Dvpp init failed for create config failed"); return FAILED; } - g_vencChannelDesc_ = aclvencCreateChannelDesc(); if (g_vencChannelDesc_ == nullptr) { ERROR_LOG("aclvencCreateChannelDesc failed"); @@ -127,7 +131,6 @@ Result DvppProcess::InitResource(aclrtStream& stream, int imgWidth, int imgHeigh aclvencSetChannelDescPicWidth(g_vencChannelDesc_, width); aclvencSetChannelDescPicHeight(g_vencChannelDesc_, height); aclvencCreateChannel(g_vencChannelDesc_); - g_vpcInputDesc_ = acldvppCreatePicDesc(); if (g_vpcInputDesc_ == nullptr) { ERROR_LOG("acldvppCreatePicDesc g_vpcInputDesc_ failed"); @@ -142,18 +145,20 @@ Result DvppProcess::InitResource(aclrtStream& stream, int imgWidth, int imgHeigh return SUCCESS; } -Result DvppProcess::Venc(cv::Mat& srcImage) { +Result DvppProcess::Venc(cv::Mat& srcImage) +{ aclError ret; if (g_runMode == ACL_HOST) { - ret = aclrtMemcpy(g_codeInputBufferDev_, g_inputBufferSize, srcImage.data, g_inputBufferSize, ACL_MEMCPY_HOST_TO_DEVICE); + ret = aclrtMemcpy(g_codeInputBufferDev_, g_inputBufferSize, srcImage.data, + g_inputBufferSize, ACL_MEMCPY_HOST_TO_DEVICE); if (ret != ACL_SUCCESS) { - // ERROR_LOG("aclrtMemcpy failed"); + ERROR_LOG("aclrtMemcpy failed"); } - } - else { - ret = aclrtMemcpy(g_codeInputBufferDev_, g_inputBufferSize, srcImage.data, g_inputBufferSize, ACL_MEMCPY_DEVICE_TO_DEVICE); + } else { + ret = aclrtMemcpy(g_codeInputBufferDev_, g_inputBufferSize, + srcImage.data, g_inputBufferSize, ACL_MEMCPY_DEVICE_TO_DEVICE); if (ret != ACL_SUCCESS) { - // ERROR_LOG("aclrtMemcpy failed"); + ERROR_LOG("aclrtMemcpy failed"); } } @@ -168,8 +173,8 @@ Result DvppProcess::Venc(cv::Mat& srcImage) { return SUCCESS; } -void DvppProcess::DestroyResource() { - +void DvppProcess::DestroyResource() +{ aclvencSetFrameConfigEos(g_vencFrameConfig_, 1); aclvencSetFrameConfigForceIFrame(g_vencFrameConfig_, 0); aclvencSendFrame(g_vencChannelDesc_, nullptr, nullptr, g_vencFrameConfig_, nullptr); @@ -205,7 +210,7 @@ void DvppProcess::DestroyResource() { } (void)aclrtUnSubscribeReport(static_cast(g_threadId_), g_stream_); - g_runFlag_ = false; + g_runFlag = false; void *res = nullptr; pthread_cancel(g_threadId_); int joinThreadErr = pthread_join(g_threadId_, &res); diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/main.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/main.cpp old mode 100755 new mode 100644 index 3239e7e94..2c520f033 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/main.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/main.cpp @@ -26,14 +26,16 @@ using namespace std; namespace { - uint32_t g_ModelWidth = 416; - uint32_t g_ModelHeight = 416; - const char* g_ModelPath = "../model/yolov3.om"; + uint32_t g_modelWidth = 416; + uint32_t g_modelHeight = 416; + const char* g_modelPath = "../model/yolov3.om"; } FILE *outFileFp; -int main(int argc, char *argv[]) { +int main(int argc, char *argv[]) +{ // Check the input when the application executes, which takes the path to the input video file - if ((argc < 2) || (argv[1] == nullptr)) { + int argNum = 2; + if ((argc < argNum) || (argv[1] == nullptr)) { ERROR_LOG("Please input: ./main "); return FAILED; } @@ -52,8 +54,8 @@ int main(int argc, char *argv[]) { int imgWidth = static_cast(capture.get(cv::CAP_PROP_FRAME_WIDTH)); // Instantiate the object detection class - ObjectDetect detect(g_ModelPath, g_ModelWidth, g_ModelHeight); - // Initializes the ACL resource for categorical reasoning, dvpp, loads the model + ObjectDetect detect(g_modelPath, g_modelWidth, g_modelHeight); + // Initializes the ACL resource for categorical reasoning, dvpp, loads the model // and requests the memory used for reasoning input Result ret = detect.Init(imgWidth, imgHeight); if (ret != SUCCESS) { @@ -81,7 +83,6 @@ int main(int argc, char *argv[]) { // Read a frame of an image if (!capture.read(frame)) { INFO_LOG("Video capture return false"); - // detect.DestroyDvpp(); detect.DestroyResource(); break; } @@ -89,7 +90,7 @@ int main(int argc, char *argv[]) { Result ret = detect.Preprocess(frame); if (ret != SUCCESS) { ERROR_LOG("Read file %s failed, continue to read next", - videoFile.c_str()); + videoFile.c_str()); continue; } // The preprocessed images are fed into model reasoning and the reasoning results are obtained @@ -107,7 +108,9 @@ int main(int argc, char *argv[]) { } } fclose(outFileFp); - sleep(10); + outFileFp = nullptr; + int timep = 10; + sleep(timep); INFO_LOG("Execute video object detection success"); return SUCCESS; } diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/model_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/model_process.cpp old mode 100755 new mode 100644 index 91698060c..c1199cfb2 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/model_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/model_process.cpp @@ -14,22 +14,32 @@ * limitations under the License. */ -#include "model_process.h" #include +#include "model_process.h" #include "utils.h" using namespace std; -ModelProcess::ModelProcess():g_loadFlag_(false), g_modelId_(0), g_modelMemPtr_(nullptr), g_modelMemSize_(0), -g_modelWeightPtr_(nullptr),g_modelWeightSize_(0), g_modelDesc_(nullptr), g_input_(nullptr), g_output_(nullptr), -g_isReleased_(false) { - +ModelProcess::ModelProcess() + : g_loadFlag_(false), + g_modelId_(0), + g_modelMemPtr_(nullptr), + g_modelMemSize_(0), + g_modelWeightPtr_(nullptr), + g_modelWeightSize_(0), + g_modelDesc_(nullptr), + g_input_(nullptr), + g_output_(nullptr), + g_isReleased_(false) +{ } -ModelProcess::~ModelProcess() { +ModelProcess::~ModelProcess() +{ DestroyResource(); } -void ModelProcess::DestroyResource() { +void ModelProcess::DestroyResource() +{ if (g_isReleased_) return; @@ -40,7 +50,8 @@ void ModelProcess::DestroyResource() { g_isReleased_ = true; } -Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath) { +Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath) +{ if (g_loadFlag_) { ERROR_LOG("has already loaded a model"); return FAILED; @@ -94,15 +105,17 @@ Result ModelProcess::CreateDesc() return SUCCESS; } -void ModelProcess::DestroyDesc() { +void ModelProcess::DestroyDesc() +{ if (g_modelDesc_ != nullptr) { (void)aclmdlDestroyDesc(g_modelDesc_); g_modelDesc_ = nullptr; } } -Result ModelProcess::CreateInput(void *input1, size_t input1Size, - void* input2, size_t input2Size) { +Result ModelProcess::CreateInput(void *input1, size_t input1Size, + void* input2, size_t input2Size) +{ g_input_ = aclmdlCreateDataset(); if (g_input_ == nullptr) { ERROR_LOG("can't create dataset, create input failed"); @@ -140,7 +153,8 @@ Result ModelProcess::CreateInput(void *input1, size_t input1Size, return SUCCESS; } -void ModelProcess::DestroyInput() { +void ModelProcess::DestroyInput() +{ if (g_input_ == nullptr) { return; } @@ -153,7 +167,8 @@ void ModelProcess::DestroyInput() { g_input_ = nullptr; } -Result ModelProcess::CreateOutput() { +Result ModelProcess::CreateOutput() +{ if (g_modelDesc_ == nullptr) { ERROR_LOG("no model description, create ouput failed"); return FAILED; @@ -196,7 +211,8 @@ Result ModelProcess::CreateOutput() { return SUCCESS; } -void ModelProcess::DestroyOutput() { +void ModelProcess::DestroyOutput() +{ if (g_output_ == nullptr) { return; } @@ -212,7 +228,8 @@ void ModelProcess::DestroyOutput() { g_output_ = nullptr; } -Result ModelProcess::Execute() { +Result ModelProcess::Execute() +{ aclError ret = aclmdlExecute(g_modelId_, g_input_, g_output_); if (ret != ACL_SUCCESS) { ERROR_LOG("execute model failed, modelId is %u", g_modelId_); @@ -223,7 +240,8 @@ Result ModelProcess::Execute() { return SUCCESS; } -void ModelProcess::Unload() { +void ModelProcess::Unload() +{ if (!g_loadFlag_) { WARN_LOG("no model had been loaded, unload failed"); return; @@ -255,6 +273,7 @@ void ModelProcess::Unload() { INFO_LOG("unload model success, modelId is %u", g_modelId_); } -aclmdlDataset *ModelProcess::GetModelOutputData() { +aclmdlDataset *ModelProcess::GetModelOutputData() +{ return g_output_; } \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/object_detect.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/object_detect.cpp old mode 100755 new mode 100644 index 1c3c3f9b6..5ef3aa03d --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/object_detect.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/object_detect.cpp @@ -15,44 +15,43 @@ */ #include +#include #include "object_detect.h" #include "opencv2/opencv.hpp" - #include "acl/acl.h" #include "model_process.h" #include "utils.h" -#include using namespace std; namespace { const static std::vector yolov3Label = { "person", "bicycle", "car", "motorbike", - "aeroplane","bus", "train", "truck", "boat", - "traffic light", "fire hydrant", "stop sign", "parking meter", - "bench", "bird", "cat", "dog", "horse", - "sheep", "cow", "elephant", "bear", "zebra", - "giraffe", "backpack", "umbrella", "handbag","tie", - "suitcase", "frisbee", "skis", "snowboard", "sports ball", - "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", - "tennis racket", "bottle", "wine glass", "cup", - "fork", "knife", "spoon", "bowl", "banana", - "apple", "sandwich", "orange", "broccoli", "carrot", - "hot dog", "pizza", "donut", "cake", "chair", - "sofa", "potted plant", "bed", "dining table", "toilet", - "TV monitor", "laptop", "mouse", "remote", "keyboard", - "cell phone", "microwave", "oven", "toaster", "sink", - "refrigerator", "book", "clock", "vase","scissors", - "teddy bear", "hair drier", "toothbrush" }; - const uint32_t kBBoxDataBufId = 0; - const uint32_t kBoxNumDataBufId = 1; + "aeroplane","bus", "train", "truck", "boat", + "traffic light", "fire hydrant", "stop sign", "parking meter", + "bench", "bird", "cat", "dog", "horse", + "sheep", "cow", "elephant", "bear", "zebra", + "giraffe", "backpack", "umbrella", "handbag", "tie", + "suitcase", "frisbee", "skis", "snowboard", "sports ball", + "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", + "tennis racket", "bottle", "wine glass", "cup", + "fork", "knife", "spoon", "bowl", "banana", + "apple", "sandwich", "orange", "broccoli", "carrot", + "hot dog", "pizza", "donut", "cake", "chair", + "sofa", "potted plant", "bed", "dining table", "toilet", + "TV monitor", "laptop", "mouse", "remote", "keyboard", + "cell phone", "microwave", "oven", "toaster", "sink", + "refrigerator", "book", "clock", "vase","scissors", + "teddy bear", "hair drier", "toothbrush"}; + const uint32_t g_bBoxDataBufId = 0; + const uint32_t g_boxNumDataBufId = 1; enum BBoxIndex { TOPLEFTX = 0, TOPLEFTY, BOTTOMRIGHTX, BOTTOMRIGHTY, SCORE, LABEL }; - const uint32_t kLineSolid = 2; - const string kOutputFilePrefix = "out_"; - const double kFountScale = 0.5; - const cv::Scalar kFontColor(0, 0, 255); - const uint32_t kLabelOffset = 11; - const string kFileSperator = "/"; - const vector kColors { + const uint32_t g_lineSolid = 2; + const string g_outputFilePrefix = "out_"; + const double g_fountScale = 0.5; + const cv::Scalar g_fontColor(0, 0, 255); + const uint32_t g_labelOffset = 11; + const string g_fileSperator = "/"; + const vector g_colors { cv::Scalar(237, 149, 100), cv::Scalar(0, 215, 255), cv::Scalar(50, 205, 50), cv::Scalar(139, 85, 26) }; @@ -60,18 +59,26 @@ namespace { ObjectDetect::ObjectDetect(const char* modelPath, uint32_t modelWidth, uint32_t modelHeight) -:g_deviceId_(0), g_context_(nullptr), g_stream_(nullptr), g_modelWidth_(modelWidth), -g_modelHeight_(modelHeight), g_isInited_(false), g_imageDataBuf_(nullptr), g_imageInfoBuf_(nullptr) { + : g_deviceId_(0), + g_context_(nullptr), + g_stream_(nullptr), + g_modelWidth_(modelWidth), + g_modelHeight_(modelHeight), + g_isInited_(false), + g_imageDataBuf_(nullptr), + g_imageInfoBuf_(nullptr) +{ g_imageInfoSize_ = 0; g_modelPath_ = modelPath; g_imageDataSize_ = RGBU8_IMAGE_SIZE(modelWidth, modelHeight); } -ObjectDetect::~ObjectDetect() { - // DestroyResource(); +ObjectDetect::~ObjectDetect() +{ } -Result ObjectDetect::InitResource() { +Result ObjectDetect::InitResource() +{ // ACL init const char *aclConfigPath = "../src/acl.json"; aclError ret = aclInit(aclConfigPath); @@ -147,17 +154,17 @@ Result ObjectDetect::CreateModelInputdDataset() } // The second input to Yolov3 is the input image width and height parameter const float imageInfo[4] = {(float)g_modelWidth_, (float)g_modelHeight_, - (float)g_modelWidth_, (float)g_modelHeight_}; + (float)g_modelWidth_, (float)g_modelHeight_}; g_imageInfoSize_ = sizeof(imageInfo); if (g_runMode_ == ACL_HOST) g_imageInfoBuf_ = Utils::CopyDataHostToDevice((void *)imageInfo, g_imageInfoSize_); - else + else g_imageInfoBuf_ = Utils::CopyDataDeviceToDevice((void *)imageInfo, g_imageInfoSize_); if (g_imageInfoBuf_ == nullptr) { ERROR_LOG("Copy image info to device failed"); return FAILED; } - // Use the applied memory to create the model and input dataset. After creation, + // Use the applied memory to create the model and input dataset. After creation, // only update the memory data for each frame of inference, instead of creating the input dataset every time Result ret = g_model_.CreateInput(g_imageDataBuf_, g_imageDataSize_, g_imageInfoBuf_, g_imageInfoSize_); @@ -169,8 +176,8 @@ Result ObjectDetect::CreateModelInputdDataset() return SUCCESS; } -Result ObjectDetect::Init(int imgWidth, int imgHeight) { - +Result ObjectDetect::Init(int imgWidth, int imgHeight) +{ if (g_isInited_) { INFO_LOG("Object detection instance is initied already!"); return SUCCESS; @@ -204,7 +211,8 @@ Result ObjectDetect::Init(int imgWidth, int imgHeight) { return SUCCESS; } -Result ObjectDetect::Preprocess(cv::Mat& frame) { +Result ObjectDetect::Preprocess(cv::Mat& frame) +{ // Scale the frame image to the desired size of the model cv::Mat reiszeMat; cv::resize(frame, reiszeMat, cv::Size(g_modelWidth_, g_modelHeight_)); @@ -225,7 +233,8 @@ Result ObjectDetect::Preprocess(cv::Mat& frame) { return SUCCESS; } -Result ObjectDetect::Inference(aclmdlDataset*& inferenceOutput) { +Result ObjectDetect::Inference(aclmdlDataset*& inferenceOutput) +{ Result ret = g_model_.Execute(); if (ret != SUCCESS) { ERROR_LOG("Execute model inference failed"); @@ -236,24 +245,26 @@ Result ObjectDetect::Inference(aclmdlDataset*& inferenceOutput) { return SUCCESS; } -Result ObjectDetect::Postprocess(cv::Mat& frame, aclmdlDataset* modelOutput) { +Result ObjectDetect::Postprocess(cv::Mat& frame, aclmdlDataset* modelOutput) +{ uint32_t dataSize = 0; float* detectData = (float*)GetInferenceOutputItem(dataSize, modelOutput, - kBBoxDataBufId); + g_bBoxDataBufId); if (detectData == nullptr) return FAILED; uint32_t* boxNum = (uint32_t*)GetInferenceOutputItem(dataSize, modelOutput, - kBoxNumDataBufId); + g_boxNumDataBufId); if (boxNum == nullptr) return FAILED; uint32_t totalBox = boxNum[0]; + uint32_t scoreLine = 90; vector detectResults; float widthScale = (float)(frame.cols) / g_modelWidth_; float heightScale = (float)(frame.rows) / g_modelHeight_; for (uint32_t i = 0; i < totalBox; i++) { BBox boundBox; uint32_t score = uint32_t(detectData[totalBox * SCORE + i] * 100); - if (score < 90) continue; + if (score < scoreLine) continue; boundBox.rect.ltX = detectData[totalBox * TOPLEFTX + i] * widthScale; boundBox.rect.ltY = detectData[totalBox * TOPLEFTY + i] * heightScale; @@ -275,26 +286,26 @@ Result ObjectDetect::Postprocess(cv::Mat& frame, aclmdlDataset* modelOutput) { } void* ObjectDetect::GetInferenceOutputItem(uint32_t& itemDataSize, - aclmdlDataset* inferenceOutput,uint32_t idx) { - + aclmdlDataset* inferenceOutput,uint32_t idx) +{ aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(inferenceOutput, idx); if (dataBuffer == nullptr) { - ERROR_LOG("Get the %dth dataset buffer from model " - "inference output failed", idx); + ERROR_LOG("Get the %dth dataset buffer from model" + "inference output failed", idx); return nullptr; } void* dataBufferDev = aclGetDataBufferAddr(dataBuffer); if (dataBufferDev == nullptr) { ERROR_LOG("Get the %dth dataset buffer address " - "from model inference output failed", idx); + "from model inference output failed", idx); return nullptr; } size_t bufferSize = aclGetDataBufferSize(dataBuffer); if (bufferSize == 0) { ERROR_LOG("The %dth dataset buffer size of " - "model inference output is 0", idx); + "model inference output is 0", idx); return nullptr; } @@ -305,8 +316,7 @@ void* ObjectDetect::GetInferenceOutputItem(uint32_t& itemDataSize, ERROR_LOG("Copy inference output to host failed"); return nullptr; } - } - else { + } else { data = dataBufferDev; } @@ -314,16 +324,17 @@ void* ObjectDetect::GetInferenceOutputItem(uint32_t& itemDataSize, return data; } -Result ObjectDetect::DrawBoundBoxToImage(vector& detectionResults,cv::Mat& origImg) { +Result ObjectDetect::DrawBoundBoxToImage(vector& detectionResults,cv::Mat& origImg) +{ for (int i = 0; i < detectionResults.size(); ++i) { cv::Point p1, p2; p1.x = detectionResults[i].rect.ltX; p1.y = detectionResults[i].rect.ltY; p2.x = detectionResults[i].rect.rbX; p2.y = detectionResults[i].rect.rbY; - cv::rectangle(origImg, p1, p2, kColors[i % kColors.size()], kLineSolid); - cv::putText(origImg, detectionResults[i].text, cv::Point(p1.x, p1.y + kLabelOffset), - cv::FONT_HERSHEY_COMPLEX, kFountScale, kFontColor); + cv::rectangle(origImg, p1, p2, g_colors[i % g_colors.size()], g_lineSolid); + cv::putText(origImg, detectionResults[i].text, cv::Point(p1.x, p1.y + g_labelOffset), + cv::FONT_HERSHEY_COMPLEX, g_fountScale, g_fontColor); } int cols = origImg.cols; int rows = origImg.rows; @@ -332,7 +343,7 @@ Result ObjectDetect::DrawBoundBoxToImage(vector& detectionResults,cv::Mat& cv::Mat NV21(rows + rows / 2, cols, CV_8UC1); int UVRow{ 0 }; for (int i = 0; i < rows; i++) { - for (int j=0; j(i); int B = origImg.at(i, j)[0]; int G = origImg.at(i, j)[1]; @@ -356,11 +367,12 @@ Result ObjectDetect::DrawBoundBoxToImage(vector& detectionResults,cv::Mat& return SUCCESS; } -void ObjectDetect::DestroyResource() { +void ObjectDetect::DestroyResource() +{ aclrtFree(g_imageDataBuf_); aclrtFree(g_imageInfoBuf_); - // The ACL resource held by the model instance must be released + // The ACL resource held by the model instance must be released // before the ACL exits or ABORT will be torn down g_dvpp_.DestroyResource(); g_model_.DestroyResource(); @@ -393,5 +405,4 @@ void ObjectDetect::DestroyResource() { ERROR_LOG("finalize acl failed"); } INFO_LOG("end to finalize acl"); - -} +} \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/utils.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/utils.cpp old mode 100755 new mode 100644 index 318d3b5c6..c7a905de1 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/utils.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/utils.cpp @@ -14,7 +14,6 @@ * limitations under the License. */ -#include "utils.h" #include #include #include @@ -25,21 +24,23 @@ #include #include #include +#include "utils.h" #include "acl/acl.h" #include "acl/ops/acl_dvpp.h" using namespace std; namespace { -const std::string g_ImagePathSeparator = ","; -const int g_StatSuccess = 0; -const std::string g_FileSperator = "/"; -const std::string g_PathSeparator = "/"; -const std::string g_OutputFilePrefix = "out_"; +const std::string g_imagePathSeparator = ","; +const int g_statSuccess = 0; +const std::string g_fileSperator = "/"; +const std::string g_pathSeparator = "/"; +const std::string g_outputFilePrefix = "out_"; } -string Utils::getTime() { +string Utils::getTime() +{ time_t timep; time (& timep); char tmp[64]; @@ -47,10 +48,11 @@ string Utils::getTime() { return tmp; } -bool Utils::IsDirectory(const string &path) { +bool Utils::IsDirectory(const string &path) +{ // get path stat struct stat buf; - if (stat(path.c_str(), &buf) != g_StatSuccess) { + if (stat(path.c_str(), &buf) != g_statSuccess) { return false; } @@ -62,7 +64,8 @@ bool Utils::IsDirectory(const string &path) { } } -bool Utils::IsPathExist(const string &path) { +bool Utils::IsPathExist(const string &path) +{ ifstream file(path); if (!file) { return false; @@ -70,17 +73,17 @@ bool Utils::IsPathExist(const string &path) { return true; } -void Utils::SplitPath(const string &path, vector &path_vec) { - char *char_path = const_cast(path.c_str()); - const char *char_split = g_ImagePathSeparator.c_str(); - char *tmp_path = strtok(char_path, char_split); +void Utils::SplitPath(const string &path, vector &path_vec) +{ + char *tmp_path = strtok(const_castpath.c_str(), g_imagePathSeparator.c_str()); while (tmp_path) { path_vec.emplace_back(tmp_path); tmp_path = strtok(nullptr, char_split); } } -void Utils::GetAllFiles(const string &path, vector &file_vec) { +void Utils::GetAllFiles(const string &path, vector &file_vec) +{ // split file path vector path_vector; SplitPath(path, path_vector); @@ -89,7 +92,7 @@ void Utils::GetAllFiles(const string &path, vector &file_vec) { // check path exist or not if (!IsPathExist(every_path)) { ERROR_LOG("Failed to deal path=%s. Reason: not exist " - "or can not access.", every_path.c_str()); + "or can not access.", every_path.c_str()); continue; } // get files in path and sub-path @@ -97,7 +100,8 @@ void Utils::GetAllFiles(const string &path, vector &file_vec) { } } -void Utils::GetPathFiles(const string &path, vector &file_vec) { +void Utils::GetPathFiles(const string &path, vector &file_vec) +{ struct dirent *dirent_ptr = nullptr; DIR *dir = nullptr; if (IsDirectory(path)) { @@ -109,7 +113,7 @@ void Utils::GetPathFiles(const string &path, vector &file_vec) { } // file path - string full_path = path + g_PathSeparator + dirent_ptr->d_name; + string full_path = path + g_pathSeparator + dirent_ptr->d_name; // directory need recursion if (IsDirectory(full_path)) { GetPathFiles(full_path, file_vec); @@ -123,7 +127,8 @@ void Utils::GetPathFiles(const string &path, vector &file_vec) { } } -void* Utils::CopyDataDeviceToLocal(void* deviceData, uint32_t dataSize) { +void* Utils::CopyDataDeviceToLocal(void* deviceData, uint32_t dataSize) +{ uint8_t* buffer = new uint8_t[dataSize]; if (buffer == nullptr) { ERROR_LOG("New malloc memory failed"); @@ -140,7 +145,8 @@ void* Utils::CopyDataDeviceToLocal(void* deviceData, uint32_t dataSize) { return (void*)buffer; } -void* Utils::CopyDataToDevice(void* data, uint32_t dataSize, aclrtMemcpyKind policy) { +void* Utils::CopyDataToDevice(void* data, uint32_t dataSize, aclrtMemcpyKind policy) +{ void* buffer = nullptr; aclError aclRet = aclrtMalloc(&buffer, dataSize, ACL_MEM_MALLOC_HUGE_FIRST); if (aclRet != ACL_SUCCESS) { @@ -158,10 +164,12 @@ void* Utils::CopyDataToDevice(void* data, uint32_t dataSize, aclrtMemcpyKind pol return buffer; } -void* Utils::CopyDataDeviceToDevice(void* deviceData, uint32_t dataSize) { +void* Utils::CopyDataDeviceToDevice(void* deviceData, uint32_t dataSize) +{ return CopyDataToDevice(deviceData, dataSize, ACL_MEMCPY_DEVICE_TO_DEVICE); } -void* Utils::CopyDataHostToDevice(void* deviceData, uint32_t dataSize) { +void* Utils::CopyDataHostToDevice(void* deviceData, uint32_t dataSize) +{ return CopyDataToDevice(deviceData, dataSize, ACL_MEMCPY_HOST_TO_DEVICE); } \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/dvpp_jpegd.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/dvpp_jpegd.h old mode 100755 new mode 100644 index 90482db0c..55da65f67 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/dvpp_jpegd.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/dvpp_jpegd.h @@ -84,7 +84,7 @@ private: acldvppChannelDesc *g_dvppChannelDesc_; void* g_decodeOutBufferDev_; // decode output buffer - acldvppPicDesc *g_decodeOutputDesc_; //decode output desc + acldvppPicDesc *g_decodeOutputDesc_; // decode output desc uint8_t *g_inDevBuffer_; // input pic dev buffer uint32_t g_inDevBufferSizeD_; // input pic size for decode diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/dvpp_process.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/dvpp_process.h old mode 100755 new mode 100644 diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/dvpp_resize.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/dvpp_resize.h old mode 100755 new mode 100644 index 8a0483370..4c23b6f51 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/dvpp_resize.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/dvpp_resize.h @@ -52,7 +52,6 @@ public: */ Result InitOutputPara(int modelInputWidth, int modelInputHeight); - /** * @brief gett dvpp output * @param [in] outputBuffer: pointer which points to dvpp output buffer diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/model_process.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/model_process.h old mode 100755 new mode 100644 index 03a47b185..4dd3ca2cc --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/model_process.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/model_process.h @@ -26,7 +26,7 @@ * ModelProcess */ class ModelProcess { - public: +public: /** * @brief Constructor */ @@ -66,13 +66,13 @@ class ModelProcess { void DestroyDesc(); /** - * @brief Add input buffer to input dataset + * @brief Add input buffer to input dataset * @brief [in] dataset: dst dataset * @brief [in] buffer: src buffer * @brief [in] bufferSize: src bufferSize * @return result */ - Result AddDatasetBuffer(aclmdlDataset *dataset, + Result AddDatasetBuffer(aclmdlDataset *dataset, void* buffer, uint32_t bufferSize); /** @@ -135,7 +135,7 @@ class ModelProcess { */ aclmdlDataset *GetModelOutputData(); - private: +private: bool g_loadFlag_; // model load flag uint32_t g_modelId_; void *g_modelMemPtr_; @@ -145,7 +145,7 @@ class ModelProcess { aclmdlDesc *g_modelDesc_; aclmdlDataset *g_input_; aclmdlDataset *g_output_; - void * g_inputAIPP_; + void *g_inputAIPP_; uint32_t g_inputAIPPSize_; size_t g_aipp_index_; bool g_isReleased_; diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/object_detect.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/object_detect.h old mode 100755 new mode 100644 index e7b5d3704..ad00fb497 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/object_detect.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/object_detect.h @@ -18,12 +18,12 @@ #define YOLOV3_COCO_DETECTION_DYNAMIC_AIPP_INC_OBJECT_DETECT_H #pragma once +#include #include "utils.h" #include "acl/acl.h" #include "opencv2/opencv.hpp" #include "model_process.h" #include "dvpp_process.h" -#include using namespace std; @@ -32,7 +32,7 @@ using namespace std; */ class ObjectDetect { public: - ObjectDetect(const char* modelPath, + ObjectDetect(const char* modelPath, uint32_t modelWidth, uint32_t modelHeight); ~ObjectDetect(); @@ -40,11 +40,11 @@ public: Result ProcessForDvpp(ImageData& srcImage, const string& origImagePath); Result ProcessForOpenCV(cv::Mat& srcMat, const string& origImagePath); Result Preprocess(ImageData& resizedImage, ImageData& srcImage); - Result PreprocessOpencv(cv::Mat& srcMat, uint32_t& reiszeMatLen, + Result PreprocessOpencv(cv::Mat& srcMat, uint32_t& reiszeMatLen, void*& reiszeMatBuffer); Result Inference(aclmdlDataset*& inferenceOutput, ImageData& resizedImage); - Result InferenceOpenCV(aclmdlDataset*& inferenceOutput, - uint32_t& reiszeMatLen, + Result InferenceOpenCV(aclmdlDataset*& inferenceOutput, + uint32_t& reiszeMatLen, void*& reiszeMatBuffer); Result Postprocess(ImageData& image, aclmdlDataset* modelOutput, const string& origImagePath); diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/utils.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/utils.h old mode 100755 new mode 100644 index 93232d711..944cbc129 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/utils.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/utils.h @@ -44,7 +44,8 @@ using namespace std; #define SHARED_PTR_U8_BUF(buf) (shared_ptr((uint8_t *)(buf), [](uint8_t* p) { delete[](p); })) template -std::shared_ptr MakeSharedNoThrow() { +std::shared_ptr MakeSharedNoThrow() +{ try { return std::make_shared(); } @@ -55,8 +56,8 @@ std::shared_ptr MakeSharedNoThrow() { #define MAKE_SHARED_NO_THROW(memory, memory_type) \ do { \ - memory = MakeSharedNoThrow(); \ - }while(0); + memory = MakeSharedNoThrow<(memory_type)>(); \ + }while(0); typedef enum Result { SUCCESS = 0, diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_jpegd.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_jpegd.cpp old mode 100755 new mode 100644 index 08159fad6..ef0aec206 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_jpegd.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_jpegd.cpp @@ -20,16 +20,19 @@ #include "utils.h" using namespace std; -DvppJpegD::DvppJpegD(aclrtStream& stream, acldvppChannelDesc *dvppChannelDesc) +DvppJpegD::DvppJpegD(aclrtStream& stream, acldvppChannelDesc *dvppChannelDesc) : g_stream_(stream), g_dvppChannelDesc_(dvppChannelDesc), - g_decodeOutBufferDev_(nullptr), g_decodeOutputDesc_(nullptr) { + g_decodeOutBufferDev_(nullptr), g_decodeOutputDesc_(nullptr) +{ } -DvppJpegD::~DvppJpegD() { +DvppJpegD::~DvppJpegD() +{ DestroyDecodeResource(); } -Result DvppJpegD::InitDecodeOutputDesc(ImageData& inputImage) { +Result DvppJpegD::InitDecodeOutputDesc(ImageData& inputImage) +{ uint32_t decodeOutWidthStride = ALIGN_UP128(inputImage.width); uint32_t decodeOutHeightStride = ALIGN_UP16(inputImage.height); if (decodeOutWidthStride == 0 || decodeOutHeightStride == 0) { @@ -37,8 +40,8 @@ Result DvppJpegD::InitDecodeOutputDesc(ImageData& inputImage) { return FAILED; } - /*Allocate a large enough memory*/ - uint32_t decodeOutBufferSize = + /* Allocate a large enough memory */ + uint32_t decodeOutBufferSize = YUV420SP_SIZE(decodeOutWidthStride, decodeOutHeightStride) * 4; aclError aclRet = acldvppMalloc(&g_decodeOutBufferDev_, decodeOutBufferSize); @@ -63,7 +66,8 @@ Result DvppJpegD::InitDecodeOutputDesc(ImageData& inputImage) { return SUCCESS; } -Result DvppJpegD::Process(ImageData& dest, ImageData& src) { +Result DvppJpegD::Process(ImageData& dest, ImageData& src) +{ int ret = InitDecodeOutputDesc(src); if (ret != SUCCESS) { ERROR_LOG("InitDecodeOutputDesc failed"); @@ -94,7 +98,8 @@ Result DvppJpegD::Process(ImageData& dest, ImageData& src) { return SUCCESS; } -void DvppJpegD::DestroyDecodeResource() { +void DvppJpegD::DestroyDecodeResource() +{ if (g_decodeOutputDesc_ != nullptr) { acldvppDestroyPicDesc(g_decodeOutputDesc_); g_decodeOutputDesc_ = nullptr; diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_process.cpp old mode 100755 new mode 100644 index 103aeed65..d928cd2f3 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_process.cpp @@ -22,14 +22,17 @@ using namespace std; -DvppProcess::DvppProcess():g_dvppChannelDesc_(nullptr) { +DvppProcess::DvppProcess():g_dvppChannelDesc_(nullptr) +{ } -DvppProcess::~DvppProcess() { +DvppProcess::~DvppProcess() +{ DestroyResource(); } -void DvppProcess::DestroyResource() { +void DvppProcess::DestroyResource() +{ aclError aclRet; if (g_dvppChannelDesc_ != nullptr) { aclRet = acldvppDestroyChannel(g_dvppChannelDesc_); @@ -42,7 +45,8 @@ void DvppProcess::DestroyResource() { } } -Result DvppProcess::InitResource(aclrtStream& stream) { +Result DvppProcess::InitResource(aclrtStream& stream) +{ aclError aclRet; g_dvppChannelDesc_ = acldvppCreateChannelDesc(); @@ -62,13 +66,14 @@ Result DvppProcess::InitResource(aclrtStream& stream) { } Result DvppProcess::Resize(ImageData& dest, ImageData& src, - uint32_t width, uint32_t height) { + uint32_t width, uint32_t height) +{ DvppResize resizeOp(g_stream_, g_dvppChannelDesc_, width, height); return resizeOp.Process(dest, src); } -Result DvppProcess::CvtJpegToYuv420sp(ImageData& dest, ImageData& src) { +Result DvppProcess::CvtJpegToYuv420sp(ImageData& dest, ImageData& src) +{ DvppJpegD jpegD(g_stream_, g_dvppChannelDesc_); return jpegD.Process(dest, src); -} - +} \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_resize.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_resize.cpp old mode 100755 new mode 100644 index 17f067be8..927062b58 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_resize.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_resize.cpp @@ -22,23 +22,26 @@ using namespace std; DvppResize::DvppResize(aclrtStream& stream, acldvppChannelDesc *dvppChannelDesc, uint32_t width, uint32_t height) -: g_stream_(stream), g_dvppChannelDesc_(dvppChannelDesc), -g_resizeConfig_(nullptr), g_vpcInputDesc_(nullptr), g_vpcOutputDesc_(nullptr), -g_inDevBuffer_(nullptr),g_vpcOutBufferDev_(nullptr),g_vpcOutBufferSize_(0) { + : g_stream_(stream), g_dvppChannelDesc_(dvppChannelDesc), + g_resizeConfig_(nullptr), g_vpcInputDesc_(nullptr), g_vpcOutputDesc_(nullptr), + g_inDevBuffer_(nullptr), g_vpcOutBufferDev_(nullptr), g_vpcOutBufferSize_(0) +{ g_size_.width = width; g_size_.height = height; } -DvppResize::~DvppResize() { +DvppResize::~DvppResize() +{ DestroyResizeResource(); } -Result DvppResize::InitResizeInputDesc(ImageData& inputImage) { +Result DvppResize::InitResizeInputDesc(ImageData& inputImage) +{ uint32_t alignWidth = ALIGN_UP128(inputImage.width); uint32_t alignHeight = ALIGN_UP16(inputImage.height); if (alignWidth == 0 || alignHeight == 0) { ERROR_LOG("InitResizeInputDesc AlignmentHelper failed. image w %d, h %d, align w%d, h%d", - inputImage.width, inputImage.height, alignWidth, alignHeight); + inputImage.width, inputImage.height, alignWidth, alignHeight); return FAILED; } uint32_t inputBufferSize = YUV420SP_SIZE(alignWidth, alignHeight); @@ -58,7 +61,8 @@ Result DvppResize::InitResizeInputDesc(ImageData& inputImage) { return SUCCESS; } -Result DvppResize::InitResizeOutputDesc() { +Result DvppResize::InitResizeOutputDesc() +{ int resizeOutWidth = g_size_.width; int resizeOutHeight = g_size_.height; int resizeOutWidthStride = ALIGN_UP16(resizeOutWidth); @@ -92,7 +96,8 @@ Result DvppResize::InitResizeOutputDesc() { return SUCCESS; } -Result DvppResize::InitResizeResource(ImageData& inputImage) { +Result DvppResize::InitResizeResource(ImageData& inputImage) +{ g_format_ = static_cast(PIXEL_FORMAT_YUV_SEMIPLANAR_420); g_resizeConfig_ = acldvppCreateResizeConfig(); if (g_resizeConfig_ == nullptr) { @@ -103,17 +108,18 @@ Result DvppResize::InitResizeResource(ImageData& inputImage) { if (SUCCESS != InitResizeInputDesc(inputImage)) { ERROR_LOG("InitResizeInputDesc failed"); return FAILED; - } + } if (SUCCESS != InitResizeOutputDesc()) { ERROR_LOG("InitResizeOutputDesc failed"); return FAILED; - } + } - return SUCCESS; + return SUCCESS; } -Result DvppResize::Process(ImageData& resizedImage, ImageData& srcImage) { +Result DvppResize::Process(ImageData& resizedImage, ImageData& srcImage) +{ if (SUCCESS != InitResizeResource(srcImage)) { ERROR_LOG("Dvpp resize failed for init error"); return FAILED; @@ -145,7 +151,8 @@ Result DvppResize::Process(ImageData& resizedImage, ImageData& srcImage) { return SUCCESS; } -void DvppResize::DestroyResizeResource() { +void DvppResize::DestroyResizeResource() +{ if (g_resizeConfig_ != nullptr) { (void)acldvppDestroyResizeConfig(g_resizeConfig_); g_resizeConfig_ = nullptr; diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/main.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/main.cpp index 5fe8a420e..ffe1997ba 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/main.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/main.cpp @@ -23,20 +23,22 @@ using namespace std; namespace { -uint32_t g_ModelWidth = 416; -uint32_t g_ModelHeight = 416; -const char* g_ModelPath = "../model/yolov3.om"; +uint32_t g_modelWidth = 416; +uint32_t g_modelHeight = 416; +const char* g_modelPath = "../model/yolov3.om"; } -int main(int argc, char *argv[]) { +int main(int argc, char *argv[]) +{ // Check input args: the path of input picture, and ignore hidden dotfile directory - if ((argc != 2) || (argv[1] == nullptr)) { + int argNum = 2; + if ((argc != argNum) || (argv[1] == nullptr)) { ERROR_LOG("Please input: ./main "); return FAILED; } // Instantiate the object detection class - ObjectDetect detect(g_ModelPath, g_ModelWidth, g_ModelHeight); - // Initialize the acl resources, dvpp, load model, + ObjectDetect detect(g_modelPath, g_modelWidth, g_modelHeight); + // Initialize the acl resources, dvpp, load model, // and malloc input memory of input which is const Result ret = detect.Init(); if (ret != SUCCESS) { @@ -73,20 +75,20 @@ int main(int argc, char *argv[]) { Result ret = detect.ProcessForDvpp(image, imageFile); if (ret != SUCCESS) { ERROR_LOG("Process pic %s by dvpp failed", - imageFile.c_str()); + imageFile.c_str()); return FAILED; } INFO_LOG("Process pic %s by dvpp success", - imageFile.c_str()); + imageFile.c_str()); ret = detect.ProcessForOpenCV(image_opencv, imageFile); if (ret != SUCCESS) { ERROR_LOG("Process pic %s by OpenCV failed", - imageFile.c_str()); + imageFile.c_str()); return FAILED; } INFO_LOG("Process pic %s by OpenCV success", - imageFile.c_str()); + imageFile.c_str()); } INFO_LOG("Execute sample success"); diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/model_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/model_process.cpp old mode 100755 new mode 100644 index ca43196d8..c9003e19c --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/model_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/model_process.cpp @@ -14,23 +14,27 @@ * limitations under the License. */ -#include "model_process.h" #include #include +#include "model_process.h" #include "utils.h" using namespace std; -ModelProcess::ModelProcess():g_loadFlag_(false), g_modelId_(0), g_modelMemPtr_(nullptr), g_modelMemSize_(0), -g_modelWeightPtr_(nullptr),g_modelWeightSize_(0), g_modelDesc_(nullptr), g_input_(nullptr), g_output_(nullptr), -g_isReleased_(false) { - +ModelProcess::ModelProcess() + : g_loadFlag_(false), g_modelId_(0), g_modelMemPtr_(nullptr), g_modelMemSize_(0), + g_modelWeightPtr_(nullptr), g_modelWeightSize_(0), g_modelDesc_(nullptr), + g_input_(nullptr), g_output_(nullptr), + g_isReleased_(false) +{ } -ModelProcess::~ModelProcess() { +ModelProcess::~ModelProcess() +{ DestroyResource(); } -void ModelProcess::DestroyResource() { +void ModelProcess::DestroyResource() +{ if (g_isReleased_) {return;} Unload(); DestroyDesc(); @@ -39,7 +43,8 @@ void ModelProcess::DestroyResource() { g_isReleased_ = true; } -Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath){ +Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath) +{ if (g_loadFlag_) { ERROR_LOG("has already loaded a model"); return FAILED; @@ -75,7 +80,8 @@ Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath){ return SUCCESS; } -Result ModelProcess::CreateDesc() { +Result ModelProcess::CreateDesc() +{ g_modelDesc_ = aclmdlCreateDesc(); if (g_modelDesc_ == nullptr) { ERROR_LOG("create model description failed"); @@ -104,15 +110,17 @@ Result ModelProcess::CreateDesc() { return SUCCESS; } -void ModelProcess::DestroyDesc() { +void ModelProcess::DestroyDesc() +{ if (g_modelDesc_ != nullptr) { (void)aclmdlDestroyDesc(g_modelDesc_); g_modelDesc_ = nullptr; } } -Result ModelProcess::AddDatasetBuffer(aclmdlDataset *dataset, - void* buffer, uint32_t bufferSize) { +Result ModelProcess::AddDatasetBuffer(aclmdlDataset *dataset, + void* buffer, uint32_t bufferSize) +{ aclDataBuffer* dataBuf = aclCreateDataBuffer(buffer, bufferSize); if (dataBuf == nullptr) { ERROR_LOG("Create data buffer error"); @@ -129,7 +137,8 @@ Result ModelProcess::AddDatasetBuffer(aclmdlDataset *dataset, return SUCCESS; } -aclmdlAIPP* ModelProcess::SetAIPPTensor(uint64_t batch_size) { +aclmdlAIPP* ModelProcess::SetAIPPTensor(uint64_t batch_size) +{ aclmdlAIPP* aippParamTensor = aclmdlCreateAIPP(batch_size); if (aippParamTensor == nullptr) { ERROR_LOG("call aclmdlCreateAIPP failed"); @@ -192,7 +201,8 @@ aclmdlAIPP* ModelProcess::SetAIPPTensor(uint64_t batch_size) { return aippParamTensor; } -aclmdlAIPP* ModelProcess::SetAIPPTensorOpenCV(uint64_t batch_size) { +aclmdlAIPP* ModelProcess::SetAIPPTensorOpenCV(uint64_t batch_size) +{ aclmdlAIPP* aippParamTensor = aclmdlCreateAIPP(batch_size); if (aippParamTensor == nullptr) { ERROR_LOG("call aclmdlCreateAIPP failed"); @@ -256,8 +266,9 @@ aclmdlAIPP* ModelProcess::SetAIPPTensorOpenCV(uint64_t batch_size) { } Result ModelProcess::CreateInput(void* input1, uint32_t input1Size, - void* input2, uint32_t input2Size) { - vector inputData = {{input1, input1Size},{input2, input2Size}, + void* input2, uint32_t input2Size) +{ + vector inputData = {{input1, input1Size}, {input2, input2Size}, {g_inputAIPP_, g_inputAIPPSize_}}; uint32_t dataNum = aclmdlGetNumInputs(g_modelDesc_); if (dataNum == 0) { @@ -279,10 +290,10 @@ Result ModelProcess::CreateInput(void* input1, uint32_t input1Size, size_t modelInputSize = aclmdlGetInputSizeByIndex(g_modelDesc_, i); if (modelInputSize != inputData[i].size) { WARNNING_LOG("Input size verify failed " - "input[%d] size: %d, provide size : %d", + "input[%d] size: %d, provide size : %d", i, modelInputSize, inputData[i].size); } - Result ret = AddDatasetBuffer(g_input_, + Result ret = AddDatasetBuffer(g_input_, inputData[i].data, inputData[i].size); if (ret != SUCCESS) { @@ -308,8 +319,9 @@ Result ModelProcess::CreateInput(void* input1, uint32_t input1Size, } Result ModelProcess::CreateInputOpenCV(void* input1, uint32_t input1Size, - void* input2, uint32_t input2Size) { - vector inputData = {{input1, input1Size},{input2, input2Size}, + void* input2, uint32_t input2Size) +{ + vector inputData = {{input1, input1Size}, {input2, input2Size}, {g_inputAIPP_, g_inputAIPPSize_}}; uint32_t dataNum = aclmdlGetNumInputs(g_modelDesc_); if (dataNum == 0) { @@ -331,10 +343,10 @@ Result ModelProcess::CreateInputOpenCV(void* input1, uint32_t input1Size, size_t modelInputSize = aclmdlGetInputSizeByIndex(g_modelDesc_, i); if (modelInputSize != inputData[i].size) { WARNNING_LOG("Input size verify failed " - "input[%d] size: %d, provide size : %d", + "input[%d] size: %d, provide size : %d", i, modelInputSize, inputData[i].size); } - Result ret = AddDatasetBuffer(g_input_, + Result ret = AddDatasetBuffer(g_input_, inputData[i].data, inputData[i].size); if (ret != SUCCESS) { @@ -359,7 +371,8 @@ Result ModelProcess::CreateInputOpenCV(void* input1, uint32_t input1Size, return SUCCESS; } -void ModelProcess::DestroyInput() { +void ModelProcess::DestroyInput() +{ if (g_input_ == nullptr) { return; } @@ -372,7 +385,8 @@ void ModelProcess::DestroyInput() { g_input_ = nullptr; } -Result ModelProcess::CreateOutput() { +Result ModelProcess::CreateOutput() +{ if (g_modelDesc_ == nullptr) { ERROR_LOG("no model description, create ouput failed"); return FAILED; @@ -415,7 +429,8 @@ Result ModelProcess::CreateOutput() { return SUCCESS; } -void ModelProcess::DestroyOutput() { +void ModelProcess::DestroyOutput() +{ if (g_output_ == nullptr) { return; } @@ -431,7 +446,8 @@ void ModelProcess::DestroyOutput() { g_output_ = nullptr; } -Result ModelProcess::Execute() { +Result ModelProcess::Execute() +{ aclError ret = aclmdlExecute(g_modelId_, g_input_, g_output_); if (ret != ACL_SUCCESS) { ERROR_LOG("execute model failed, modelId is %u", g_modelId_); @@ -475,6 +491,7 @@ void ModelProcess::Unload() INFO_LOG("unload model success, modelId is %u", g_modelId_); } -aclmdlDataset *ModelProcess::GetModelOutputData() { +aclmdlDataset *ModelProcess::GetModelOutputData() +{ return g_output_; } diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/object_detect.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/object_detect.cpp old mode 100755 new mode 100644 index 45060c370..14bfc7e46 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/object_detect.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/object_detect.cpp @@ -14,8 +14,8 @@ * limitations under the License. */ -#include "object_detect.h" #include +#include "object_detect.h" #include "opencv2/opencv.hpp" #include "acl/acl.h" #include "model_process.h" @@ -25,62 +25,65 @@ using namespace std; namespace { const static std::vector yolov3Label = { "person", "bicycle", "car", "motorbike", - "aeroplane","bus", "train", "truck", "boat", - "traffic light", "fire hydrant", "stop sign", "parking meter", - "bench", "bird", "cat", "dog", "horse", - "sheep", "cow", "elephant", "bear", "zebra", - "giraffe", "backpack", "umbrella", "handbag","tie", - "suitcase", "frisbee", "skis", "snowboard", "sports ball", - "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", - "tennis racket", "bottle", "wine glass", "cup", - "fork", "knife", "spoon", "bowl", "banana", - "apple", "sandwich", "orange", "broccoli", "carrot", - "hot dog", "pizza", "donut", "cake", "chair", - "sofa", "potted plant", "bed", "dining table", "toilet", - "TV monitor", "laptop", "mouse", "remote", "keyboard", - "cell phone", "microwave", "oven", "toaster", "sink", - "refrigerator", "book", "clock", "vase","scissors", - "teddy bear", "hair drier", "toothbrush" }; - - const uint32_t kBBoxDataBufId = 0; - const uint32_t kBoxNumDataBufId = 1; + "aeroplane", "bus", "train", "truck", "boat", + "traffic light", "fire hydrant", "stop sign", "parking meter", + "bench", "bird", "cat", "dog", "horse", + "sheep", "cow", "elephant", "bear", "zebra", + "giraffe", "backpack", "umbrella", "handbag", "tie", + "suitcase", "frisbee", "skis", "snowboard", "sports ball", + "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", + "tennis racket", "bottle", "wine glass", "cup", + "fork", "knife", "spoon", "bowl", "banana", + "apple", "sandwich", "orange", "broccoli", "carrot", + "hot dog", "pizza", "donut", "cake", "chair", + "sofa", "potted plant", "bed", "dining table", "toilet", + "TV monitor", "laptop", "mouse", "remote", "keyboard", + "cell phone", "microwave", "oven", "toaster", "sink", + "refrigerator", "book", "clock", "vase", "scissors", + "teddy bear", "hair drier", "toothbrush" }; + + const uint32_t g_bBoxDataBufId = 0; + const uint32_t g_boxNumDataBufId = 1; enum BBoxIndex { TOPLEFTX = 0, TOPLEFTY, BOTTOMRIGHTX, BOTTOMRIGHTY, SCORE, LABEL }; // bounding box line solid - const uint32_t kLineSolid = 2; + const uint32_t g_lineSolid = 2; // output image prefix - const string kOutputFilePrefix = "out_"; + const string g_outputFilePrefix = "out_"; // opencv draw label params. - const double kFountScale = 0.5; - const cv::Scalar kFontColor(0, 0, 255); - const uint32_t kLabelOffset = 11; - const string kFileSperator = "/"; + const double g_fountScale = 0.5; + const cv::Scalar g_fontColor(0, 0, 255); + const uint32_t g_labelOffset = 11; + const string g_fileSperator = "/"; // opencv color list for boundingbox - const vector kColors { + const vector g_colors { cv::Scalar(237, 149, 100), cv::Scalar(0, 215, 255), cv::Scalar(50, 205, 50), cv::Scalar(139, 85, 26) }; - struct timespec kTimeStart = {0, 0}; - struct timespec kTimeEnd = {0, 0}; + struct timespec g_timeStart = {0, 0}; + struct timespec g_timeEnd = {0, 0}; } ObjectDetect::ObjectDetect(const char* modelPath, uint32_t modelWidth, uint32_t modelHeight) -:g_deviceId_(0), g_context_(nullptr), g_stream_(nullptr), g_modelWidth_(modelWidth), -g_modelHeight_(modelHeight), g_isInited_(false), g_isDeviceSet_(false) { + : g_deviceId_(0), g_context_(nullptr), g_stream_(nullptr), g_modelWidth_(modelWidth), + g_modelHeight_(modelHeight), g_isInited_(false), g_isDeviceSet_(false) +{ g_imageInfoSize_ = 0; g_imageInfoBuf_ = nullptr; g_modelPath_ = modelPath; } -ObjectDetect::~ObjectDetect() { +ObjectDetect::~ObjectDetect() +{ DestroyResource(); } -Result ObjectDetect::InitResource() { +Result ObjectDetect::InitResource() +{ // ACL init const char *aclConfigPath = "../src/acl.json"; aclError ret = aclInit(aclConfigPath); @@ -124,7 +127,8 @@ Result ObjectDetect::InitResource() { return SUCCESS; } -Result ObjectDetect::InitModel(const char* omModelPath) { +Result ObjectDetect::InitModel(const char* omModelPath) +{ Result ret = g_model_.LoadModelFromFileWithMem(omModelPath); if (ret != SUCCESS) { ERROR_LOG("execute LoadModelFromFileWithMem failed"); @@ -146,13 +150,14 @@ Result ObjectDetect::InitModel(const char* omModelPath) { return SUCCESS; } -Result ObjectDetect::CreateImageInfoBuffer() { +Result ObjectDetect::CreateImageInfoBuffer() +{ const float imageInfo[4] = {(float)g_modelWidth_, (float)g_modelHeight_, - (float)g_modelWidth_, (float)g_modelHeight_}; + (float)g_modelWidth_, (float)g_modelHeight_}; g_imageInfoSize_ = sizeof(imageInfo); if (g_runMode_ == ACL_HOST) g_imageInfoBuf_ = Utils::CopyDataHostToDevice((void *)imageInfo, g_imageInfoSize_); - else + else g_imageInfoBuf_ = Utils::CopyDataDeviceToDevice((void *)imageInfo, g_imageInfoSize_); if (g_imageInfoBuf_ == nullptr) { ERROR_LOG("Copy image info to device failed"); @@ -162,7 +167,8 @@ Result ObjectDetect::CreateImageInfoBuffer() { return SUCCESS; } -Result ObjectDetect::Init() { +Result ObjectDetect::Init() +{ if (g_isInited_) { INFO_LOG("Object detection instance is initied already!"); return SUCCESS; @@ -196,15 +202,16 @@ Result ObjectDetect::Init() { return SUCCESS; } -Result ObjectDetect::ProcessForDvpp(ImageData& srcImage, const string& origImagePath) { +Result ObjectDetect::ProcessForDvpp(ImageData& srcImage, const string& origImagePath) +{ ImageData resizedImage; Result ret = Preprocess(resizedImage, srcImage); if (ret != SUCCESS) { ERROR_LOG("Read file %s failed", - origImagePath.c_str()); + origImagePath.c_str()); return FAILED; } - // Send the resized picture to the model for inference + // Send the resized picture to the model for inference // and get the inference results aclmdlDataset* inferenceOutput = nullptr; ret = Inference(inferenceOutput, resizedImage); @@ -212,7 +219,7 @@ Result ObjectDetect::ProcessForDvpp(ImageData& srcImage, const string& origImage ERROR_LOG("Inference model inference output data failed"); return FAILED; } - // Analyze the inference output, mark the object category and + // Analyze the inference output, mark the object category and // location by the inference result ret = Postprocess(srcImage, inferenceOutput, origImagePath); if (ret != SUCCESS) { @@ -222,7 +229,8 @@ Result ObjectDetect::ProcessForDvpp(ImageData& srcImage, const string& origImage return SUCCESS; } -Result ObjectDetect::ProcessForOpenCV(cv::Mat& srcMat, const string& origImagePath) { +Result ObjectDetect::ProcessForOpenCV(cv::Mat& srcMat, const string& origImagePath) +{ uint32_t reiszeMatLen = 0; void* reiszeMatBuffer = nullptr; Result ret = PreprocessOpencv(srcMat, reiszeMatLen, reiszeMatBuffer); @@ -230,7 +238,7 @@ Result ObjectDetect::ProcessForOpenCV(cv::Mat& srcMat, const string& origImagePa ERROR_LOG("Preprocess for Opencv mat failed"); } - // Send the resized picture to the model for inference + // Send the resized picture to the model for inference // and get the inference results aclmdlDataset* inferenceOutput = nullptr; ret = InferenceOpenCV(inferenceOutput, reiszeMatLen, reiszeMatBuffer); @@ -244,11 +252,11 @@ Result ObjectDetect::ProcessForOpenCV(cv::Mat& srcMat, const string& origImagePa ERROR_LOG("Process model inference output data failed"); return FAILED; } - return SUCCESS; - + return SUCCESS; } -Result ObjectDetect::Preprocess(ImageData& resizedImage, ImageData& srcImage) { +Result ObjectDetect::Preprocess(ImageData& resizedImage, ImageData& srcImage) +{ ImageData imageDevice; Utils::CopyImageDataToDevice(imageDevice, srcImage, g_runMode_); @@ -269,7 +277,8 @@ Result ObjectDetect::Preprocess(ImageData& resizedImage, ImageData& srcImage) { return SUCCESS; } -Result ObjectDetect::PreprocessOpencv(cv::Mat& srcMat, uint32_t& reiszeMatLen, void*& reiszeMatBuffer) { +Result ObjectDetect::PreprocessOpencv(cv::Mat& srcMat, uint32_t& reiszeMatLen, void*& reiszeMatBuffer) +{ cv::Mat reiszeMat,dstMat; // color convert & resize cv::cvtColor(srcMat, dstMat, cv::COLOR_BGR2RGB); @@ -290,8 +299,8 @@ Result ObjectDetect::PreprocessOpencv(cv::Mat& srcMat, uint32_t& reiszeMatLen, v return SUCCESS; } -Result ObjectDetect::Inference(aclmdlDataset*& inferenceOutput, -ImageData& resizedImage) { +Result ObjectDetect::Inference(aclmdlDataset*& inferenceOutput, ImageData& resizedImage) +{ Result ret = g_model_.CreateInput(resizedImage.data.get(), resizedImage.size, g_imageInfoBuf_, g_imageInfoSize_); if (ret != SUCCESS) { @@ -310,7 +319,8 @@ ImageData& resizedImage) { } Result ObjectDetect::InferenceOpenCV(aclmdlDataset*& inferenceOutput, -uint32_t& reiszeMatLen, void*& reiszeMatBuffer) { + uint32_t& reiszeMatLen, void*& reiszeMatBuffer) +{ Result ret = g_model_.CreateInputOpenCV(reiszeMatBuffer, reiszeMatLen, g_imageInfoBuf_, g_imageInfoSize_); if (ret != SUCCESS) { @@ -329,13 +339,14 @@ uint32_t& reiszeMatLen, void*& reiszeMatBuffer) { } Result ObjectDetect::Postprocess(ImageData& image, aclmdlDataset* modelOutput, -const string& origImagePath) { + const string& origImagePath) +{ uint32_t dataSize = 0; float* detectData = (float *)GetInferenceOutputItem(dataSize, modelOutput, - kBBoxDataBufId); + g_bBoxDataBufId); uint32_t* boxNum = (uint32_t *)GetInferenceOutputItem(dataSize, modelOutput, - kBoxNumDataBufId); + g_boxNumDataBufId); if (boxNum == nullptr) return FAILED; uint32_t totalBox = boxNum[0]; @@ -354,7 +365,7 @@ const string& origImagePath) { uint32_t objIndex = (uint32_t)detectData[totalBox * LABEL + i]; boundBox.text = yolov3Label[objIndex] + std::to_string(score) + "\%"; printf("%d %d %d %d %s\n", boundBox.rect.ltX, boundBox.rect.ltY, - boundBox.rect.rbX, boundBox.rect.rbY, boundBox.text.c_str()); + boundBox.rect.rbX, boundBox.rect.rbY, boundBox.text.c_str()); detectResults.emplace_back(boundBox); } @@ -369,13 +380,13 @@ const string& origImagePath) { } Result ObjectDetect::PostprocessOpenCV(cv::Mat& srcMat, aclmdlDataset* modelOutput, -const string& origImagePath) { + const string& origImagePath) { uint32_t dataSize = 0; float* detectData = (float *)GetInferenceOutputItem(dataSize, modelOutput, - kBBoxDataBufId); + g_bBoxDataBufId); uint32_t* boxNum = (uint32_t *)GetInferenceOutputItem(dataSize, modelOutput, - kBoxNumDataBufId); + g_boxNumDataBufId); if (boxNum == nullptr) {return FAILED;} uint32_t totalBox = boxNum[0]; @@ -394,7 +405,7 @@ const string& origImagePath) { uint32_t objIndex = (uint32_t)detectData[totalBox * LABEL + i]; boundBox.text = yolov3Label[objIndex] + std::to_string(score) + "\%"; printf("%d %d %d %d %s\n", boundBox.rect.ltX, boundBox.rect.ltY, - boundBox.rect.rbX, boundBox.rect.rbY, boundBox.text.c_str()); + boundBox.rect.rbX, boundBox.rect.rbY, boundBox.text.c_str()); detectResults.emplace_back(boundBox); } @@ -410,25 +421,26 @@ const string& origImagePath) { void* ObjectDetect::GetInferenceOutputItem(uint32_t& itemDataSize, aclmdlDataset* inferenceOutput, - uint32_t idx) { + uint32_t idx) +{ aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(inferenceOutput, idx); if (dataBuffer == nullptr) { ERROR_LOG("Get the %dth dataset buffer from model " - "inference output failed", idx); + "inference output failed", idx); return nullptr; } void* dataBufferDev = aclGetDataBufferAddr(dataBuffer); if (dataBufferDev == nullptr) { ERROR_LOG("Get the %dth dataset buffer address " - "from model inference output failed", idx); + "from model inference output failed", idx); return nullptr; } size_t bufferSize = aclGetDataBufferSize(dataBuffer); if (bufferSize == 0) { ERROR_LOG("The %dth dataset buffer size of " - "model inference output is 0", idx); + "model inference output is 0", idx); return nullptr; } @@ -448,7 +460,8 @@ void* ObjectDetect::GetInferenceOutputItem(uint32_t& itemDataSize, } void ObjectDetect::DrowBoundBoxToImage(vector& detectionResults, - const string& origImagePath) { + const string& origImagePath) +{ cv::Mat image = cv::imread(origImagePath, CV_LOAD_IMAGE_UNCHANGED); for (int i = 0; i < detectionResults.size(); ++i) { cv::Point p1, p2; @@ -456,9 +469,9 @@ void ObjectDetect::DrowBoundBoxToImage(vector& detectionResults, p1.y = detectionResults[i].rect.ltY; p2.x = detectionResults[i].rect.rbX; p2.y = detectionResults[i].rect.rbY; - cv::rectangle(image, p1, p2, kColors[i % kColors.size()], kLineSolid); - cv::putText(image, detectionResults[i].text, cv::Point(p1.x, p1.y + kLabelOffset), - cv::FONT_HERSHEY_COMPLEX, kFountScale, kFontColor); + cv::rectangle(image, p1, p2, g_colors[i % g_colors.size()], g_lineSolid); + cv::putText(image, detectionResults[i].text, cv::Point(p1.x, p1.y + g_labelOffset), + cv::FONT_HERSHEY_COMPLEX, g_fountScale, g_fontColor); } int pos = origImagePath.find_last_of("/"); @@ -470,7 +483,8 @@ void ObjectDetect::DrowBoundBoxToImage(vector& detectionResults, } void ObjectDetect::DrowBoundBoxToOpenCVImage(vector& detectionResults, - const string& origImagePath) { + const string& origImagePath) +{ cv::Mat image = cv::imread(origImagePath, CV_LOAD_IMAGE_UNCHANGED); for (int i = 0; i < detectionResults.size(); ++i) { cv::Point p1, p2; @@ -478,9 +492,9 @@ void ObjectDetect::DrowBoundBoxToOpenCVImage(vector& detectionResults, p1.y = detectionResults[i].rect.ltY; p2.x = detectionResults[i].rect.rbX; p2.y = detectionResults[i].rect.rbY; - cv::rectangle(image, p1, p2, kColors[i % kColors.size()], kLineSolid); - cv::putText(image, detectionResults[i].text, cv::Point(p1.x, p1.y + kLabelOffset), - cv::FONT_HERSHEY_COMPLEX, kFountScale, kFontColor); + cv::rectangle(image, p1, p2, g_colors[i % g_colors.size()], g_lineSolid); + cv::putText(image, detectionResults[i].text, cv::Point(p1.x, p1.y + g_labelOffset), + cv::FONT_HERSHEY_COMPLEX, g_fountScale, g_fontColor); } int pos = origImagePath.find_last_of("/"); @@ -491,7 +505,8 @@ void ObjectDetect::DrowBoundBoxToOpenCVImage(vector& detectionResults, cv::imwrite(sstream.str(), image); } -void ObjectDetect::DestroyResource() { +void ObjectDetect::DestroyResource() +{ aclrtFree(g_imageInfoBuf_); g_model_.DestroyResource(); g_dvpp_.DestroyResource(); @@ -527,4 +542,4 @@ void ObjectDetect::DestroyResource() { ERROR_LOG("finalize acl failed"); } INFO_LOG("end to finalize acl"); -} +} \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/utils.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/utils.cpp old mode 100755 new mode 100644 index 531c4a5d4..a35fe525d --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/utils.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/utils.cpp @@ -14,7 +14,6 @@ * limitations under the License. */ -#include "utils.h" #include #include #include @@ -25,24 +24,26 @@ #include #include #include +#include "utils.h" #include "acl/acl.h" #include "acl/ops/acl_dvpp.h" using namespace std; namespace { -const std::string g_ImagePathSeparator = ","; -const int g_StatSuccess = 0; -const std::string g_FileSperator = "/"; -const std::string g_PathSeparator = "/"; +const std::string g_imagePathSeparator = ","; +const int STAT_SUCCESS = 0; +const std::string g_fileSperator = "/"; +const std::string g_pathSeparator = "/"; // output image prefix -const std::string g_OutputFilePrefix = "out_"; +const std::string g_outputFilePrefix = "out_"; } -bool Utils::IsDirectory(const string &path) { +bool Utils::IsDirectory(const string &path) +{ // get path stat struct stat buf; - if (stat(path.c_str(), &buf) != g_StatSuccess) { + if (stat(path.c_str(), &buf) != STAT_SUCCESS) { return false; } @@ -54,7 +55,8 @@ bool Utils::IsDirectory(const string &path) { } } -bool Utils::IsPathExist(const string &path) { +bool Utils::IsPathExist(const string &path) +{ ifstream file(path); if (!file) { return false; @@ -62,17 +64,17 @@ bool Utils::IsPathExist(const string &path) { return true; } -void Utils::SplitPath(const string &path, vector &path_vec) { - char *char_path = const_cast(path.c_str()); - const char *char_split = g_ImagePathSeparator.c_str(); - char *tmp_path = strtok(char_path, char_split); +void Utils::SplitPath(const string &path, vector &path_vec) +{ + char *tmp_path = strtok(const_cast(path.c_str()), g_imagePathSeparator.c_str()); while (tmp_path) { path_vec.emplace_back(tmp_path); - tmp_path = strtok(nullptr, char_split); + tmp_path = strtok(nullptr, g_imagePathSeparator.c_str()); } } -void Utils::GetAllFiles(const string &path, vector &file_vec) { +void Utils::GetAllFiles(const string &path, vector &file_vec) +{ // split file path vector path_vector; SplitPath(path, path_vector); @@ -81,7 +83,7 @@ void Utils::GetAllFiles(const string &path, vector &file_vec) { // check path exist or not if (!IsPathExist(every_path)) { ERROR_LOG("Failed to deal path=%s. Reason: not exist " - "or can not access.", every_path.c_str()); + "or can not access.", every_path.c_str()); continue; } // get files in path and sub-path @@ -89,7 +91,8 @@ void Utils::GetAllFiles(const string &path, vector &file_vec) { } } -void Utils::GetPathFiles(const string &path, vector &file_vec) { +void Utils::GetPathFiles(const string &path, vector &file_vec) +{ struct dirent *dirent_ptr = nullptr; DIR *dir = nullptr; if (IsDirectory(path)) { @@ -101,7 +104,7 @@ void Utils::GetPathFiles(const string &path, vector &file_vec) { } // file path - string full_path = path + g_PathSeparator + dirent_ptr->d_name; + string full_path = path + g_pathSeparator + dirent_ptr->d_name; // directory need recursion if (IsDirectory(full_path)) { GetPathFiles(full_path, file_vec); @@ -115,7 +118,8 @@ void Utils::GetPathFiles(const string &path, vector &file_vec) { } } -void* Utils::CopyDataDeviceToLocal(void* deviceData, uint32_t dataSize) { +void* Utils::CopyDataDeviceToLocal(void* deviceData, uint32_t dataSize) +{ uint8_t* buffer = new uint8_t[dataSize]; if (buffer == nullptr) { ERROR_LOG("New malloc memory failed"); @@ -132,7 +136,8 @@ void* Utils::CopyDataDeviceToLocal(void* deviceData, uint32_t dataSize) { return (void*)buffer; } -void* Utils::CopyDataToDevice(void* data, uint32_t dataSize, aclrtMemcpyKind policy) { +void* Utils::CopyDataToDevice(void* data, uint32_t dataSize, aclrtMemcpyKind policy) +{ void* buffer = nullptr; aclError aclRet = acldvppMalloc(&buffer, dataSize); if (aclRet != ACL_SUCCESS) { @@ -150,17 +155,20 @@ void* Utils::CopyDataToDevice(void* data, uint32_t dataSize, aclrtMemcpyKind pol return buffer; } -void* Utils::CopyDataDeviceToDevice(void* deviceData, uint32_t dataSize) { +void* Utils::CopyDataDeviceToDevice(void* deviceData, uint32_t dataSize) +{ return CopyDataToDevice(deviceData, dataSize, ACL_MEMCPY_DEVICE_TO_DEVICE); } -void* Utils::CopyDataHostToDevice(void* deviceData, uint32_t dataSize) { +void* Utils::CopyDataHostToDevice(void* deviceData, uint32_t dataSize) +{ return CopyDataToDevice(deviceData, dataSize, ACL_MEMCPY_HOST_TO_DEVICE); } Result Utils::CopyImageDataToDevice(ImageData& imageDevice, - ImageData srcImage, aclrtRunMode mode) { - void * buffer; + ImageData srcImage, aclrtRunMode mode) +{ + void* buffer; if (mode == ACL_HOST) buffer = Utils::CopyDataHostToDevice(srcImage.data.get(), srcImage.size); else @@ -179,8 +187,9 @@ Result Utils::CopyImageDataToDevice(ImageData& imageDevice, return SUCCESS; } -Result Utils::CopyOpenCVMatToDevice(cv::Mat srcMat, uint32_t& reiszeMatLen, void*& reiszeMatBuffer, -aclrtRunMode runMode) { +Result Utils::CopyOpenCVMatToDevice(cv::Mat srcMat, uint32_t& reiszeMatLen, void*& reiszeMatBuffer, + aclrtRunMode runMode) +{ reiszeMatLen = srcMat.rows*srcMat.cols*srcMat.elemSize(); if (reiszeMatLen == 0) { ERROR_LOG("ReiszeMatLen is zero"); @@ -194,8 +203,8 @@ aclrtRunMode runMode) { } if (runMode == ACL_HOST) { aclRet = aclrtMemcpy(buffer, reiszeMatLen, - srcMat.ptr(), reiszeMatLen, - ACL_MEMCPY_HOST_TO_DEVICE); + srcMat.ptr(), reiszeMatLen, + ACL_MEMCPY_HOST_TO_DEVICE); if (aclRet != ACL_SUCCESS) { ERROR_LOG("Copy resized mat data to device failed."); return FAILED; @@ -207,7 +216,8 @@ aclrtRunMode runMode) { return SUCCESS; } -int Utils::ReadImageFile(ImageData& image, std::string fileName) { +int Utils::ReadImageFile(ImageData& image, std::string fileName) +{ struct stat sBuf; int fileStatus = stat(fileName.data(), &sBuf); if (fileStatus == -1) { @@ -245,7 +255,7 @@ int Utils::ReadImageFile(ImageData& image, std::string fileName) { int32_t ch = 0; acldvppJpegGetImageInfo(binFileBufferData, binFileBufferLen, - &(image.width), &(image.height), &ch); + &(image.width), &(image.height), &ch); image.data.reset(binFileBufferData, [](uint8_t* p) { delete[](p); }); image.size = binFileBufferLen; diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/dvpp_jpegd.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/dvpp_jpegd.h old mode 100755 new mode 100644 index 9a7508cea..e71dc2692 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/dvpp_jpegd.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/dvpp_jpegd.h @@ -83,7 +83,7 @@ private: acldvppChannelDesc *g_dvppChannelDesc_; void* g_decodeOutBufferDev_; // decode output buffer - acldvppPicDesc *g_decodeOutputDesc_; //decode output desc + acldvppPicDesc *g_decodeOutputDesc_; // decode output desc uint8_t *g_inDevBuffer_; // input pic dev buffer uint32_t g_inDevBufferSizeD_; // input pic size for decode diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/dvpp_process.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/dvpp_process.h old mode 100755 new mode 100644 diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/dvpp_resize.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/dvpp_resize.h old mode 100755 new mode 100644 index 140d5f2d4..6bfb2997a --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/dvpp_resize.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/dvpp_resize.h @@ -51,7 +51,6 @@ public: */ Result InitOutputPara(int modelInputWidth, int modelInputHeight); - /** * @brief gett dvpp output * @param [in] outputBuffer: pointer which points to dvpp output buffer diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/model_process.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/model_process.h old mode 100755 new mode 100644 index c533c6795..7f555f9c6 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/model_process.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/model_process.h @@ -26,7 +26,7 @@ * ModelProcess */ class ModelProcess { - public: +public: /** * @brief Constructor */ @@ -102,7 +102,7 @@ class ModelProcess { */ aclmdlDataset *GetModelOutputData(); - private: +private: bool g_loadFlag_; // model load flag uint32_t g_modelId_; void *g_modelMemPtr_; diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/object_detect.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/object_detect.h old mode 100755 new mode 100644 index 4cdf562dc..99227b8a0 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/object_detect.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/object_detect.h @@ -18,11 +18,11 @@ #define YOLOV3_COCO_DETECTION_PICTURE_INC_OBJECT_DETECT_H #pragma once +#include #include "utils.h" #include "acl/acl.h" #include "model_process.h" #include "dvpp_process.h" -#include using namespace std; @@ -31,7 +31,7 @@ using namespace std; */ class ObjectDetect { public: - ObjectDetect(const char* modelPath, + ObjectDetect(const char* modelPath, uint32_t modelWidth, uint32_t modelHeight); ~ObjectDetect(); diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/utils.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/utils.h old mode 100755 new mode 100644 index 32e30341a..f14d45d1b --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/utils.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/inc/utils.h @@ -20,9 +20,9 @@ #pragma once #include #include -#include "acl/acl.h" #include #include +#include "acl/acl.h" #include "opencv2/opencv.hpp" #include "opencv2/imgproc/types_c.h" @@ -44,7 +44,8 @@ using namespace std; #define SHARED_PTR_U8_BUF(buf) (shared_ptr((uint8_t *)(buf), [](uint8_t* p) { delete[](p); })) template -std::shared_ptr MakeSharedNoThrow() { +std::shared_ptr MakeSharedNoThrow() +{ try { return std::make_shared(); } @@ -55,8 +56,8 @@ std::shared_ptr MakeSharedNoThrow() { #define MAKE_SHARED_NO_THROW(memory, memory_type) \ do { \ - memory = MakeSharedNoThrow(); \ - }while(0); + memory = MakeSharedNoThrow<(memory_type)>(); \ + }while(0) typedef enum Result { SUCCESS = 0, diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/dvpp_jpegd.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/dvpp_jpegd.cpp old mode 100755 new mode 100644 index 942b8d8ed..85126733b --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/dvpp_jpegd.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/dvpp_jpegd.cpp @@ -22,14 +22,17 @@ using namespace std; DvppJpegD::DvppJpegD(aclrtStream& stream, acldvppChannelDesc *dvppChannelDesc) : g_stream_(stream), g_dvppChannelDesc_(dvppChannelDesc), - g_decodeOutBufferDev_(nullptr), g_decodeOutputDesc_(nullptr) { + g_decodeOutBufferDev_(nullptr), g_decodeOutputDesc_(nullptr) +{ } -DvppJpegD::~DvppJpegD() { +DvppJpegD::~DvppJpegD() +{ DestroyDecodeResource(); } -Result DvppJpegD::InitDecodeOutputDesc(ImageData& inputImage) { +Result DvppJpegD::InitDecodeOutputDesc(ImageData& inputImage) +{ uint32_t decodeOutWidthStride = ALIGN_UP128(inputImage.width); uint32_t decodeOutHeightStride = ALIGN_UP16(inputImage.height); if (decodeOutWidthStride == 0 || decodeOutHeightStride == 0) { @@ -37,8 +40,8 @@ Result DvppJpegD::InitDecodeOutputDesc(ImageData& inputImage) { return FAILED; } - /*Allocate a large enough memory*/ - uint32_t decodeOutBufferSize = + /* Allocate a large enough memory */ + uint32_t decodeOutBufferSize = YUV420SP_SIZE(decodeOutWidthStride, decodeOutHeightStride) * 4; aclError aclRet = acldvppMalloc(&g_decodeOutBufferDev_, decodeOutBufferSize); @@ -63,7 +66,8 @@ Result DvppJpegD::InitDecodeOutputDesc(ImageData& inputImage) { return SUCCESS; } -Result DvppJpegD::Process(ImageData& dest, ImageData& src) { +Result DvppJpegD::Process(ImageData& dest, ImageData& src) +{ int ret = InitDecodeOutputDesc(src); if (ret != SUCCESS) { ERROR_LOG("InitDecodeOutputDesc failed"); @@ -96,7 +100,8 @@ Result DvppJpegD::Process(ImageData& dest, ImageData& src) { return SUCCESS; } -void DvppJpegD::DestroyDecodeResource() { +void DvppJpegD::DestroyDecodeResource() +{ if (g_decodeOutputDesc_ != nullptr) { acldvppDestroyPicDesc(g_decodeOutputDesc_); g_decodeOutputDesc_ = nullptr; diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/dvpp_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/dvpp_process.cpp old mode 100755 new mode 100644 index e84315c0d..362ba3229 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/dvpp_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/dvpp_process.cpp @@ -22,14 +22,17 @@ using namespace std; -DvppProcess::DvppProcess():g_dvppChannelDesc_(nullptr) { +DvppProcess::DvppProcess():g_dvppChannelDesc_(nullptr) +{ } -DvppProcess::~DvppProcess() { +DvppProcess::~DvppProcess() +{ DestroyResource(); } -void DvppProcess::DestroyResource() { +void DvppProcess::DestroyResource() +{ aclError aclRet; if (g_dvppChannelDesc_ != nullptr) { aclRet = acldvppDestroyChannel(g_dvppChannelDesc_); @@ -42,7 +45,8 @@ void DvppProcess::DestroyResource() { } } -Result DvppProcess::InitResource(aclrtStream& stream) { +Result DvppProcess::InitResource(aclrtStream& stream) +{ aclError aclRet; g_dvppChannelDesc_ = acldvppCreateChannelDesc(); @@ -62,12 +66,14 @@ Result DvppProcess::InitResource(aclrtStream& stream) { } Result DvppProcess::Resize(ImageData& dest, ImageData& src, - uint32_t width, uint32_t height) { + uint32_t width, uint32_t height) +{ DvppResize resizeOp(g_stream_, g_dvppChannelDesc_, width, height); return resizeOp.Process(dest, src); } -Result DvppProcess::CvtJpegToYuv420sp(ImageData& dest, ImageData& src) { +Result DvppProcess::CvtJpegToYuv420sp(ImageData& dest, ImageData& src) +{ DvppJpegD jpegD(g_stream_, g_dvppChannelDesc_); return jpegD.Process(dest, src); } diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/dvpp_resize.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/dvpp_resize.cpp old mode 100755 new mode 100644 index 4747176eb..fed9c03e7 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/dvpp_resize.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/dvpp_resize.cpp @@ -22,23 +22,26 @@ using namespace std; DvppResize::DvppResize(aclrtStream& stream, acldvppChannelDesc *dvppChannelDesc, uint32_t width, uint32_t height) -: stream_(stream), g_dvppChannelDesc_(dvppChannelDesc), -g_resizeConfig_(nullptr), g_vpcInputDesc_(nullptr), g_vpcOutputDesc_(nullptr), -g_inDevBuffer_(nullptr),g_vpcOutBufferDev_(nullptr),g_vpcOutBufferSize_(0) { + : stream_(stream), g_dvppChannelDesc_(dvppChannelDesc), + g_resizeConfig_(nullptr), g_vpcInputDesc_(nullptr), g_vpcOutputDesc_(nullptr), + g_inDevBuffer_(nullptr), g_vpcOutBufferDev_(nullptr), g_vpcOutBufferSize_(0) +{ g_size_.width = width; g_size_.height = height; } -DvppResize::~DvppResize() { +DvppResize::~DvppResize() +{ DestroyResizeResource(); } -Result DvppResize::InitResizeInputDesc(ImageData& inputImage) { +Result DvppResize::InitResizeInputDesc(ImageData& inputImage) +{ uint32_t alignWidth = ALIGN_UP128(inputImage.width); uint32_t alignHeight = ALIGN_UP16(inputImage.height); if (alignWidth == 0 || alignHeight == 0) { ERROR_LOG("InitResizeInputDesc AlignmentHelper failed. image w %d, h %d, align w%d, h%d", - inputImage.width, inputImage.height, alignWidth, alignHeight); + inputImage.width, inputImage.height, alignWidth, alignHeight); return FAILED; } uint32_t inputBufferSize = YUV420SP_SIZE(alignWidth, alignHeight); @@ -49,9 +52,10 @@ Result DvppResize::InitResizeInputDesc(ImageData& inputImage) { } uint32_t inputWidth = inputImage.width; uint32_t inputHeight = inputImage.height; - if (inputImage.width % 2 != 0) + uint32_t n = 2; + if (inputImage.width % n != 0) inputWidth--; - if (inputImage.height % 2 != 0) + if (inputImage.height % n != 0) inputHeight--; acldvppSetPicDescData(g_vpcInputDesc_, inputImage.data.get()); @@ -64,7 +68,8 @@ Result DvppResize::InitResizeInputDesc(ImageData& inputImage) { return SUCCESS; } -Result DvppResize::InitResizeOutputDesc() { +Result DvppResize::InitResizeOutputDesc() +{ int resizeOutWidth = g_size_.width; int resizeOutHeight = g_size_.height; int resizeOutWidthStride = ALIGN_UP16(resizeOutWidth); @@ -98,7 +103,8 @@ Result DvppResize::InitResizeOutputDesc() { return SUCCESS; } -Result DvppResize::InitResizeResource(ImageData& inputImage) { +Result DvppResize::InitResizeResource(ImageData& inputImage) +{ g_format_ = static_cast(PIXEL_FORMAT_YUV_SEMIPLANAR_420); g_resizeConfig_ = acldvppCreateResizeConfig(); if (g_resizeConfig_ == nullptr) { @@ -109,17 +115,18 @@ Result DvppResize::InitResizeResource(ImageData& inputImage) { if (SUCCESS != InitResizeInputDesc(inputImage)) { ERROR_LOG("InitResizeInputDesc failed"); return FAILED; - } + } if (SUCCESS != InitResizeOutputDesc()) { ERROR_LOG("InitResizeOutputDesc failed"); return FAILED; - } + } - return SUCCESS; + return SUCCESS; } -Result DvppResize::Process(ImageData& resizedImage, ImageData& srcImage) { +Result DvppResize::Process(ImageData& resizedImage, ImageData& srcImage) +{ if (SUCCESS != InitResizeResource(srcImage)) { ERROR_LOG("Dvpp resize failed for init error"); return FAILED; @@ -151,7 +158,8 @@ Result DvppResize::Process(ImageData& resizedImage, ImageData& srcImage) { return SUCCESS; } -void DvppResize::DestroyResizeResource() { +void DvppResize::DestroyResizeResource() +{ if (g_resizeConfig_ != nullptr) { (void)acldvppDestroyResizeConfig(g_resizeConfig_); g_resizeConfig_ = nullptr; diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/main.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/main.cpp index 80d08fa4e..9baf502e9 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/main.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/main.cpp @@ -23,20 +23,22 @@ using namespace std; namespace { -uint32_t g_ModelWidth = 416; -uint32_t g_ModelHeight = 416; -const char* g_ModelPath = "../model/yolov3.om"; +uint32_t g_modelWidth = 416; +uint32_t g_modelHeight = 416; +uint32_t argNum = 2; +const char* g_modelPath = "../model/yolov3.om"; } -int main(int argc, char *argv[]) { +int main(int argc, char *argv[]) +{ // Check input args: the path of input picture, and ignore hidden dotfile directory - if ((argc != 2) || (argv[1] == nullptr)) { + if ((argc != argNum) || (argv[1] == nullptr)) { ERROR_LOG("Please input: ./main "); return FAILED; } // Instantiate the object detection class - ObjectDetect detect(g_ModelPath, g_ModelWidth, g_ModelHeight); - // Initialize the acl resources, dvpp, load model, + ObjectDetect detect(g_modelPath, g_modelWidth, g_modelHeight); + // Initialize the acl resources, dvpp, load model, // and malloc input memory of input which is const Result ret = detect.Init(); if (ret != SUCCESS) { @@ -67,10 +69,10 @@ int main(int argc, char *argv[]) { Result ret = detect.Preprocess(resizedImage, image); if (ret != SUCCESS) { ERROR_LOG("Read file %s failed, continue to read next", - imageFile.c_str()); + imageFile.c_str()); continue; } - // Send the resized picture to the model for inference + // Send the resized picture to the model for inference // and get the inference results aclmdlDataset* inferenceOutput = nullptr; ret = detect.Inference(inferenceOutput, resizedImage); @@ -78,7 +80,7 @@ int main(int argc, char *argv[]) { ERROR_LOG("Inference model inference output data failed"); return FAILED; } - // Analyze the inference output, mark the object category and + // Analyze the inference output, mark the object category and // location by the inference result ret = detect.Postprocess(image, inferenceOutput, imageFile); if (ret != SUCCESS) { diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/model_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/model_process.cpp old mode 100755 new mode 100644 index 07ddb80a3..6d624db38 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/model_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/model_process.cpp @@ -14,22 +14,25 @@ * limitations under the License. */ -#include "model_process.h" #include +#include "model_process.h" #include "utils.h" using namespace std; -ModelProcess::ModelProcess():g_loadFlag_(false), g_modelId_(0), g_modelMemPtr_(nullptr), g_modelMemSize_(0), -g_modelWeightPtr_(nullptr),g_modelWeightSize_(0), g_modelDesc_(nullptr), g_input_(nullptr), g_output_(nullptr), -g_isReleased_(false) { - +ModelProcess::ModelProcess() + : g_loadFlag_(false), g_modelId_(0), g_modelMemPtr_(nullptr), g_modelMemSize_(0), + g_modelWeightPtr_(nullptr), g_modelWeightSize_(0), g_modelDesc_(nullptr), + g_input_(nullptr), g_output_(nullptr), g_isReleased_(false) +{ } -ModelProcess::~ModelProcess() { +ModelProcess::~ModelProcess() +{ DestroyResource(); } -void ModelProcess::DestroyResource() { +void ModelProcess::DestroyResource() +{ if (g_isReleased_) return; Unload(); @@ -39,7 +42,8 @@ void ModelProcess::DestroyResource() { g_isReleased_ = true; } -Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath) { +Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath) +{ if (g_loadFlag_) { ERROR_LOG("has already loaded a model"); return FAILED; @@ -75,7 +79,8 @@ Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath) { return SUCCESS; } -Result ModelProcess::CreateDesc() { +Result ModelProcess::CreateDesc() +{ g_modelDesc_ = aclmdlCreateDesc(); if (g_modelDesc_ == nullptr) { ERROR_LOG("create model description failed"); @@ -92,7 +97,8 @@ Result ModelProcess::CreateDesc() { return SUCCESS; } -void ModelProcess::DestroyDesc() { +void ModelProcess::DestroyDesc() +{ if (g_modelDesc_ != nullptr) { (void)aclmdlDestroyDesc(g_modelDesc_); g_modelDesc_ = nullptr; @@ -100,7 +106,8 @@ void ModelProcess::DestroyDesc() { } Result ModelProcess::CreateInput(void *input1, size_t input1size, - void* input2, size_t input2size) { + void* input2, size_t input2size) +{ g_input_ = aclmdlCreateDataset(); if (g_input_ == nullptr) { ERROR_LOG("can't create dataset, create input failed"); @@ -138,7 +145,8 @@ Result ModelProcess::CreateInput(void *input1, size_t input1size, return SUCCESS; } -void ModelProcess::DestroyInput() { +void ModelProcess::DestroyInput() +{ if (g_input_ == nullptr) { return; } @@ -151,7 +159,8 @@ void ModelProcess::DestroyInput() { g_input_ = nullptr; } -Result ModelProcess::CreateOutput() { +Result ModelProcess::CreateOutput() +{ if (g_modelDesc_ == nullptr) { ERROR_LOG("no model description, create ouput failed"); return FAILED; @@ -194,7 +203,8 @@ Result ModelProcess::CreateOutput() { return SUCCESS; } -void ModelProcess::DestroyOutput() { +void ModelProcess::DestroyOutput() +{ if (g_output_ == nullptr) { return; } @@ -210,7 +220,8 @@ void ModelProcess::DestroyOutput() { g_output_ = nullptr; } -Result ModelProcess::Execute() { +Result ModelProcess::Execute() +{ aclError ret = aclmdlExecute(g_modelId_, g_input_, g_output_); if (ret != ACL_SUCCESS) { ERROR_LOG("execute model failed, modelId is %u", g_modelId_); @@ -221,7 +232,8 @@ Result ModelProcess::Execute() { return SUCCESS; } -void ModelProcess::Unload() { +void ModelProcess::Unload() +{ if (!g_loadFlag_) { WARN_LOG("no model had been loaded, unload failed"); return; @@ -253,6 +265,7 @@ void ModelProcess::Unload() { INFO_LOG("unload model success, modelId is %u", g_modelId_); } -aclmdlDataset *ModelProcess::GetModelOutputData() { +aclmdlDataset *ModelProcess::GetModelOutputData() +{ return g_output_; -} +} \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/object_detect.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/object_detect.cpp old mode 100755 new mode 100644 index 3fdab378a..98b8f4a07 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/object_detect.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/object_detect.cpp @@ -14,75 +14,77 @@ * limitations under the License. */ -#include "object_detect.h" #include #include - +#include +#include +#include +#include "object_detect.h" #include "opencv2/opencv.hpp" #include "acl/acl.h" #include "model_process.h" #include "utils.h" -#include -#include -#include using namespace std; namespace { const static std::vector yolov3Label = { "person", "bicycle", "car", "motorbike", - "aeroplane","bus", "train", "truck", "boat", - "traffic light", "fire hydrant", "stop sign", "parking meter", - "bench", "bird", "cat", "dog", "horse", - "sheep", "cow", "elephant", "bear", "zebra", - "giraffe", "backpack", "umbrella", "handbag","tie", - "suitcase", "frisbee", "skis", "snowboard", "sports ball", - "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", - "tennis racket", "bottle", "wine glass", "cup", - "fork", "knife", "spoon", "bowl", "banana", - "apple", "sandwich", "orange", "broccoli", "carrot", - "hot dog", "pizza", "donut", "cake", "chair", - "sofa", "potted plant", "bed", "dining table", "toilet", - "TV monitor", "laptop", "mouse", "remote", "keyboard", - "cell phone", "microwave", "oven", "toaster", "sink", - "refrigerator", "book", "clock", "vase","scissors", - "teddy bear", "hair drier", "toothbrush" }; - - const uint32_t kBBoxDataBufId = 0; - const uint32_t kBoxNumDataBufId = 1; + "aeroplane", "bus", "train", "truck", "boat", + "traffic light", "fire hydrant", "stop sign", "parking meter", + "bench", "bird", "cat", "dog", "horse", + "sheep", "cow", "elephant", "bear", "zebra", + "giraffe", "backpack", "umbrella", "handbag", "tie", + "suitcase", "frisbee", "skis", "snowboard", "sports ball", + "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", + "tennis racket", "bottle", "wine glass", "cup", + "fork", "knife", "spoon", "bowl", "banana", + "apple", "sandwich", "orange", "broccoli", "carrot", + "hot dog", "pizza", "donut", "cake", "chair", + "sofa", "potted plant", "bed", "dining table", "toilet", + "TV monitor", "laptop", "mouse", "remote", "keyboard", + "cell phone", "microwave", "oven", "toaster", "sink", + "refrigerator", "book", "clock", "vase", "scissors", + "teddy bear", "hair drier", "toothbrush" }; + + const uint32_t g_bBoxDataBufId = 0; + const uint32_t g_boxNumDataBufId = 1; enum BBoxIndex { TOPLEFTX = 0, TOPLEFTY, BOTTOMRIGHTX, BOTTOMRIGHTY, SCORE, LABEL }; // bounding box line solid - const uint32_t kLineSolid = 2; + const uint32_t g_lineSolid = 2; // output image prefix - const string kOutputFilePrefix = "out_"; + const string g_outputFilePrefix = "out_"; // opencv draw label params. - const double kFountScale = 0.5; - const cv::Scalar kFontColor(0, 0, 255); - const uint32_t kLabelOffset = 11; - const string kFileSperator = "/"; + const double g_fountScale = 0.5; + const cv::Scalar g_fontColor(0, 0, 255); + const uint32_t g_labelOffset = 11; + const string g_fileSperator = "/"; // opencv color list for boundingbox - const vector kColors { + const vector g_colors { cv::Scalar(237, 149, 100), cv::Scalar(0, 215, 255), cv::Scalar(50, 205, 50), cv::Scalar(139, 85, 26) }; } ObjectDetect::ObjectDetect(const char* modelPath, uint32_t modelWidth, -uint32_t modelHeight) -:g_deviceId_(0), g_context_(nullptr), g_stream_(nullptr), g_modelWidth_(modelWidth), -g_modelHeight_(modelHeight), g_isInited_(false), g_isDeviceSet_(false) { + uint32_t modelHeight) + : g_deviceId_(0), g_context_(nullptr), g_stream_(nullptr), g_modelWidth_(modelWidth), + g_modelHeight_(modelHeight), g_isInited_(false), g_isDeviceSet_(false) +{ g_imageInfoSize_ = 0; g_imageInfoBuf_ = nullptr; g_modelPath_ = modelPath; } -ObjectDetect::~ObjectDetect() { +ObjectDetect::~ObjectDetect() +{ DestroyResource(); } -Result ObjectDetect::InitResource() { +Result ObjectDetect::InitResource() +{ // ACL init const char *aclConfigPath = "../src/acl.json"; aclError ret = aclInit(aclConfigPath); @@ -125,7 +127,8 @@ Result ObjectDetect::InitResource() { return SUCCESS; } -Result ObjectDetect::InitModel(const char* omModelPath) { +Result ObjectDetect::InitModel(const char* omModelPath) +{ Result ret = g_model_.LoadModelFromFileWithMem(omModelPath); if (ret != SUCCESS) { ERROR_LOG("execute LoadModelFromFileWithMem failed"); @@ -147,9 +150,10 @@ Result ObjectDetect::InitModel(const char* omModelPath) { return SUCCESS; } -Result ObjectDetect::CreateImageInfoBuffer() { +Result ObjectDetect::CreateImageInfoBuffer() +{ const float imageInfo[4] = {(float)g_modelWidth_, (float)g_modelHeight_, - (float)g_modelWidth_, (float)g_modelHeight_}; + (float)g_modelWidth_, (float)g_modelHeight_}; g_imageInfoSize_ = sizeof(imageInfo); if (g_runMode_ == ACL_HOST) g_imageInfoBuf_ = Utils::CopyDataHostToDevice((void *)imageInfo, g_imageInfoSize_); @@ -163,7 +167,8 @@ Result ObjectDetect::CreateImageInfoBuffer() { return SUCCESS; } -Result ObjectDetect::Init() { +Result ObjectDetect::Init() +{ if (g_isInited_) { INFO_LOG("Object detection instance is initied already!"); return SUCCESS; @@ -197,7 +202,8 @@ Result ObjectDetect::Init() { return SUCCESS; } -Result ObjectDetect::Preprocess(ImageData& resizedImage, ImageData& srcImage) { +Result ObjectDetect::Preprocess(ImageData& resizedImage, ImageData& srcImage) +{ ImageData imageDevice; Utils::CopyImageDataToDevice(imageDevice, srcImage, g_runMode_); @@ -219,7 +225,8 @@ Result ObjectDetect::Preprocess(ImageData& resizedImage, ImageData& srcImage) { } Result ObjectDetect::Inference(aclmdlDataset*& inferenceOutput, -ImageData& resizedImage) { + ImageData& resizedImage) +{ Result ret = g_model_.CreateInput(resizedImage.data.get(), resizedImage.size, g_imageInfoBuf_, g_imageInfoSize_); @@ -240,13 +247,14 @@ ImageData& resizedImage) { } Result ObjectDetect::Postprocess(ImageData& image, aclmdlDataset* modelOutput, -const string& origImagePath) { + const string& origImagePath) +{ uint32_t dataSize = 0; float* detectData = (float *)GetInferenceOutputItem(dataSize, modelOutput, - kBBoxDataBufId); + g_bBoxDataBufId); uint32_t* boxNum = (uint32_t *)GetInferenceOutputItem(dataSize, modelOutput, - kBoxNumDataBufId); + g_boxNumDataBufId); if (boxNum == nullptr) return FAILED; uint32_t totalBox = boxNum[0]; @@ -265,7 +273,7 @@ const string& origImagePath) { uint32_t objIndex = (uint32_t)detectData[totalBox * LABEL + i]; boundBox.text = yolov3Label[objIndex] + std::to_string(score) + "\%"; printf("%d %d %d %d %s\n", boundBox.rect.ltX, boundBox.rect.ltY, - boundBox.rect.rbX, boundBox.rect.rbY, boundBox.text.c_str()); + boundBox.rect.rbX, boundBox.rect.rbY, boundBox.text.c_str()); detectResults.emplace_back(boundBox); } @@ -280,26 +288,27 @@ const string& origImagePath) { } void* ObjectDetect::GetInferenceOutputItem(uint32_t& itemDataSize, -aclmdlDataset* inferenceOutput, -uint32_t idx) { + aclmdlDataset* inferenceOutput, + uint32_t idx) +{ aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(inferenceOutput, idx); if (dataBuffer == nullptr) { ERROR_LOG("Get the %dth dataset buffer from model " - "inference output failed", idx); + "inference output failed", idx); return nullptr; } void* dataBufferDev = aclGetDataBufferAddr(dataBuffer); if (dataBufferDev == nullptr) { ERROR_LOG("Get the %dth dataset buffer address " - "from model inference output failed", idx); + "from model inference output failed", idx); return nullptr; } size_t bufferSize = aclGetDataBufferSize(dataBuffer); if (bufferSize == 0) { ERROR_LOG("The %dth dataset buffer size of " - "model inference output is 0", idx); + "model inference output is 0", idx); return nullptr; } @@ -319,7 +328,8 @@ uint32_t idx) { } void ObjectDetect::DrawBoundBoxToImage(vector& detectionResults, -const string& origImagePath) { + const string& origImagePath) +{ cv::Mat image = cv::imread(origImagePath, CV_LOAD_IMAGE_UNCHANGED); for (int i = 0; i < detectionResults.size(); ++i) { cv::Point p1, p2; @@ -327,9 +337,9 @@ const string& origImagePath) { p1.y = detectionResults[i].rect.ltY; p2.x = detectionResults[i].rect.rbX; p2.y = detectionResults[i].rect.rbY; - cv::rectangle(image, p1, p2, kColors[i % kColors.size()], kLineSolid); - cv::putText(image, detectionResults[i].text, cv::Point(p1.x, p1.y + kLabelOffset), - cv::FONT_HERSHEY_COMPLEX, kFountScale, kFontColor); + cv::rectangle(image, p1, p2, g_colors[i % g_colors.size()], g_lineSolid); + cv::putText(image, detectionResults[i].text, cv::Point(p1.x, p1.y + g_labelOffset), + cv::FONT_HERSHEY_COMPLEX, g_fountScale, g_fontColor); } string folderPath = "./output"; @@ -344,7 +354,8 @@ const string& origImagePath) { cv::imwrite(sstream.str(), image); } -void ObjectDetect::DestroyResource() { +void ObjectDetect::DestroyResource() +{ aclrtFree(g_imageInfoBuf_); g_model_.DestroyResource(); g_dvpp_.DestroyResource(); diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/utils.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/utils.cpp old mode 100755 new mode 100644 index b79d509d2..af0abf98c --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/utils.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture/src/utils.cpp @@ -14,7 +14,6 @@ * limitations under the License. */ -#include "utils.h" #include #include #include @@ -25,25 +24,27 @@ #include #include #include +#include "utils.h" #include "acl/acl.h" #include "acl/ops/acl_dvpp.h" using namespace std; namespace { -const std::string kImagePathSeparator = ","; -const int kStatSuccess = 0; -const std::string kFileSperator = "/"; -const std::string kPathSeparator = "/"; +const std::string g_imagePathSeparator = ","; +const int STATSUCCESS = 0; +const std::string g_fileSperator = "/"; +const std::string g_pathSeparator = "/"; // output image prefix -const std::string kOutputFilePrefix = "out_"; +const std::string g_outputFilePrefix = "out_"; } -bool Utils::IsDirectory(const string &path) { +bool Utils::IsDirectory(const string &path) +{ // get path stat struct stat buf; - if (stat(path.c_str(), &buf) != kStatSuccess) { + if (stat(path.c_str(), &buf) != STATSUCCESS) { return false; } @@ -55,7 +56,8 @@ bool Utils::IsDirectory(const string &path) { } } -bool Utils::IsPathExist(const string &path) { +bool Utils::IsPathExist(const string &path) +{ ifstream file(path); if (!file) { return false; @@ -63,17 +65,17 @@ bool Utils::IsPathExist(const string &path) { return true; } -void Utils::SplitPath(const string &path, vector &path_vec) { - char *char_path = const_cast(path.c_str()); - const char *char_split = kImagePathSeparator.c_str(); - char *tmp_path = strtok(char_path, char_split); +void Utils::SplitPath(const string &path, vector &path_vec) +{ + char *tmp_path = strtok(const_cast(path.c_str()), g_imagePathSeparator.c_str()); while (tmp_path) { path_vec.emplace_back(tmp_path); - tmp_path = strtok(nullptr, char_split); + tmp_path = strtok(nullptr, const_cast(path.c_str())); } } -void Utils::GetAllFiles(const string &path, vector &file_vec) { +void Utils::GetAllFiles(const string &path, vector &file_vec) +{ // split file path vector path_vector; SplitPath(path, path_vector); @@ -82,7 +84,7 @@ void Utils::GetAllFiles(const string &path, vector &file_vec) { // check path exist or not if (!IsPathExist(every_path)) { ERROR_LOG("Failed to deal path=%s. Reason: not exist " - "or can not access.", every_path.c_str()); + "or can not access.", every_path.c_str()); continue; } // get files in path and sub-path @@ -90,7 +92,8 @@ void Utils::GetAllFiles(const string &path, vector &file_vec) { } } -void Utils::GetPathFiles(const string &path, vector &file_vec) { +void Utils::GetPathFiles(const string &path, vector &file_vec) +{ struct dirent *dirent_ptr = nullptr; DIR *dir = nullptr; if (IsDirectory(path)) { @@ -102,7 +105,7 @@ void Utils::GetPathFiles(const string &path, vector &file_vec) { } // file path - string full_path = path + kPathSeparator + dirent_ptr->d_name; + string full_path = path + g_pathSeparator + dirent_ptr->d_name; // directory need recursion if (IsDirectory(full_path)) { GetPathFiles(full_path, file_vec); @@ -116,13 +119,14 @@ void Utils::GetPathFiles(const string &path, vector &file_vec) { } } -void* Utils::CopyDataHostToDvpp(void* data, int size) { +void* Utils::CopyDataHostToDvpp(void* data, int size) +{ void* buffer = nullptr; auto aclRet = acldvppMalloc(&buffer, size); if (aclRet != ACL_SUCCESS) { ERROR_LOG("acl malloc dvpp data failed, dataSize=%u, ret=%d", - size, aclRet); + size, aclRet); return nullptr; } INFO_LOG("malloc dvpp memory size %d ok", size); @@ -138,13 +142,14 @@ void* Utils::CopyDataHostToDvpp(void* data, int size) { return buffer; } -void* Utils::CopyDataDeviceToDvpp(void* data, int size) { +void* Utils::CopyDataDeviceToDvpp(void* data, int size) +{ void* buffer = nullptr; auto aclRet = acldvppMalloc(&buffer, size); if (aclRet != ACL_SUCCESS) { ERROR_LOG("acl malloc dvpp data failed, dataSize=%u, ret=%d", - size, aclRet); + size, aclRet); return nullptr; } INFO_LOG("malloc dvpp memory size %d ok", size); @@ -152,7 +157,7 @@ void* Utils::CopyDataDeviceToDvpp(void* data, int size) { aclRet = aclrtMemcpy(buffer, size, data, size, ACL_MEMCPY_DEVICE_TO_DEVICE); if (aclRet != ACL_SUCCESS) { ERROR_LOG("acl memcpy data to dvpp failed, size %u, error %d", size, aclRet); - acldvppFree(buffer); + acldvppFree(buffer); return nullptr; } INFO_LOG("copy data to dvpp ok"); @@ -160,7 +165,8 @@ void* Utils::CopyDataDeviceToDvpp(void* data, int size) { return buffer; } -Result Utils::CopyImageDataToDvpp(ImageData& imageDevice, ImageData srcImage) { +Result Utils::CopyImageDataToDvpp(ImageData& imageDevice, ImageData srcImage) +{ aclrtRunMode runMode_; aclError ret = aclrtGetRunMode(&runMode_); if (ret != ACL_SUCCESS) { @@ -175,8 +181,7 @@ Result Utils::CopyImageDataToDvpp(ImageData& imageDevice, ImageData srcImage) { ERROR_LOG("Copy image to device failed"); return FAILED; } - } - else { + } else { buffer = Utils::CopyDataDeviceToDvpp(srcImage.data.get(), srcImage.size); if (buffer == nullptr) { ERROR_LOG("Copy image to device failed"); @@ -191,7 +196,8 @@ Result Utils::CopyImageDataToDvpp(ImageData& imageDevice, ImageData srcImage) { return SUCCESS; } -void* Utils::CopyDataDeviceToLocal(void* deviceData, uint32_t dataSize) { +void* Utils::CopyDataDeviceToLocal(void* deviceData, uint32_t dataSize) +{ uint8_t* buffer = new uint8_t[dataSize]; if (buffer == nullptr) { ERROR_LOG("New malloc memory failed"); @@ -208,7 +214,8 @@ void* Utils::CopyDataDeviceToLocal(void* deviceData, uint32_t dataSize) { return (void*)buffer; } -void* Utils::CopyDataToDevice(void* data, uint32_t dataSize, aclrtMemcpyKind policy) { +void* Utils::CopyDataToDevice(void* data, uint32_t dataSize, aclrtMemcpyKind policy) +{ void* buffer = nullptr; aclError aclRet = aclrtMalloc(&buffer, dataSize, ACL_MEM_MALLOC_HUGE_FIRST); if (aclRet != ACL_SUCCESS) { @@ -226,17 +233,20 @@ void* Utils::CopyDataToDevice(void* data, uint32_t dataSize, aclrtMemcpyKind pol return buffer; } -void* Utils::CopyDataDeviceToDevice(void* deviceData, uint32_t dataSize) { +void* Utils::CopyDataDeviceToDevice(void* deviceData, uint32_t dataSize) +{ return CopyDataToDevice(deviceData, dataSize, ACL_MEMCPY_DEVICE_TO_DEVICE); } -void* Utils::CopyDataHostToDevice(void* deviceData, uint32_t dataSize) { +void* Utils::CopyDataHostToDevice(void* deviceData, uint32_t dataSize) +{ return CopyDataToDevice(deviceData, dataSize, ACL_MEMCPY_HOST_TO_DEVICE); } -Result Utils::CopyImageDataToDevice(ImageData& imageDevice, - ImageData srcImage, aclrtRunMode mode) { - void * buffer; +Result Utils::CopyImageDataToDevice(ImageData& imageDevice, + ImageData srcImage, aclrtRunMode mode) +{ + void *buffer; if (mode == ACL_HOST) buffer = Utils::CopyDataHostToDevice(srcImage.data.get(), srcImage.size); else @@ -255,7 +265,8 @@ Result Utils::CopyImageDataToDevice(ImageData& imageDevice, return SUCCESS; } -int Utils::ReadImageFile(ImageData& image, std::string fileName) { +int Utils::ReadImageFile(ImageData& image, std::string fileName) +{ struct stat sBuf; int fileStatus = stat(fileName.data(), &sBuf); if (fileStatus == -1) { @@ -293,9 +304,9 @@ int Utils::ReadImageFile(ImageData& image, std::string fileName) { int32_t ch = 0; acldvppJpegGetImageInfo(binFileBufferData, binFileBufferLen, - &(image.width), &(image.height), &ch); + &(image.width), &(image.height), &ch); image.data.reset(binFileBufferData, [](uint8_t* p) { delete[](p); }); image.size = binFileBufferLen; return SUCCESS; -} +} \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/inc/utils.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/inc/utils.h index 97e2ef04b..de9e8542a 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/inc/utils.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/inc/utils.h @@ -65,10 +65,12 @@ struct Resolution { class RunStatus { public: - static void SetDeviceStatus(bool isDevice) { + static void SetDeviceStatus(bool isDevice) + { isDevice_ = isDevice; } - static bool GetDeviceStatus() { + static bool GetDeviceStatus() + { return isDevice_; } private: diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/dvpp_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/dvpp_process.cpp index 91b91d51c..4ecf5e559 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/dvpp_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/dvpp_process.cpp @@ -19,7 +19,8 @@ #include "utils.h" using namespace std; -namespace { +namespace +{ uint32_t AlignSize(uint32_t origSize, uint32_t alignment) { if (alignment == 0) { return 0; @@ -29,20 +30,24 @@ namespace { } } -DvppProcess::DvppProcess(aclrtStream& stream) : g_stream_(stream), g_dvppChannelDesc_(nullptr), - g_resizeConfig_(nullptr), g_decodeOutDevBuffer_(nullptr),g_decodeOutputDesc_(nullptr), g_resizeInputDesc_(nullptr), - g_resizeOutputDesc_(nullptr), g_inDevBuffer_(nullptr),g_inDevBufferSize_(0), g_jpegDecodeOutputSize_(0), - g_decodeOutputWidth_(0), g_decodeOutputWidthStride_(0), g_decodeOutputHeight_(0), g_resizeOutBufferDev_(nullptr), - g_resizeOutBufferSize_(0), g_modelInputWidth_(0), g_modelInputHeight_(0), g_resizeOutWidthStride_(0), - g_resizeOutHeightStride_(0) { +DvppProcess::DvppProcess(aclrtStream& stream) + : g_stream_(stream), g_dvppChannelDesc_(nullptr), + g_resizeConfig_(nullptr), g_decodeOutDevBuffer_(nullptr), g_decodeOutputDesc_(nullptr), g_resizeInputDesc_(nullptr), + g_resizeOutputDesc_(nullptr), g_inDevBuffer_(nullptr), g_inDevBufferSize_(0), g_jpegDecodeOutputSize_(0), + g_decodeOutputWidth_(0), g_decodeOutputWidthStride_(0), g_decodeOutputHeight_(0), g_resizeOutBufferDev_(nullptr), + g_resizeOutBufferSize_(0), g_modelInputWidth_(0), g_modelInputHeight_(0), g_resizeOutWidthStride_(0), + g_resizeOutHeightStride_(0) +{ } -DvppProcess::~DvppProcess() { +DvppProcess::~DvppProcess() +{ DestroyResource(); DestroyOutputPara(); } -Result DvppProcess::InitResource() { +Result DvppProcess::InitResource() +{ g_dvppChannelDesc_ = acldvppCreateChannelDesc(); if (g_dvppChannelDesc_ == nullptr) { ERROR_LOG("acldvppCreateChannelDesc failed"); @@ -65,13 +70,15 @@ Result DvppProcess::InitResource() { return SUCCESS; } -void DvppProcess::SetInput(void *inDevBuffer, uint32_t inDevBufferSize, const PicDesc &picDesc) { +void DvppProcess::SetInput(void *inDevBuffer, uint32_t inDevBufferSize, const PicDesc &picDesc) +{ g_inDevBuffer_ = inDevBuffer; g_inDevBufferSize_ = inDevBufferSize; g_jpegDecodeOutputSize_ = picDesc.jpegDecodeSize; } -void DvppProcess::GetDvppOutput(void **outputBuffer, int &outputSize) { +void DvppProcess::GetDvppOutput(void **outputBuffer, int &outputSize) +{ if (outputBuffer == nullptr) { ERROR_LOG("outputBuffer is nullptr"); return; @@ -82,10 +89,11 @@ void DvppProcess::GetDvppOutput(void **outputBuffer, int &outputSize) { g_resizeOutBufferSize_ = 0; } -Result DvppProcess::InitDvppOutputPara(int modelInputWidth, int modelInputHeight) { +Result DvppProcess::InitDvppOutputPara(int modelInputWidth, int modelInputHeight) +{ if ((modelInputWidth <= 0) || (modelInputHeight <= 0)) { ERROR_LOG("InitInput para invalid, modelInputWidth %d, modelInputHeight %d", - modelInputWidth, modelInputHeight); + modelInputWidth, modelInputHeight); return FAILED; } @@ -105,14 +113,16 @@ Result DvppProcess::InitDvppOutputPara(int modelInputWidth, int modelInputHeight return SUCCESS; } -void DvppProcess::DestroyOutputPara() { +void DvppProcess::DestroyOutputPara() +{ if (g_resizeOutBufferDev_ != nullptr) { (void)acldvppFree(g_resizeOutBufferDev_); g_resizeOutBufferDev_ = nullptr; } } -Result DvppProcess::InitDecodeOutputDesc() { +Result DvppProcess::InitDecodeOutputDesc() +{ aclError ret = acldvppMalloc(&g_decodeOutDevBuffer_, g_jpegDecodeOutputSize_); if (ret != ACL_SUCCESS) { ERROR_LOG("acldvppMalloc jpegOutBufferDev failed, errorCode = %d", static_cast(ret)); @@ -131,7 +141,8 @@ Result DvppProcess::InitDecodeOutputDesc() { return SUCCESS; } -Result DvppProcess::ProcessDecode() { +Result DvppProcess::ProcessDecode() +{ // decode to yuv format aclError ret = acldvppJpegDecodeAsync(g_dvppChannelDesc_, g_inDevBuffer_, g_inDevBufferSize_, g_decodeOutputDesc_, g_stream_); @@ -152,14 +163,16 @@ Result DvppProcess::ProcessDecode() { return SUCCESS; } -void DvppProcess::DestroyDecodeResource() { +void DvppProcess::DestroyDecodeResource() +{ if (g_decodeOutputDesc_ != nullptr) { (void)acldvppDestroyPicDesc(g_decodeOutputDesc_); g_decodeOutputDesc_ = nullptr; } } -Result DvppProcess::InitResizeInputDesc() { +Result DvppProcess::InitResizeInputDesc() +{ uint32_t jpegOutWidthStride = g_decodeOutputWidthStride_; // 128-byte alignment on 310, 64-byte alignment on 310P uint32_t jpegOutHeightStride = AlignSize(g_decodeOutputHeight_, 16); // 16-byte alignment uint32_t jpegOutBufferSize = jpegOutWidthStride * jpegOutHeightStride * 3 / 2; // yuv format size @@ -179,7 +192,8 @@ Result DvppProcess::InitResizeInputDesc() { return SUCCESS; } -Result DvppProcess::InitResizeOutputDesc() { +Result DvppProcess::InitResizeOutputDesc() +{ g_resizeOutputDesc_ = acldvppCreatePicDesc(); if (g_resizeOutputDesc_ == nullptr) { ERROR_LOG("acldvppCreatePicDesc failed"); @@ -196,7 +210,8 @@ Result DvppProcess::InitResizeOutputDesc() { return SUCCESS; } -Result DvppProcess::ProcessResize() { +Result DvppProcess::ProcessResize() +{ // resize pic size aclError ret = acldvppSetResizeConfigInterpolation(g_resizeConfig_, 0); ret = acldvppVpcResizeAsync(g_dvppChannelDesc_, g_resizeInputDesc_, @@ -215,7 +230,8 @@ Result DvppProcess::ProcessResize() { return SUCCESS; } -void DvppProcess::DestroyResizeResource() { +void DvppProcess::DestroyResizeResource() +{ if (g_decodeOutDevBuffer_ != nullptr) { (void)acldvppFree(g_decodeOutDevBuffer_); g_decodeOutDevBuffer_ = nullptr; @@ -232,7 +248,8 @@ void DvppProcess::DestroyResizeResource() { } } -void DvppProcess::DestroyResource() { +void DvppProcess::DestroyResource() +{ // g_resizeConfig_ is created in initResource if (g_resizeConfig_ != nullptr) { (void)acldvppDestroyResizeConfig(g_resizeConfig_); @@ -250,7 +267,8 @@ void DvppProcess::DestroyResource() { } } -Result DvppProcess::Process() { +Result DvppProcess::Process() +{ // pic decode Result ret = InitDecodeOutputDesc(); if (ret != SUCCESS) { @@ -294,4 +312,4 @@ Result DvppProcess::Process() { INFO_LOG("Process dvpp success"); return SUCCESS; -} +} \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/main.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/main.cpp index 34d9a506e..329070ae6 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/main.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/main.cpp @@ -19,7 +19,8 @@ #include "utils.h" using namespace std; -int main() { +int main() +{ SampleProcess sampleProcess; Result ret = sampleProcess.InitResource(); if (ret != SUCCESS) { diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/model_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/model_process.cpp index 06e68d762..29b4584d5 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/model_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/model_process.cpp @@ -14,23 +14,28 @@ * limitations under the License. */ -#include "model_process.h" #include +#include "model_process.h" #include "utils.h" using namespace std; -ModelProcess::ModelProcess() : g_loadFlag_(false), g_modelId_(0), g_modelWorkPtr_(nullptr), g_modelWorkSize_(0), - g_modelWeightPtr_(nullptr),g_modelWeightSize_(0), g_modelDesc_(nullptr), g_input_(nullptr), g_output_(nullptr) { +ModelProcess::ModelProcess() + : g_loadFlag_(false), g_modelId_(0), g_modelWorkPtr_(nullptr), g_modelWorkSize_(0), + g_modelWeightPtr_(nullptr), g_modelWeightSize_(0), g_modelDesc_(nullptr), + g_input_(nullptr), g_output_(nullptr) +{ } -ModelProcess::~ModelProcess() { +ModelProcess::~ModelProcess() +{ UnloadModel(); DestroyDesc(); DestroyInput(); DestroyOutput(); } -Result ModelProcess::LoadModel(const char *modelPath) { +Result ModelProcess::LoadModel(const char *modelPath) +{ if (g_loadFlag_) { ERROR_LOG("model has already been loaded"); return FAILED; @@ -73,7 +78,8 @@ Result ModelProcess::LoadModel(const char *modelPath) { return SUCCESS; } -Result ModelProcess::CreateDesc() { +Result ModelProcess::CreateDesc() +{ g_modelDesc_ = aclmdlCreateDesc(); if (g_modelDesc_ == nullptr) { ERROR_LOG("create model description failed"); @@ -91,7 +97,8 @@ Result ModelProcess::CreateDesc() { return SUCCESS; } -void ModelProcess::DestroyDesc() { +void ModelProcess::DestroyDesc() +{ if (g_modelDesc_ != nullptr) { (void)aclmdlDestroyDesc(g_modelDesc_); g_modelDesc_ = nullptr; @@ -99,7 +106,8 @@ void ModelProcess::DestroyDesc() { INFO_LOG("destroy model description success"); } -Result ModelProcess::CreateInput(void *input1, size_t input1Size, void *input2, size_t input2Size) { +Result ModelProcess::CreateInput(void *input1, size_t input1Size, void *input2, size_t input2Size) +{ vector inputData = {{input1, input1Size}, {input2, input2Size}}; uint32_t dataNum = aclmdlGetNumInputs(g_modelDesc_); @@ -124,8 +132,8 @@ Result ModelProcess::CreateInput(void *input1, size_t input1Size, void *input2, size_t modelInputSize = aclmdlGetInputSizeByIndex(g_modelDesc_, i); if (modelInputSize != inputData[i].size) { ERROR_LOG("Input size verify failed " - "input[%d] size: %ld, provide size : %d", - i, modelInputSize, inputData[i].size); + "input[%d] size: %ld, provide size : %d", + i, modelInputSize, inputData[i].size); } aclDataBuffer *dataBuf = aclCreateDataBuffer(inputData[i].data, inputData[i].size); @@ -146,7 +154,8 @@ Result ModelProcess::CreateInput(void *input1, size_t input1Size, void *input2, return SUCCESS; } -void ModelProcess::DestroyInput() { +void ModelProcess::DestroyInput() +{ if (g_input_ == nullptr) { return; } @@ -160,7 +169,8 @@ void ModelProcess::DestroyInput() { INFO_LOG("destroy model input success"); } -Result ModelProcess::CreateOutput() { +Result ModelProcess::CreateOutput() +{ if (g_modelDesc_ == nullptr) { ERROR_LOG("no model description, create ouput failed"); return FAILED; @@ -204,7 +214,8 @@ Result ModelProcess::CreateOutput() { return SUCCESS; } -void ModelProcess::DestroyOutput() { +void ModelProcess::DestroyOutput() +{ if (g_output_ == nullptr) { return; } @@ -221,7 +232,8 @@ void ModelProcess::DestroyOutput() { INFO_LOG("destroy model output success"); } -Result ModelProcess::Execute() { +Result ModelProcess::Execute() +{ aclError ret = aclmdlExecute(g_modelId_, g_input_, g_output_); if (ret != ACL_SUCCESS) { ERROR_LOG("execute model failed, modelId is %u, errorCode is %d", g_modelId_, static_cast(ret)); @@ -232,7 +244,8 @@ Result ModelProcess::Execute() { return SUCCESS; } -void ModelProcess::UnloadModel() { +void ModelProcess::UnloadModel() +{ if (!g_loadFlag_) { WARN_LOG("no model had been loaded, unload failed"); return; @@ -264,11 +277,13 @@ void ModelProcess::UnloadModel() { INFO_LOG("unload model success, modelId is %u", g_modelId_); } -const aclmdlDataset *ModelProcess::GetModelOutputData() { +const aclmdlDataset *ModelProcess::GetModelOutputData() +{ return g_output_; } -Result ModelProcess::GetModelInputWH(int &width, int &height) { +Result ModelProcess::GetModelInputWH(int &width, int &height) +{ if (g_modelDesc_ == nullptr) { ERROR_LOG("no model description, get input hw failed"); return FAILED; @@ -292,4 +307,4 @@ Result ModelProcess::GetModelInputWH(int &width, int &height) { INFO_LOG("model input width %d, input height %d", width, height); return SUCCESS; -} +} \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/sample_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/sample_process.cpp index 62abfc1ad..3c5419dcf 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/sample_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/sample_process.cpp @@ -14,67 +14,72 @@ * limitations under the License. */ -#include "sample_process.h" #include -#include "dvpp_process.h" -#include "model_process.h" -#include "acl/acl.h" -#include "utils.h" #include #include #include #include +#include "sample_process.h" +#include "dvpp_process.h" +#include "model_process.h" +#include "acl/acl.h" +#include "utils.h" + using namespace std; namespace { const static std::vector yolov3Label = { "person", "bicycle", "car", "motorbike", - "aeroplane","bus", "train", "truck", "boat", - "traffic light", "fire hydrant", "stop sign", "parking meter", - "bench", "bird", "cat", "dog", "horse", - "sheep", "cow", "elephant", "bear", "zebra", - "giraffe", "backpack", "umbrella", "handbag","tie", - "suitcase", "frisbee", "skis", "snowboard", "sports ball", - "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", - "tennis racket", "bottle", "wine glass", "cup", - "fork", "knife", "spoon", "bowl", "banana", - "apple", "sandwich", "orange", "broccoli", "carrot", - "hot dog", "pizza", "donut", "cake", "chair", - "sofa", "potted plant", "bed", "dining table", "toilet", - "TV monitor", "laptop", "mouse", "remote", "keyboard", - "cell phone", "microwave", "oven", "toaster", "sink", - "refrigerator", "book", "clock", "vase","scissors", - "teddy bear", "hair drier", "toothbrush" }; - - const uint32_t g_BBoxDataBufId = 0; - const uint32_t g_BoxNumDataBufId = 1; + "aeroplane", "bus", "train", "truck", "boat", + "traffic light", "fire hydrant", "stop sign", "parking meter", + "bench", "bird", "cat", "dog", "horse", + "sheep", "cow", "elephant", "bear", "zebra", + "giraffe", "backpack", "umbrella", "handbag", "tie", + "suitcase", "frisbee", "skis", "snowboard", "sports ball", + "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", + "tennis racket", "bottle", "wine glass", "cup", + "fork", "knife", "spoon", "bowl", "banana", + "apple", "sandwich", "orange", "broccoli", "carrot", + "hot dog", "pizza", "donut", "cake", "chair", + "sofa", "potted plant", "bed", "dining table", "toilet", + "TV monitor", "laptop", "mouse", "remote", "keyboard", + "cell phone", "microwave", "oven", "toaster", "sink", + "refrigerator", "book", "clock", "vase", "scissors", + "teddy bear", "hair drier", "toothbrush" }; + + const uint32_t g_bBoxDataBufId = 0; + const uint32_t g_boxNumDataBufId = 1; enum BBoxIndex { TOPLEFTX = 0, TOPLEFTY, BOTTOMRIGHTX, BOTTOMRIGHTY, SCORE, LABEL }; // bounding box line solid - const uint32_t g_LineSolid = 2; + const uint32_t g_lineSolid = 2; // output image prefix - const string g_OutputFilePrefix = "out_"; + const string g_outputFilePrefix = "out_"; // opencv draw label params. - const double g_FountScale = 0.5; - const cv::Scalar g_FontColor(0, 0, 255); - const uint32_t g_LabelOffset = 11; - const string g_FileSperator = "/"; + const double g_fountScale = 0.5; + const cv::Scalar g_fontColor(0, 0, 255); + const uint32_t g_labelOffset = 11; + const string g_fileSperator = "/"; // opencv color list for boundingbox - const vector g_Colors { + const vector g_colors { cv::Scalar(237, 149, 100), cv::Scalar(0, 215, 255), cv::Scalar(50, 205, 50), cv::Scalar(139, 85, 26) }; } -SampleProcess::SampleProcess():g_deviceId_(0), g_context_(nullptr), g_stream_(nullptr) { +SampleProcess::SampleProcess() + : g_deviceId_(0), g_context_(nullptr), g_stream_(nullptr) +{ } -SampleProcess::~SampleProcess() { +SampleProcess::~SampleProcess() +{ DestroyResource(); } -Result SampleProcess::InitResource() { +Result SampleProcess::InitResource() +{ // ACL init const char *aclConfigPath = "../src/acl.json"; aclError ret = aclInit(aclConfigPath); @@ -124,13 +129,14 @@ Result SampleProcess::InitResource() { return SUCCESS; } -Result SampleProcess::Postprocess(const aclmdlDataset* modelOutput, PicDesc &picDesc, int modelWidth, int modelHeight) { +Result SampleProcess::Postprocess(const aclmdlDataset* modelOutput, PicDesc &picDesc, int modelWidth, int modelHeight) +{ uint32_t dataSize = 0; float* detectData = (float *)GetInferenceOutputItem(dataSize, modelOutput, - g_BBoxDataBufId); + g_bBoxDataBufId); uint32_t* boxNum = (uint32_t *)GetInferenceOutputItem(dataSize, modelOutput, - g_BoxNumDataBufId); + g_boxNumDataBufId); if (boxNum == nullptr) return FAILED; uint32_t totalBox = boxNum[0]; @@ -149,7 +155,7 @@ Result SampleProcess::Postprocess(const aclmdlDataset* modelOutput, PicDesc &pic uint32_t objIndex = (uint32_t)detectData[totalBox * LABEL + i]; boundBox.text = yolov3Label[objIndex] + std::to_string(score) + "\%"; printf("%d %d %d %d %s\n", boundBox.rect.ltX, boundBox.rect.ltY, - boundBox.rect.rbX, boundBox.rect.rbY, boundBox.text.c_str()); + boundBox.rect.rbX, boundBox.rect.rbY, boundBox.text.c_str()); detectResults.emplace_back(boundBox); } @@ -164,25 +170,26 @@ Result SampleProcess::Postprocess(const aclmdlDataset* modelOutput, PicDesc &pic } void* SampleProcess::GetInferenceOutputItem(uint32_t& itemDataSize, -const aclmdlDataset* inferenceOutput, uint32_t idx) { + const aclmdlDataset* inferenceOutput, uint32_t idx) +{ aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(inferenceOutput, idx); if (dataBuffer == nullptr) { ERROR_LOG("Get the %dth dataset buffer from model " - "inference output failed", idx); + "inference output failed", idx); return nullptr; } void* dataBufferDev = aclGetDataBufferAddr(dataBuffer); if (dataBufferDev == nullptr) { ERROR_LOG("Get the %dth dataset buffer address " - "from model inference output failed", idx); + "from model inference output failed", idx); return nullptr; } size_t bufferSize = aclGetDataBufferSizeV2(dataBuffer); if (bufferSize == 0) { ERROR_LOG("The %dth dataset buffer size of " - "model inference output is 0", idx); + "model inference output is 0", idx); return nullptr; } @@ -202,7 +209,8 @@ const aclmdlDataset* inferenceOutput, uint32_t idx) { } void SampleProcess::DrawBoundBoxToImage(vector& detectionResults, -const string& origImagePath) { + const string& origImagePath) +{ cv::Mat image = cv::imread(origImagePath, CV_LOAD_IMAGE_UNCHANGED); for (int i = 0; i < detectionResults.size(); ++i) { cv::Point p1, p2; @@ -210,9 +218,9 @@ const string& origImagePath) { p1.y = detectionResults[i].rect.ltY; p2.x = detectionResults[i].rect.rbX; p2.y = detectionResults[i].rect.rbY; - cv::rectangle(image, p1, p2, g_Colors[i % g_Colors.size()], g_LineSolid); - cv::putText(image, detectionResults[i].text, cv::Point(p1.x, p1.y + g_LabelOffset), - cv::FONT_HERSHEY_COMPLEX, g_FountScale, g_FontColor); + cv::rectangle(image, p1, p2, g_colors[i % g_colors.size()], g_lineSolid); + cv::putText(image, detectionResults[i].text, cv::Point(p1.x, p1.y + g_labelOffset), + cv::FONT_HERSHEY_COMPLEX, g_fountScale, g_fontColor); } string folderPath = "./output"; @@ -227,7 +235,8 @@ const string& origImagePath) { cv::imwrite(sstream.str(), image); } -Result SampleProcess::Process() { +Result SampleProcess::Process() +{ // dvpp init DvppProcess dvppProcess(g_stream_); Result ret = dvppProcess.InitResource(); @@ -263,7 +272,7 @@ Result SampleProcess::Process() { } const float imageInfo[4] = {(float)modelInputWidth, (float)modelInputHeight, - (float)modelInputWidth, (float)modelInputHeight}; + (float)modelInputWidth, (float)modelInputHeight}; size_t imageInfoSize_ = sizeof(imageInfo); void *imageInfoBuf_; if (g_runMode_ == ACL_HOST) @@ -277,7 +286,6 @@ Result SampleProcess::Process() { // input image PicDesc testPic[] = { {"../data/dog1_1024_683.jpg", 0, 0}, - // {"../data/dog2_1024_683.jpg", 0, 0}, }; for (size_t index = 0; index < sizeof(testPic) / sizeof(testPic[0]); ++index) { @@ -317,7 +325,7 @@ Result SampleProcess::Process() { dvppProcess.GetDvppOutput(&dvppOutputBuffer, dvppOutputSize); // 2.model process - ret = modelProcess.CreateInput(dvppOutputBuffer, dvppOutputSize, + ret = modelProcess.CreateInput(dvppOutputBuffer, dvppOutputSize, imageInfoBuf_, imageInfoSize_); if (ret != SUCCESS) { ERROR_LOG("execute CreateInput failed"); @@ -349,7 +357,8 @@ Result SampleProcess::Process() { return SUCCESS; } -void SampleProcess::DestroyResource() { +void SampleProcess::DestroyResource() +{ aclError ret; if (g_stream_ != nullptr) { ret = aclrtDestroyStream(g_stream_); @@ -380,4 +389,4 @@ void SampleProcess::DestroyResource() { ERROR_LOG("finalize acl failed, errorCode = %d", static_cast(ret)); } INFO_LOG("end to finalize acl"); -} +} \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/utils.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/utils.cpp index fe569b8c1..d777d32c1 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/utils.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/src/utils.cpp @@ -14,10 +14,10 @@ * limitations under the License. */ -#include "utils.h" #include #include #include +#include "utils.h" #if defined(_MSC_VER) #include @@ -29,7 +29,8 @@ bool RunStatus::isDevice_ = false; -Result Utils::ReadBinFile(const std::string &fileName, void *&inputBuff, uint32_t &fileSize) { +Result Utils::ReadBinFile(const std::string &fileName, void *&inputBuff, uint32_t &fileSize) +{ if (CheckPathIsFile(fileName) == FAILED) { ERROR_LOG("%s is not a file", fileName.c_str()); return FAILED; @@ -75,12 +76,12 @@ Result Utils::ReadBinFile(const std::string &fileName, void *&inputBuff, uint32_ return SUCCESS; } -Result Utils::GetDeviceBufferOfPicture(PicDesc &picDesc, void *&picDevBuffer, uint32_t &devPicBufferSize) { +Result Utils::GetDeviceBufferOfPicture(PicDesc &picDesc, void *&picDevBuffer, uint32_t &devPicBufferSize) +{ if (picDesc.picName.empty()) { ERROR_LOG("picture file name is empty"); return FAILED; } - uint32_t inputBuffSize = 0; void *inputBuff = nullptr; Result ret = ReadBinFile(picDesc.picName, inputBuff, inputBuffSize); @@ -94,9 +95,7 @@ Result Utils::GetDeviceBufferOfPicture(PicDesc &picDesc, void *&picDevBuffer, ui ERROR_LOG("get jpeg image info failed, errorCode is %d", static_cast(aclRet)); if (!(RunStatus::GetDeviceStatus())) { (void)aclrtFreeHost(inputBuff); - } else { - (void)acldvppFree(inputBuff); - } + } else (void)acldvppFree(inputBuff); return FAILED; } aclRet = acldvppJpegPredictDecSize(inputBuff, inputBuffSize, PIXEL_FORMAT_YUV_SEMIPLANAR_420, @@ -105,12 +104,9 @@ Result Utils::GetDeviceBufferOfPicture(PicDesc &picDesc, void *&picDevBuffer, ui ERROR_LOG("get jpeg decode size failed, errorCode is %d", static_cast(aclRet)); if (!(RunStatus::GetDeviceStatus())) { (void)aclrtFreeHost(inputBuff); - } else { - (void)acldvppFree(inputBuff); - } + } else (void)acldvppFree(inputBuff); return FAILED; } - if (!(RunStatus::GetDeviceStatus())) { // app is running in host aclRet = acldvppMalloc(&picDevBuffer, inputBuffSize); if (aclRet != ACL_SUCCESS) { @@ -119,7 +115,6 @@ Result Utils::GetDeviceBufferOfPicture(PicDesc &picDesc, void *&picDevBuffer, ui (void)aclrtFreeHost(inputBuff); return FAILED; } - // if app is running in host, need copy data from host to device aclRet = aclrtMemcpy(picDevBuffer, inputBuffSize, inputBuff, inputBuffSize, ACL_MEMCPY_HOST_TO_DEVICE); if (aclRet != ACL_SUCCESS) { @@ -130,15 +125,13 @@ Result Utils::GetDeviceBufferOfPicture(PicDesc &picDesc, void *&picDevBuffer, ui return FAILED; } (void)aclrtFreeHost(inputBuff); - } else { // app is running in device - picDevBuffer = inputBuff; - } + } else picDevBuffer = inputBuff; // app is running in device devPicBufferSize = inputBuffSize; - return SUCCESS; } -Result Utils::CheckPathIsFile(const std::string &fileName) { +Result Utils::CheckPathIsFile(const std::string &fileName) +{ #if defined(_MSC_VER) DWORD bRet = GetFileAttributes((LPCSTR)fileName.c_str()); if (bRet == FILE_ATTRIBUTE_DIRECTORY) { @@ -160,7 +153,8 @@ Result Utils::CheckPathIsFile(const std::string &fileName) { return SUCCESS; } -void* Utils::CopyDataToDevice(void* data, uint32_t dataSize, aclrtMemcpyKind policy) { +void* Utils::CopyDataToDevice(void* data, uint32_t dataSize, aclrtMemcpyKind policy) +{ void* buffer = nullptr; aclError aclRet = aclrtMalloc(&buffer, dataSize, ACL_MEM_MALLOC_HUGE_FIRST); if (aclRet != ACL_SUCCESS) { @@ -178,15 +172,18 @@ void* Utils::CopyDataToDevice(void* data, uint32_t dataSize, aclrtMemcpyKind pol return buffer; } -void* Utils::CopyDataDeviceToDevice(void* deviceData, uint32_t dataSize) { +void* Utils::CopyDataDeviceToDevice(void* deviceData, uint32_t dataSize) +{ return CopyDataToDevice(deviceData, dataSize, ACL_MEMCPY_DEVICE_TO_DEVICE); } -void* Utils::CopyDataHostToDevice(void* deviceData, uint32_t dataSize) { +void* Utils::CopyDataHostToDevice(void* deviceData, uint32_t dataSize) +{ return CopyDataToDevice(deviceData, dataSize, ACL_MEMCPY_HOST_TO_DEVICE); } -void* Utils::CopyDataDeviceToLocal(void* deviceData, uint32_t dataSize) { +void* Utils::CopyDataDeviceToLocal(void* deviceData, uint32_t dataSize) +{ uint8_t* buffer = new uint8_t[dataSize]; if (buffer == nullptr) { ERROR_LOG("New malloc memory failed"); diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/drawing.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/drawing.h index 4480903a2..712ed6991 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/drawing.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/drawing.h @@ -26,6 +26,6 @@ void DrawText(ImageData& image, int x, int y, const std::string &text, const YUVColor &color); void DrawRect(ImageData& image, int x1, int y1, int x2, int y2, const YUVColor &color, - int lineWidth); + int lineWidth); #endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/dvpp_jpegd.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/dvpp_jpegd.h old mode 100755 new mode 100644 diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/dvpp_process.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/dvpp_process.h old mode 100755 new mode 100644 diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/model_process.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/model_process.h old mode 100755 new mode 100644 index b3f918ae4..7355f13ce --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/model_process.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/model_process.h @@ -26,7 +26,7 @@ * ModelProcess */ class ModelProcess { - public: +public: /** * @brief Constructor */ @@ -102,7 +102,7 @@ class ModelProcess { */ aclmdlDataset *GetModelOutputData(); - private: +private: bool g_loadFlag_; // model load flag uint32_t g_modelId_; void *g_modelMemPtr_; diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/object_detect.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/object_detect.h old mode 100755 new mode 100644 index 0c8509d6a..6ce79bc83 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/object_detect.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/object_detect.h @@ -18,11 +18,11 @@ #define YOLOV3_COCO_DETECTION_PICTURE_WITH_FREETYPE_INC_OBJECT_DETECT_H #pragma once +#include #include "utils.h" #include "acl/acl.h" #include "model_process.h" #include "dvpp_process.h" -#include using namespace std; @@ -31,7 +31,7 @@ using namespace std; */ class ObjectDetect { public: - ObjectDetect(const char* modelPath, + ObjectDetect(const char* modelPath, uint32_t modelWidth, uint32_t modelHeight); ~ObjectDetect(); @@ -47,8 +47,8 @@ private: void* GetInferenceOutputItem(uint32_t& itemDataSize, aclmdlDataset* inferenceOutput, uint32_t idx); - void DrawBoundBoxToImage(ImageData& image, - vector& detectionResults, const string& origImagePath); + void DrawBoundBoxToImage(ImageData& image, + vector& detectionResults, const string& origImagePath); void DestroyResource(); private: diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/utils.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/utils.h old mode 100755 new mode 100644 index 4e4020806..0adcd16b1 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/utils.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/utils.h @@ -42,7 +42,8 @@ using namespace std; #define SHARED_PTR_U8_BUF(buf) (shared_ptr((uint8_t *)(buf), [](uint8_t* p) { delete[](p); })) template -std::shared_ptr MakeSharedNoThrow() { +std::shared_ptr MakeSharedNoThrow() +{ try { return std::make_shared(); } @@ -53,8 +54,8 @@ std::shared_ptr MakeSharedNoThrow() { #define MAKE_SHARED_NO_THROW(memory, memory_type) \ do { \ - memory = MakeSharedNoThrow(); \ - }while(0); + memory = MakeSharedNoThrow<(memory_type)>(); \ + }while(0) typedef enum Result { SUCCESS = 0, diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/drawing.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/drawing.cpp index c9e87a5ec..5025d4ff4 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/drawing.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/drawing.cpp @@ -19,15 +19,17 @@ #include #include #include -#include "drawing.h" #include +#include "drawing.h" -void DrawText(ImageData& image, int x, int y, const std::string &text, const YUVColor &color) { +void DrawText(ImageData& image, int x, int y, const std::string &text, const YUVColor &color) +{ RenderText(image, x, y, text, &color); } void DrawRect(ImageData& image, int x1, int y1, int x2, int y2, const YUVColor &color, - int lineWidth) { + int lineWidth) +{ if (x1 > x2) { std::swap(x1, x2); } @@ -37,50 +39,50 @@ void DrawRect(ImageData& image, int x1, int y1, int x2, int y2, const YUVColor & } int i, j; - int i_bound, j_bound; - int i_start, j_start; + int iBound, jBound; + int iStart, jStart; int width = (int)image.width; int height = (int)image.height; - j_bound = std::min(height, y1 + lineWidth); - i_bound = std::min(width - 1, x2); + jBound = std::min(height, y1 + lineWidth); + iBound = std::min(width - 1, x2); - for (j = y1; j < j_bound; ++j) { - for (i = x1; i <= i_bound; ++i) { + for (j = y1; j < jBound; ++j) { + for (i = x1; i <= iBound; ++i) { SetPixel(image, i, j, color); } } - j_start = std::max(0, y2 - lineWidth + 1); - j_bound = std::min(height - 1, y2); + jStart = std::max(0, y2 - lineWidth + 1); + jBound = std::min(height - 1, y2); - i_start = std::max(0, x1); - i_bound = std::min(width - 1, x2); + iStart = std::max(0, x1); + iBound = std::min(width - 1, x2); - for (j = j_start; j <= j_bound; ++j) { - for (i = i_start; i <= i_bound; ++i) { + for (j = jStart; j <= jBound; ++j) { + for (i = iStart; i <= iBound; ++i) { SetPixel(image, i, j, color); } } - i_bound = std::min(width, x1 + lineWidth); - j_bound = std::min(height - 1, y2); + iBound = std::min(width, x1 + lineWidth); + jBound = std::min(height - 1, y2); - for (i = x1; i < i_bound; ++i) { - for (j = y1; j <= j_bound; ++j) { + for (i = x1; i < iBound; ++i) { + for (j = y1; j <= jBound; ++j) { SetPixel(image, i, j, color); } } - i_start = std::max(0, (x2 - lineWidth + 1)); - i_bound = std::min(width - 1, x2); + iStart = std::max(0, (x2 - lineWidth + 1)); + iBound = std::min(width - 1, x2); - j_start = std::max(0, y1); - j_bound = std::min(height - 1, y2); + jStart = std::max(0, y1); + jBound = std::min(height - 1, y2); - for (i = i_start; i <= i_bound; ++i) { - for (j = j_start; j <= j_bound; ++j) { + for (i = iStart; i <= iBound; ++i) { + for (j = jStart; j <= jBound; ++j) { SetPixel(image, i, j, color); } } - } +} \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/dvpp_cropandpaste.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/dvpp_cropandpaste.cpp index a226ce007..f1c31a993 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/dvpp_cropandpaste.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/dvpp_cropandpaste.cpp @@ -21,26 +21,29 @@ using namespace std; DvppCropAndPaste::DvppCropAndPaste(aclrtStream& stream, acldvppChannelDesc *dvppChannelDesc, - uint32_t width, uint32_t height) -: g_stream_(stream), g_dvppChannelDesc_(dvppChannelDesc), -g_vpcInputDesc_(nullptr), g_vpcOutputDesc_(nullptr), -g_vpcOutBufferDev_(nullptr),g_vpcOutBufferSize_(0) { + uint32_t width, uint32_t height) + : g_stream_(stream), g_dvppChannelDesc_(dvppChannelDesc), + g_vpcInputDesc_(nullptr), g_vpcOutputDesc_(nullptr), + g_vpcOutBufferDev_(nullptr), g_vpcOutBufferSize_(0) +{ g_size_.width = width; g_size_.height = height; } -DvppCropAndPaste::~DvppCropAndPaste() { +DvppCropAndPaste::~DvppCropAndPaste() +{ DestroyCropAndPasteResource(); } -Result DvppCropAndPaste::InitCropAndPasteInputDesc(ImageData& inputImage) { +Result DvppCropAndPaste::InitCropAndPasteInputDesc(ImageData& inputImage) +{ g_originalImageWidth_ = inputImage.width; g_originalImageHeight_ = inputImage.height; uint32_t alignWidth = ALIGN_UP128(inputImage.width); uint32_t alignHeight = ALIGN_UP16(inputImage.height); if (alignWidth == 0 || alignHeight == 0) { ERROR_LOG("InitResizeInputDesc AlignmentHelper failed. image w %d, h %d, align w%d, h%d", - inputImage.width, inputImage.height, alignWidth, alignHeight); + inputImage.width, inputImage.height, alignWidth, alignHeight); return FAILED; } uint32_t inputBufferSize = YUV420SP_SIZE(alignWidth, alignHeight); @@ -60,7 +63,8 @@ Result DvppCropAndPaste::InitCropAndPasteInputDesc(ImageData& inputImage) { return SUCCESS; } -Result DvppCropAndPaste::InitCropAndPasteOutputDesc() { +Result DvppCropAndPaste::InitCropAndPasteOutputDesc() +{ int resizeOutWidth = g_size_.width; int resizeOutHeight = g_size_.height; int resizeOutWidthStride = ALIGN_UP16(resizeOutWidth); @@ -94,22 +98,24 @@ Result DvppCropAndPaste::InitCropAndPasteOutputDesc() { } // IN/OUT Desc -Result DvppCropAndPaste::InitCropAndPasteResource(ImageData& inputImage) { +Result DvppCropAndPaste::InitCropAndPasteResource(ImageData& inputImage) +{ g_format_ = static_cast(PIXEL_FORMAT_YUV_SEMIPLANAR_420); if (SUCCESS != InitCropAndPasteInputDesc(inputImage)) { ERROR_LOG("InitCropAndPasteInputDesc failed"); return FAILED; - } + } if (SUCCESS != InitCropAndPasteOutputDesc()) { ERROR_LOG("InitCropAndPasteOutputDesc failed"); return FAILED; - } + } - return SUCCESS; + return SUCCESS; } -Result DvppCropAndPaste::Process(ImageData& resizedImage, ImageData& srcImage) { +Result DvppCropAndPaste::Process(ImageData& resizedImage, ImageData& srcImage) +{ if (SUCCESS != InitCropAndPasteResource(srcImage)) { ERROR_LOG("Dvpp cropandpaste failed for init error"); return FAILED; @@ -172,7 +178,8 @@ Result DvppCropAndPaste::Process(ImageData& resizedImage, ImageData& srcImage) { return SUCCESS; } -void DvppCropAndPaste::DestroyCropAndPasteResource() { +void DvppCropAndPaste::DestroyCropAndPasteResource() +{ if (g_cropArea_ != nullptr) { (void)acldvppDestroyRoiConfig(g_cropArea_); g_cropArea_ = nullptr; diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/dvpp_jpegd.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/dvpp_jpegd.cpp old mode 100755 new mode 100644 index 942b8d8ed..85126733b --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/dvpp_jpegd.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/dvpp_jpegd.cpp @@ -22,14 +22,17 @@ using namespace std; DvppJpegD::DvppJpegD(aclrtStream& stream, acldvppChannelDesc *dvppChannelDesc) : g_stream_(stream), g_dvppChannelDesc_(dvppChannelDesc), - g_decodeOutBufferDev_(nullptr), g_decodeOutputDesc_(nullptr) { + g_decodeOutBufferDev_(nullptr), g_decodeOutputDesc_(nullptr) +{ } -DvppJpegD::~DvppJpegD() { +DvppJpegD::~DvppJpegD() +{ DestroyDecodeResource(); } -Result DvppJpegD::InitDecodeOutputDesc(ImageData& inputImage) { +Result DvppJpegD::InitDecodeOutputDesc(ImageData& inputImage) +{ uint32_t decodeOutWidthStride = ALIGN_UP128(inputImage.width); uint32_t decodeOutHeightStride = ALIGN_UP16(inputImage.height); if (decodeOutWidthStride == 0 || decodeOutHeightStride == 0) { @@ -37,8 +40,8 @@ Result DvppJpegD::InitDecodeOutputDesc(ImageData& inputImage) { return FAILED; } - /*Allocate a large enough memory*/ - uint32_t decodeOutBufferSize = + /* Allocate a large enough memory */ + uint32_t decodeOutBufferSize = YUV420SP_SIZE(decodeOutWidthStride, decodeOutHeightStride) * 4; aclError aclRet = acldvppMalloc(&g_decodeOutBufferDev_, decodeOutBufferSize); @@ -63,7 +66,8 @@ Result DvppJpegD::InitDecodeOutputDesc(ImageData& inputImage) { return SUCCESS; } -Result DvppJpegD::Process(ImageData& dest, ImageData& src) { +Result DvppJpegD::Process(ImageData& dest, ImageData& src) +{ int ret = InitDecodeOutputDesc(src); if (ret != SUCCESS) { ERROR_LOG("InitDecodeOutputDesc failed"); @@ -96,7 +100,8 @@ Result DvppJpegD::Process(ImageData& dest, ImageData& src) { return SUCCESS; } -void DvppJpegD::DestroyDecodeResource() { +void DvppJpegD::DestroyDecodeResource() +{ if (g_decodeOutputDesc_ != nullptr) { acldvppDestroyPicDesc(g_decodeOutputDesc_); g_decodeOutputDesc_ = nullptr; diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/dvpp_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/dvpp_process.cpp old mode 100755 new mode 100644 index bba407d8e..161b3bf52 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/dvpp_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/dvpp_process.cpp @@ -22,14 +22,17 @@ using namespace std; -DvppProcess::DvppProcess():g_dvppChannelDesc_(nullptr) { +DvppProcess::DvppProcess() : g_dvppChannelDesc_(nullptr) +{ } -DvppProcess::~DvppProcess() { +DvppProcess::~DvppProcess() +{ DestroyResource(); } -void DvppProcess::DestroyResource() { +void DvppProcess::DestroyResource() +{ aclError aclRet; if (g_dvppChannelDesc_ != nullptr) { aclRet = acldvppDestroyChannel(g_dvppChannelDesc_); @@ -42,7 +45,8 @@ void DvppProcess::DestroyResource() { } } -Result DvppProcess::InitResource(aclrtStream& stream) { +Result DvppProcess::InitResource(aclrtStream& stream) +{ aclError aclRet; g_dvppChannelDesc_ = acldvppCreateChannelDesc(); @@ -62,13 +66,14 @@ Result DvppProcess::InitResource(aclrtStream& stream) { } Result DvppProcess::CropAndPaste(ImageData& dest, ImageData& src, - uint32_t width, uint32_t height) { + uint32_t width, uint32_t height) +{ DvppCropAndPaste cropandpasteOp(g_stream_, g_dvppChannelDesc_, width, height); return cropandpasteOp.Process(dest, src); } -Result DvppProcess::CvtJpegToYuv420sp(ImageData& dest, ImageData& src) { +Result DvppProcess::CvtJpegToYuv420sp(ImageData& dest, ImageData& src) +{ DvppJpegD jpegD(g_stream_, g_dvppChannelDesc_); return jpegD.Process(dest, src); -} - +} \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/freetype_helper.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/freetype_helper.cpp index 99b24ed0c..c53fe679c 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/freetype_helper.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/freetype_helper.cpp @@ -25,10 +25,10 @@ #include "utils.h" extern "C" { - #include #include FT_FREETYPE_H -const char *FreeTypeErrorMessage(FT_Error err) { +const char *FreeTypeErrorMessage(FT_Error err) +{ #undef FTERRORS_H_ #define FT_ERRORDEF(e, v, s) \ case e: \ @@ -43,27 +43,29 @@ const char *FreeTypeErrorMessage(FT_Error err) { #define CHECK_FREETYPE(x) { \ auto error = x; \ if (error) { \ - auto err_msg = FreeTypeErrorMessage(error); \ - std::cerr << "FreeType Error: " << err_msg << std::endl; \ - throw std::runtime_error(err_msg); \ + auto err_msg = FreeTypeErrorMessage(error); \ + std::cerr << "FreeType Error: " << err_msg << std::endl; \ + throw std::runtime_error(err_msg); \ } \ } class GlpyhContext { public: -static GlpyhContext &GetInstance() { +static GlpyhContext &GetInstance() +{ static thread_local GlpyhContext ctx; return ctx; } int DrawChar(char32_t ch, int x, int y, const YUVColor *color, - ImageData& image) { + ImageData& image) +{ FT_UInt glyph_idx = 0; FT_Face face; - int font_idx = 0; - int font_list_size = faces.size(); - for (font_idx = 0; font_idx < font_list_size; ++font_idx) { - face = faces[font_idx]; + int fontIdx = 0; + int fontListSize = faces.size(); + for (fontIdx = 0; fontIdx < fontListSize; ++fontIdx) { + face = faces[fontIdx]; glyph_idx = FT_Get_Char_Index(face, ch); CHECK_FREETYPE(FT_Load_Glyph(face, glyph_idx, FT_LOAD_RENDER)); // if glyph_idx is 0 that means symbol not found, @@ -80,14 +82,14 @@ int DrawChar(char32_t ch, int x, int y, const YUVColor *color, int h = image.width; int w = image.height; - int image_y = y - slot->bitmap_top + font_size; + int imageY = y - slot->bitmap_top + fontSize; - for (int i = 0; i < slot->bitmap.rows && image_y < h; ++i, ++image_y) { - int image_x = x + slot->bitmap_left; - for (int j = 0; j < slot->bitmap.width && image_x < w; ++j, ++image_x) { + for (int i = 0; i < slot->bitmap.rows && imageY < h; ++i, ++imageY) { + int imageX = x + slot->bitmap_left; + for (int j = 0; j < slot->bitmap.width && imageX < w; ++j, ++imageX) { auto bitmap_val = slot->bitmap.buffer[i * slot->bitmap.width + j]; if (bitmap_val > 0) { - SetPixel(image, image_x, image_y, *color); + SetPixel(image, imageX, imageY, *color); } } } @@ -95,30 +97,32 @@ int DrawChar(char32_t ch, int x, int y, const YUVColor *color, } private: -GlpyhContext() { - std::vector font_list { +GlpyhContext() +{ + std::vector fontList { "/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf", // For CJK "/usr/share/fonts/truetype/noto/NotoMono-Regular.ttf" // For latin and // digits }; - for (const char *font_path : font_list) { + for (const char *fontPath : fontList) { FT_Face face; CHECK_FREETYPE(FT_Init_FreeType(&library)); - CHECK_FREETYPE(FT_New_Face(library, font_path, 0, &face)); + CHECK_FREETYPE(FT_New_Face(library, fontPath, 0, &face)); - CHECK_FREETYPE(FT_Set_Pixel_Sizes(face, font_size, font_size)); + CHECK_FREETYPE(FT_Set_Pixel_Sizes(face, fontSize, fontSize)); faces.push_back(face); } } - int font_size{20}; + int fontSize{20}; FT_Library library; std::vector faces; std::mutex mtx; }; -void SetPixel(ImageData& image, int x, int y, const YUVColor &color) { +void SetPixel(ImageData& image, int x, int y, const YUVColor &color) +{ *(image.data.get() + y * image.width + x) = color.y; uint8_t *uv_offset = image.data.get() + image.width * image.height + (y / 2) * image.width + x / 2 * 2; uv_offset[0] = color.u; @@ -126,7 +130,8 @@ void SetPixel(ImageData& image, int x, int y, const YUVColor &color) { } // cache glyphs -void RenderText(ImageData& image, int x, int y, const std::string &text, const YUVColor *color) { +void RenderText(ImageData& image, int x, int y, const std::string &text, const YUVColor *color) +{ auto &ctx = GlpyhContext::GetInstance(); std::wstring_convert, char32_t> cvt; std::u32string utf32_str = cvt.from_bytes(text); diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/main.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/main.cpp index ae6461b8d..539226e0c 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/main.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/main.cpp @@ -22,20 +22,22 @@ using namespace std; namespace { -uint32_t g_ModelWidth = 416; -uint32_t g_ModelHeight = 416; -const char* g_ModelPath = "../model/yolov3.om"; +uint32_t g_modelWidth = 416; +uint32_t g_modelHeight = 416; +const char* g_modelPath = "../model/yolov3.om"; } -int main(int argc, char *argv[]) { +int main(int argc, char *argv[]) +{ // Check input args: the path of input picture, and ignore hidden dotfile directory - if ((argc != 2) || (argv[1] == nullptr)) { + uint32_t argNum = 2; + if ((argc != argNum) || (argv[1] == nullptr)) { ERROR_LOG("Please input: ./main "); return FAILED; } // Instantiate the object detection class - ObjectDetect detect(g_ModelPath, g_ModelWidth, g_ModelHeight); - // Initialize the acl resources, dvpp, load model, + ObjectDetect detect(g_modelPath, g_modelWidth, g_modelHeight); + // Initialize the acl resources, dvpp, load model, // and malloc input memory of input which is const Result ret = detect.Init(); if (ret != SUCCESS) { @@ -67,10 +69,10 @@ int main(int argc, char *argv[]) { Result ret = detect.Preprocess(resizedImage, image, yuvImage); if (ret != SUCCESS) { ERROR_LOG("Read file %s failed, continue to read next", - imageFile.c_str()); + imageFile.c_str()); continue; } - // Send the resized picture to the model for inference + // Send the resized picture to the model for inference // and get the inference results aclmdlDataset* inferenceOutput = nullptr; ret = detect.Inference(inferenceOutput, resizedImage); @@ -78,7 +80,7 @@ int main(int argc, char *argv[]) { ERROR_LOG("Inference model inference output data failed"); return FAILED; } - // Analyze the inference output, mark the object category and + // Analyze the inference output, mark the object category and // location by the inference result ret = detect.Postprocess(yuvImage, inferenceOutput, imageFile); if (ret != SUCCESS) { diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/model_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/model_process.cpp old mode 100755 new mode 100644 index bd5ebf557..8c75a36d8 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/model_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/model_process.cpp @@ -19,17 +19,20 @@ #include "utils.h" using namespace std; -ModelProcess::ModelProcess():g_loadFlag_(false), g_modelId_(0), g_modelMemPtr_(nullptr), g_modelMemSize_(0), -g_modelWeightPtr_(nullptr),g_modelWeightSize_(0), g_modelDesc_(nullptr), g_input_(nullptr), g_output_(nullptr), -g_isReleased_(false) { - +ModelProcess::ModelProcess() + : g_loadFlag_(false), g_modelId_(0), g_modelMemPtr_(nullptr), g_modelMemSize_(0), + g_modelWeightPtr_(nullptr), g_modelWeightSize_(0), g_modelDesc_(nullptr), + g_input_(nullptr), g_output_(nullptr), g_isReleased_(false) +{ } -ModelProcess::~ModelProcess() { +ModelProcess::~ModelProcess() +{ DestroyResource(); } -void ModelProcess::DestroyResource() { +void ModelProcess::DestroyResource() +{ if (g_isReleased_) return; Unload(); @@ -39,7 +42,8 @@ void ModelProcess::DestroyResource() { g_isReleased_ = true; } -Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath) { +Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath) +{ if (g_loadFlag_) { ERROR_LOG("has already loaded a model"); return FAILED; @@ -75,7 +79,8 @@ Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath) { return SUCCESS; } -Result ModelProcess::CreateDesc() { +Result ModelProcess::CreateDesc() +{ g_modelDesc_ = aclmdlCreateDesc(); if (g_modelDesc_ == nullptr) { ERROR_LOG("create model description failed"); @@ -92,7 +97,8 @@ Result ModelProcess::CreateDesc() { return SUCCESS; } -void ModelProcess::DestroyDesc() { +void ModelProcess::DestroyDesc() +{ if (g_modelDesc_ != nullptr) { (void)aclmdlDestroyDesc(g_modelDesc_); g_modelDesc_ = nullptr; @@ -100,7 +106,8 @@ void ModelProcess::DestroyDesc() { } Result ModelProcess::CreateInput(void *input1, size_t input1Size, - void* input2, size_t input2Size) { + void* input2, size_t input2Size) +{ g_input_ = aclmdlCreateDataset(); if (g_input_ == nullptr) { ERROR_LOG("can't create dataset, create input failed"); @@ -138,7 +145,8 @@ Result ModelProcess::CreateInput(void *input1, size_t input1Size, return SUCCESS; } -void ModelProcess::DestroyInput(){ +void ModelProcess::DestroyInput() +{ if (g_input_ == nullptr) { return; } @@ -151,7 +159,8 @@ void ModelProcess::DestroyInput(){ g_input_ = nullptr; } -Result ModelProcess::CreateOutput(){ +Result ModelProcess::CreateOutput() +{ if (g_modelDesc_ == nullptr) { ERROR_LOG("no model description, create ouput failed"); return FAILED; @@ -194,7 +203,8 @@ Result ModelProcess::CreateOutput(){ return SUCCESS; } -void ModelProcess::DestroyOutput(){ +void ModelProcess::DestroyOutput() +{ if (g_output_ == nullptr) { return; } @@ -210,7 +220,8 @@ void ModelProcess::DestroyOutput(){ g_output_ = nullptr; } -Result ModelProcess::Execute(){ +Result ModelProcess::Execute() +{ aclError ret = aclmdlExecute(g_modelId_, g_input_, g_output_); if (ret != ACL_SUCCESS) { ERROR_LOG("execute model failed, modelId is %u", g_modelId_); @@ -254,6 +265,7 @@ void ModelProcess::Unload() INFO_LOG("unload model success, modelId is %u", g_modelId_); } -aclmdlDataset *ModelProcess::GetModelOutputData(){ +aclmdlDataset *ModelProcess::GetModelOutputData() +{ return g_output_; } diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/object_detect.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/object_detect.cpp old mode 100755 new mode 100644 index 1bdf56254..a1b7c1e4b --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/object_detect.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/object_detect.cpp @@ -14,9 +14,8 @@ * limitations under the License. */ -#include "object_detect.h" #include - +#include "object_detect.h" #include "acl/acl.h" #include "model_process.h" #include "utils.h" @@ -26,22 +25,22 @@ using namespace std; namespace { const static std::vector yolov3Label = { "person", "bicycle", "car", "motorbike", - "aeroplane","bus", "train", "truck", "boat", - "traffic light", "fire hydrant", "stop sign", "parking meter", - "bench", "bird", "cat", "dog", "horse", - "sheep", "cow", "elephant", "bear", "zebra", - "giraffe", "backpack", "umbrella", "handbag","tie", - "suitcase", "frisbee", "skis", "snowboard", "sports ball", - "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", - "tennis racket", "bottle", "wine glass", "cup", - "fork", "knife", "spoon", "bowl", "banana", - "apple", "sandwich", "orange", "broccoli", "carrot", - "hot dog", "pizza", "donut", "cake", "chair", - "sofa", "potted plant", "bed", "dining table", "toilet", - "TV monitor", "laptop", "mouse", "remote", "keyboard", - "cell phone", "microwave", "oven", "toaster", "sink", - "refrigerator", "book", "clock", "vase","scissors", - "teddy bear", "hair drier", "toothbrush" }; + "aeroplane", "bus", "train", "truck", "boat", + "traffic light", "fire hydrant", "stop sign", "parking meter", + "bench", "bird", "cat", "dog", "horse", + "sheep", "cow", "elephant", "bear", "zebra", + "giraffe", "backpack", "umbrella", "handbag", "tie", + "suitcase", "frisbee", "skis", "snowboard", "sports ball", + "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", + "tennis racket", "bottle", "wine glass", "cup", + "fork", "knife", "spoon", "bowl", "banana", + "apple", "sandwich", "orange", "broccoli", "carrot", + "hot dog", "pizza", "donut", "cake", "chair", + "sofa", "potted plant", "bed", "dining table", "toilet", + "TV monitor", "laptop", "mouse", "remote", "keyboard", + "cell phone", "microwave", "oven", "toaster", "sink", + "refrigerator", "book", "clock", "vase", "scissors", + "teddy bear", "hair drier", "toothbrush" }; const uint32_t kBBoxDataBufId = 0; const uint32_t kBoxNumDataBufId = 1; @@ -51,18 +50,20 @@ namespace { ObjectDetect::ObjectDetect(const char* modelPath, uint32_t modelWidth, uint32_t modelHeight) -:g_deviceId_(0), g_context_(nullptr), g_stream_(nullptr), g_modelWidth_(modelWidth), -g_modelHeight_(modelHeight), g_isInited_(false), g_isDeviceSet_(false) { - g_imageInfoSize_ = 0; - g_imageInfoBuf_ = nullptr; - g_modelPath_ = modelPath; + : g_deviceId_(0), g_context_(nullptr), g_stream_(nullptr), g_modelWidth_(modelWidth), + g_modelHeight_(modelHeight), g_isInited_(false), g_isDeviceSet_(false) { + g_imageInfoSize_ = 0; + g_imageInfoBuf_ = nullptr; + g_modelPath_ = modelPath; } -ObjectDetect::~ObjectDetect() { +ObjectDetect::~ObjectDetect() +{ DestroyResource(); } -Result ObjectDetect::InitResource() { +Result ObjectDetect::InitResource() +{ // ACL init const char *aclConfigPath = "../src/acl.json"; aclError ret = aclInit(aclConfigPath); @@ -106,7 +107,8 @@ Result ObjectDetect::InitResource() { return SUCCESS; } -Result ObjectDetect::InitModel(const char* omModelPath) { +Result ObjectDetect::InitModel(const char* omModelPath) +{ Result ret = g_model_.LoadModelFromFileWithMem(omModelPath); if (ret != SUCCESS) { ERROR_LOG("execute LoadModelFromFileWithMem failed"); @@ -128,9 +130,10 @@ Result ObjectDetect::InitModel(const char* omModelPath) { return SUCCESS; } -Result ObjectDetect::CreateImageInfoBuffer() { +Result ObjectDetect::CreateImageInfoBuffer() +{ const float imageInfo[4] = {(float)g_modelWidth_, (float)g_modelHeight_, - (float)g_modelWidth_, (float)g_modelHeight_}; + (float)g_modelWidth_, (float)g_modelHeight_}; g_imageInfoSize_ = sizeof(imageInfo); if (g_runMode_ == ACL_HOST) g_imageInfoBuf_ = Utils::CopyDataHostToDevice((void *)imageInfo, g_imageInfoSize_); @@ -144,7 +147,8 @@ Result ObjectDetect::CreateImageInfoBuffer() { return SUCCESS; } -Result ObjectDetect::Init() { +Result ObjectDetect::Init() +{ if (g_isInited_) { INFO_LOG("Object detection instance is initied already!"); return SUCCESS; @@ -179,8 +183,8 @@ Result ObjectDetect::Init() { } Result ObjectDetect::Preprocess(ImageData& resizedImage, ImageData& srcImage, -ImageData& oriImage) { - + ImageData& oriImage) +{ ImageData imageDevice, yuvImage; Utils::CopyImageDataToDevice(imageDevice, srcImage, g_runMode_); @@ -201,7 +205,8 @@ ImageData& oriImage) { } Result ObjectDetect::Inference(aclmdlDataset*& inferenceOutput, -ImageData& resizedImage) { + ImageData& resizedImage) +{ Result ret = g_model_.CreateInput(resizedImage.data.get(), resizedImage.size, g_imageInfoBuf_, g_imageInfoSize_); @@ -222,7 +227,8 @@ ImageData& resizedImage) { } Result ObjectDetect::Postprocess(ImageData& image, aclmdlDataset* modelOutput, -const string& origImagePath) { + const string& origImagePath) +{ uint32_t dataSize = 0; float* detectData = (float *)GetInferenceOutputItem(dataSize, modelOutput, kBBoxDataBufId); @@ -247,7 +253,7 @@ const string& origImagePath) { uint32_t objIndex = (uint32_t)detectData[totalBox * LABEL + i]; boundBox.text = yolov3Label[objIndex] + std::to_string(score) + "\%"; printf("%d %d %d %d %s\n", boundBox.rect.ltX, boundBox.rect.ltY, - boundBox.rect.rbX, boundBox.rect.rbY, boundBox.text.c_str()); + boundBox.rect.rbX, boundBox.rect.rbY, boundBox.text.c_str()); detectResults.emplace_back(boundBox); } @@ -261,8 +267,9 @@ const string& origImagePath) { return SUCCESS; } -void ObjectDetect::DrawBoundBoxToImage(ImageData& image, -vector& detectionResults, const string& origImagePath) { +void ObjectDetect::DrawBoundBoxToImage(ImageData& image, + vector& detectionResults, const string& origImagePath) +{ YUVColor box_color(0, 0, 0xff); for (int i = 0; i < detectionResults.size(); ++i) { @@ -282,12 +289,12 @@ vector& detectionResults, const string& origImagePath) { sstream.str(""); sstream << "./output/out_" << name << ".yuv"; Utils::SaveBinFile(sstream.str(), image.data.get(), image.size); - } void* ObjectDetect::GetInferenceOutputItem(uint32_t& itemDataSize, aclmdlDataset* inferenceOutput, - uint32_t idx) { + uint32_t idx) +{ aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(inferenceOutput, idx); if (dataBuffer == nullptr) { ERROR_LOG("Get the %dth dataset buffer from model " @@ -324,7 +331,8 @@ void* ObjectDetect::GetInferenceOutputItem(uint32_t& itemDataSize, return data; } -void ObjectDetect::DestroyResource() { +void ObjectDetect::DestroyResource() +{ aclrtFree(g_imageInfoBuf_); g_model_.DestroyResource(); g_dvpp_.DestroyResource(); diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/utils.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/utils.cpp old mode 100755 new mode 100644 index 43b511ed5..c7fc98bdb --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/utils.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/src/utils.cpp @@ -14,7 +14,6 @@ * limitations under the License. */ -#include "utils.h" #include #include #include @@ -25,25 +24,27 @@ #include #include #include +#include "utils.h" #include "acl/acl.h" #include "acl/ops/acl_dvpp.h" using namespace std; namespace { -const std::string g_ImagePathSeparator = ","; -const int g_StatSuccess = 0; -const std::string g_FileSperator = "/"; -const std::string g_PathSeparator = "/"; +const std::string g_imagePathSeparator = ","; +const int STATSUCCESS = 0; +const std::string g_fileSperator = "/"; +const std::string g_pathSeparator = "/"; // output image prefix -const std::string g_OutputFilePrefix = "out_"; +const std::string g_outputFilePrefix = "out_"; } -bool Utils::IsDirectory(const string &path) { +bool Utils::IsDirectory(const string &path) +{ // get path stat struct stat buf; - if (stat(path.c_str(), &buf) != g_StatSuccess) { + if (stat(path.c_str(), &buf) != STATSUCCESS) { return false; } @@ -55,7 +56,8 @@ bool Utils::IsDirectory(const string &path) { } } -bool Utils::IsPathExist(const string &path) { +bool Utils::IsPathExist(const string &path) +{ ifstream file(path); if (!file) { return false; @@ -63,17 +65,17 @@ bool Utils::IsPathExist(const string &path) { return true; } -void Utils::SplitPath(const string &path, vector &path_vec) { - char *char_path = const_cast(path.c_str()); - const char *char_split = g_ImagePathSeparator.c_str(); - char *tmp_path = strtok(char_path, char_split); +void Utils::SplitPath(const string &path, vector &path_vec) +{ + char *tmp_path = strtok(const_cast(path.c_str()), g_imagePathSeparator.c_str()); while (tmp_path) { path_vec.emplace_back(tmp_path); - tmp_path = strtok(nullptr, char_split); + tmp_path = strtok(nullptr, g_imagePathSeparator.c_str()); } } -void Utils::GetAllFiles(const string &path, vector &file_vec) { +void Utils::GetAllFiles(const string &path, vector &file_vec) +{ // split file path vector path_vector; SplitPath(path, path_vector); @@ -82,7 +84,7 @@ void Utils::GetAllFiles(const string &path, vector &file_vec) { // check path exist or not if (!IsPathExist(every_path)) { ERROR_LOG("Failed to deal path=%s. Reason: not exist " - "or can not access.", every_path.c_str()); + "or can not access.", every_path.c_str()); continue; } // get files in path and sub-path @@ -90,7 +92,8 @@ void Utils::GetAllFiles(const string &path, vector &file_vec) { } } -void Utils::GetPathFiles(const string &path, vector &file_vec) { +void Utils::GetPathFiles(const string &path, vector &file_vec) +{ struct dirent *dirent_ptr = nullptr; DIR *dir = nullptr; if (IsDirectory(path)) { @@ -102,7 +105,7 @@ void Utils::GetPathFiles(const string &path, vector &file_vec) { } // file path - string full_path = path + g_PathSeparator + dirent_ptr->d_name; + string full_path = path + g_pathSeparator + dirent_ptr->d_name; // directory need recursion if (IsDirectory(full_path)) { GetPathFiles(full_path, file_vec); @@ -116,13 +119,14 @@ void Utils::GetPathFiles(const string &path, vector &file_vec) { } } -void* Utils::CopyDataHostToDvpp(void* data, int size) { +void* Utils::CopyDataHostToDvpp(void* data, int size) +{ void* buffer = nullptr; auto aclRet = acldvppMalloc(&buffer, size); if (aclRet != ACL_SUCCESS) { ERROR_LOG("acl malloc dvpp data failed, dataSize=%u, ret=%d", - size, aclRet); + size, aclRet); return nullptr; } INFO_LOG("malloc dvpp memory size %d ok", size); @@ -138,13 +142,14 @@ void* Utils::CopyDataHostToDvpp(void* data, int size) { return buffer; } -void* Utils::CopyDataDeviceToDvpp(void* data, int size) { +void* Utils::CopyDataDeviceToDvpp(void* data, int size) +{ void* buffer = nullptr; auto aclRet = acldvppMalloc(&buffer, size); if (aclRet != ACL_SUCCESS) { ERROR_LOG("acl malloc dvpp data failed, dataSize=%u, ret=%d", - size, aclRet); + size, aclRet); return nullptr; } INFO_LOG("malloc dvpp memory size %d ok", size); @@ -160,7 +165,8 @@ void* Utils::CopyDataDeviceToDvpp(void* data, int size) { return buffer; } -Result Utils::CopyImageDataToDvpp(ImageData& imageDevice, ImageData srcImage) { +Result Utils::CopyImageDataToDvpp(ImageData& imageDevice, ImageData srcImage) +{ aclrtRunMode runMode_; aclError ret = aclrtGetRunMode(&runMode_); if (ret != ACL_SUCCESS) { @@ -175,8 +181,7 @@ Result Utils::CopyImageDataToDvpp(ImageData& imageDevice, ImageData srcImage) { ERROR_LOG("Copy image to device failed"); return FAILED; } - } - else { + } else { buffer = Utils::CopyDataDeviceToDvpp(srcImage.data.get(), srcImage.size); if (buffer == nullptr) { ERROR_LOG("Copy image to device failed"); @@ -191,7 +196,8 @@ Result Utils::CopyImageDataToDvpp(ImageData& imageDevice, ImageData srcImage) { return SUCCESS; } -void* Utils::CopyDataDeviceToLocal(void* deviceData, uint32_t dataSize) { +void* Utils::CopyDataDeviceToLocal(void* deviceData, uint32_t dataSize) +{ uint8_t* buffer = new uint8_t[dataSize]; if (buffer == nullptr) { ERROR_LOG("New malloc memory failed\n"); @@ -208,12 +214,12 @@ void* Utils::CopyDataDeviceToLocal(void* deviceData, uint32_t dataSize) { return (void*)buffer; } -Result Utils::CopyDeviceToLocal(ImageData& imageout, ImageData srcImage, aclrtRunMode mode) { - void * buffer = nullptr; +Result Utils::CopyDeviceToLocal(ImageData& imageout, ImageData srcImage, aclrtRunMode mode) +{ + void* buffer = nullptr; if (mode == ACL_HOST) { buffer = Utils::CopyDataDeviceToLocal(srcImage.data.get(), srcImage.size); - } - else { + } else { buffer = Utils::CopyDataDeviceToLocal(srcImage.data.get(), srcImage.size); } @@ -232,7 +238,8 @@ Result Utils::CopyDeviceToLocal(ImageData& imageout, ImageData srcImage, aclrtRu return SUCCESS; } -void* Utils::CopyDataToDevice(void* data, uint32_t dataSize, aclrtMemcpyKind policy) { +void* Utils::CopyDataToDevice(void* data, uint32_t dataSize, aclrtMemcpyKind policy) +{ void* buffer = nullptr; aclError aclRet = aclrtMalloc(&buffer, dataSize, ACL_MEM_MALLOC_HUGE_FIRST); if (aclRet != ACL_SUCCESS) { @@ -250,17 +257,20 @@ void* Utils::CopyDataToDevice(void* data, uint32_t dataSize, aclrtMemcpyKind pol return buffer; } -void* Utils::CopyDataDeviceToDevice(void* deviceData, uint32_t dataSize) { +void* Utils::CopyDataDeviceToDevice(void* deviceData, uint32_t dataSize) +{ return CopyDataToDevice(deviceData, dataSize, ACL_MEMCPY_DEVICE_TO_DEVICE); } -void* Utils::CopyDataHostToDevice(void* deviceData, uint32_t dataSize) { +void* Utils::CopyDataHostToDevice(void* deviceData, uint32_t dataSize) +{ return CopyDataToDevice(deviceData, dataSize, ACL_MEMCPY_HOST_TO_DEVICE); } -Result Utils::CopyImageDataToDevice(ImageData& imageDevice, - ImageData srcImage, aclrtRunMode mode) { - void * buffer; +Result Utils::CopyImageDataToDevice(ImageData& imageDevice, + ImageData srcImage, aclrtRunMode mode) +{ + void* buffer; if (mode == ACL_HOST) buffer = Utils::CopyDataHostToDevice(srcImage.data.get(), srcImage.size); else @@ -279,7 +289,8 @@ Result Utils::CopyImageDataToDevice(ImageData& imageDevice, return SUCCESS; } -int Utils::ReadImageFile(ImageData& image, std::string fileName) { +int Utils::ReadImageFile(ImageData& image, std::string fileName) +{ struct stat sBuf; int fileStatus = stat(fileName.data(), &sBuf); if (fileStatus == -1) { @@ -317,14 +328,15 @@ int Utils::ReadImageFile(ImageData& image, std::string fileName) { int32_t ch = 0; acldvppJpegGetImageInfo(binFileBufferData, binFileBufferLen, - &(image.width), &(image.height), &ch); + &(image.width), &(image.height), &ch); image.data.reset(binFileBufferData, [](uint8_t* p) { delete[](p); }); image.size = binFileBufferLen; return SUCCESS; } -void Utils::SaveBinFile(const string& filename, const void* data, uint32_t size) { +void Utils::SaveBinFile(const string& filename, const void* data, uint32_t size) +{ FILE *outFileFp = fopen(filename.c_str(), "wb+"); if (outFileFp == nullptr) { ERROR_LOG("Save file %s failed for open error", filename.c_str()); diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/inc/model_process.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/inc/model_process.h index f74b0753d..501d24e33 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/inc/model_process.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/inc/model_process.h @@ -71,8 +71,8 @@ public: * @param [in] bufferSize: input buffer size * @return result */ - Result CreateInput(void *input1, size_t input1size, - void* input2, size_t input2size); + Result CreateInput(void *input1, size_t input1Size, + void* input2, size_t input2Size); /** * @brief destroy input resource diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/inc/utils.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/inc/utils.h index 27719252f..1aebbcd50 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/inc/utils.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/inc/utils.h @@ -46,7 +46,8 @@ using namespace std; #define SHARED_PTR_U8_BUF(buf) (shared_ptr((uint8_t *)(buf), [](uint8_t* p) { delete[](p); })) template -std::shared_ptr MakeSharedNoThrow() { +std::shared_ptr MakeSharedNoThrow() +{ try { return std::make_shared(); } @@ -57,8 +58,8 @@ std::shared_ptr MakeSharedNoThrow() { #define MAKE_SHARED_NO_THROW(memory, memory_type) \ do { \ - memory = MakeSharedNoThrow(); \ - }while(0); + memory = MakeSharedNoThrow<(memory_type)>(); \ + }while (0) typedef enum Result { SUCCESS = 0, diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/main.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/main.cpp index ce735855d..450491bfa 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/main.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/main.cpp @@ -22,20 +22,20 @@ using namespace std; namespace { -uint32_t g_ModelWidth = 416; -uint32_t g_ModelHeight = 416; -const char* g_ModelPath = "../model/yolov3.om"; +uint32_t g_modelWidth = 416; +uint32_t g_modelHeight = 416; +const char* g_modelPath = "../model/yolov3.om"; } -int main(int argc, char *argv[]) { +int main(int argc, char *argv[]) +{ // Check the input when the application executes, which takes the path to the input video file - if ((argc < 2) || (argv[1] == nullptr)) { + uint32_t argNum = 2; + if ((argc < argNum) || (argv[1] == nullptr)) { ERROR_LOG("Please input: ./main "); return FAILED; } - // Instantiate the target detection class with the parameters of the classification model path and the required width and height of the model input - ObjectDetect detect(g_ModelPath, g_ModelWidth, g_ModelHeight); - // Initializes the ACL resource for categorical reasoning, loads the model and requests the memory used for reasoning input + ObjectDetect detect(g_modelPath, g_modelWidth, g_modelHeight); Result ret = detect.Init(); if (ret != SUCCESS) { ERROR_LOG("Classification Init resource failed"); @@ -50,7 +50,7 @@ int main(int argc, char *argv[]) { return FAILED; } // Frame by frame reasoning - while(1) { + while (1) { // Read a frame of an image cv::Mat frame; if (!capture.read(frame)) { @@ -71,7 +71,6 @@ int main(int argc, char *argv[]) { ERROR_LOG("Inference model inference output data failed"); return FAILED; } - // Parses the inference output and sends the inference class, location, confidence, and image to the Presenter Server for display ret = detect.Postprocess(frame, inferenceOutput); if (ret != SUCCESS) { ERROR_LOG("Process model inference output data failed"); @@ -81,4 +80,4 @@ int main(int argc, char *argv[]) { INFO_LOG("Execute video object detection success"); return SUCCESS; -} +} \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/model_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/model_process.cpp index 66dcaa534..cc93db24d 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/model_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/model_process.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,26 +12,27 @@ * 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 model_process.cpp -* Description: handle model process */ + #include #include "model_process.h" #include "utils.h" using namespace std; -ModelProcess::ModelProcess():g_loadFlag_(false), g_modelId_(0), g_modelMemPtr_(nullptr), g_modelMemSize_(0), -g_modelWeightPtr_(nullptr),g_modelWeightSize_(0), g_modelDesc_(nullptr), g_input_(nullptr), g_output_(nullptr), -g_isReleased_(false) { - +ModelProcess::ModelProcess() + : g_loadFlag_(false), g_modelId_(0), g_modelMemPtr_(nullptr), g_modelMemSize_(0), + g_modelWeightPtr_(nullptr), g_modelWeightSize_(0), g_modelDesc_(nullptr), + g_input_(nullptr), g_output_(nullptr), g_isReleased_(false) +{ } -ModelProcess::~ModelProcess() { +ModelProcess::~ModelProcess() +{ DestroyResource(); } -void ModelProcess::DestroyResource() { +void ModelProcess::DestroyResource() +{ if (g_isReleased_) return; @@ -42,7 +43,8 @@ void ModelProcess::DestroyResource() { g_isReleased_ = true; } -Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath) { +Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath) +{ if (g_loadFlag_) { ERROR_LOG("has already loaded a model"); return FAILED; @@ -78,7 +80,8 @@ Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath) { return SUCCESS; } -Result ModelProcess::CreateDesc() { +Result ModelProcess::CreateDesc() +{ g_modelDesc_ = aclmdlCreateDesc(); if (g_modelDesc_ == nullptr) { ERROR_LOG("create model description failed"); @@ -95,22 +98,23 @@ Result ModelProcess::CreateDesc() { return SUCCESS; } -void ModelProcess::DestroyDesc() { +void ModelProcess::DestroyDesc() +{ if (g_modelDesc_ != nullptr) { (void)aclmdlDestroyDesc(g_modelDesc_); g_modelDesc_ = nullptr; } } -Result ModelProcess::CreateInput(void *input1, size_t input1size, - void* input2, size_t input2size) { +Result ModelProcess::CreateInput(void *input1, size_t input1Size, + void* input2, size_t input2Size) { g_input_ = aclmdlCreateDataset(); if (g_input_ == nullptr) { ERROR_LOG("can't create dataset, create input failed"); return FAILED; } - aclDataBuffer* inputData = aclCreateDataBuffer(input1, input1size); + aclDataBuffer* inputData = aclCreateDataBuffer(input1, input1Size); if (inputData == nullptr) { ERROR_LOG("can't create data buffer, create input failed"); return FAILED; @@ -124,7 +128,7 @@ Result ModelProcess::CreateInput(void *input1, size_t input1size, return FAILED; } - aclDataBuffer* inputData2 = aclCreateDataBuffer(input2, input2size); + aclDataBuffer* inputData2 = aclCreateDataBuffer(input2, input2Size); if (inputData == nullptr) { ERROR_LOG("can't create data buffer, create input failed"); return FAILED; @@ -141,7 +145,8 @@ Result ModelProcess::CreateInput(void *input1, size_t input1size, return SUCCESS; } -void ModelProcess::DestroyInput() { +void ModelProcess::DestroyInput() +{ if (g_input_ == nullptr) { return; } @@ -154,7 +159,8 @@ void ModelProcess::DestroyInput() { g_input_ = nullptr; } -Result ModelProcess::CreateOutput() { +Result ModelProcess::CreateOutput() +{ if (g_modelDesc_ == nullptr) { ERROR_LOG("no model description, create ouput failed"); return FAILED; @@ -197,7 +203,8 @@ Result ModelProcess::CreateOutput() { return SUCCESS; } -void ModelProcess::DestroyOutput() { +void ModelProcess::DestroyOutput() +{ if (g_output_ == nullptr) { return; } @@ -213,7 +220,8 @@ void ModelProcess::DestroyOutput() { g_output_ = nullptr; } -Result ModelProcess::Execute() { +Result ModelProcess::Execute() +{ aclError ret = aclmdlExecute(g_modelId_, g_input_, g_output_); if (ret != ACL_SUCCESS) { ERROR_LOG("execute model failed, modelId is %u", g_modelId_); @@ -223,7 +231,8 @@ Result ModelProcess::Execute() { return SUCCESS; } -void ModelProcess::Unload() { +void ModelProcess::Unload() +{ if (!g_loadFlag_) { WARN_LOG("no model had been loaded, unload failed"); return; @@ -255,6 +264,7 @@ void ModelProcess::Unload() { INFO_LOG("unload model success, modelId is %u", g_modelId_); } -aclmdlDataset *ModelProcess::GetModelOutputData() { +aclmdlDataset *ModelProcess::GetModelOutputData() +{ return g_output_; } diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/object_detect.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/object_detect.cpp index 28bee0718..953931446 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/object_detect.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/object_detect.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,8 @@ * 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 "object_detect.h" #include "model_process.h" @@ -26,34 +24,36 @@ using namespace std; namespace { const static std::vector yolov3Label = { "person", "bicycle", "car", "motorbike", -"aeroplane","bus", "train", "truck", "boat", -"traffic light", "fire hydrant", "stop sign", "parking meter", -"bench", "bird", "cat", "dog", "horse", -"sheep", "cow", "elephant", "bear", "zebra", -"giraffe", "backpack", "umbrella", "handbag","tie", -"suitcase", "frisbee", "skis", "snowboard", "sports ball", -"kite", "baseball bat", "baseball glove", "skateboard", "surfboard", -"tennis racket", "bottle", "wine glass", "cup", -"fork", "knife", "spoon", "bowl", "banana", -"apple", "sandwich", "orange", "broccoli", "carrot", -"hot dog", "pizza", "donut", "cake", "chair", -"sofa", "potted plant", "bed", "dining table", "toilet", -"TV monitor", "laptop", "mouse", "remote", "keyboard", -"cell phone", "microwave", "oven", "toaster", "sink", -"refrigerator", "book", "clock", "vase","scissors", -"teddy bear", "hair drier", "toothbrush" }; + "aeroplane","bus", "train", "truck", "boat", + "traffic light", "fire hydrant", "stop sign", "parking meter", + "bench", "bird", "cat", "dog", "horse", + "sheep", "cow", "elephant", "bear", "zebra", + "giraffe", "backpack", "umbrella", "handbag","tie", + "suitcase", "frisbee", "skis", "snowboard", "sports ball", + "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", + "tennis racket", "bottle", "wine glass", "cup", + "fork", "knife", "spoon", "bowl", "banana", + "apple", "sandwich", "orange", "broccoli", "carrot", + "hot dog", "pizza", "donut", "cake", "chair", + "sofa", "potted plant", "bed", "dining table", "toilet", + "TV monitor", "laptop", "mouse", "remote", "keyboard", + "cell phone", "microwave", "oven", "toaster", "sink", + "refrigerator", "book", "clock", "vase", "scissors", + "teddy bear", "hair drier", "toothbrush" }; // Inferential output dataset subscript 0 unit is detection box information data -const uint32_t g_BBoxDataBufId = 0; +const uint32_t g_bBoxDataBufId = 0; // The unit with subscript 1 is the number of boxes -const uint32_t g_BoxNumDataBufId = 1; +const uint32_t g_boxNumDataBufId = 1; // Each field subscript in the box message enum BBoxIndex { TOPLEFTX = 0, TOPLEFTY, BOTTOMRIGHTX, BOTTOMRIGHTY, SCORE, LABEL }; } ObjectDetect::ObjectDetect(const char* modelPath, uint32_t modelWidth, uint32_t modelHeight) -:g_deviceId_(0), g_imageDataBuf_(nullptr), g_imageInfoBuf_(nullptr), g_modelWidth_(modelWidth), -g_modelHeight_(modelHeight), g_isInited_(false) { + : g_deviceId_(0), g_imageDataBuf_(nullptr), g_imageInfoBuf_(nullptr), + g_modelWidth_(modelWidth), g_modelHeight_(modelHeight), + g_isInited_(false) +{ g_modelPath_ = modelPath; g_imageDataSize_ = RGBU8_IMAGE_SIZE(modelWidth, modelHeight); g_channel_ = nullptr; @@ -63,11 +63,13 @@ g_modelHeight_(modelHeight), g_isInited_(false) { } } -ObjectDetect::~ObjectDetect() { +ObjectDetect::~ObjectDetect() +{ DestroyResource(); } -Result ObjectDetect::InitResource() { +Result ObjectDetect::InitResource() +{ // ACL init const char *aclConfigPath = "../src/acl.json"; aclError ret = aclInit(aclConfigPath); @@ -94,7 +96,8 @@ Result ObjectDetect::InitResource() { return SUCCESS; } -Result ObjectDetect::InitModel(const char* omModelPath) { +Result ObjectDetect::InitModel(const char* omModelPath) +{ Result ret = g_model_.LoadModelFromFileWithMem(omModelPath); if (ret != SUCCESS) { ERROR_LOG("execute LoadModelFromFileWithMem failed"); @@ -116,7 +119,8 @@ Result ObjectDetect::InitModel(const char* omModelPath) { return SUCCESS; } -Result ObjectDetect::CreateModelInputdDataset() { +Result ObjectDetect::CreateModelInputdDataset() +{ // Request image data memory for input model aclError aclRet = aclrtMalloc(&g_imageDataBuf_, g_imageDataSize_, ACL_MEM_MALLOC_HUGE_FIRST); if (aclRet != ACL_SUCCESS) { @@ -125,7 +129,7 @@ Result ObjectDetect::CreateModelInputdDataset() { } // The second input to Yolov3 is the input image width and height parameter const float imageInfo[4] = {(float)g_modelWidth_, (float)g_modelHeight_, - (float)g_modelWidth_, (float)g_modelHeight_}; + (float)g_modelWidth_, (float)g_modelHeight_}; g_imageInfoSize_ = sizeof(imageInfo); if (g_runMode_ == ACL_HOST) g_imageInfoBuf_ = Utils::CopyDataHostToDevice((void *)imageInfo, g_imageInfoSize_); @@ -135,7 +139,8 @@ Result ObjectDetect::CreateModelInputdDataset() { ERROR_LOG("Copy image info to device failed"); return FAILED; } - // Use the applied memory to create the model and input dataset. After creation, only update the memory data for each frame of inference, instead of creating the input dataset every time + // Use the applied memory to create the model and input dataset. After creation, + // only update the memory data for each frame of inference, instead of creating the input dataset every time Result ret = g_model_.CreateInput(g_imageDataBuf_, g_imageDataSize_, g_imageInfoBuf_, g_imageInfoSize_); if (ret != SUCCESS) { @@ -146,7 +151,8 @@ Result ObjectDetect::CreateModelInputdDataset() { return SUCCESS; } -Result ObjectDetect::Init() { +Result ObjectDetect::Init() +{ // If it is already initialized, it is returned if (g_isInited_) { INFO_LOG("Classify instance is initied already!"); @@ -175,7 +181,8 @@ Result ObjectDetect::Init() { return SUCCESS; } -Result ObjectDetect::Preprocess(cv::Mat& frame) { +Result ObjectDetect::Preprocess(cv::Mat& frame) +{ // Scale the frame image to the desired size of the model cv::Mat reiszeMat; cv::resize(frame, reiszeMat, cv::Size(g_modelWidth_, g_modelHeight_)); @@ -196,7 +203,8 @@ Result ObjectDetect::Preprocess(cv::Mat& frame) { return SUCCESS; } -Result ObjectDetect::Inference(aclmdlDataset*& inferenceOutput) { +Result ObjectDetect::Inference(aclmdlDataset*& inferenceOutput) +{ // Perform reasoning Result ret = g_model_.Execute(); if (ret != SUCCESS) { @@ -210,20 +218,20 @@ Result ObjectDetect::Inference(aclmdlDataset*& inferenceOutput) { } Result ObjectDetect::Postprocess(cv::Mat& frame, - aclmdlDataset* modelOutput) { + aclmdlDataset* modelOutput) +{ // Get box information data uint32_t dataSize = 0; float* detectData = (float*)GetInferenceOutputItem(dataSize, modelOutput, - g_BBoxDataBufId); + g_bBoxDataBufId); if (detectData == nullptr) return FAILED; // Gets the number of boxes uint32_t* boxNum = (uint32_t*)GetInferenceOutputItem(dataSize, modelOutput, - g_BoxNumDataBufId); + g_boxNumDataBufId); if (boxNum == nullptr) return FAILED; // Number of boxes The first data is valid uint32_t totalBox = boxNum[0]; - // float widthScale = (float)(frame.cols) / g_modelWidth_; float heightScale = (float)(frame.rows) / g_modelHeight_; @@ -233,7 +241,8 @@ Result ObjectDetect::Postprocess(cv::Mat& frame, Point point_lt, point_rb; // get the confidence of the detected object. Anything less than 0.8 is considered invalid uint32_t score = uint32_t(detectData[totalBox * SCORE + i] * 100); - if (score < 80) continue; + uint32_t scoreLine = 80; + if (score < scoreLine) continue; // get the frame coordinates and converts them to the coordinates on the original frame oneResult.lt.x = detectData[totalBox * TOPLEFTX + i] * widthScale; oneResult.lt.y = detectData[totalBox * TOPLEFTY + i] * heightScale; @@ -259,26 +268,26 @@ Result ObjectDetect::Postprocess(cv::Mat& frame, void* ObjectDetect::GetInferenceOutputItem(uint32_t& itemDataSize, aclmdlDataset* inferenceOutput, - uint32_t idx) { - + uint32_t idx) +{ aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(inferenceOutput, idx); if (dataBuffer == nullptr) { ERROR_LOG("Get the %dth dataset buffer from model " - "inference output failed", idx); + "inference output failed", idx); return nullptr; } void* dataBufferDev = aclGetDataBufferAddr(dataBuffer); if (dataBufferDev == nullptr) { ERROR_LOG("Get the %dth dataset buffer address " - "from model inference output failed", idx); + "from model inference output failed", idx); return nullptr; } size_t bufferSize = aclGetDataBufferSize(dataBuffer); if (bufferSize == 0) { ERROR_LOG("The %dth dataset buffer size of " - "model inference output is 0", idx); + "model inference output is 0", idx); return nullptr; } @@ -289,8 +298,7 @@ void* ObjectDetect::GetInferenceOutputItem(uint32_t& itemDataSize, ERROR_LOG("Copy inference output to host failed"); return nullptr; } - } - else { + } else { data = dataBufferDev; } @@ -299,16 +307,18 @@ void* ObjectDetect::GetInferenceOutputItem(uint32_t& itemDataSize, } void ObjectDetect::EncodeImage(vector& encodeImg, - cv::Mat& origImg) { + cv::Mat& origImg) +{ vector param = vector(2); param[0] = CV_IMWRITE_JPEG_QUALITY; - param[1] = 95;// default(95) 0-100 + param[1] = 95; // default(95) 0-100 // Jpeg images must serialize the Proto message before they can be sent cv::imencode(".jpg", origImg, encodeImg, param); } Result ObjectDetect::SendImage(vector& detectionResults, - cv::Mat& origImg) { + cv::Mat& origImg) +{ vector encodeImg; EncodeImage(encodeImg, origImg); @@ -329,8 +339,9 @@ Result ObjectDetect::SendImage(vector& detectionResults, return SUCCESS; } -void ObjectDetect::DestroyResource() { - aclrtFree(g_imageDataBuf_); +void ObjectDetect::DestroyResource() +{ + aclrtFree(g_imageDataBuf_); aclrtFree(g_imageInfoBuf_); delete g_channel_; @@ -338,8 +349,7 @@ void ObjectDetect::DestroyResource() { // The ACL resource held by the model instance must be released before the ACL exits or ABORT will be torn down g_model_.DestroyResource(); - aclError ret; - ret = aclrtResetDevice(g_deviceId_); + aclError ret = aclrtResetDevice(g_deviceId_); if (ret != ACL_SUCCESS) { ERROR_LOG("reset device failed"); } @@ -350,5 +360,4 @@ void ObjectDetect::DestroyResource() { ERROR_LOG("finalize acl failed"); } INFO_LOG("end to finalize acl"); - -} +} \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/utils.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/utils.cpp index 62a58e4f3..8dffe224d 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/utils.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/utils.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,10 +12,8 @@ * 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 utils.cpp -* Description: handle file operations */ + #include #include #include @@ -32,19 +30,19 @@ using namespace std; namespace { -const std::string g_ImagePathSeparator = ","; -const int g_StatSuccess = 0; -const std::string g_FileSperator = "/"; -const std::string g_PathSeparator = "/"; +const std::string g_imagePathSeparator = ","; +const int STAT_SUCCESS = 0; +const std::string g_fileSperator = "/"; +const std::string g_pathSeparator = "/"; // output image prefix -const std::string g_OutputFilePrefix = "out_"; - +const std::string g_outputFilePrefix = "out_"; } -bool Utils::IsDirectory(const string &path) { +bool Utils::IsDirectory(const string &path) +{ // get path stat struct stat buf; - if (stat(path.c_str(), &buf) != g_StatSuccess) { + if (stat(path.c_str(), &buf) != STAT_SUCCESS) { return false; } @@ -56,7 +54,8 @@ bool Utils::IsDirectory(const string &path) { } } -bool Utils::IsPathExist(const string &path) { +bool Utils::IsPathExist(const string &path) +{ ifstream file(path); if (!file) { return false; @@ -64,17 +63,17 @@ bool Utils::IsPathExist(const string &path) { return true; } -void Utils::SplitPath(const string &path, vector &path_vec) { - char *char_path = const_cast(path.c_str()); - const char *char_split = g_ImagePathSeparator.c_str(); - char *tmp_path = strtok(char_path, char_split); +void Utils::SplitPath(const string &path, vector &path_vec) +{ + char *tmp_path = strtok(const_cast(path.c_str()), g_imagePathSeparator.c_str()); while (tmp_path) { path_vec.emplace_back(tmp_path); - tmp_path = strtok(nullptr, char_split); + tmp_path = strtok(nullptr, g_imagePathSeparator.c_str()); } } -void Utils::GetAllFiles(const string &path, vector &file_vec) { +void Utils::GetAllFiles(const string &path, vector &file_vec) +{ // split file path vector path_vector; SplitPath(path, path_vector); @@ -83,7 +82,7 @@ void Utils::GetAllFiles(const string &path, vector &file_vec) { // check path exist or not if (!IsPathExist(path)) { ERROR_LOG("Failed to deal path=%s. Reason: not exist or can not access.", - every_path.c_str()); + every_path.c_str()); continue; } // get files in path and sub-path @@ -91,7 +90,8 @@ void Utils::GetAllFiles(const string &path, vector &file_vec) { } } -void Utils::GetPathFiles(const string &path, vector &file_vec) { +void Utils::GetPathFiles(const string &path, vector &file_vec) +{ struct dirent *dirent_ptr = nullptr; DIR *dir = nullptr; if (IsDirectory(path)) { @@ -103,7 +103,7 @@ void Utils::GetPathFiles(const string &path, vector &file_vec) { } // file path - string full_path = path + g_PathSeparator + dirent_ptr->d_name; + string full_path = path + g_pathSeparator + dirent_ptr->d_name; // directory need recursion if (IsDirectory(full_path)) { GetPathFiles(full_path, file_vec); @@ -112,13 +112,13 @@ void Utils::GetPathFiles(const string &path, vector &file_vec) { file_vec.emplace_back(full_path); } } - } - else { + } else { file_vec.emplace_back(path); } } -void* Utils::CopyDataDeviceToLocal(void* deviceData, uint32_t dataSize) { +void* Utils::CopyDataDeviceToLocal(void* deviceData, uint32_t dataSize) +{ uint8_t* buffer = new uint8_t[dataSize]; if (buffer == nullptr) { ERROR_LOG("New malloc memory failed"); @@ -135,7 +135,8 @@ void* Utils::CopyDataDeviceToLocal(void* deviceData, uint32_t dataSize) { return (void*)buffer; } -void* Utils::CopyDataToDevice(void* data, uint32_t dataSize, aclrtMemcpyKind policy) { +void* Utils::CopyDataToDevice(void* data, uint32_t dataSize, aclrtMemcpyKind policy) +{ void* buffer = nullptr; aclError aclRet = aclrtMalloc(&buffer, dataSize, ACL_MEM_MALLOC_HUGE_FIRST); if (aclRet != ACL_SUCCESS) { @@ -153,16 +154,19 @@ void* Utils::CopyDataToDevice(void* data, uint32_t dataSize, aclrtMemcpyKind pol return buffer; } -void* Utils::CopyDataDeviceToDevice(void* deviceData, uint32_t dataSize) { +void* Utils::CopyDataDeviceToDevice(void* deviceData, uint32_t dataSize) +{ return CopyDataToDevice(deviceData, dataSize, ACL_MEMCPY_DEVICE_TO_DEVICE); } -void* Utils::CopyDataHostToDevice(void* deviceData, uint32_t dataSize) { +void* Utils::CopyDataHostToDevice(void* deviceData, uint32_t dataSize) +{ return CopyDataToDevice(deviceData, dataSize, ACL_MEMCPY_HOST_TO_DEVICE); } -Result Utils::CopyImageDataToDevice(ImageData& imageDevice, ImageData srcImage, aclrtRunMode mode) { - void * buffer; +Result Utils::CopyImageDataToDevice(ImageData& imageDevice, ImageData srcImage, aclrtRunMode mode) +{ + void* buffer; if (mode == ACL_HOST) buffer = Utils::CopyDataHostToDevice(srcImage.data.get(), srcImage.size); else diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video_DVPP_with_AIPP/inc/sample_process.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video_DVPP_with_AIPP/inc/sample_process.h index 5d5ef81fd..629ef352b 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video_DVPP_with_AIPP/inc/sample_process.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video_DVPP_with_AIPP/inc/sample_process.h @@ -38,7 +38,8 @@ public: AclLiteError Process(); void DrawBoundBoxToImage(std::vector& detectionResults, cv::Mat& origImage); - AclLiteError Postprocess(const std::vector& modelOutput, cv::Mat& srcImg, int modelWidth, int modelHeight); + AclLiteError Postprocess(const std::vector& modelOutput, + cv::Mat& srcImg, int modelWidth, int modelHeight); private: void DestroyResource(); diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video_DVPP_with_AIPP/src/main.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video_DVPP_with_AIPP/src/main.cpp index 605c4e0bf..13d77aa78 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video_DVPP_with_AIPP/src/main.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video_DVPP_with_AIPP/src/main.cpp @@ -18,9 +18,11 @@ #include "sample_process.h" using namespace std; -int main(int argc, char *argv[]) { +int main(int argc, char *argv[]) +{ // Check the input when the application executes, which takes the path to the input video file - if ((argc < 2) || (argv[1] == nullptr)) { + int argNum = 2; + if ((argc < argNum) || (argv[1] == nullptr)) { ACLLITE_LOG_ERROR("Please input: ./main "); return ACLLITE_ERROR; } diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video_DVPP_with_AIPP/src/sample_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video_DVPP_with_AIPP/src/sample_process.cpp index 2f8cf64d8..831d69d06 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video_DVPP_with_AIPP/src/sample_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video_DVPP_with_AIPP/src/sample_process.cpp @@ -15,65 +15,68 @@ */ #include -#include "acl/acl.h" #include #include #include #include +#include "acl/acl.h" #include "sample_process.h" using namespace std; namespace { const static std::vector yolov3Label = { "person", "bicycle", "car", "motorbike", - "aeroplane","bus", "train", "truck", "boat", - "traffic light", "fire hydrant", "stop sign", "parking meter", - "bench", "bird", "cat", "dog", "horse", - "sheep", "cow", "elephant", "bear", "zebra", - "giraffe", "backpack", "umbrella", "handbag","tie", - "suitcase", "frisbee", "skis", "snowboard", "sports ball", - "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", - "tennis racket", "bottle", "wine glass", "cup", - "fork", "knife", "spoon", "bowl", "banana", - "apple", "sandwich", "orange", "broccoli", "carrot", - "hot dog", "pizza", "donut", "cake", "chair", - "sofa", "potted plant", "bed", "dining table", "toilet", - "TV monitor", "laptop", "mouse", "remote", "keyboard", - "cell phone", "microwave", "oven", "toaster", "sink", - "refrigerator", "book", "clock", "vase","scissors", - "teddy bear", "hair drier", "toothbrush" }; - - const uint32_t g_BBoxDataBufId = 0; - const uint32_t g_BoxNumDataBufId = 1; + "aeroplane", "bus", "train", "truck", "boat", + "traffic light", "fire hydrant", "stop sign", "parking meter", + "bench", "bird", "cat", "dog", "horse", + "sheep", "cow", "elephant", "bear", "zebra", + "giraffe", "backpack", "umbrella", "handbag", "tie", + "suitcase", "frisbee", "skis", "snowboard", "sports ball", + "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", + "tennis racket", "bottle", "wine glass", "cup", + "fork", "knife", "spoon", "bowl", "banana", + "apple", "sandwich", "orange", "broccoli", "carrot", + "hot dog", "pizza", "donut", "cake", "chair", + "sofa", "potted plant", "bed", "dining table", "toilet", + "TV monitor", "laptop", "mouse", "remote", "keyboard", + "cell phone", "microwave", "oven", "toaster", "sink", + "refrigerator", "book", "clock", "vase", "scissors", + "teddy bear", "hair drier", "toothbrush" }; + + const uint32_t g_bBoxDataBufId = 0; + const uint32_t g_boxNumDataBufId = 1; enum BBoxIndex { TOPLEFTX = 0, TOPLEFTY, BOTTOMRIGHTX, BOTTOMRIGHTY, SCORE, LABEL }; // bounding box line solid - const uint32_t g_LineSolid = 2; + const uint32_t g_lineSolid = 2; // opencv draw label params. - const double g_FountScale = 0.5; - const cv::Scalar g_FontColor(0, 0, 255); - const uint32_t g_LabelOffset = 11; + const double g_fountScale = 0.5; + const cv::Scalar g_fontColor(0, 0, 255); + const uint32_t g_labelOffset = 11; // opencv color list for boundingbox - const vector g_Colors { + const vector g_colors { cv::Scalar(237, 149, 100), cv::Scalar(0, 215, 255), cv::Scalar(50, 205, 50), cv::Scalar(139, 85, 26) }; - const char* omModelPath = "../model/yolov3.om"; - const uint32_t modelInputWidth = 416; - const uint32_t modelInputHeight = 416; + const char* g_omModelPath = "../model/yolov3.om"; + const uint32_t g_modelInputWidth = 416; + const uint32_t g_modelInputHeight = 416; } -SampleProcess::SampleProcess(string streamName): -g_cap_(nullptr), -g_model_(omModelPath), -g_streamName_(streamName) { +SampleProcess::SampleProcess(string streamName) + : g_cap_(nullptr), + g_model_(g_omModelPath), + g_streamName_(streamName) +{ g_imageInfoSize_ = 0; g_imageInfoBuf_ = nullptr; } -SampleProcess::~SampleProcess() { +SampleProcess::~SampleProcess() +{ DestroyResource(); } -AclLiteError SampleProcess::InitResource() { +AclLiteError SampleProcess::InitResource() +{ AclLiteError ret = g_aclDev_.Init(); if (ret) { ACLLITE_LOG_ERROR("Init resource failed, error %d", ret); @@ -97,11 +100,11 @@ AclLiteError SampleProcess::InitResource() { } g_runMode_ = g_aclDev_.GetRunMode(); - const float imageInfo[4] = {(float)modelInputWidth, (float)modelInputHeight, - (float)modelInputWidth, (float)modelInputHeight}; + const float imageInfo[4] = {(float)g_modelInputWidth, (float)g_modelInputHeight, + (float)g_modelInputWidth, (float)g_modelInputHeight}; g_imageInfoSize_ = sizeof(imageInfo); g_imageInfoBuf_ = CopyDataToDevice((void *)imageInfo, g_imageInfoSize_, - g_runMode_, MEMORY_DEVICE); + g_runMode_, MEMORY_DEVICE); if (g_imageInfoBuf_ == nullptr) { ACLLITE_LOG_ERROR("Copy image info to device failed"); return ACLLITE_ERROR; @@ -110,7 +113,8 @@ AclLiteError SampleProcess::InitResource() { return ACLLITE_OK; } -AclLiteError SampleProcess::OpenVideoCapture() { +AclLiteError SampleProcess::OpenVideoCapture() +{ if (IsRtspAddr(g_streamName_)) { g_cap_ = new AclLiteVideoProc(g_streamName_); } else if (IsVideoFile(g_streamName_)) { @@ -134,14 +138,16 @@ AclLiteError SampleProcess::OpenVideoCapture() { return ACLLITE_OK; } -AclLiteError SampleProcess::Postprocess(const vector& modelOutput, cv::Mat& srcImg, int modelWidth, int modelHeight) { +AclLiteError SampleProcess::Postprocess(const vector& modelOutput, cv::Mat& srcImg, + int modelWidth, int modelHeight) +{ uint32_t dataSize = 0; - float* detectData = (float *)modelOutput[g_BBoxDataBufId].data.get(); - uint32_t* boxNum = (uint32_t *)modelOutput[g_BoxNumDataBufId].data.get(); + float* detectData = (float *)modelOutput[g_bBoxDataBufId].data.get(); + uint32_t* boxNum = (uint32_t *)modelOutput[g_boxNumDataBufId].data.get(); uint32_t totalBox = boxNum[0]; - float widthScale = (float)(srcImg.cols) / modelInputWidth; - float heightScale = (float)(srcImg.rows) / modelInputHeight; + float widthScale = (float)(srcImg.cols) / g_modelInputWidth; + float heightScale = (float)(srcImg.rows) / g_modelInputHeight; vector detectResults; for (uint32_t i = 0; i < totalBox; i++) { @@ -154,7 +160,7 @@ AclLiteError SampleProcess::Postprocess(const vector& modelOutp uint32_t objIndex = (uint32_t)detectData[totalBox * LABEL + i]; boundBox.text = yolov3Label[objIndex] + std::to_string(score) + "\%"; printf("%d %d %d %d %s\n", boundBox.rect.ltX, boundBox.rect.ltY, - boundBox.rect.rbX, boundBox.rect.rbY, boundBox.text.c_str()); + boundBox.rect.rbX, boundBox.rect.rbY, boundBox.text.c_str()); detectResults.emplace_back(boundBox); } @@ -162,27 +168,30 @@ AclLiteError SampleProcess::Postprocess(const vector& modelOutp return ACLLITE_OK; } -void SampleProcess::DrawBoundBoxToImage(vector& detectionResults, cv::Mat& origImage) { - +void SampleProcess::DrawBoundBoxToImage(vector& detectionResults, cv::Mat& origImage) +{ for (int i = 0; i < detectionResults.size(); ++i) { cv::Point p1, p2; p1.x = detectionResults[i].rect.ltX; p1.y = detectionResults[i].rect.ltY; p2.x = detectionResults[i].rect.rbX; p2.y = detectionResults[i].rect.rbY; - cv::rectangle(origImage, p1, p2, g_Colors[i % g_Colors.size()], g_LineSolid); - cv::putText(origImage, detectionResults[i].text, cv::Point(p1.x, p1.y + g_LabelOffset), - cv::FONT_HERSHEY_COMPLEX, g_FountScale, g_FontColor); + cv::rectangle(origImage, p1, p2, g_colors[i % g_colors.size()], g_lineSolid); + cv::putText(origImage, detectionResults[i].text, cv::Point(p1.x, p1.y + g_labelOffset), + cv::FONT_HERSHEY_COMPLEX, g_fountScale, g_fontColor); } g_outputVideo_ << origImage; } -AclLiteError SampleProcess::Process() { - string outputVideoPath_ = "./test1.mp4"; +AclLiteError SampleProcess::Process() +{ + string outputVideoPath_ = "./output/test1.mp4"; uint32_t videoWidth_ = g_cap_->Get(FRAME_WIDTH); uint32_t videoHeight_ = g_cap_->Get(FRAME_HEIGHT); + float fps = 25.0; cout << "videoWidth_ and videoHeight_ is" << " " << videoWidth_ << " " << videoHeight_ << endl; - g_outputVideo_.open(outputVideoPath_, cv::VideoWriter::fourcc('m', 'p', '4', 'v'), 25.0, cv::Size(videoWidth_,videoHeight_)); + g_outputVideo_.open(outputVideoPath_, cv::VideoWriter::fourcc('m', 'p', '4', 'v'), + fps, cv::Size(videoWidth_,videoHeight_)); ImageData testPic; bool readflag = true; @@ -192,7 +201,7 @@ AclLiteError SampleProcess::Process() { break; } ImageData resizedImage; - ret = g_dvpp_.Resize(resizedImage, testPic, modelInputWidth, modelInputHeight); + ret = g_dvpp_.Resize(resizedImage, testPic, g_modelInputWidth, g_modelInputHeight); if (ret == ACLLITE_ERROR) { ACLLITE_LOG_ERROR("Resize image failed"); return ACLLITE_ERROR; @@ -200,7 +209,7 @@ AclLiteError SampleProcess::Process() { // 2.model process ret = g_model_.CreateInput(resizedImage.data.get(), resizedImage.size, - g_imageInfoBuf_, g_imageInfoSize_); + g_imageInfoBuf_, g_imageInfoSize_); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Create mode input dataset failed, error:%d", ret); return ACLLITE_ERROR; @@ -223,7 +232,7 @@ AclLiteError SampleProcess::Process() { cv::Mat yuvimg(yuvImage.height * 3 / 2, yuvImage.width, CV_8UC1, yuvImage.data.get()); cv::Mat origImage; cv::cvtColor(yuvimg, origImage, CV_YUV2BGR_NV12); - ret = Postprocess(inferenceOutput, origImage, modelInputWidth, modelInputHeight); + ret = Postprocess(inferenceOutput, origImage, g_modelInputWidth, g_modelInputHeight); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Postprocess failed"); return ACLLITE_ERROR; @@ -233,7 +242,8 @@ AclLiteError SampleProcess::Process() { return ACLLITE_OK; } -void SampleProcess::DestroyResource() { +void SampleProcess::DestroyResource() +{ if (g_cap_ != nullptr) { g_cap_->Close(); delete g_cap_; diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/inc/post_process.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/inc/post_process.h index c32584994..f5ffda37f 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/inc/post_process.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/inc/post_process.h @@ -43,9 +43,11 @@ public: * @param [in] yScale: ouput buffer for y scale * @param [in] originImage: origin image path */ - PostProcess(void *outputClass, void *outputBox, float xScale, float yScale, std::string &originImage) : - g_outputClass_(outputClass), g_outputBox_(outputBox), g_xScale_(xScale), g_yScale_(yScale), - g_originImage_(originImage) {} + PostProcess(void *outputClass, void *outputBox, float xScale, float yScale, std::string &originImage) + : g_outputClass_(outputClass), g_outputBox_(outputBox), g_xScale_(xScale), g_yScale_(yScale), + g_originImage_(originImage) + { + } /** * @brief Constructor @@ -55,7 +57,9 @@ public: /** * @brief Destructor */ - virtual ~PostProcess() {} + virtual ~PostProcess() + { + } /** * @brief sort box diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/inc/utils.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/inc/utils.h index ab0d58aa0..03946ab25 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/inc/utils.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/inc/utils.h @@ -40,11 +40,13 @@ typedef struct PicDesc { class RunStatus { public: - static void SetDeviceStatus(bool isDevice) { + static void SetDeviceStatus(bool isDevice) + { isDevice_ = isDevice; } - static bool GetDeviceStatus() { + static bool GetDeviceStatus() + { return isDevice_; } diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/dvpp_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/dvpp_process.cpp index 4795bcdb4..dd440767f 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/dvpp_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/dvpp_process.cpp @@ -21,7 +21,8 @@ using namespace std; namespace { - uint32_t AlignSize(uint32_t origSize, uint32_t alignment) { + uint32_t AlignSize(uint32_t origSize, uint32_t alignment) + { if (alignment == 0) { return 0; } @@ -30,24 +31,28 @@ namespace { } } -DvppProcess::DvppProcess(aclrtStream &stream) : g_stream_(stream), g_dvppChannelDesc_(nullptr), - g_resizeConfig_(nullptr), g_decodeOutDevBuffer_(nullptr), - g_decodeOutputDesc_(nullptr), g_resizeInputDesc_(nullptr), - g_resizeOutputDesc_(nullptr), g_inDevBuffer_(nullptr), g_inDevBufferSize_(0), - g_jpegDecodeOutputSize_(0), - g_decodeOutputWidth_(0), g_decodeOutputWidthStride_(0), - g_decodeOutputHeight_(0), g_resizeOutBufferDev_(nullptr), - g_resizeOutBufferSize_(0), g_modelInputWidth_(0), g_modelInputHeight_(0), - g_resizeOutWidthStride_(0), - g_resizeOutHeightStride_(0) { +DvppProcess::DvppProcess(aclrtStream &stream) + : g_stream_(stream), g_dvppChannelDesc_(nullptr), + g_resizeConfig_(nullptr), g_decodeOutDevBuffer_(nullptr), + g_decodeOutputDesc_(nullptr), g_resizeInputDesc_(nullptr), + g_resizeOutputDesc_(nullptr), g_inDevBuffer_(nullptr), g_inDevBufferSize_(0), + g_jpegDecodeOutputSize_(0), + g_decodeOutputWidth_(0), g_decodeOutputWidthStride_(0), + g_decodeOutputHeight_(0), g_resizeOutBufferDev_(nullptr), + g_resizeOutBufferSize_(0), g_modelInputWidth_(0), g_modelInputHeight_(0), + g_resizeOutWidthStride_(0), + g_resizeOutHeightStride_(0) +{ } -DvppProcess::~DvppProcess() { +DvppProcess::~DvppProcess() +{ DestroyResource(); DestroyOutputPara(); } -Result DvppProcess::InitResource() { +Result DvppProcess::InitResource() +{ g_dvppChannelDesc_ = acldvppCreateChannelDesc(); if (g_dvppChannelDesc_ == nullptr) { ERROR_LOG("acldvppCreateChannelDesc failed"); @@ -70,13 +75,15 @@ Result DvppProcess::InitResource() { return SUCCESS; } -void DvppProcess::SetInput(void *inDevBuffer, uint32_t inDevBufferSize, const PicDesc &picDesc) { +void DvppProcess::SetInput(void *inDevBuffer, uint32_t inDevBufferSize, const PicDesc &picDesc) +{ g_inDevBuffer_ = inDevBuffer; g_inDevBufferSize_ = inDevBufferSize; g_jpegDecodeOutputSize_ = picDesc.jpegDecodeSize; } -void DvppProcess::GetDvppOutput(void **outputBuffer, int &outputSize) { +void DvppProcess::GetDvppOutput(void **outputBuffer, int &outputSize) +{ if (outputBuffer == nullptr) { ERROR_LOG("outputBuffer is nullptr"); return; @@ -87,17 +94,19 @@ void DvppProcess::GetDvppOutput(void **outputBuffer, int &outputSize) { g_resizeOutBufferSize_ = 0; } -Result DvppProcess::InitDvppOutputPara(int modelInputWidth, int modelInputHeight) { +Result DvppProcess::InitDvppOutputPara(int modelInputWidth, int modelInputHeight) +{ if ((modelInputWidth <= 0) || (modelInputHeight <= 0)) { ERROR_LOG("InitInput para invalid, modelInputWidth %d, modelInputHeight %d", modelInputWidth, modelInputHeight); return FAILED; } - + int byte1 = 16; + int byte2 = 2; g_modelInputWidth_ = modelInputWidth; g_modelInputHeight_ = modelInputHeight; - g_resizeOutWidthStride_ = AlignSize(modelInputWidth, 16); // 16-byte alignment - g_resizeOutHeightStride_ = AlignSize(modelInputHeight, 2); // 2-byte alignment + g_resizeOutWidthStride_ = AlignSize(modelInputWidth, byte1); // 16-byte alignment + g_resizeOutHeightStride_ = AlignSize(modelInputHeight, byte2); // 2-byte alignment // output buffer, adjust the value based on the actual model g_resizeOutBufferSize_ = g_resizeOutWidthStride_ * g_resizeOutHeightStride_ * 3 / 2; // yuv format size @@ -110,14 +119,16 @@ Result DvppProcess::InitDvppOutputPara(int modelInputWidth, int modelInputHeight return SUCCESS; } -void DvppProcess::DestroyOutputPara() { +void DvppProcess::DestroyOutputPara() +{ if (g_resizeOutBufferDev_ != nullptr) { (void) acldvppFree(g_resizeOutBufferDev_); g_resizeOutBufferDev_ = nullptr; } } -Result DvppProcess::InitDecodeOutputDesc() { +Result DvppProcess::InitDecodeOutputDesc() +{ aclError ret = acldvppMalloc(&g_decodeOutDevBuffer_, g_jpegDecodeOutputSize_); if (ret != ACL_SUCCESS) { ERROR_LOG("acldvppMalloc jpegOutBufferDev failed, errorCode = %d", static_cast(ret)); @@ -136,7 +147,8 @@ Result DvppProcess::InitDecodeOutputDesc() { return SUCCESS; } -Result DvppProcess::ProcessDecode() { +Result DvppProcess::ProcessDecode() +{ // decode to yuv format aclError ret = acldvppJpegDecodeAsync(g_dvppChannelDesc_, g_inDevBuffer_, g_inDevBufferSize_, g_decodeOutputDesc_, g_stream_); @@ -157,14 +169,16 @@ Result DvppProcess::ProcessDecode() { return SUCCESS; } -void DvppProcess::DestroyDecodeResource() { +void DvppProcess::DestroyDecodeResource() +{ if (g_decodeOutputDesc_ != nullptr) { (void) acldvppDestroyPicDesc(g_decodeOutputDesc_); g_decodeOutputDesc_ = nullptr; } } -Result DvppProcess::InitResizeInputDesc() { +Result DvppProcess::InitResizeInputDesc() +{ uint32_t jpegOutWidthStride = g_decodeOutputWidthStride_; // 128-byte alignment on 310, 64-byte alignment on 310P uint32_t jpegOutHeightStride = AlignSize(g_decodeOutputHeight_, 16); // 16-byte alignment uint32_t jpegOutBufferSize = jpegOutWidthStride * jpegOutHeightStride * 3 / 2; // yuv format size @@ -184,7 +198,8 @@ Result DvppProcess::InitResizeInputDesc() { return SUCCESS; } -Result DvppProcess::InitResizeOutputDesc() { +Result DvppProcess::InitResizeOutputDesc() +{ g_resizeOutputDesc_ = acldvppCreatePicDesc(); if (g_resizeOutputDesc_ == nullptr) { ERROR_LOG("acldvppCreatePicDesc failed"); @@ -201,7 +216,8 @@ Result DvppProcess::InitResizeOutputDesc() { return SUCCESS; } -Result DvppProcess::ProcessResize() { +Result DvppProcess::ProcessResize() +{ // resize pic size aclError ret = acldvppVpcResizeAsync(g_dvppChannelDesc_, g_resizeInputDesc_, g_resizeOutputDesc_, g_resizeConfig_, g_stream_); @@ -219,7 +235,8 @@ Result DvppProcess::ProcessResize() { return SUCCESS; } -void DvppProcess::DestroyResizeResource() { +void DvppProcess::DestroyResizeResource() +{ if (g_decodeOutDevBuffer_ != nullptr) { (void) acldvppFree(g_decodeOutDevBuffer_); g_decodeOutDevBuffer_ = nullptr; @@ -236,7 +253,8 @@ void DvppProcess::DestroyResizeResource() { } } -void DvppProcess::DestroyResource() { +void DvppProcess::DestroyResource() +{ // g_resizeConfig_ is created in initResource if (g_resizeConfig_ != nullptr) { acldvppDestroyResizeConfig(g_resizeConfig_); @@ -254,7 +272,8 @@ void DvppProcess::DestroyResource() { } } -Result DvppProcess::Process() { +Result DvppProcess::Process() +{ // pic decode Result ret = InitDecodeOutputDesc(); if (ret != SUCCESS) { @@ -298,4 +317,4 @@ Result DvppProcess::Process() { INFO_LOG("Process dvpp success"); return SUCCESS; -} +} \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/main.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/main.cpp index 1bbb9a13a..7dd069935 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/main.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/main.cpp @@ -29,14 +29,15 @@ using namespace std; #define CHECK_FUNCTION_RESULT(name, result) \ { \ - if (result != SUCCESS) \ + if ((result) != SUCCESS) \ { \ ERROR_LOG(#name" failed"); \ return -1; \ } \ } -void BufferDeleter(void *p) { +void BufferDeleter(void *p) +{ if (!RunStatus::GetDeviceStatus()) { if (p != nullptr) { (void) aclrtFreeHost(p); @@ -51,13 +52,13 @@ void BufferDeleter(void *p) { * @param [in] xScale: x scale of the origin image and the model input image * @param [in] yScale: y scale of the origin image and the model input image */ -void ProcessOutput(const aclmdlDataset *output, const char *originImage, float xScale, float yScale) { +void ProcessOutput(const aclmdlDataset *output, const char *originImage, float xScale, float yScale) +{ FILE *outputFile = nullptr; shared_ptr dataBuff[2] = {nullptr, nullptr}; size_t index = 0; for (size_t i = 0; (i < aclmdlGetDatasetNumBuffers(output)) && (i < 2); ++i) { - std::string name; - name = "../out/output_" + to_string(i) + ".bin"; + std::string name = "../out/output_" + to_string(i) + ".bin"; if (outputFile != nullptr) { fclose(outputFile); } @@ -88,7 +89,6 @@ void ProcessOutput(const aclmdlDataset *output, const char *originImage, float x shared_ptr ptr(outHostData, BufferDeleter); dataBuff[i] = ptr; } - } if (outputFile != nullptr) { fclose(outputFile); @@ -116,7 +116,8 @@ void ProcessOutput(const aclmdlDataset *output, const char *originImage, float x */ Result GetImageResizeBuffer(const char *imageFile, int resizeWidth, int resizeHeight, void *&buffer, size_t &bufferLen, - float &xScale, float &yScale) { + float &xScale, float &yScale) +{ aclrtStream stream = nullptr; aclError ret = aclrtCreateStream(&stream); if (ret != ACL_SUCCESS) { @@ -178,8 +179,10 @@ Result GetImageResizeBuffer(const char *imageFile, int resizeWidth, int resizeHe * @param [in] argv: the value of arguments * @return process result, 0 is success, other is failure */ -int main(int argc, const char *argv[]) { - if ((argc < 3) || (argv[1] == nullptr) || (argv[2] == nullptr)) { +int main(int argc, const char *argv[]) +{ + int argNum = 3; + if ((argc < argNum) || (argv[1] == nullptr) || (argv[2] == nullptr)) { ERROR_LOG("Please input:./main "); return -1; } diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/model_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/model_process.cpp index ab2eaea13..532fcf000 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/model_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/model_process.cpp @@ -14,22 +14,26 @@ * limitations under the License. */ -#include "model_process.h" #include +#include "model_process.h" #include "utils.h" #include "acl/ops/acl_dvpp.h" using namespace std; -ModelProcess::ModelProcess() : g_loadFlag_(false), g_modelId_(0), g_modelDesc_(nullptr), g_input_(nullptr), g_output_(nullptr), - g_deviceId_(-1), g_initFlag_(false) { +ModelProcess::ModelProcess() + : g_loadFlag_(false), g_modelId_(0), g_modelDesc_(nullptr), + g_input_(nullptr), g_output_(nullptr), + g_deviceId_(-1), g_initFlag_(false) { } -ModelProcess::~ModelProcess() { +ModelProcess::~ModelProcess() +{ Finalize(); } -Result ModelProcess::Init(int deviceId) { +Result ModelProcess::Init(int deviceId) +{ // acl initialize aclError ret = aclInit(nullptr); if (ret != ACL_SUCCESS) { @@ -68,7 +72,8 @@ Result ModelProcess::Init(int deviceId) { return SUCCESS; } -void ModelProcess::Finalize() { +void ModelProcess::Finalize() +{ UnloadModel(); DestroyInput(); DestroyOutput(); @@ -90,7 +95,8 @@ void ModelProcess::Finalize() { INFO_LOG("finalize model process success."); } -Result ModelProcess::LoadModel(const char *modelPath) { +Result ModelProcess::LoadModel(const char *modelPath) +{ if (g_loadFlag_) { ERROR_LOG("model has already been loaded"); return FAILED; @@ -124,7 +130,8 @@ Result ModelProcess::LoadModel(const char *modelPath) { return SUCCESS; } -void ModelProcess::UnloadModel() { +void ModelProcess::UnloadModel() +{ if (!g_loadFlag_) { return; } @@ -143,7 +150,8 @@ void ModelProcess::UnloadModel() { INFO_LOG("unload model success, modelId is %u", g_modelId_); } -Result ModelProcess::CreateInput(void *inputDataBuffer, size_t bufferSize) { +Result ModelProcess::CreateInput(void *inputDataBuffer, size_t bufferSize) +{ // om used in this sample has only one input if (g_modelDesc_ == nullptr) { ERROR_LOG("no model description, create input failed"); @@ -183,7 +191,8 @@ Result ModelProcess::CreateInput(void *inputDataBuffer, size_t bufferSize) { return SUCCESS; } -void ModelProcess::DestroyInput() { +void ModelProcess::DestroyInput() +{ if (g_input_ == nullptr) { return; } @@ -199,7 +208,8 @@ void ModelProcess::DestroyInput() { INFO_LOG("destroy model input success"); } -Result ModelProcess::CreateOutput() { +Result ModelProcess::CreateOutput() +{ if (g_modelDesc_ == nullptr) { ERROR_LOG("no model description, create output failed"); return FAILED; @@ -242,7 +252,8 @@ Result ModelProcess::CreateOutput() { return SUCCESS; } -void ModelProcess::DestroyOutput() { +void ModelProcess::DestroyOutput() +{ if (g_output_ == nullptr) { return; } @@ -259,7 +270,8 @@ void ModelProcess::DestroyOutput() { INFO_LOG("destroy model output success"); } -Result ModelProcess::Execute() { +Result ModelProcess::Execute() +{ aclError ret = aclmdlExecute(g_modelId_, g_input_, g_output_); if (ret != ACL_SUCCESS) { ERROR_LOG("execute model failed, modelId is %u, errorCode is %d", g_modelId_, static_cast(ret)); @@ -274,7 +286,8 @@ const aclmdlDataset *ModelProcess::GetModelOutputData() { return g_output_; } -Result ModelProcess::GetModelInputWH(int &width, int &height) { +Result ModelProcess::GetModelInputWH(int &width, int &height) +{ if (g_modelDesc_ == nullptr) { ERROR_LOG("no model description, get input hw failed"); return FAILED; @@ -298,4 +311,4 @@ Result ModelProcess::GetModelInputWH(int &width, int &height) { INFO_LOG("model input width %d, input height %d", width, height); return SUCCESS; -} +} \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/post_process.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/post_process.cpp index b476817ee..76c018700 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/post_process.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/post_process.cpp @@ -14,8 +14,8 @@ * limitations under the License. */ -#include "post_process.h" #include +#include "post_process.h" #include "opencv2/opencv.hpp" #include "opencv2/imgproc/types_c.h" @@ -42,24 +42,25 @@ namespace { "teddy bear", "hair drier", "toothbrush"}; // bounding box line solid - const uint32_t kLineSolid = 2; + const uint32_t g_lineSolid = 2; // opencv draw label params. - const double kFountScale = 0.5; - const cv::Scalar kFontColor(0, 0, 255); - const uint32_t kLabelOffset = 11; - const size_t kClassNum = 80; - const size_t kModelOutputBoxNum = 10647; - const float kNMSThreshold = 0.8; - const float kScoreThreshold = 0.4; + const double g_fountScale = 0.5; + const cv::Scalar g_fontColor(0, 0, 255); + const uint32_t g_labelOffset = 11; + const size_t g_classNum = 80; + const size_t g_modelOutputBoxNum = 10647; + const float g_NMSThreshold = 0.8; + const float g_scoreThreshold = 0.4; // opencv color list for boundingbox - const vector kColors{ + const vector g_colors{ cv::Scalar(237, 149, 100), cv::Scalar(0, 215, 255), cv::Scalar(50, 205, 50), cv::Scalar(139, 85, 26)}; } -void PostProcess::SetBoxInfo(size_t index, BBox &box) { +void PostProcess::SetBoxInfo(size_t index, BBox &box) +{ float *boxBuff = static_cast(g_outputBox_); boxBuff += (index * sizeof(float)); box.x = boxBuff[0] * g_xScale_; @@ -68,11 +69,13 @@ void PostProcess::SetBoxInfo(size_t index, BBox &box) { box.h = boxBuff[3] * g_yScale_; } -bool PostProcess::SortScore(BBox box1, BBox box2) { +bool PostProcess::SortScore(BBox box1, BBox box2) +{ return box1.score > box2.score; } -float PostProcess::IOU(const BBox &b1, const BBox &b2) { +float PostProcess::IOU(const BBox &b1, const BBox &b2) +{ float x1 = max(b1.x, b2.x); float y1 = max(b1.y, b2.y); float x2 = min(b1.x + b1.w, b2.x + b2.w); @@ -83,7 +86,8 @@ float PostProcess::IOU(const BBox &b1, const BBox &b2) { return area / (b1.w * b1.h + b2.w * b2.h - area); } -void PostProcess::NMS(vector &boxes, vector &result) { +void PostProcess::NMS(vector &boxes, vector &result) +{ result.clear(); std::sort(boxes.begin(), boxes.end(), SortScore); @@ -92,7 +96,7 @@ void PostProcess::NMS(vector &boxes, vector &result) { size_t index = 1; while (boxes.size() > index) { float iou = IOU(boxes[0], boxes[index]); - if (iou > kNMSThreshold) { + if (iou > g_NMSThreshold) { boxes.erase(boxes.begin() + index); continue; } @@ -102,18 +106,20 @@ void PostProcess::NMS(vector &boxes, vector &result) { } } -void PostProcess::DrawBoundBoxToImage(const vector &result) { +void PostProcess::DrawBoundBoxToImage(const vector &result) +{ cv::Mat image = cv::imread(g_originImage_, CV_LOAD_IMAGE_UNCHANGED); + int half = 2; for (size_t i = 0; i < result.size(); ++i) { cv::Point p1, p2; - p1.x = result[i].x - result[i].w / 2; - p1.y = result[i].y - result[i].h / 2; - p2.x = result[i].x + result[i].w / 2; - p2.y = result[i].y + result[i].h / 2; - cv::rectangle(image, p1, p2, kColors[i % kColors.size()], kLineSolid); + p1.x = result[i].x - result[i].w / half; + p1.y = result[i].y - result[i].h / half; + p2.x = result[i].x + result[i].w / half; + p2.y = result[i].y + result[i].h / half; + cv::rectangle(image, p1, p2, g_colors[i % g_colors.size()], g_lineSolid); string className = kLabels[result[i].classIndex]; // classIndex is valid for kLabels - cv::putText(image, className, cv::Point(p1.x, p1.y + kLabelOffset), - cv::FONT_HERSHEY_COMPLEX, kFountScale, kFontColor); + cv::putText(image, className, cv::Point(p1.x, p1.y + g_labelOffset), + cv::FONT_HERSHEY_COMPLEX, g_fountScale, g_fontColor); } size_t pos = g_originImage_.find_last_of("/"); @@ -125,26 +131,27 @@ void PostProcess::DrawBoundBoxToImage(const vector &result) { INFO_LOG("running success, you can get the running result from the %s", sstream.str().c_str()); } -Result PostProcess::Process() { +Result PostProcess::Process() +{ vector boxes; float *classBuff = static_cast(g_outputClass_); - for (size_t i = 0; i < kModelOutputBoxNum; ++i) { + for (size_t i = 0; i < g_modelOutputBoxNum; ++i) { float maxValue = 0; float maxIndex = 0; - for (size_t j = 0; j < kClassNum; ++j) { - float value = classBuff[i * kClassNum + j]; + for (size_t j = 0; j < g_classNum; ++j) { + float value = classBuff[i * g_classNum + j]; if (value > maxValue) { maxIndex = j; maxValue = value; } } - if (maxValue >= kScoreThreshold) { + if (maxValue >= g_scoreThreshold) { BBox b; SetBoxInfo(i, b); b.score = maxValue; b.classIndex = maxIndex; b.index = i; - if (maxIndex < kClassNum) { + if (maxIndex < g_classNum) { boxes.push_back(b); } } diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/utils.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/utils.cpp index e49d7c9c0..e20554681 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/utils.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV4_coco_detection_picture/src/utils.cpp @@ -14,10 +14,10 @@ * limitations under the License. */ -#include "utils.h" #include #include #include +#include "utils.h" #if defined(_MSC_VER) #include @@ -30,7 +30,8 @@ bool RunStatus::isDevice_ = false; -Result Utils::ReadBinFile(PicDesc &picDesc, void *&inputBuff, uint32_t &fileSize) { +Result Utils::ReadBinFile(PicDesc &picDesc, void *&inputBuff, uint32_t &fileSize) +{ std::string fileName = picDesc.picName; if (CheckPathIsFile(fileName) == FAILED) { ERROR_LOG("%s is not a file", fileName.c_str()); @@ -86,7 +87,8 @@ Result Utils::ReadBinFile(PicDesc &picDesc, void *&inputBuff, uint32_t &fileSize return SUCCESS; } -Result Utils::GetDeviceBufferOfPicture(PicDesc &picDesc, void *&picDevBuffer, uint32_t &devPicBufferSize) { +Result Utils::GetDeviceBufferOfPicture(PicDesc &picDesc, void *&picDevBuffer, uint32_t &devPicBufferSize) +{ if (picDesc.picName.empty()) { ERROR_LOG("picture file name is empty"); return FAILED; @@ -148,7 +150,8 @@ Result Utils::GetDeviceBufferOfPicture(PicDesc &picDesc, void *&picDevBuffer, ui return SUCCESS; } -Result Utils::CheckPathIsFile(const std::string &fileName) { +Result Utils::CheckPathIsFile(const std::string &fileName) +{ #if defined(_MSC_VER) DWORD bRet = GetFileAttributes((LPCSTR)fileName.c_str()); if (bRet == FILE_ATTRIBUTE_DIRECTORY) { diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/CMakeLists.txt b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/inc/object_detect.h b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/inc/object_detect.h index 91571bec1..0f2e614b6 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/inc/object_detect.h +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/inc/object_detect.h @@ -19,11 +19,10 @@ #pragma once #include +#include #include "acl/acl.h" #include "acllite/AclLiteModel.h" #include "acllite/AclLiteUtils.h" -#include - #include "opencv2/imgproc/types_c.h" /** diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/inc/queue.h b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/inc/queue.h index 25a299637..471aa548b 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/inc/queue.h +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/inc/queue.h @@ -45,13 +45,16 @@ struct message_video { cv::Mat resultImage; }; +static const int APP_ERR_QUEUE_STOPED = 1; +static const int APP_ERR_QUEUE_EMPTY = 2; +static const int APP_ERROR_QUEUE_FULL = 3; static const int DEFAULT_MAX_QUEUE_SIZE = 128; -template class BlockingQueue { - public: - BlockingQueue(uint32_t maxSize = DEFAULT_MAX_QUEUE_SIZE) : maxSize_(maxSize), isStoped_(false) {} +template class Queue { +public: + Queue(uint32_t maxSize = DEFAULT_MAX_QUEUE_SIZE) : maxSize_(maxSize), isStoped_(false) {} - ~BlockingQueue() {} + ~Queue() {} int Pop(T &item) { @@ -62,13 +65,11 @@ template class BlockingQueue { } if (isStoped_) { - // APP_ERR_QUEUE_STOPED - return 1; + return APP_ERR_QUEUE_STOPED; } if (queue_.empty()) { - // APP_ERR_QUEUE_EMPTY - return 2; + return APP_ERR_QUEUE_EMPTY; } else { item = queue_.front(); queue_.pop_front(); @@ -89,13 +90,11 @@ template class BlockingQueue { } if (isStoped_) { - // APP_ERR_QUEUE_STOPED - return 1; + return APP_ERR_QUEUE_STOPED; } if (queue_.empty()) { - // APP_ERR_QUEUE_EMPTY - return 2; + return APP_ERR_QUEUE_EMPTY; } else { item = queue_.front(); queue_.pop_front(); @@ -115,13 +114,11 @@ template class BlockingQueue { } if (isStoped_) { - // APP_ERR_QUEUE_STOPED return 1; } if (queue_.size() >= maxSize_) { - // APP_ERROR_QUEUE_FULL - return 3; + return APP_ERROR_QUEUE_FULL; } queue_.push_back(item); @@ -139,13 +136,11 @@ template class BlockingQueue { } if (isStoped_) { - // APP_ERR_QUEUE_STOPED - return 1; + return APP_ERR_QUEUE_STOPED; } if (queue_.size() >= maxSize_) { - // APP_ERROR_QUEUE_FULL - return 3; + return APP_ERROR_QUEUE_FULL; } queue_.push_front(item); @@ -189,13 +184,11 @@ template class BlockingQueue { int GetBackItem(T &item) { if (isStoped_) { - // APP_ERR_QUEUE_STOPED - return 1; + return APP_ERR_QUEUE_STOPED; } if (queue_.empty()) { - // APP_ERR_QUEUE_EMPTY - return 2; + return APP_ERR_QUEUE_EMPTY; } item = queue_.back(); @@ -229,7 +222,7 @@ template class BlockingQueue { queue_.clear(); } - private: +private: std::list queue_; std::mutex mutex_; std::condition_variable emptyCond_; diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/src/acl.json b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/src/acl.json old mode 100755 new mode 100644 diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/src/main.cpp b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/src/main.cpp index d66710bf1..dda68c857 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/src/main.cpp +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/src/main.cpp @@ -16,63 +16,65 @@ #include #include -#include "acllite/AclLiteUtils.h" -#include "acllite/AclLiteResource.h" -#include "queue.h" -#include "acl/acl.h" #include #include #include #include #include +#include +#include "acllite/AclLiteUtils.h" +#include "acllite/AclLiteResource.h" +#include "queue.h" +#include "acl/acl.h" #include "object_detect.h" #include "opencv2/opencv.hpp" #include "opencv2/videoio.hpp" -#include -#define VIDEONUM 4 +const int VIDEONUM = 4; using namespace std; struct timespec g_time1 = {0, 0}; struct timespec g_time2 = {0, 0}; namespace { - uint32_t g_ModelWidth = 416; - uint32_t g_ModelHeight = 416; - const char* g_ModelPath = "../model/yolov3.om"; + uint32_t g_modelWidth = 416; + uint32_t g_modelHeight = 416; + const char* g_modelPath = "../model/yolov3.om"; const static std::vector g_yolov3Label = { "person", "bicycle", "car", "motorbike", - "aeroplane","bus", "train", "truck", "boat", - "traffic light", "fire hydrant", "stop sign", "parking meter", - "bench", "bird", "cat", "dog", "horse", - "sheep", "cow", "elephant", "bear", "zebra", - "giraffe", "backpack", "umbrella", "handbag","tie", - "suitcase", "frisbee", "skis", "snowboard", "sports ball", - "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", - "tennis racket", "bottle", "wine glass", "cup", - "fork", "knife", "spoon", "bowl", "banana", - "apple", "sandwich", "orange", "broccoli", "carrot", - "hot dog", "pizza", "donut", "cake", "chair", - "sofa", "potted plant", "bed", "dining table", "toilet", - "TV monitor", "laptop", "mouse", "remote", "keyboard", - "cell phone", "microwave", "oven", "toaster", "sink", - "refrigerator", "book", "clock", "vase","scissors", - "teddy bear", "hair drier", "toothbrush" }; - const uint32_t g_BBoxDataBufId = 0; - const uint32_t g_BoxNumDataBufId = 1; + "aeroplane", "bus", "train", "truck", "boat", + "traffic light", "fire hydrant", "stop sign", "parking meter", + "bench", "bird", "cat", "dog", "horse", + "sheep", "cow", "elephant", "bear", "zebra", + "giraffe", "backpack", "umbrella", "handbag","tie", + "suitcase", "frisbee", "skis", "snowboard", "sports ball", + "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", + "tennis racket", "bottle", "wine glass", "cup", + "fork", "knife", "spoon", "bowl", "banana", + "apple", "sandwich", "orange", "broccoli", "carrot", + "hot dog", "pizza", "donut", "cake", "chair", + "sofa", "potted plant", "bed", "dining table", "toilet", + "TV monitor", "laptop", "mouse", "remote", "keyboard", + "cell phone", "microwave", "oven", "toaster", "sink", + "refrigerator", "book", "clock", "vase", "scissors", + "teddy bear", "hair drier", "toothbrush" }; + const uint32_t g_bBoxDataBufId = 0; + const uint32_t g_boxNumDataBufId = 1; enum BBoxIndex { TOPLEFTX = 0, TOPLEFTY, BOTTOMRIGHTX, BOTTOMRIGHTY, SCORE, LABEL }; - const uint32_t g_LineSolid = 2; - const string g_OutputFilePrefix = "out_"; - const double g_FountScale = 0.5; - const cv::Scalar g_FontColor(0, 0, 255); - const uint32_t g_LabelOffset = 11; - const string g_FileSperator = "/"; - const vector g_Colors { + const uint32_t g_lineSolid = 2; + const int timep = 1000; + const string g_outputFilePrefix = "out_"; + const double FOUNT_SCALE = 0.5; + const cv::Scalar g_fontColor(0, 0, 255); + const uint32_t g_labelOffset = 11; + const string g_fileSperator = "/"; + const vector g_colors { cv::Scalar(237, 149, 100), cv::Scalar(0, 215, 255), cv::Scalar(50, 205, 50), cv::Scalar(139, 85, 26) }; } -int g_video_need_postprocess = VIDEONUM; -void Preprocess(cv::VideoCapture capture, aclrtContext context, - BlockingQueue* queue_pre, int threadNum) { +int g_videoNeedPostprocess = VIDEONUM; +void Preprocess(cv::VideoCapture capture, aclrtContext context, + Queue* queue_pre, int threadNum) +{ message_pre premsg; aclrtSetCurrentContext(context); @@ -86,7 +88,7 @@ void Preprocess(cv::VideoCapture capture, aclrtContext context, premsg.videoIndex = threadNum; premsg.isLastFrame = 1; cv::Mat reiszeMat; - cv::resize(frame, reiszeMat, cv::Size(g_ModelWidth, g_ModelHeight)); + cv::resize(frame, reiszeMat, cv::Size(g_modelWidth, g_modelHeight)); if (reiszeMat.empty()) { ACLLITE_LOG_ERROR("Resize image failed"); break; @@ -94,9 +96,8 @@ void Preprocess(cv::VideoCapture capture, aclrtContext context, premsg.reiszeMat = reiszeMat; while (1) { if (queue_pre->Push(premsg) != 0) { - usleep(1000); - } - else + usleep(timep); + } else break; } } @@ -105,25 +106,24 @@ void Preprocess(cv::VideoCapture capture, aclrtContext context, premsg.isLastFrame = 0; while (1) { if (queue_pre->Push(premsg) != 0) { - usleep(1000); - } - else { + usleep(timep); + } else { ACLLITE_LOG_INFO("preprocess end"); break; } } } -void Postprocess(aclrtContext context, BlockingQueue* queue_post) { - +void Postprocess(aclrtContext context, Queue* queue_post) +{ message postmsg; uint32_t* boxNum = nullptr; float* detectData = nullptr; aclrtSetCurrentContext(context); while (1) { - if(queue_post->Pop(postmsg) != 0) { - usleep(1000); + if (queue_post->Pop(postmsg) != 0) { + usleep(timep); continue; } @@ -139,13 +139,14 @@ void Postprocess(aclrtContext context, BlockingQueue* queue_post) { } uint32_t totalBox = boxNum[0]; vector detectResults; - float widthScale = (float)(frame.cols) / g_ModelWidth; - float heightScale = (float)(frame.rows) / g_ModelHeight; + float widthScale = (float)(frame.cols) / g_modelWidth; + float heightScale = (float)(frame.rows) / g_modelHeight; for (uint32_t i = 0; i < totalBox; i++) { BBox boundBox; uint32_t score = uint32_t(detectData[totalBox * SCORE + i] * 100); - if (score < 90) { + uint32_t scoreLine = 90; + if (score < scoreLine) { continue; } boundBox.rect.ltX = detectData[totalBox * TOPLEFTX + i] * widthScale; @@ -165,11 +166,12 @@ void Postprocess(aclrtContext context, BlockingQueue* queue_post) { cout << detectResults[i].text << endl; } } - g_video_need_postprocess--; + g_videoNeedPostprocess--; ACLLITE_LOG_INFO("postprocess end"); } -int main(int argc, char *argv[]) { +int main(int argc, char *argv[]) +{ // init acl resource AclLiteResource aclDev; AclLiteError ret = aclDev.Init(); @@ -178,7 +180,7 @@ int main(int argc, char *argv[]) { return ACLLITE_ERROR; } - ObjectDetect detect(g_ModelPath, g_ModelWidth, g_ModelHeight); + ObjectDetect detect(g_modelPath, g_modelWidth, g_modelHeight); ret = detect.Init(); if (ret != ACLLITE_OK) { @@ -204,8 +206,8 @@ int main(int argc, char *argv[]) { return 1; } - BlockingQueuequeue_pre; - BlockingQueuequeue_post[VIDEONUM]; + Queuequeue_pre; + Queuequeue_post[VIDEONUM]; thread task1(Preprocess, ref(capture1), ref(context), &queue_pre, 0); thread task2(Preprocess, ref(capture2), ref(context), &queue_pre, 1); @@ -228,28 +230,25 @@ int main(int argc, char *argv[]) { message_pre premsg; message msg; - int video_need_preprocess = VIDEONUM; + int videoNeedPreprocess = VIDEONUM; clock_gettime(CLOCK_REALTIME, &g_time1); while (1) { if (queue_pre.Pop(premsg) != 0) { - usleep(1000); + usleep(timep); continue; } msg.videoIndex = premsg.videoIndex; msg.isLastFrame = premsg.isLastFrame; - if (msg.isLastFrame == 0) - { - video_need_preprocess = video_need_preprocess - 1; + if (msg.isLastFrame == 0) { + videoNeedPreprocess = videoNeedPreprocess - 1; } - if (video_need_preprocess == 0) - { + if (videoNeedPreprocess == 0) { while (1) { int i = msg.videoIndex; if (queue_post[i].Push(msg) != 0) { - usleep(1000); - } - else { + usleep(timep); + } else { break; } } @@ -262,31 +261,29 @@ int main(int argc, char *argv[]) { ACLLITE_LOG_ERROR("Inference model inference output data failed"); return 1; } - msg.detectData = inferenceOutput[g_BBoxDataBufId].data; - msg.boxNum = inferenceOutput[g_BoxNumDataBufId].data; + msg.detectData = inferenceOutput[g_bBoxDataBufId].data; + msg.boxNum = inferenceOutput[g_boxNumDataBufId].data; int i = msg.videoIndex; while (1) { if (queue_post[i].Push(msg) != 0) { - usleep(1000); - } - else { + usleep(timep); + } else { break; } } } clock_gettime(CLOCK_REALTIME, &g_time2); - cout << "Execute time passed is: " << (g_time2.tv_sec - g_time1.tv_sec) * 1000 + cout << "Execute time passed is: " << (g_time2.tv_sec - g_time1.tv_sec) * timep + (g_time2.tv_nsec - g_time1.tv_nsec) / 1000000 << "ms" << endl; while (1) { - if (!g_video_need_postprocess) { + if (!g_videoNeedPostprocess) { break; } } - usleep(1000); + usleep(timep); ACLLITE_LOG_INFO("Execute sample success"); return ACLLITE_OK; -} - +} \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/src/object_detect.cpp b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/src/object_detect.cpp index 0e68da078..819b05020 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/src/object_detect.cpp +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_4_thread/src/object_detect.cpp @@ -16,8 +16,8 @@ #include #include -#include "acl/acl.h" #include +#include "acl/acl.h" #include "acllite/AclLiteModel.h" #include "object_detect.h" #include "opencv2/opencv.hpp" @@ -25,38 +25,41 @@ using namespace std; ObjectDetect::ObjectDetect(const char* modelPath, uint32_t modelWidth, -uint32_t modelHeight) -: g_modelWidth_(modelWidth), g_modelHeight_(modelHeight), g_isInited_(false), - g_imageDataBuf_(nullptr), g_imageInfoBuf_(nullptr) { - g_imageInfoSize_ = 0; - g_modelPath_ = modelPath; + uint32_t modelHeight) + : g_modelWidth_(modelWidth), g_modelHeight_(modelHeight), g_isInited_(false), + g_imageDataBuf_(nullptr), g_imageInfoBuf_(nullptr) +{ + g_imageInfoSize_ = 0; + g_modelPath_ = modelPath; } -ObjectDetect::~ObjectDetect() { +ObjectDetect::~ObjectDetect() +{ DestroyResource(); } -AclLiteError ObjectDetect::CreateInput() { +AclLiteError ObjectDetect::CreateInput() +{ // Request image data memory for input model - aclError aclRet = aclrtMalloc(& g_imageDataBuf_, (size_t)( g_imageDataSize_), ACL_MEM_MALLOC_HUGE_FIRST); + aclError aclRet = aclrtMalloc(&g_imageDataBuf_, (size_t)(g_imageDataSize_), ACL_MEM_MALLOC_HUGE_FIRST); if (aclRet != ACL_SUCCESS) { ACLLITE_LOG_ERROR("malloc device data buffer failed, aclRet is %d", aclRet); return ACLLITE_ERROR; } // The second input to Yolov3 is the input image width and height parameter - const float imageInfo[4] = {(float) g_modelWidth_, (float) g_modelHeight_, - (float) g_modelWidth_, (float) g_modelHeight_}; + const float imageInfo[4] = {(float)g_modelWidth_, (float)g_modelHeight_, + (float)g_modelWidth_, (float)g_modelHeight_}; g_imageInfoSize_ = sizeof(imageInfo); g_imageInfoBuf_ = CopyDataToDevice((void *)imageInfo, g_imageInfoSize_, - g_runMode_, MEMORY_DEVICE); - if ( g_imageInfoBuf_ == nullptr) { + g_runMode_, MEMORY_DEVICE); + if (g_imageInfoBuf_ == nullptr) { ACLLITE_LOG_ERROR("Copy image info to device failed"); return ACLLITE_ERROR; } - AclLiteError ret = g_model_.CreateInput( g_imageDataBuf_, g_imageDataSize_, - g_imageInfoBuf_, g_imageInfoSize_); + AclLiteError ret = g_model_.CreateInput(g_imageDataBuf_, g_imageDataSize_, + g_imageInfoBuf_, g_imageInfoSize_); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Create mode input dataset failed"); return ACLLITE_ERROR; @@ -65,9 +68,10 @@ AclLiteError ObjectDetect::CreateInput() { return ACLLITE_OK; } -AclLiteError ObjectDetect::Init() { +AclLiteError ObjectDetect::Init() +{ // If it is already initialized, it is returned - if ( g_isInited_) { + if (g_isInited_) { ACLLITE_LOG_INFO("Classify instance is initied already!"); return ACLLITE_OK; } @@ -86,7 +90,7 @@ AclLiteError ObjectDetect::Init() { return ACLLITE_ERROR; } - g_imageDataSize_ = g_model_.GetModelInputSize(0); + g_imageDataSize_ = g_model_.GetModelInputSize(0); ret = CreateInput(); if (ret != ACLLITE_OK) { @@ -94,15 +98,15 @@ AclLiteError ObjectDetect::Init() { return ACLLITE_ERROR; } - g_isInited_ = true; + g_isInited_ = true; return ACLLITE_OK; } -AclLiteError ObjectDetect::Inference(std::vector& inferenceOutput, cv::Mat& reiszeMat) { - - AclLiteError ret = CopyDataToDeviceEx( g_imageDataBuf_, g_imageDataSize_, - reiszeMat.ptr(), g_imageDataSize_, - g_runMode_); +AclLiteError ObjectDetect::Inference(std::vector& inferenceOutput, cv::Mat& reiszeMat) +{ + AclLiteError ret = CopyDataToDeviceEx(g_imageDataBuf_, g_imageDataSize_, + reiszeMat.ptr(), g_imageDataSize_, + g_runMode_); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Copy resized image data to device failed."); return ACLLITE_ERROR; @@ -118,11 +122,11 @@ AclLiteError ObjectDetect::Inference(std::vector& inferenceOutp return ACLLITE_OK; } -void ObjectDetect::DestroyResource() { - g_model_.DestroyInput(); - g_model_.DestroyResource(); - - aclrtFree( g_imageDataBuf_); - aclrtFree( g_imageInfoBuf_); -} +void ObjectDetect::DestroyResource() +{ + g_model_.DestroyInput(); + g_model_.DestroyResource(); + aclrtFree(g_imageDataBuf_); + aclrtFree(g_imageInfoBuf_); +} \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/CMakeLists.txt b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/inc/object_detect.h b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/inc/object_detect.h index 8db19f5b8..86085d6c4 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/inc/object_detect.h +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/inc/object_detect.h @@ -19,18 +19,17 @@ #pragma once #include +#include #include "acl/acl.h" #include "acllite/AclLiteModel.h" #include "acllite/AclLiteUtils.h" -#include - #include "opencv2/imgproc/types_c.h" /** * ObjectDetect */ class ObjectDetect { - public: +public: ObjectDetect(const char* modelPath, uint32_t modelWidth, uint32_t modelHeight); @@ -42,7 +41,7 @@ class ObjectDetect { void DestroyResource(); - private: +private: AclLiteError CreateInput(); AclLiteModel g_model_; diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/inc/queue.h b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/inc/queue.h index f73ac3fa8..95c5b0ef7 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/inc/queue.h +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/inc/queue.h @@ -37,11 +37,11 @@ struct message { static const int DEFAULT_MAX_QUEUE_SIZE = 5; -template class BlockingQueue { +template class Queue { public: - BlockingQueue(uint32_t maxSize = DEFAULT_MAX_QUEUE_SIZE) : maxSize_(maxSize), isStoped_(false) {} + Queue(uint32_t maxSize = DEFAULT_MAX_QUEUE_SIZE) : maxSize_(maxSize), isStoped_(false) {} - ~BlockingQueue() {} + ~Queue() {} int Pop(T &item) { diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/src/acl.json b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/src/acl.json old mode 100755 new mode 100644 diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/src/main.cpp b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/src/main.cpp index 20e203c60..353352e53 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/src/main.cpp +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/src/main.cpp @@ -33,42 +33,45 @@ using namespace std; namespace { - uint32_t g_ModelWidth = 416; - uint32_t g_ModelHeight = 416; - const char* g_ModelPath = "../model/yolov3.om"; + uint32_t g_modelWidth = 416; + uint32_t g_modelHeight = 416; + const char* g_modelPath = "../model/yolov3.om"; const static std::vector g_yolov3Label = { "person", "bicycle", "car", "motorbike", - "aeroplane","bus", "train", "truck", "boat", - "traffic light", "fire hydrant", "stop sign", "parking meter", - "bench", "bird", "cat", "dog", "horse", - "sheep", "cow", "elephant", "bear", "zebra", - "giraffe", "backpack", "umbrella", "handbag","tie", - "suitcase", "frisbee", "skis", "snowboard", "sports ball", - "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", - "tennis racket", "bottle", "wine glass", "cup", - "fork", "knife", "spoon", "bowl", "banana", - "apple", "sandwich", "orange", "broccoli", "carrot", - "hot dog", "pizza", "donut", "cake", "chair", - "sofa", "potted plant", "bed", "dining table", "toilet", - "TV monitor", "laptop", "mouse", "remote", "keyboard", - "cell phone", "microwave", "oven", "toaster", "sink", - "refrigerator", "book", "clock", "vase","scissors", - "teddy bear", "hair drier", "toothbrush" }; - const uint32_t g_BBoxDataBufId = 0; - const uint32_t g_BoxNumDataBufId = 1; + "aeroplane", "bus", "train", "truck", "boat", + "traffic light", "fire hydrant", "stop sign", "parking meter", + "bench", "bird", "cat", "dog", "horse", + "sheep", "cow", "elephant", "bear", "zebra", + "giraffe", "backpack", "umbrella", "handbag","tie", + "suitcase", "frisbee", "skis", "snowboard", "sports ball", + "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", + "tennis racket", "bottle", "wine glass", "cup", + "fork", "knife", "spoon", "bowl", "banana", + "apple", "sandwich", "orange", "broccoli", "carrot", + "hot dog", "pizza", "donut", "cake", "chair", + "sofa", "potted plant", "bed", "dining table", "toilet", + "TV monitor", "laptop", "mouse", "remote", "keyboard", + "cell phone", "microwave", "oven", "toaster", "sink", + "refrigerator", "book", "clock", "vase", "scissors", + "teddy bear", "hair drier", "toothbrush" }; + const uint32_t g_bBoxDataBufId = 0; + const uint32_t g_boxNumDataBufId = 1; enum BBoxIndex { TOPLEFTX = 0, TOPLEFTY, BOTTOMRIGHTX, BOTTOMRIGHTY, SCORE, LABEL }; - const uint32_t g_LineSolid = 2; - const string g_OutputFilePrefix = "out_"; - const double g_FountScale = 0.5; - const cv::Scalar g_FontColor(0, 0, 255); - const uint32_t g_LabelOffset = 11; - const string g_FileSperator = "/"; - const vector g_Colors { + const uint32_t g_lineSolid = 2; + const int timep1 = 1000; + const int timep2 = 5000; + const string g_outputFilePrefix = "out_"; + const double FOUNT_SCALE = 0.5; + const cv::Scalar g_fontColor(0, 0, 255); + const uint32_t g_labelOffset = 11; + const string g_fileSperator = "/"; + const vector g_colors { cv::Scalar(237, 149, 100), cv::Scalar(0, 215, 255), cv::Scalar(50, 205, 50), cv::Scalar(139, 85, 26) }; } -int g_video_need_postprocess = VIDEONUM; +int g_videoNeedPostprocess = VIDEONUM; void Preprocess(cv::VideoCapture capture, aclrtContext context, - BlockingQueue* queue_pre, int threadNum) { + Queue* queue_pre, int threadNum) +{ message premsg; aclrtSetCurrentContext(context); @@ -82,7 +85,7 @@ void Preprocess(cv::VideoCapture capture, aclrtContext context, premsg.videoIndex = threadNum; premsg.isLastFrame = 1; cv::Mat reiszeMat; - cv::resize(frame, reiszeMat, cv::Size(g_ModelWidth, g_ModelHeight)); + cv::resize(frame, reiszeMat, cv::Size(g_modelWidth, g_modelHeight)); if (reiszeMat.empty()) { ACLLITE_LOG_ERROR("Resize image failed"); break; @@ -90,9 +93,8 @@ void Preprocess(cv::VideoCapture capture, aclrtContext context, premsg.reiszeMat = reiszeMat; while (1) { if (queue_pre->Push(premsg) != 0) { - usleep(1000); - } - else + usleep(timep1); + } else break; } } @@ -101,7 +103,7 @@ void Preprocess(cv::VideoCapture capture, aclrtContext context, premsg.isLastFrame = 0; while (1) { if (queue_pre->Push(premsg) != 0) { - usleep(1000); + usleep(timep1); } else { ACLLITE_LOG_INFO("preprocess end"); @@ -110,7 +112,7 @@ void Preprocess(cv::VideoCapture capture, aclrtContext context, } } -void Postprocess(cv::VideoWriter& outputVideo, aclrtContext context, BlockingQueue* queue_post) { +void Postprocess(cv::VideoWriter& outputVideo, aclrtContext context, Queue* queue_post) { message postmsg; uint32_t* boxNum = nullptr; float* detectData = nullptr; @@ -118,9 +120,8 @@ void Postprocess(cv::VideoWriter& outputVideo, aclrtContext context, BlockingQue while (1) { - if (queue_post->Pop(postmsg) != 0) - { - usleep(1000); + if (queue_post->Pop(postmsg) != 0) { + usleep(timep1); continue; } @@ -136,12 +137,13 @@ void Postprocess(cv::VideoWriter& outputVideo, aclrtContext context, BlockingQue } uint32_t totalBox = boxNum[0]; vector detectResults; - float widthScale = (float)(frame.cols) / g_ModelWidth; - float heightScale = (float)(frame.rows) / g_ModelHeight; + float widthScale = (float)(frame.cols) / g_modelWidth; + float heightScale = (float)(frame.rows) / g_modelHeight; for (uint32_t i = 0; i < totalBox; i++) { BBox boundBox; uint32_t score = uint32_t(detectData[totalBox * SCORE + i] * 100); - if (score < 90) { + uint32_t scoreLine = 90; + if (score < scoreLine) { continue; } boundBox.rect.ltX = detectData[totalBox * TOPLEFTX + i] * widthScale; @@ -159,15 +161,15 @@ void Postprocess(cv::VideoWriter& outputVideo, aclrtContext context, BlockingQue p1.y = detectResults[i].rect.ltY; p2.x = detectResults[i].rect.rbX; p2.y = detectResults[i].rect.rbY; - cv::rectangle(frame, p1, p2, g_Colors[i % g_Colors.size()], g_LineSolid); - cv::putText(frame, detectResults[i].text, cv::Point(p1.x, p1.y + g_LabelOffset), - cv::FONT_HERSHEY_COMPLEX, g_FountScale, g_FontColor); + cv::rectangle(frame, p1, p2, g_Colors[i % g_Colors.size()], g_lineSolid); + cv::putText(frame, detectResults[i].text, cv::Point(p1.x, p1.y + g_labelOffset), + cv::FONT_HERSHEY_COMPLEX, FOUNT_SCALE, g_fontColor); } outputVideo << frame; } - g_video_need_postprocess--; + g_videoNeedPostprocess--; outputVideo.release(); ACLLITE_LOG_INFO("postprocess end"); } @@ -180,7 +182,7 @@ int main(int argc, char *argv[]) { ACLLITE_LOG_ERROR("Init resource failed, error %d", ret); return ACLLITE_ERROR; } - ObjectDetect detect(g_ModelPath, g_ModelWidth, g_ModelHeight); + ObjectDetect detect(g_modelPath, g_modelWidth, g_modelHeight); ret = detect.Init(); if (ret != ACLLITE_OK) { @@ -209,8 +211,8 @@ int main(int argc, char *argv[]) { cv::Size(static_cast(capture2.get(cv::CAP_PROP_FRAME_WIDTH)), static_cast(capture2.get(cv::CAP_PROP_FRAME_HEIGHT)))); - BlockingQueuequeue_pre; - BlockingQueuequeue_post[VIDEONUM]; + Queuequeue_pre; + Queuequeue_post[VIDEONUM]; thread task1(Preprocess, ref(capture1), ref(context), &queue_pre, 0); thread task2(Preprocess, ref(capture2), ref(context), &queue_pre, 1); @@ -226,7 +228,7 @@ int main(int argc, char *argv[]) { while (1) { if (queue_pre.Pop(msg) != 0) { - usleep(1000); + usleep(timep1); continue; } if (msg.isLastFrame == 0) @@ -238,9 +240,8 @@ int main(int argc, char *argv[]) { while (1) { int i = msg.videoIndex; if (queue_post[i].Push(msg) != 0) { - usleep(1000); - } - else { + usleep(timep1); + } else { break; } } @@ -252,25 +253,24 @@ int main(int argc, char *argv[]) { ACLLITE_LOG_ERROR("Inference model inference output data failed"); return 1; } - msg.detectData = inferenceOutput[g_BBoxDataBufId].data; - msg.boxNum = inferenceOutput[g_BoxNumDataBufId].data; + msg.detectData = inferenceOutput[g_bBoxDataBufId].data; + msg.boxNum = inferenceOutput[g_boxNumDataBufId].data; int i = msg.videoIndex; while (1) { if (queue_post[i].Push(msg) != 0) { - usleep(1000); - } - else { + usleep(timep1); + } else { break; } } } while (1) { - if (!g_video_need_postprocess) { + if (!g_videoNeedPostprocess) { break; } } - usleep(5000); + usleep(timep2); ACLLITE_LOG_INFO("Execute sample success"); return ACLLITE_OK; } diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/src/object_detect.cpp b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/src/object_detect.cpp index b82f7ffa1..819b05020 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/src/object_detect.cpp +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/src/object_detect.cpp @@ -16,6 +16,7 @@ #include #include +#include #include "acl/acl.h" #include "acllite/AclLiteModel.h" #include "object_detect.h" @@ -24,18 +25,21 @@ using namespace std; ObjectDetect::ObjectDetect(const char* modelPath, uint32_t modelWidth, -uint32_t modelHeight) -: g_modelWidth_(modelWidth), g_modelHeight_(modelHeight), g_isInited_(false), - g_imageDataBuf_(nullptr), g_imageInfoBuf_(nullptr) { + uint32_t modelHeight) + : g_modelWidth_(modelWidth), g_modelHeight_(modelHeight), g_isInited_(false), + g_imageDataBuf_(nullptr), g_imageInfoBuf_(nullptr) +{ g_imageInfoSize_ = 0; g_modelPath_ = modelPath; } -ObjectDetect::~ObjectDetect() { +ObjectDetect::~ObjectDetect() +{ DestroyResource(); } -AclLiteError ObjectDetect::CreateInput() { +AclLiteError ObjectDetect::CreateInput() +{ // Request image data memory for input model aclError aclRet = aclrtMalloc(&g_imageDataBuf_, (size_t)(g_imageDataSize_), ACL_MEM_MALLOC_HUGE_FIRST); if (aclRet != ACL_SUCCESS) { @@ -45,17 +49,17 @@ AclLiteError ObjectDetect::CreateInput() { // The second input to Yolov3 is the input image width and height parameter const float imageInfo[4] = {(float)g_modelWidth_, (float)g_modelHeight_, - (float)g_modelWidth_, (float)g_modelHeight_}; - g_imageInfoSize_ = sizeof(imageInfo); - g_imageInfoBuf_ = CopyDataToDevice((void *)imageInfo, g_imageInfoSize_, + (float)g_modelWidth_, (float)g_modelHeight_}; + g_imageInfoSize_ = sizeof(imageInfo); + g_imageInfoBuf_ = CopyDataToDevice((void *)imageInfo, g_imageInfoSize_, g_runMode_, MEMORY_DEVICE); if (g_imageInfoBuf_ == nullptr) { ACLLITE_LOG_ERROR("Copy image info to device failed"); return ACLLITE_ERROR; } - AclLiteError ret = g_model_.CreateInput(g_imageDataBuf_, g_imageDataSize_, - g_imageInfoBuf_, g_imageInfoSize_); + AclLiteError ret = g_model_.CreateInput(g_imageDataBuf_, g_imageDataSize_, + g_imageInfoBuf_, g_imageInfoSize_); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Create mode input dataset failed"); return ACLLITE_ERROR; @@ -64,7 +68,8 @@ AclLiteError ObjectDetect::CreateInput() { return ACLLITE_OK; } -AclLiteError ObjectDetect::Init() { +AclLiteError ObjectDetect::Init() +{ // If it is already initialized, it is returned if (g_isInited_) { ACLLITE_LOG_INFO("Classify instance is initied already!"); @@ -72,14 +77,14 @@ AclLiteError ObjectDetect::Init() { } // Gets whether the current application is running on host or Device - AclLiteError ret = aclrtGetRunMode(&g_runMode_); + AclLiteError ret = aclrtGetRunMode(& g_runMode_); if (ret != ACL_SUCCESS) { ACLLITE_LOG_ERROR("acl get run mode failed"); return ACLLITE_ERROR; } // Initializes the model management instance - ret = g_model_.Init(g_modelPath_); + ret = g_model_.Init( g_modelPath_); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Init model failed"); return ACLLITE_ERROR; @@ -97,18 +102,17 @@ AclLiteError ObjectDetect::Init() { return ACLLITE_OK; } -AclLiteError ObjectDetect::Inference(std::vector& inferenceOutput, cv::Mat& reiszeMat) { - - AclLiteError ret = CopyDataToDeviceEx(g_imageDataBuf_, g_imageDataSize_, - reiszeMat.ptr(), g_imageDataSize_, - g_runMode_); +AclLiteError ObjectDetect::Inference(std::vector& inferenceOutput, cv::Mat& reiszeMat) +{ + AclLiteError ret = CopyDataToDeviceEx(g_imageDataBuf_, g_imageDataSize_, + reiszeMat.ptr(), g_imageDataSize_, + g_runMode_); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Copy resized image data to device failed."); return ACLLITE_ERROR; } - // Perform reasoning - ret = g_model_.Execute(inferenceOutput); + ret = g_model_.Execute(inferenceOutput); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Execute model inference failed"); return ACLLITE_ERROR; @@ -118,10 +122,11 @@ AclLiteError ObjectDetect::Inference(std::vector& inferenceOutp return ACLLITE_OK; } -void ObjectDetect::DestroyResource() { +void ObjectDetect::DestroyResource() +{ g_model_.DestroyInput(); g_model_.DestroyResource(); aclrtFree(g_imageDataBuf_); aclrtFree(g_imageInfoBuf_); -} +} \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/inc/object_detection.h b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/inc/object_detection.h index d2524dee5..0ec9df56f 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/inc/object_detection.h +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/inc/object_detection.h @@ -25,13 +25,14 @@ #include #include #include +#include #include "AclLiteType.h" #include "acllite/AclLiteModel.h" #include "acllite/AclLiteImageProc.h" #include #include "opencv2/opencv.hpp" #include "opencv2/imgproc/types_c.h" -#include + #define RGBF32_CHAN_SIZE(width, height) ((width) * (height) * 4) using namespace std; @@ -48,10 +49,10 @@ const int MSG_PREPROC_END = 8; const int MSG_APP_EXIT = 10; const std::vector g_inferName = {"inference_0", "inference_1", "inference_2", "inference_3"}; -const std::vector g_postprocName = {"postprocess_0","postprocess_1", - "postprocess_2","postprocess_3","postprocess_4","postprocess_5", - "postprocess_6","postprocess_7","postprocess_8","postprocess_9", - "postprocess_10","postprocess_11"}; +const std::vector g_postprocName = {"postprocess_0", "postprocess_1", + "postprocess_2", "postprocess_3", "postprocess_4", "postprocess_5", + "postprocess_6", "postprocess_7", "postprocess_8", "postprocess_9", + "postprocess_10", "postprocess_11"}; } const string g_videoprocName = "videoprocess_0"; diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/scripts/sample_build.sh b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/scripts/sample_build.sh index da46b1af4..7ec78a472 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/scripts/sample_build.sh +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/scripts/sample_build.sh @@ -17,7 +17,6 @@ function main() wget -O ${ModelPath}/../data/test_video.mp4 https://obs-9be7.obs.cn-east-2.myhuaweicloud.com/models/animeGAN/test_video/test_video.mp4 --no-check-certificate fi - find_model AnimeGANv2_256.om if [ $? -ne 0 ];then return 1 fi diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/inference/inference.cpp b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/inference/inference.cpp index 049f3a9c1..af9f34e66 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/inference/inference.cpp +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/inference/inference.cpp @@ -26,15 +26,14 @@ using namespace std; InferenceThread::InferenceThread(const string& modelPath, uint32_t modelWidth, uint32_t modelHeight, - aclrtContext& context) : -g_stream_(nullptr), -g_context_(context), -g_model_(modelPath), -g_modelWidth_(modelWidth), -g_modelHeight_(modelHeight) { + aclrtContext& context) + : g_stream_(nullptr), g_context_(context), g_model_(modelPath), + g_modelWidth_(modelWidth), g_modelHeight_(modelHeight) +{ } -InferenceThread::~InferenceThread() { +InferenceThread::~InferenceThread() +{ aclError ret = aclrtSetCurrentContext(g_context_); if (ret != ACL_SUCCESS) { ACLLITE_LOG_ERROR("InferenceThread destructor set context failed, error: %d", ret); @@ -42,8 +41,8 @@ InferenceThread::~InferenceThread() { g_model_.DestroyResource(); } -AclLiteError InferenceThread::Init() { - +AclLiteError InferenceThread::Init() +{ AclLiteError ret = aclrtCreateStream(&g_stream_); if (ret != ACL_ERROR_NONE) { ACLLITE_LOG_ERROR("Create acl stream failed, error %d", ret); @@ -66,8 +65,8 @@ AclLiteError InferenceThread::Init() { } AclLiteError InferenceThread::ModelExecute(shared_ptr preprocDataMsg, - shared_ptr &inferOutputMsg) { - + shared_ptr &inferOutputMsg) +{ inferOutputMsg->isLastFrame = preprocDataMsg->isLastFrame; inferOutputMsg->channelId = preprocDataMsg->channelId; inferOutputMsg->frameNum = preprocDataMsg->frameNum; @@ -86,7 +85,7 @@ AclLiteError InferenceThread::ModelExecute(shared_ptr preprocDat return ACLLITE_ERROR; } ret = g_model_.CreateInput(imageDevice.data.get(), - imageDevice.size); + imageDevice.size); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Create mode input dataset failed"); return ACLLITE_ERROR; @@ -103,17 +102,17 @@ AclLiteError InferenceThread::ModelExecute(shared_ptr preprocDat } AclLiteError InferenceThread::MsgSend(shared_ptr preprocDataMsg, - shared_ptr &inferOutputMsg) { + shared_ptr &inferOutputMsg) +{ while (1) { - AclLiteError ret = SendMessage(preprocDataMsg->postprocThreadId,MSG_INFER_OUTPUT, inferOutputMsg); + AclLiteError ret = SendMessage(preprocDataMsg->postprocThreadId, MSG_INFER_OUTPUT, inferOutputMsg); + int timep = 500; if (ret == ACLLITE_ERROR_ENQUEUE) { - usleep(500); + usleep(timep); continue; - } - else if (ret == ACLLITE_OK) { + } else if (ret == ACLLITE_OK) { break; - } - else { + } else { ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); return ret; } @@ -122,17 +121,17 @@ AclLiteError InferenceThread::MsgSend(shared_ptr preprocDataMsg, return ACLLITE_OK; } -AclLiteError InferenceThread::Process(int msgId, shared_ptr data) { +AclLiteError InferenceThread::Process(int msgId, shared_ptr data) +{ struct timespec time3 = {0, 0}; struct timespec time4 = {0, 0}; shared_ptr inferOutputMsg = make_shared(); - switch(msgId) { + switch (msgId) { case MSG_PREPROC_DATA: clock_gettime(CLOCK_REALTIME, &time3); ModelExecute(static_pointer_cast(data), inferOutputMsg); MsgSend(static_pointer_cast(data), inferOutputMsg); clock_gettime(CLOCK_REALTIME, &time4); - // cout << "inference time is: " << (time4.tv_sec - time3.tv_sec)*1000 + (time4.tv_nsec - time3.tv_nsec)/1000000 << "ms" << endl; break; default: ACLLITE_LOG_INFO("Inference thread ignore msg %d", msgId); diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/inference/inference.h b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/inference/inference.h index 444c4a588..4ad492dea 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/inference/inference.h +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/inference/inference.h @@ -36,7 +36,7 @@ using namespace std; class InferenceThread : public AclLiteThread { public: InferenceThread(const string& modelPath, - uint32_t modelWidth, uint32_t modelHeight, + uint32_t modelWidth, uint32_t modelHeight, aclrtContext& contex); ~InferenceThread(); @@ -51,7 +51,7 @@ private: shared_ptr &inferOutputMsg); void DestroyResource(); -private: +private: AclLiteModel g_model_; uint32_t g_modelWidth_; uint32_t g_modelHeight_; diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/main.cpp b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/main.cpp index efee4a13e..9d834baaa 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/main.cpp +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/main.cpp @@ -1,5 +1,5 @@ -/** -* Copyright 2020 Huawei Technologies Co., Ltd +/* +* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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. @@ -12,9 +12,6 @@ * 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 main.cpp -* Description: dvpp sample main func */ #include @@ -42,6 +39,12 @@ struct timespec time10 = {0, 0}; namespace { uint32_t g_exitCount = 0; +uint32_t g_height1 = 256; +uint32_t g_width1 = 256; +uint32_t g_height2 = 512; +uint32_t g_width2 = 512; +uint32_t g_height3 = 1024; +uint32_t g_width3 = 1024; uint32_t g_modelWidth; uint32_t g_modelHeight; const char* g_modelPath; @@ -53,11 +56,12 @@ const string g_regexVideoWidth = "^video_width+$"; const string g_regexDeviceNum = "^device_num+$"; aclrtContext context; aclrtRunMode runMode; -vector g_context; +vector g_contextVector; } -int MainThreadProcess(uint32_t msgId, - shared_ptr msgData, void* userData) { +int MainThreadProcess(uint32_t msgId, + shared_ptr msgData, void* userData) +{ if (msgId == MSG_APP_EXIT) { AclLiteApp& app = GetAclLiteAppInstance(); app.WaitEnd(); @@ -67,9 +71,10 @@ int MainThreadProcess(uint32_t msgId, } AclLiteError ParseConfig(string& dataAddr, uint32_t& postNum, uint32_t& deviceNum, - uint32_t& videoHeight, uint32_t& videoWidth) { + uint32_t& videoHeight, uint32_t& videoWidth) +{ map config; - if(!ReadConfig(config, g_configFile)) { + if (!ReadConfig(config, g_configFile)) { return ACLLITE_ERROR; } @@ -83,28 +88,24 @@ AclLiteError ParseConfig(string& dataAddr, uint32_t& postNum, uint32_t& deviceNu printf("config item: %s=%s\n", mIter->first.c_str(), mIter->second.c_str()); if (regex_match(mIter->first, dataAddrRegex)) { dataAddr = mIter->second; - ACLLITE_LOG_INFO("Rtsp config item: %s=%s", - mIter->first.c_str(), mIter->second.c_str()); - } - else if (regex_match(mIter->first, postNumRegex)) { + ACLLITE_LOG_INFO("Rtsp config item: %s=%s", + mIter->first.c_str(), mIter->second.c_str()); + } else if (regex_match(mIter->first, postNumRegex)) { postNum = stoi(mIter->second); - ACLLITE_LOG_INFO("Rtsp config item: %s=%s", - mIter->first.c_str(), mIter->second.c_str()); - } - else if (regex_match(mIter->first, videoHeightRegex)) { + ACLLITE_LOG_INFO("Rtsp config item: %s=%s", + mIter->first.c_str(), mIter->second.c_str()); + } else if (regex_match(mIter->first, videoHeightRegex)) { videoHeight = stoi(mIter->second); - ACLLITE_LOG_INFO("Rtsp config item: %s=%s", - mIter->first.c_str(), mIter->second.c_str()); - } - else if (regex_match(mIter->first, videoWidthRegex)) { + ACLLITE_LOG_INFO("Rtsp config item: %s=%s", + mIter->first.c_str(), mIter->second.c_str()); + } else if (regex_match(mIter->first, videoWidthRegex)) { videoWidth = stoi(mIter->second); - ACLLITE_LOG_INFO("Rtsp config item: %s=%s", - mIter->first.c_str(), mIter->second.c_str()); - } - else if (regex_match(mIter->first, deviceNumRegex)) { + ACLLITE_LOG_INFO("Rtsp config item: %s=%s", + mIter->first.c_str(), mIter->second.c_str()); + } else if (regex_match(mIter->first, deviceNumRegex)) { deviceNum = stoi(mIter->second); - ACLLITE_LOG_INFO("Data config item: %s=%s", - mIter->first.c_str(), mIter->second.c_str()); + ACLLITE_LOG_INFO("Data config item: %s=%s", + mIter->first.c_str(), mIter->second.c_str()); } } @@ -112,7 +113,8 @@ AclLiteError ParseConfig(string& dataAddr, uint32_t& postNum, uint32_t& deviceNu } void CreatePreprocessInstances(vector& threadTbl, - string dataAddr, uint32_t postNum, uint32_t deviceCount, AclLiteResource& aclDev) { + string dataAddr, uint32_t postNum, uint32_t deviceCount, AclLiteResource& aclDev) +{ aclrtContext context = aclDev.GetContext(); AclLiteThreadParam param; param.threadInst = new PreprocessThread(dataAddr, g_modelWidth, @@ -120,10 +122,11 @@ void CreatePreprocessInstances(vector& threadTbl, param.context = aclDev.GetContext(); param.runMode = aclDev.GetRunMode(); threadTbl.push_back(param); - } -void CreateInferenceInstance(vector& threadTbl, int32_t i, aclrtContext& context, aclrtRunMode& runMode) { +void CreateInferenceInstance(vector& threadTbl, int32_t i, + aclrtContext& context, aclrtRunMode& runMode) +{ AclLiteThreadParam param; param.threadInst = new InferenceThread(g_modelPath, g_modelWidth, @@ -135,11 +138,12 @@ void CreateInferenceInstance(vector& threadTbl, int32_t i, a } void CreatePostprocessInstances(vector& threadTbl, - uint32_t postNum, int32_t i, - aclrtContext& context, aclrtRunMode& runMode, - uint32_t videoHeight, uint32_t videoWidth) { + uint32_t postNum, int32_t i, + aclrtContext& context, aclrtRunMode& runMode, + uint32_t videoHeight, uint32_t videoWidth) +{ AclLiteThreadParam param; - for (int j = 0; j < postNum; j++) { + for (int j = 0; j < postNum; j++) { param.threadInst = new PostprocessThread(videoWidth, videoHeight); param.threadInstName.assign(g_postprocName[i*postNum+j].c_str()); param.context = context; @@ -150,7 +154,8 @@ void CreatePostprocessInstances(vector& threadTbl, void CreateVideoprocessInstance(vector& threadTbl, uint32_t videoHeight, uint32_t videoWidth, uint32_t postNum, - AclLiteResource& aclDev) { + AclLiteResource& aclDev) +{ AclLiteThreadParam param; param.threadInst = new VideoprocessThread(videoHeight, videoWidth, postNum); @@ -160,7 +165,8 @@ void CreateVideoprocessInstance(vector& threadTbl, threadTbl.push_back(param); } -void CreateThreadInstance(vector& threadTbl, AclLiteResource& aclDev) { +void CreateThreadInstance(vector& threadTbl, AclLiteResource& aclDev) +{ string dataAddr; uint32_t postNum; uint32_t videoHeight; @@ -178,7 +184,7 @@ void CreateThreadInstance(vector& threadTbl, AclLiteResource CreatePreprocessInstances(threadTbl, dataAddr, postNum, deviceNum, aclDev); CreateVideoprocessInstance(threadTbl, videoHeight, videoWidth, g_exitCount, aclDev); - for(int32_t i=0; i < deviceNum; i++){ + for (int32_t i = 0; i < deviceNum; i++) { ret = aclrtSetDevice(i); if (ret != ACL_ERROR_NONE) { ACLLITE_LOG_ERROR("Acl open device %d failed", i); @@ -191,28 +197,30 @@ void CreateThreadInstance(vector& threadTbl, AclLiteResource return; } - g_context.push_back(context); + g_contextVector.push_back(context); CreateInferenceInstance(threadTbl, i, context, runMode); CreatePostprocessInstances(threadTbl, postNum, i, context, runMode, videoHeight, videoWidth); } } -void ExitApp(AclLiteApp& app, vector& threadTbl) { +void ExitApp(AclLiteApp& app, vector& threadTbl) +{ for (int i = 0; i < threadTbl.size(); i++) { delete threadTbl[i].threadInst; } app.Exit(); - for(int i = 0; i < g_context.size(); i++) { - aclrtDestroyContext(g_context[i]); - if(i){ + for (int i = 0; i < g_contextVector.size(); i++) { + aclrtDestroyContext(g_contextVector[i]); + if (i) { aclrtResetDevice(i); } } } -void StartApp(AclLiteResource& aclDev) { +void StartApp(AclLiteResource& aclDev) +{ vector threadTbl; CreateThreadInstance(threadTbl, aclDev); @@ -238,17 +246,19 @@ void StartApp(AclLiteResource& aclDev) { } -int main(int argc, char *argv[]) { - if((argc < 2)){ +int main(int argc, char *argv[]) +{ + int argNum = 2; + if ((argc < argNum)) { ACLLITE_LOG_ERROR("invalid parameter number, must input four parameters."); ACLLITE_LOG_ERROR("Please input: ./main size"); return ACLLITE_ERROR; - }else{ + } else { uint64_t width = atoll(argv[1]); uint64_t height = atoll(argv[1]); - if (!(((height == 256) && (width == 256)) || - ((height == 512) && (width == 512)) || - ((height == 1024) && (width == 1024)))) { + if (!(((height == g_height1) && (width == g_width1)) || + ((height == g_height2) && (width == g_width2)) || + ((height == g_height3) && (width == g_width3)))) { ACLLITE_LOG_ERROR("invalid dynamic hw, should be 256*256,512*512,1024*1024."); return ACLLITE_ERROR; } @@ -256,11 +266,11 @@ int main(int argc, char *argv[]) { g_modelWidth = atoi(argv[1]); g_modelHeight = atoi(argv[1]); - if(256 == atoi(argv[1])){ + if (g_width1 == atoi(argv[1])) { g_modelPath = "../model/AnimeGANv2_256.om"; - }else if(512 == atoi(argv[1])){ + } else if (g_width2 == atoi(argv[1])) { g_modelPath = "../model/AnimeGANv2_512.om"; - }else{ + } else { g_modelPath = "../model/AnimeGANv2_1024.om"; } diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/postprocess/postprocess.cpp b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/postprocess/postprocess.cpp index 00be5a79b..ceef13ddd 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/postprocess/postprocess.cpp +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/postprocess/postprocess.cpp @@ -23,29 +23,32 @@ using namespace std; -PostprocessThread::PostprocessThread(uint32_t outputWidth, uint32_t outputHeight) : -g_outputWidth_(outputWidth), g_outputHeight_(outputHeight) { +PostprocessThread::PostprocessThread(uint32_t outputWidth, uint32_t outputHeight) + : g_outputWidth_(outputWidth), g_outputHeight_(outputHeight) +{ } -PostprocessThread::~PostprocessThread() { +PostprocessThread::~PostprocessThread() +{ } -AclLiteError PostprocessThread::Init() { +AclLiteError PostprocessThread::Init() +{ return ACLLITE_OK; } -AclLiteError PostprocessThread::Process(int msgId, shared_ptr data) { +AclLiteError PostprocessThread::Process(int msgId, shared_ptr data) +{ struct timespec time5 = {0, 0}; struct timespec time6 = {0, 0}; AclLiteError ret = ACLLITE_OK; shared_ptr postOutputMsg = make_shared(); - switch(msgId) { + switch (msgId) { case MSG_INFER_OUTPUT: clock_gettime(CLOCK_REALTIME, &time5); InferOutputProcess(static_pointer_cast(data), postOutputMsg); MsgSend(static_pointer_cast(data), postOutputMsg); clock_gettime(CLOCK_REALTIME, &time6); - // cout << "postprocess time is: " << (time6.tv_sec - time5.tv_sec)*1000 + (time6.tv_nsec - time5.tv_nsec)/1000000 << "ms" << endl; break; default: ACLLITE_LOG_INFO("PostprocessThread thread ignore msg %d", msgId); @@ -56,21 +59,21 @@ AclLiteError PostprocessThread::Process(int msgId, shared_ptr data) { } AclLiteError PostprocessThread::MsgSend(shared_ptr inferMsg, - shared_ptr &postOutputMsg) { + shared_ptr &postOutputMsg) +{ int sendFlag = MSG_VIDEO_ENCODE; + int timep = 500; if (inferMsg->isLastFrame == 1) { sendFlag = MSG_ENCODE_FINISH; } while (1) { AclLiteError ret = SendMessage(inferMsg->videoProcessThreadId, sendFlag, postOutputMsg); if (ret == ACLLITE_ERROR_ENQUEUE) { - usleep(500); + usleep(timep); continue; - } - else if (ret == ACLLITE_OK) { + } else if (ret == ACLLITE_OK) { break; - } - else { + } else { ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); return ret; } @@ -80,11 +83,12 @@ AclLiteError PostprocessThread::MsgSend(shared_ptr inferMsg, } AclLiteError PostprocessThread::InferOutputProcess(shared_ptr inferMsg, - shared_ptr &postOutputMsg) { + shared_ptr &postOutputMsg) +{ postOutputMsg->isLastFrame = inferMsg->isLastFrame; postOutputMsg->channelId = inferMsg->channelId; postOutputMsg->frameNum = inferMsg->frameNum; - if (postOutputMsg->isLastFrame) + if (postOutputMsg->isLastFrame) return ACLLITE_OK; void* data = inferMsg->inferData[0].data.get(); @@ -95,7 +99,8 @@ AclLiteError PostprocessThread::InferOutputProcess(shared_ptr in uint32_t dataSize = inferMsg->inferData[0].size; uint32_t size = static_cast(dataSize) / sizeof(float); - cv::Mat mat_result(inferMsg->resizedMat.height, inferMsg->resizedMat.width, CV_32FC3, const_cast((float*)data)); + cv::Mat mat_result(inferMsg->resizedMat.height, inferMsg->resizedMat.width, + CV_32FC3, const_cast((float*)data)); mat_result = (mat_result + 1) * 127.5; cv::Mat resultImage; diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/postprocess/postprocess.h b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/postprocess/postprocess.h index baf900bbc..94b3f2353 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/postprocess/postprocess.h +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/postprocess/postprocess.h @@ -29,13 +29,13 @@ using namespace std; -class PostprocessThread: public AclLiteThread { +class PostprocessThread : public AclLiteThread { public: PostprocessThread(uint32_t outputWidth, uint32_t outputHeight); ~PostprocessThread(); AclLiteError Init(); - AclLiteError Process(int msgId, shared_ptr data); + AclLiteError Process(int msgId, shared_ptr data); private: AclLiteError InferOutputProcess(shared_ptr inferMsg, diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/preprocess/preprocess.cpp b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/preprocess/preprocess.cpp index 5019da8c3..95686b5df 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/preprocess/preprocess.cpp +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/preprocess/preprocess.cpp @@ -25,68 +25,63 @@ using namespace std; struct timespec time1 = {0, 0}; struct timespec time2 = {0, 0}; -PreprocessThread::PreprocessThread(string& videoPath, uint32_t modelWidth, - uint32_t modelHeight, uint32_t postThreadNum, - uint32_t inferThreadNum, aclrtContext& context) : -g_videoPath_(videoPath), -g_context_(context), -g_cap_(nullptr), -g_stream_(nullptr), -g_modelWidth_(modelWidth), -g_modelHeight_(modelHeight), -g_selfThreadId_(INVALID_INSTANCE_ID), -g_videoThreadId_(INVALID_INSTANCE_ID), -g_inferThreadNum_(inferThreadNum), -g_postThreadNum_(postThreadNum), -g_inferChannel_(0), -g_postChannel_(0), -g_indexCount_(postThreadNum - 1), -g_frameCnt_(0) { +PreprocessThread::PreprocessThread(string& videoPath, uint32_t modelWidth, + uint32_t modelHeight, uint32_t postThreadNum, + uint32_t inferThreadNum, aclrtContext& context) + : videoPath_(videoPath), context_(context), cap_(nullptr), stream_(nullptr), + modelWidth_(modelWidth), modelHeight_(modelHeight), + selfThreadId_(INVALID_INSTANCE_ID), videoThreadId_(INVALID_INSTANCE_ID), + inferThreadNum_(inferThreadNum), postThreadNum_(postThreadNum), + inferChannel_(0), postChannel_(0), indexCount_(postThreadNum - 1), + frameCnt_(0) +{ for (int i = 0; i < inferThreadNum; i++) { - g_nextThreadId_.push_back(INVALID_INSTANCE_ID); + nextThreadId_.push_back(INVALID_INSTANCE_ID); for (int j = 0; j < postThreadNum; j++) { - g_postprocThreadId_.push_back(INVALID_INSTANCE_ID); + postprocThreadId_.push_back(INVALID_INSTANCE_ID); } } } -PreprocessThread::~PreprocessThread() { - aclError ret = aclrtSetCurrentContext(g_context_); +PreprocessThread::~PreprocessThread() +{ + aclError ret = aclrtSetCurrentContext(context_); if (ret != ACL_SUCCESS) { ACLLITE_LOG_ERROR("PreprocessThread destructor set context failed, error: %d", ret); } - if (g_cap_ != nullptr) { - g_cap_->Close(); - delete g_cap_; + if (cap_ != nullptr) { + cap_->Close(); + delete cap_; } - g_dvpp_.DestroyResource(); + dvpp_.DestroyResource(); } -AclLiteError PreprocessThread::OpenVideoCapture() { - if (IsDigitStr(g_videoPath_)) { - int cameraId = atoi(g_videoPath_.c_str()); +AclLiteError PreprocessThread::OpenVideoCapture() +{ + if (IsDigitStr(videoPath_)) { + int cameraId = atoi(videoPath_.c_str()); if ((cameraId < 0) || (cameraId >= CAMERA_ID_INVALID)) { ACLLITE_LOG_ERROR("Invalid camera id arg %s, only allow %d and %d", - g_videoPath_.c_str(), CAMERA_ID_0, CAMERA_ID_1); + videoPath_.c_str(), CAMERA_ID_0, CAMERA_ID_1); return ACLLITE_ERROR; } - g_cap_ = new AclLiteVideoProc(cameraId); - } else if (IsRtspAddr(g_videoPath_)) { - g_cap_ = new AclLiteVideoProc(g_videoPath_); - } else if (IsVideoFile(g_videoPath_)) { - if (!IsPathExist(g_videoPath_)) { - ACLLITE_LOG_ERROR("The %s is inaccessible", g_videoPath_.c_str()); + cap_ = new AclLiteVideoProc(cameraId); + } else if (IsRtspAddr(videoPath_)) { + cap_ = new AclLiteVideoProc(videoPath_); + } else if (IsVideoFile(videoPath_)) { + if (!IsPathExist(videoPath_)) { + ACLLITE_LOG_ERROR("The %s is inaccessible", videoPath_.c_str()); return ACLLITE_ERROR; } - g_cap_ = new AclLiteVideoProc(g_videoPath_); + cap_ = new AclLiteVideoProc(videoPath_); } else { ACLLITE_LOG_ERROR("Invalid param. The arg should be accessible rtsp," " video file or camera id"); return ACLLITE_ERROR; } - if (!g_cap_->IsOpened()) { - delete g_cap_; + if (!cap_->IsOpened()) { + delete cap_; ACLLITE_LOG_ERROR("Failed to open video"); return ACLLITE_ERROR; } @@ -94,45 +89,45 @@ AclLiteError PreprocessThread::OpenVideoCapture() { return ACLLITE_OK; } -AclLiteError PreprocessThread::Init() { - +AclLiteError PreprocessThread::Init() +{ if (ACLLITE_OK != OpenVideoCapture()) { return ACLLITE_ERROR; } - aclError aclRet = aclrtGetRunMode(&g_runMode_); + aclError aclRet = aclrtGetRunMode(&runMode_); if (aclRet != ACL_ERROR_NONE) { ACLLITE_LOG_ERROR("acl get run mode failed"); return ACLLITE_ERROR_GET_RUM_MODE; } - aclRet = g_dvpp_.Init(); + aclRet = dvpp_.Init(); if (aclRet) { ACLLITE_LOG_ERROR("Dvpp init failed, error %d", aclRet); return ACLLITE_ERROR; } // Get the relevant thread instance id - g_selfThreadId_ = SelfInstanceId(); - g_videoThreadId_ = GetAclLiteThreadIdByName(g_videoprocName); - if ((g_selfThreadId_ == INVALID_INSTANCE_ID) || - (g_videoThreadId_ == INVALID_INSTANCE_ID)) { - ACLLITE_LOG_ERROR("Self instance id %d, video process instance id %d", g_selfThreadId_, g_videoThreadId_); + selfThreadId_ = SelfInstanceId(); + videoThreadId_ = GetAclLiteThreadIdByName(g_videoprocName); + if ((selfThreadId_ == INVALID_INSTANCE_ID) || + (videoThreadId_ == INVALID_INSTANCE_ID)) { + ACLLITE_LOG_ERROR("Self instance id %d, video process instance id %d", selfThreadId_, videoThreadId_); return ACLLITE_ERROR; } - for (int i = 0; i < g_inferThreadNum_; i++) { - g_nextThreadId_[i] = GetAclLiteThreadIdByName(g_inferName[i]); - if (g_nextThreadId_[i] == INVALID_INSTANCE_ID) { + for (int i = 0; i < inferThreadNum_; i++) { + nextThreadId_[i] = GetAclLiteThreadIdByName(g_inferName[i]); + if (nextThreadId_[i] == INVALID_INSTANCE_ID) { ACLLITE_LOG_ERROR("%d inference instance id %d", - i, g_nextThreadId_[i]); + i, nextThreadId_[i]); return ACLLITE_ERROR; } - for (int j = 0; j < g_postThreadNum_; j++) { - g_postprocThreadId_[i*g_postThreadNum_+j] = GetAclLiteThreadIdByName(g_postprocName[i*g_postThreadNum_+j]); - if (g_postprocThreadId_[i*g_postThreadNum_+j] == INVALID_INSTANCE_ID) { + for (int j = 0; j < postThreadNum_; j++) { + postprocThreadId_[i*postThreadNum_+j] = GetAclLiteThreadIdByName(g_postprocName[i*postThreadNum_+j]); + if (postprocThreadId_[i*postThreadNum_+j] == INVALID_INSTANCE_ID) { ACLLITE_LOG_ERROR("%d postprocess instance id %d", - i*g_postThreadNum_+j, g_postprocThreadId_[i*g_postThreadNum_+j]); + i*postThreadNum_+j, postprocThreadId_[i*postThreadNum_+j]); return ACLLITE_ERROR; } } @@ -141,11 +136,11 @@ AclLiteError PreprocessThread::Init() { return ACLLITE_OK; } -AclLiteError PreprocessThread::Process(int msgId, - shared_ptr msgData) { +AclLiteError PreprocessThread::Process(int msgId, shared_ptr msgData) +{ AclLiteError ret = ACLLITE_OK; shared_ptr preprocDataMsg = make_shared(); - switch(msgId) { + switch (msgId) { case MSG_APP_START: ret = AppStart(); break; @@ -155,7 +150,6 @@ AclLiteError PreprocessThread::Process(int msgId, ret = MsgProcess(preprocDataMsg); ret = MsgSend(preprocDataMsg); clock_gettime(CLOCK_REALTIME, &time2); - // cout << "preprocess time is: " << (time2.tv_sec - time1.tv_sec)*1000 + (time2.tv_nsec - time1.tv_nsec)/1000000 << "ms" << endl; break; default: ACLLITE_LOG_ERROR("Preprocess thread receive unknow msg %d", msgId); @@ -165,62 +159,65 @@ AclLiteError PreprocessThread::Process(int msgId, return ret; } -AclLiteError PreprocessThread::AppStart() { - AclLiteError ret = SendMessage(g_selfThreadId_, MSG_READ_FRAME, nullptr); +AclLiteError PreprocessThread::AppStart() +{ + AclLiteError ret = SendMessage(selfThreadId_, MSG_READ_FRAME, nullptr); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Process app start message failed, error %d", ret); - } + } return ret; } -AclLiteError PreprocessThread::ReadFrame(shared_ptr &preprocDataMsg) { - g_inferChannel_ = g_frameCnt_ % g_inferThreadNum_; - if (g_inferChannel_ == 0) { - g_indexCount_ = (++g_indexCount_) % g_postThreadNum_; +AclLiteError PreprocessThread::ReadFrame(shared_ptr &preprocDataMsg) +{ + inferChannel_ = frameCnt_ % inferThreadNum_; + if (inferChannel_ == 0) { + indexCount_ = (++indexCount_) % postThreadNum_; } - g_postChannel_ = g_inferChannel_ * g_postThreadNum_ + g_indexCount_; + postChannel_ = inferChannel_ * postThreadNum_ + indexCount_; - preprocDataMsg->inferThreadId = g_nextThreadId_[g_inferChannel_]; - preprocDataMsg->postprocThreadId = g_postprocThreadId_[g_postChannel_]; - preprocDataMsg->videoProcessThreadId = g_videoThreadId_; - preprocDataMsg->channelId = g_postChannel_; - preprocDataMsg->frameNum = g_frameCnt_; + preprocDataMsg->inferThreadId = nextThreadId_[inferChannel_]; + preprocDataMsg->postprocThreadId = postprocThreadId_[postChannel_]; + preprocDataMsg->videoProcessThreadId = videoThreadId_; + preprocDataMsg->channelId = postChannel_; + preprocDataMsg->frameNum = frameCnt_; preprocDataMsg->isLastFrame = 0; - AclLiteError ret = g_cap_->Read(preprocDataMsg->imageFrame); - if (ret != ACLLITE_OK) { + AclLiteError ret = cap_->Read(preprocDataMsg->imageFrame); + if (ret != ACLLITE_OK) { preprocDataMsg->isLastFrame = 1; return ret; } - g_frameCnt_++; + frameCnt_++; return ACLLITE_OK; } -AclLiteError PreprocessThread::MsgProcess(shared_ptr &preprocDataMsg) { +AclLiteError PreprocessThread::MsgProcess(shared_ptr &preprocDataMsg) +{ if (preprocDataMsg->isLastFrame == 1) return ACLLITE_OK; ImageData imageDevice; - AclLiteError ret = CopyImageToDevice(imageDevice, preprocDataMsg->imageFrame, g_runMode_, MEMORY_DVPP); + AclLiteError ret = CopyImageToDevice(imageDevice, preprocDataMsg->imageFrame, runMode_, MEMORY_DVPP); if (ret == ACLLITE_ERROR) { ACLLITE_LOG_ERROR("Copy image to device failed"); return ACLLITE_ERROR; } ImageData resizedImage; - ret = g_dvpp_.Resize(resizedImage, imageDevice, g_modelWidth_, g_modelHeight_); + ret = dvpp_.Resize(resizedImage, imageDevice, modelWidth_, modelHeight_); if (ret == ACLLITE_ERROR) { ACLLITE_LOG_ERROR("g_dvpp_cropandpaste image failed"); return ACLLITE_ERROR; } - ret = CopyImageToLocal(preprocDataMsg->resizedMat, resizedImage, g_runMode_); + ret = CopyImageToLocal(preprocDataMsg->resizedMat, resizedImage, runMode_); if (ret == ACLLITE_ERROR) { ACLLITE_LOG_ERROR("Copy image to host failed"); return ACLLITE_ERROR; } ImageData yuvImage; - ret = CopyImageToLocal(yuvImage, preprocDataMsg->imageFrame, g_runMode_); + ret = CopyImageToLocal(yuvImage, preprocDataMsg->imageFrame, runMode_); if (ret == ACLLITE_ERROR) { ACLLITE_LOG_ERROR("Copy image to host failed"); return ACLLITE_ERROR; @@ -231,7 +228,8 @@ AclLiteError PreprocessThread::MsgProcess(shared_ptr &preprocDat return ACLLITE_OK; } -AclLiteError PreprocessThread::MsgSend(shared_ptr &preprocDataMsg) { +AclLiteError PreprocessThread::MsgSend(shared_ptr &preprocDataMsg) +{ AclLiteError ret; if (preprocDataMsg->isLastFrame == 0) { while (1) { @@ -239,42 +237,37 @@ AclLiteError PreprocessThread::MsgSend(shared_ptr &preprocDataMs if (ret == ACLLITE_ERROR_ENQUEUE) { usleep(500); continue; - } - else if (ret == ACLLITE_OK) { + } else if (ret == ACLLITE_OK) { break; - } - else { + } else { ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); return ret; } } - ret = SendMessage(g_selfThreadId_, MSG_READ_FRAME, nullptr); + 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 < g_inferThreadNum_; i++) { - for (int j = 0; j < g_postThreadNum_; j++) { + } + } else { + for (int i = 0; i < inferThreadNum_; i++) { + for (int j = 0; j < postThreadNum_; j++) { shared_ptr preprocDataMsgEnd = make_shared(); preprocDataMsgEnd->videoProcessThreadId = preprocDataMsg->videoProcessThreadId; preprocDataMsgEnd->channelId = preprocDataMsg->channelId; preprocDataMsgEnd->frameNum = preprocDataMsg->frameNum; preprocDataMsgEnd->isLastFrame = preprocDataMsg->isLastFrame; - preprocDataMsgEnd->inferThreadId = g_nextThreadId_[i]; - preprocDataMsgEnd->postprocThreadId = g_postprocThreadId_[i*g_postThreadNum_+j]; + preprocDataMsgEnd->inferThreadId = nextThreadId_[i]; + preprocDataMsgEnd->postprocThreadId = postprocThreadId_[i*postThreadNum_+j]; while (1) { ret = SendMessage(preprocDataMsgEnd->inferThreadId, MSG_PREPROC_DATA, preprocDataMsgEnd); if (ret == ACLLITE_ERROR_ENQUEUE) { usleep(500); continue; - } - else if (ret == ACLLITE_OK) { + } else if (ret == ACLLITE_OK) { break; - } - else { + } else { ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); return ret; } diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/preprocess/preprocess.h b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/preprocess/preprocess.h index 0cc68d195..2da46a04e 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/preprocess/preprocess.h +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/preprocess/preprocess.h @@ -29,8 +29,8 @@ class PreprocessThread : public AclLiteThread { public: - PreprocessThread(string& fileName, uint32_t modelWidth, - uint32_t modelHeight, uint32_t postThreadNum, + PreprocessThread(string& fileName, uint32_t modelWidth, + uint32_t modelHeight, uint32_t postThreadNum, uint32_t inferThreadNum, aclrtContext& context); ~PreprocessThread(); @@ -45,25 +45,25 @@ private: AclLiteError ReadFrame(shared_ptr &preprocDataMsg); private: - string g_videoPath_; - AclLiteVideoProc* g_cap_; - aclrtStream g_stream_; - aclrtRunMode g_runMode_; - uint32_t g_modelWidth_; - uint32_t g_modelHeight_; - int g_selfThreadId_; - int g_videoThreadId_; - vector g_nextThreadId_; - vector g_postprocThreadId_; - uint32_t g_inferThreadNum_; - uint32_t g_postThreadNum_; - int g_frameCnt_; - AclLiteImageProc g_dvpp_; + string videoPath_; + AclLiteVideoProc* cap_; + aclrtStream stream_; + aclrtRunMode runMode_; + uint32_t modelWidth_; + uint32_t modelHeight_; + int selfThreadId_; + int videoThreadId_; + vector nextThreadId_; + vector postprocThreadId_; + uint32_t inferThreadNum_; + uint32_t postThreadNum_; + int frameCnt_; + AclLiteImageProc dvpp_; - int g_inferChannel_; - int g_postChannel_; - int g_indexCount_; - aclrtContext g_context_; + int inferChannel_; + int postChannel_; + int indexCount_; + aclrtContext context_; }; #endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/videoprocess/videoprocess.cpp b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/videoprocess/videoprocess.cpp index bd446aec3..a62f9c6ab 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/videoprocess/videoprocess.cpp +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/videoprocess/videoprocess.cpp @@ -27,39 +27,44 @@ using namespace std; struct timespec time7 = {0, 0}; struct timespec time8 = {0, 0}; -VideoprocessThread::VideoprocessThread(int videoHeight, int videoWidth, int postNum) : -g_videoWidth_(videoWidth), -g_videoHeight_(videoHeight), -g_postNum_(postNum), -g_shutdown_(0){ +VideoprocessThread::VideoprocessThread(int videoHeight, int videoWidth, int postNum) + : videoWidth_(videoWidth), + videoHeight_(videoHeight), + postNum_(postNum), + shutdown_(0) +{ } -VideoprocessThread::~VideoprocessThread() { +VideoprocessThread::~VideoprocessThread() +{ } -AclLiteError VideoprocessThread::Init() { - g_outputVideoPath_ = "./output/out_test_video.mp4"; - cout << "g_videoWidth_ and g_videoHeight_ is" << " " << g_videoWidth_ << " " << g_videoHeight_ << endl; - g_outputVideo_.open(g_outputVideoPath_, cv::VideoWriter::fourcc('m', 'p', '4', 'v'), 29.0, cv::Size(g_videoWidth_,g_videoHeight_)); +AclLiteError VideoprocessThread::Init() +{ + outputVideoPath_ = "./output/out_test_video.mp4"; + float fps = 29.0; + cout << "videoWidth_ and videoHeight_ is" << " " << videoWidth_ << " " << videoHeight_ << endl; + outputVideo_.open(outputVideoPath_, cv::VideoWriter::fourcc('m', 'p', '4', 'v'), + fps, cv::Size(videoWidth_, videoHeight_)); return ACLLITE_OK; } -AclLiteError VideoprocessThread::Process(int msgId, shared_ptr data) { +AclLiteError VideoprocessThread::Process(int msgId, shared_ptr data) +{ AclLiteError ret = ACLLITE_OK; shared_ptr postOutputMsg = static_pointer_cast(data); - switch(msgId) { + switch (msgId) { case MSG_VIDEO_ENCODE: clock_gettime(CLOCK_REALTIME, &time7); RecordQueue(postOutputMsg); DataProcess(); clock_gettime(CLOCK_REALTIME, &time8); - //cout << "videoprocess time is: " << (time8.tv_sec - time7.tv_sec)*1000 + (time8.tv_nsec - time7.tv_nsec)/1000000 << "ms" << endl; break; case MSG_ENCODE_FINISH: clock_gettime(CLOCK_REALTIME, &time7); - g_shutdown_++; - cout << "g_shutdown_ is " << g_shutdown_ << endl; - if(g_shutdown_ == g_postNum_){ + shutdown_++; + cout << "shutdown_ is " << shutdown_ << endl; + if (shutdown_ == postNum_) { ShutDownProcess(); SendMessage(g_MainThreadId, MSG_APP_EXIT, nullptr); } @@ -73,60 +78,57 @@ AclLiteError VideoprocessThread::Process(int msgId, shared_ptr data) { return ret; } -AclLiteError VideoprocessThread::ShutDownProcess() { +AclLiteError VideoprocessThread::ShutDownProcess() +{ int flag = 0; - shared_ptr outputData[g_postNum_]; - int noEmptyQueue[g_postNum_]; - for(int i = 0; i < g_postNum_; i++) - { - if(!g_postQueue_[i].empty()){ + shared_ptr outputData[postNum_]; + int noEmptyQueue[postNum_]; + for (int i = 0; i < postNum_; i++) { + if (!postQueue_[i].empty()) { noEmptyQueue[flag] = i; flag++; } } - if(flag != 0) - { - for(int i = 0; i < flag; i++) - { - outputData[i] = g_postQueue_[noEmptyQueue[i]].front(); - g_postQueue_[i].pop(); + if (flag != 0) { + for (int i = 0; i < flag; i++) { + outputData[i] = postQueue_[noEmptyQueue[i]].front(); + postQueue_[i].pop(); } quick_sort(outputData, 0, flag-1); - for(int i = 0; i < flag; i++) - { + for(int i = 0; i < flag; i++) { cout << "Current frame num is: " << outputData[i]->frameNum << endl; - g_outputVideo_ << outputData[i]->resultImage; + outputVideo_ << outputData[i]->resultImage; } } - g_outputVideo_.release(); + outputVideo_.release(); return ACLLITE_OK; } -AclLiteError VideoprocessThread::RecordQueue(shared_ptr postOutputMsg) { - if(postOutputMsg->channelId > 16) - { +AclLiteError VideoprocessThread::RecordQueue(shared_ptr postOutputMsg) +{ + int channelNum = 16; + if (postOutputMsg->channelId > channelNum) { ACLLITE_LOG_ERROR("Support up to 16 channels of post-processing."); return ACLLITE_ERROR; } - g_postQueue_[postOutputMsg->channelId].push(postOutputMsg); + postQueue_[postOutputMsg->channelId].push(postOutputMsg); return ACLLITE_OK; } -AclLiteError VideoprocessThread::quick_sort(shared_ptr outputData[], int low, int high) { - if(low > high) +AclLiteError VideoprocessThread::quick_sort(shared_ptr outputData[], int low, int high) +{ + if (low > high) return ACLLITE_OK; int i = low; int j = high; shared_ptr key = outputData[low]; - while (i < j) - { - while(i < j && outputData[j]->frameNum >= key->frameNum) + while (i < j) { + while (i < j && outputData[j]->frameNum >= key->frameNum) j--; outputData[i] = outputData[j]; - while(i < j && outputData[j]->frameNum <= key->frameNum) + while (i < j && outputData[j]->frameNum <= key->frameNum) i++; outputData[j] = outputData[i]; - } outputData[i] = key; @@ -135,27 +137,24 @@ AclLiteError VideoprocessThread::quick_sort(shared_ptr outputData return ACLLITE_OK; } -AclLiteError VideoprocessThread::DataProcess() { +AclLiteError VideoprocessThread::DataProcess() +{ int flag = 0; - shared_ptr outputData[g_postNum_]; - for(int i = 0; i < g_postNum_; i++) - { - if(!g_postQueue_[i].empty()) + shared_ptr outputData[postNum_]; + for (int i = 0; i < postNum_; i++) { + if (!postQueue_[i].empty()) flag++; } - if(flag == g_postNum_) - { - for(int i = 0; i < g_postNum_; i++) - { - outputData[i] = g_postQueue_[i].front(); - g_postQueue_[i].pop(); + if (flag == postNum_) { + for (int i = 0; i < postNum_; i++) { + outputData[i] = postQueue_[i].front(); + postQueue_[i].pop(); } - quick_sort(outputData, 0, g_postNum_-1); - for(int i = 0; i < g_postNum_; i++) - { + quick_sort(outputData, 0, postNum_-1); + for (int i = 0; i < postNum_; i++) { cout <<"Current frame num is: " << outputData[i]->frameNum << endl; - g_outputVideo_ << outputData[i]->resultImage; + outputVideo_ << outputData[i]->resultImage; } } return ACLLITE_OK; -} +} \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/videoprocess/videoprocess.h b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/videoprocess/videoprocess.h index 0d41c9a9f..40d94ce2e 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/videoprocess/videoprocess.h +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/animeGAN_multi_device_one_video/src/videoprocess/videoprocess.h @@ -31,13 +31,13 @@ using namespace std; -class VideoprocessThread: public AclLiteThread { +class VideoprocessThread : public AclLiteThread { public: VideoprocessThread(int videoHeight, int videoWidth, int postNum); ~VideoprocessThread(); AclLiteError Init(); - AclLiteError Process(int msgId, shared_ptr data); + AclLiteError Process(int msgId, shared_ptr data); private: void DestroyResource(); @@ -47,13 +47,13 @@ private: AclLiteError DataProcess(); private: - int g_videoWidth_; - int g_videoHeight_; - int g_postNum_; - int g_shutdown_; - string g_outputVideoPath_; - cv::VideoWriter g_outputVideo_; - queue> g_postQueue_[16]; + int videoWidth_; + int videoHeight_; + int postNum_; + int shutdown_; + string outputVideoPath_; + cv::VideoWriter outputVideo_; + queue> postQueue_[16]; }; #endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/inc/object_detection.h b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/inc/object_detection.h index 8677bd23b..b7e81f93f 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/inc/object_detection.h +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/inc/object_detection.h @@ -25,13 +25,13 @@ #include #include #include +#include #include "AclLiteType.h" #include "AclLiteModel.h" #include "AclLiteImageProc.h" #include #include "opencv2/opencv.hpp" #include "opencv2/imgproc/types_c.h" -#include #define RGBF32_CHAN_SIZE(width, height) ((width) * (height) * 4) using namespace std; @@ -48,10 +48,10 @@ const int MSG_PREPROC_END = 8; const int MSG_APP_EXIT = 10; const std::vector g_inferName = {"inference_0", "inference_1", "inference_2", "inference_3"}; -const std::vector g_postprocName = {"postprocess_0","postprocess_1", - "postprocess_2","postprocess_3","postprocess_4","postprocess_5", - "postprocess_6","postprocess_7","postprocess_8","postprocess_9", - "postprocess_10","postprocess_11"}; +const std::vector g_postprocName = {"postprocess_0", "postprocess_1", + "postprocess_2", "postprocess_3", "postprocess_4", "postprocess_5", + "postprocess_6", "postprocess_7", "postprocess_8", "postprocess_9", + "postprocess_10", "postprocess_11"}; } struct PreprocDataMsg { diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/inference/inference.cpp b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/inference/inference.cpp index 7f2328fb2..89fc025cd 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/inference/inference.cpp +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/inference/inference.cpp @@ -23,40 +23,39 @@ #include "object_detection.h" using namespace std; - +const int g_timep = 500; InferenceThread::InferenceThread(const string& modelPath, - uint32_t modelWidth, uint32_t modelHeight, - aclrtContext& context) : -g_stream_(nullptr), -g_context_(context), -g_model_(modelPath), -g_modelWidth_(modelWidth), -g_modelHeight_(modelHeight) { + uint32_t modelWidth, uint32_t modelHeight, + aclrtContext& context) + : stream_(nullptr), context_(context), model_(modelPath), + modelWidth_(modelWidth), modelHeight_(modelHeight) +{ } -InferenceThread::~InferenceThread() { - aclError ret = aclrtSetCurrentContext(g_context_); +InferenceThread::~InferenceThread() +{ + aclError ret = aclrtSetCurrentContext(context_); if (ret != ACL_SUCCESS) { ACLLITE_LOG_ERROR("InferenceThread destructor set context failed, error: %d", ret); } - g_model_.DestroyResource(); + model_.DestroyResource(); } -AclLiteError InferenceThread::Init() { - - AclLiteError ret = aclrtCreateStream(&g_stream_); +AclLiteError InferenceThread::Init() +{ + AclLiteError ret = aclrtCreateStream(&stream_); if (ret != ACL_ERROR_NONE) { ACLLITE_LOG_ERROR("Create acl stream failed, error %d", ret); return ACLLITE_ERROR_CREATE_STREAM; } - ret = g_model_.Init(); + ret = model_.Init(); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Model init failed, error:%d", ret); return ret; } - ret = aclrtGetRunMode(&g_runMode_); + ret = aclrtGetRunMode(&runMode_); if (ret != ACL_ERROR_NONE) { ACLLITE_LOG_ERROR("acl get run mode failed"); return ACLLITE_ERROR; @@ -66,65 +65,62 @@ AclLiteError InferenceThread::Init() { } AclLiteError InferenceThread::ModelExecute(shared_ptr preprocDataMsg, - shared_ptr &inferOutputMsg) { - + shared_ptr &inferOutputMsg) +{ inferOutputMsg->isLastFrame = preprocDataMsg->isLastFrame; inferOutputMsg->frameNum = preprocDataMsg->frameNum; inferOutputMsg->imageFileName = preprocDataMsg->imageFileName; ImageData imageDevice; - AclLiteError ret = CopyImageToDevice(imageDevice, preprocDataMsg->resizedMat, g_runMode_, MEMORY_DEVICE); + AclLiteError ret = CopyImageToDevice(imageDevice, preprocDataMsg->resizedMat, runMode_, MEMORY_DEVICE); if (ret == ACLLITE_ERROR) { ACLLITE_LOG_ERROR("Copy image to device failed"); return ACLLITE_ERROR; } - ret = g_model_.CreateInput(imageDevice.data.get(), - imageDevice.size); + ret = model_.CreateInput(imageDevice.data.get(), + imageDevice.size); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Create mode input dataset failed"); return ACLLITE_ERROR; } - ret = g_model_.Execute(inferOutputMsg->inferData); + ret = model_.Execute(inferOutputMsg->inferData); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Execute model inference failed, error: %d", ret); return ACLLITE_ERROR; } - g_model_.DestroyInput(); + model_.DestroyInput(); return ACLLITE_OK; } AclLiteError InferenceThread::MsgSend(shared_ptr preprocDataMsg, - shared_ptr &inferOutputMsg) { + shared_ptr &inferOutputMsg) +{ while (1) { - AclLiteError ret = SendMessage(preprocDataMsg->postprocThreadId,MSG_INFER_OUTPUT, inferOutputMsg); + AclLiteError ret = SendMessage(preprocDataMsg->postprocThreadId, MSG_INFER_OUTPUT, inferOutputMsg); if (ret == ACLLITE_ERROR_ENQUEUE) { - usleep(500); + usleep(g_timep); continue; - } - else if (ret == ACLLITE_OK) { + } else if (ret == ACLLITE_OK) { break; - } - else { + } else { ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); return ret; } } - return ACLLITE_OK; } -AclLiteError InferenceThread::MsgSendEnd(shared_ptr preprocDataMsg) { +AclLiteError InferenceThread::MsgSendEnd(shared_ptr preprocDataMsg) +{ while (1) { AclLiteError ret = SendMessage(preprocDataMsg->postprocThreadId, MSG_ENCODE_FINISH, nullptr); if (ret == ACLLITE_ERROR_ENQUEUE) { - usleep(500); + usleep(g_timep); continue; - } - else if (ret == ACLLITE_OK) { + } else if (ret == ACLLITE_OK) { break; - } - else { + } else { ACLLITE_LOG_ERROR("Send read frame message failed, error %d", ret); return ret; } @@ -133,12 +129,13 @@ AclLiteError InferenceThread::MsgSendEnd(shared_ptr preprocDataM return ACLLITE_OK; } -AclLiteError InferenceThread::Process(int msgId, shared_ptr data) { +AclLiteError InferenceThread::Process(int msgId, shared_ptr data) +{ struct timespec time3 = {0, 0}; struct timespec time4 = {0, 0}; shared_ptr inferOutputMsg = make_shared(); AclLiteError ret; - switch(msgId) { + switch (msgId) { case MSG_PREPROC_DATA: clock_gettime(CLOCK_REALTIME, &time3); ModelExecute(static_pointer_cast(data), inferOutputMsg); @@ -155,5 +152,4 @@ AclLiteError InferenceThread::Process(int msgId, shared_ptr data) { } return ACLLITE_OK; -} - +} \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/inference/inference.h b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/inference/inference.h index 032825c90..aa2f6763a 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/inference/inference.h +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/inference/inference.h @@ -36,8 +36,8 @@ using namespace std; class InferenceThread : public AclLiteThread { public: InferenceThread(const string& modelPath, - uint32_t modelWidth, uint32_t modelHeight, - aclrtContext& context); + uint32_t modelWidth, uint32_t modelHeight, + aclrtContext& context); ~InferenceThread(); AclLiteError Init(); @@ -51,13 +51,13 @@ private: shared_ptr &inferOutputMsg); void DestroyResource(); -private: - AclLiteModel g_model_; - uint32_t g_modelWidth_; - uint32_t g_modelHeight_; - aclrtRunMode g_runMode_; - aclrtStream g_stream_; - aclrtContext g_context_; +private: + AclLiteModel model_; + uint32_t modelWidth_; + uint32_t modelHeight_; + aclrtRunMode runMode_; + aclrtStream stream_; + aclrtContext context_; }; #endif \ No newline at end of file diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/main.cpp b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/main.cpp index 6e0ce2bbd..8316c5f67 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/main.cpp +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/main.cpp @@ -49,15 +49,15 @@ const string g_regexOutputWidth = "^output_width+$"; const string g_regexDeviceNum = "^device_num+$"; aclrtContext context; aclrtRunMode runMode; -vector g_context; +vector contextVector; } -int MainThreadProcess(uint32_t msgId, - shared_ptr msgData, void* userData) { +int MainThreadProcess(uint32_t msgId, + shared_ptr msgData, void* userData) +{ if (msgId == MSG_APP_EXIT) { g_exitCount--; - } - else { + } else { return ACLLITE_ERROR; } ACLLITE_LOG_INFO("g_exitCount is %d", g_exitCount); @@ -66,12 +66,12 @@ int MainThreadProcess(uint32_t msgId, app.WaitEnd(); ACLLITE_LOG_INFO("Receive exit message, exit now"); } - return ACLLITE_OK; } AclLiteError ParseConfig(string& dataAddr, uint32_t& postNum, uint32_t& deviceNum, - uint32_t& outputHeight, uint32_t& outputWidth) { + uint32_t& outputHeight, uint32_t& outputWidth) +{ map config; if (!ReadConfig(config, g_configFile)) { return ACLLITE_ERROR; @@ -87,28 +87,24 @@ AclLiteError ParseConfig(string& dataAddr, uint32_t& postNum, uint32_t& deviceNu printf("config item: %s=%s\n", mIter->first.c_str(), mIter->second.c_str()); if (regex_match(mIter->first, dataAddrRegex)) { dataAddr = mIter->second; - ACLLITE_LOG_INFO("Data config item: %s=%s", - mIter->first.c_str(), mIter->second.c_str()); - } - else if (regex_match(mIter->first, postNumRegex)) { + ACLLITE_LOG_INFO("Data config item: %s=%s", + mIter->first.c_str(), mIter->second.c_str()); + } else if (regex_match(mIter->first, postNumRegex)) { postNum = stoi(mIter->second); - ACLLITE_LOG_INFO("Data config item: %s=%s", - mIter->first.c_str(), mIter->second.c_str()); - } - else if (regex_match(mIter->first, outputHeightRegex)) { + ACLLITE_LOG_INFO("Data config item: %s=%s", + mIter->first.c_str(), mIter->second.c_str()); + } else if (regex_match(mIter->first, outputHeightRegex)) { outputHeight = stoi(mIter->second); - ACLLITE_LOG_INFO("Data config item: %s=%s", - mIter->first.c_str(), mIter->second.c_str()); - } - else if (regex_match(mIter->first, outputWidthRegex)) { + ACLLITE_LOG_INFO("Data config item: %s=%s", + mIter->first.c_str(), mIter->second.c_str()); + } else if (regex_match(mIter->first, outputWidthRegex)) { outputWidth = stoi(mIter->second); - ACLLITE_LOG_INFO("Data config item: %s=%s", - mIter->first.c_str(), mIter->second.c_str()); - } - else if (regex_match(mIter->first, deviceNumRegex)) { + ACLLITE_LOG_INFO("Data config item: %s=%s", + mIter->first.c_str(), mIter->second.c_str()); + } else if (regex_match(mIter->first, deviceNumRegex)) { deviceNum = stoi(mIter->second); - ACLLITE_LOG_INFO("Data config item: %s=%s", - mIter->first.c_str(), mIter->second.c_str()); + ACLLITE_LOG_INFO("Data config item: %s=%s", + mIter->first.c_str(), mIter->second.c_str()); } } @@ -116,7 +112,8 @@ AclLiteError ParseConfig(string& dataAddr, uint32_t& postNum, uint32_t& deviceNu } void CreatePreprocessInstances(vector& threadTbl, - string dataAddr, uint32_t postNum, uint32_t deviceCount, AclLiteResource& aclDev) { + string dataAddr, uint32_t postNum, uint32_t deviceCount, AclLiteResource& aclDev) +{ aclrtContext context = aclDev.GetContext(); AclLiteThreadParam param; param.threadInst = new PreprocessThread(dataAddr, g_modelWidth, @@ -125,14 +122,15 @@ void CreatePreprocessInstances(vector& threadTbl, param.runMode = aclDev.GetRunMode(); ACLLITE_LOG_INFO("Preprocess context is :%p", param.context); threadTbl.push_back(param); - } -void CreateInferenceInstance(vector& threadTbl, int32_t i, aclrtContext& context, aclrtRunMode& runMode) { +void CreateInferenceInstance(vector& threadTbl, int32_t i, + aclrtContext& context, aclrtRunMode& runMode) +{ AclLiteThreadParam param; param.threadInst = new InferenceThread(g_modelPath, g_modelWidth, - g_modelHeight, context); + g_modelHeight, context); param.threadInstName.assign(g_inferName[i].c_str()); param.context = context; param.runMode = runMode; @@ -141,9 +139,10 @@ void CreateInferenceInstance(vector& threadTbl, int32_t i, a } void CreatePostprocessInstances(vector& threadTbl, - uint32_t postNum, int32_t i, - aclrtContext& context, aclrtRunMode& runMode, - uint32_t outputHeight, uint32_t outputWidth) { + uint32_t postNum, int32_t i, + aclrtContext& context, aclrtRunMode& runMode, + uint32_t outputHeight, uint32_t outputWidth) +{ AclLiteThreadParam param; for (int j = 0; j < postNum; j++) { param.threadInst = new PostprocessThread(outputWidth, outputHeight); @@ -152,10 +151,11 @@ void CreatePostprocessInstances(vector& threadTbl, param.runMode = runMode; ACLLITE_LOG_INFO("Postprocess context is :%p", param.context); threadTbl.push_back(param); - } + } } -void CreateThreadInstance(vector& threadTbl, AclLiteResource& aclDev) { +void CreateThreadInstance(vector& threadTbl, AclLiteResource& aclDev) +{ string dataAddr; uint32_t postNum; uint32_t outputHeight; @@ -175,7 +175,7 @@ void CreateThreadInstance(vector& threadTbl, AclLiteResource } CreatePreprocessInstances(threadTbl, dataAddr, postNum, deviceNum, aclDev); - for (int32_t i=0; i < deviceNum; i++) { + for (int32_t i = 0; i < deviceNum; i++) { ret = aclrtSetDevice(i); if (ret != ACL_ERROR_NONE) { ACLLITE_LOG_ERROR("Acl open device %d failed", i); @@ -187,27 +187,29 @@ void CreateThreadInstance(vector& threadTbl, AclLiteResource ACLLITE_LOG_ERROR("Create acl context failed, error:%d", ret); return; } - g_context.push_back(context); + contextVector.push_back(context); CreateInferenceInstance(threadTbl, i, context, runMode); CreatePostprocessInstances(threadTbl, postNum, i, context, runMode, outputHeight, outputWidth); } } -void ExitApp(AclLiteApp& app, vector& threadTbl) { +void ExitApp(AclLiteApp& app, vector& threadTbl) +{ for (int i = 0; i < threadTbl.size(); i++) { delete threadTbl[i].threadInst; - } + } app.Exit(); - for (int i = 0; i < g_context.size(); i++) { - aclrtDestroyContext(g_context[i]); + for (int i = 0; i < contextVector.size(); i++) { + aclrtDestroyContext(contextVector[i]); if (i) { aclrtResetDevice(i); } } } -void StartApp(AclLiteResource& aclDev) { +void StartApp(AclLiteResource& aclDev) +{ vector threadTbl; CreateThreadInstance(threadTbl, aclDev); @@ -232,7 +234,8 @@ void StartApp(AclLiteResource& aclDev) { return; } -int main(int argc, char *argv[]) { +int main(int argc, char *argv[]) +{ AclLiteResource aclDev = AclLiteResource(0, "", false); AclLiteError ret = aclDev.Init(); if (ret != ACLLITE_OK) { diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/postprocess/postprocess.cpp b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/postprocess/postprocess.cpp index c54725acb..599654041 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/postprocess/postprocess.cpp +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/postprocess/postprocess.cpp @@ -35,23 +35,27 @@ namespace { 40, 156, 177, 49, 48, 79, 89, 122}; } -PostprocessThread::PostprocessThread(uint32_t outputWidth, uint32_t outputHeight) : -g_outputWidth_(outputWidth), g_outputHeight_(outputHeight) { +PostprocessThread::PostprocessThread(uint32_t outputWidth, uint32_t outputHeight) + : g_outputWidth_(outputWidth), g_outputHeight_(outputHeight) +{ } -PostprocessThread::~PostprocessThread() { +PostprocessThread::~PostprocessThread() +{ } -AclLiteError PostprocessThread::Init() { +AclLiteError PostprocessThread::Init() +{ return ACLLITE_OK; } -AclLiteError PostprocessThread::Process(int msgId, shared_ptr data) { +AclLiteError PostprocessThread::Process(int msgId, shared_ptr data) +{ struct timespec time5 = {0, 0}; struct timespec time6 = {0, 0}; AclLiteError ret = ACLLITE_OK; shared_ptr inferMsg = static_pointer_cast(data); - switch(msgId) { + switch (msgId) { case MSG_INFER_OUTPUT: clock_gettime(CLOCK_REALTIME, &time5); InferOutputProcess(inferMsg); @@ -69,8 +73,8 @@ AclLiteError PostprocessThread::Process(int msgId, shared_ptr data) { return ret; } -AclLiteError PostprocessThread::InferOutputProcess(shared_ptr inferMsg) { - +AclLiteError PostprocessThread::InferOutputProcess(shared_ptr inferMsg) +{ float* data = (float *)inferMsg->inferData[0].data.get(); if (data == nullptr) { ACLLITE_LOG_ERROR("inferoutput is null\n"); @@ -95,8 +99,7 @@ AclLiteError PostprocessThread::InferOutputProcess(shared_ptr in if (data[j] > 0.5) { // data[j] = kColors[i % kColors.size()]; data[j] = 1; - } - else { + } else { data[j] = 0; } } @@ -115,7 +118,7 @@ AclLiteError PostprocessThread::InferOutputProcess(shared_ptr in cv::Mat newChannels[3] = { mat_b_up, mat_g_up, mat_r_up }; cv::Mat resultImage; cv::merge(newChannels, 3, resultImage); - cv::addWeighted(resizeImage,1,resultImage,1,0,resizeImage); + cv::addWeighted(resizeImage, 1, resultImage, 1, 0, resizeImage); } } diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/postprocess/postprocess.h b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/postprocess/postprocess.h index 61e146e23..00ac8d3a3 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/postprocess/postprocess.h +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/postprocess/postprocess.h @@ -29,18 +29,16 @@ using namespace std; -class PostprocessThread: public AclLiteThread { +class PostprocessThread : public AclLiteThread { public: PostprocessThread(uint32_t outputWidth, uint32_t outputHeight); ~PostprocessThread(); AclLiteError Init(); - AclLiteError Process(int msgId, shared_ptr data); + AclLiteError Process(int msgId, shared_ptr data); private: AclLiteError InferOutputProcess(shared_ptr inferMsg); - // AclLiteError MsgSend(shared_ptr inferMsg, - // shared_ptr &postOutputMsg); private: uint32_t g_outputWidth_; diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/preprocess/preprocess.cpp b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/preprocess/preprocess.cpp index fa678f75f..86b5243ff 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/preprocess/preprocess.cpp +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/preprocess/preprocess.cpp @@ -29,74 +29,72 @@ vector::iterator imageFile; struct timespec time1 = {0, 0}; struct timespec time2 = {0, 0}; -PreprocessThread::PreprocessThread(string& filePath, uint32_t modelWidth, - uint32_t modelHeight, uint32_t postThreadNum, - uint32_t inferThreadNum, aclrtContext& context) : -g_filePath_(filePath), -g_context_(context), -g_modelWidth_(modelWidth), -g_modelHeight_(modelHeight), -g_selfThreadId_(INVALID_INSTANCE_ID), -g_inferThreadNum_(inferThreadNum), -g_postThreadNum_(postThreadNum), -g_inferChannel_(0), -g_postChannel_(0), -g_indexCount_(postThreadNum - 1), -g_frameCnt_(0) { +PreprocessThread::PreprocessThread(string& filePath, uint32_t modelWidth, + uint32_t modelHeight, uint32_t postThreadNum, + uint32_t inferThreadNum, aclrtContext& context) + : filePath_(filePath), context_(context), + modelWidth_(modelWidth), modelHeight_(modelHeight), + selfThreadId_(INVALID_INSTANCE_ID), inferThreadNum_(inferThreadNum), + postThreadNum_(postThreadNum), inferChannel_(0), + postChannel_(0), indexCount_(postThreadNum - 1), + frameCnt_(0) +{ for (int i = 0; i < inferThreadNum; i++) { - g_nextThreadId_.push_back(INVALID_INSTANCE_ID); + nextThreadId_.push_back(INVALID_INSTANCE_ID); for (int j = 0; j < postThreadNum; j++) { - g_postprocThreadId_.push_back(INVALID_INSTANCE_ID); + postprocThreadId_.push_back(INVALID_INSTANCE_ID); } } } -PreprocessThread::~PreprocessThread() { - aclError ret = aclrtSetCurrentContext(g_context_); +PreprocessThread::~PreprocessThread() +{ + aclError ret = aclrtSetCurrentContext(context_); if (ret != ACL_SUCCESS) { ACLLITE_LOG_ERROR("PreprocessThread destructor set context failed, error: %d", ret); } - g_dvpp_.DestroyResource(); + dvpp_.DestroyResource(); } -AclLiteError PreprocessThread::Init() { - GetAllFiles(g_filePath_, fileVec); +AclLiteError PreprocessThread::Init() +{ + GetAllFiles(filePath_, fileVec); if (fileVec.empty()) { - ACLLITE_LOG_ERROR("Failed to deal all empty path=%s.", g_filePath_.c_str()); + ACLLITE_LOG_ERROR("Failed to deal all empty path=%s.", filePath_.c_str()); } imageFile = fileVec.begin(); - aclError aclRet = aclrtGetRunMode(&g_runMode_); + aclError aclRet = aclrtGetRunMode(&runMode_); if (aclRet != ACL_ERROR_NONE) { ACLLITE_LOG_ERROR("acl get run mode failed"); return ACLLITE_ERROR_GET_RUM_MODE; } - aclRet = g_dvpp_.Init(); + aclRet = dvpp_.Init(); if (aclRet) { ACLLITE_LOG_ERROR("Dvpp init failed, error %d", aclRet); return ACLLITE_ERROR; } // Get the relevant thread instance id - g_selfThreadId_ = SelfInstanceId(); - if (g_selfThreadId_ == INVALID_INSTANCE_ID) { - ACLLITE_LOG_ERROR("Self instance id %d", g_selfThreadId_); + selfThreadId_ = SelfInstanceId(); + if (selfThreadId_ == INVALID_INSTANCE_ID) { + ACLLITE_LOG_ERROR("Self instance id %d", selfThreadId_); return ACLLITE_ERROR; } - for (int i = 0; i < g_inferThreadNum_; i++) { - g_nextThreadId_[i] = GetAclLiteThreadIdByName(g_inferName[i]); - if (g_nextThreadId_[i] == INVALID_INSTANCE_ID) { + for (int i = 0; i < inferThreadNum_; i++) { + nextThreadId_[i] = GetAclLiteThreadIdByName(g_inferName[i]); + if (nextThreadId_[i] == INVALID_INSTANCE_ID) { ACLLITE_LOG_ERROR("%d inference instance id %d", - i, g_nextThreadId_[i]); + i, nextThreadId_[i]); return ACLLITE_ERROR; } - for (int j = 0; j < g_postThreadNum_; j++) { - g_postprocThreadId_[i*g_postThreadNum_+j] = GetAclLiteThreadIdByName(g_postprocName[i*g_postThreadNum_+j]); - if (g_postprocThreadId_[i*g_postThreadNum_+j] == INVALID_INSTANCE_ID) { + for (int j = 0; j < postThreadNum_; j++) { + postprocThreadId_[i*postThreadNum_+j] = GetAclLiteThreadIdByName(g_postprocName[i*postThreadNum_+j]); + if (postprocThreadId_[i*postThreadNum_+j] == INVALID_INSTANCE_ID) { ACLLITE_LOG_ERROR("%d postprocess instance id %d", - i*g_postThreadNum_+j, g_postprocThreadId_[i*g_postThreadNum_+j]); + i*postThreadNum_+j, postprocThreadId_[i*postThreadNum_+j]); return ACLLITE_ERROR; } } @@ -105,11 +103,11 @@ AclLiteError PreprocessThread::Init() { return ACLLITE_OK; } -AclLiteError PreprocessThread::Process(int msgId, - shared_ptr msgData) { +AclLiteError PreprocessThread::Process(int msgId, shared_ptr msgData) +{ AclLiteError ret = ACLLITE_OK; shared_ptr preprocDataMsg = make_shared(); - switch(msgId) { + switch (msgId) { case MSG_APP_START: ret = AppStart(); break; @@ -129,28 +127,30 @@ AclLiteError PreprocessThread::Process(int msgId, return ret; } -AclLiteError PreprocessThread::AppStart() { - AclLiteError ret = SendMessage(g_selfThreadId_, MSG_READ_FRAME, nullptr); +AclLiteError PreprocessThread::AppStart() +{ + AclLiteError ret = SendMessage(selfThreadId_, MSG_READ_FRAME, nullptr); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Process app start message failed, error %d", ret); - } + } return ret; } -AclLiteError PreprocessThread::ReadFrame(shared_ptr &preprocDataMsg) { - g_inferChannel_ = g_frameCnt_ % g_inferThreadNum_; - if (g_inferChannel_ == 0) { - g_indexCount_ = (++g_indexCount_) % g_postThreadNum_; // postcount初始值为postnum-1; +AclLiteError PreprocessThread::ReadFrame(shared_ptr &preprocDataMsg) +{ + inferChannel_ = frameCnt_ % inferThreadNum_; + if (inferChannel_ == 0) { + indexCount_ = (++indexCount_) % postThreadNum_; // postcount初始值为postnum-1; } - g_postChannel_ = g_inferChannel_ * g_postThreadNum_ + g_indexCount_; + postChannel_ = inferChannel_ * postThreadNum_ + indexCount_; - preprocDataMsg->inferThreadId = g_nextThreadId_[g_inferChannel_]; - preprocDataMsg->postprocThreadId = g_postprocThreadId_[g_postChannel_]; + preprocDataMsg->inferThreadId = nextThreadId_[inferChannel_]; + preprocDataMsg->postprocThreadId = postprocThreadId_[postChannel_]; - preprocDataMsg->frameNum = g_frameCnt_; + preprocDataMsg->frameNum = frameCnt_; preprocDataMsg->isLastFrame = 0; - if (imageFile == fileVec.end()) { + if (imageFile == fileVec.end()) { preprocDataMsg->isLastFrame = 1; return ACLLITE_OK; } @@ -161,35 +161,36 @@ AclLiteError PreprocessThread::ReadFrame(shared_ptr &preprocData } preprocDataMsg->imageFileName = *imageFile; imageFile++; - g_frameCnt_++; + frameCnt_++; return ACLLITE_OK; } -AclLiteError PreprocessThread::MsgProcess(ImageData& imageFrame, - shared_ptr &preprocDataMsg) { +AclLiteError PreprocessThread::MsgProcess(ImageData& imageFrame, + shared_ptr &preprocDataMsg) +{ if (preprocDataMsg->isLastFrame == 1) return ACLLITE_OK; ImageData imageDevice; - AclLiteError ret = CopyImageToDevice(imageDevice, imageFrame, g_runMode_, MEMORY_DVPP); + AclLiteError ret = CopyImageToDevice(imageDevice, imageFrame, runMode_, MEMORY_DVPP); if (ret == ACLLITE_ERROR) { ACLLITE_LOG_ERROR("Copy image to device failed"); return ACLLITE_ERROR; } ImageData yuvImage; - ret = g_dvpp_.JpegD(yuvImage, imageDevice); + ret = dvpp_.JpegD(yuvImage, imageDevice); if (ret == ACLLITE_ERROR) { ACLLITE_LOG_ERROR("Convert jpeg to yuv failed"); return ACLLITE_ERROR; } ImageData resizedImage; - ret = g_dvpp_.Resize(resizedImage, yuvImage, g_modelWidth_, g_modelHeight_); + ret = dvpp_.Resize(resizedImage, yuvImage, modelWidth_, modelHeight_); if (ret == ACLLITE_ERROR) { ACLLITE_LOG_ERROR("g_dvpp_resize image failed"); return ACLLITE_ERROR; } - ret = CopyImageToLocal(preprocDataMsg->resizedMat, resizedImage, g_runMode_); + ret = CopyImageToLocal(preprocDataMsg->resizedMat, resizedImage, runMode_); if (ret == ACLLITE_ERROR) { ACLLITE_LOG_ERROR("Copy image to host failed"); return ACLLITE_ERROR; @@ -198,25 +199,25 @@ AclLiteError PreprocessThread::MsgProcess(ImageData& imageFrame, return ACLLITE_OK; } -AclLiteError PreprocessThread::MsgSend(shared_ptr &preprocDataMsg) { +AclLiteError PreprocessThread::MsgSend(shared_ptr &preprocDataMsg) +{ AclLiteError ret; if (preprocDataMsg->isLastFrame == 0) { ret = SendMessage(preprocDataMsg->inferThreadId, MSG_PREPROC_DATA, preprocDataMsg); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Send preprocess data failed, error %d", ret); } - ret = SendMessage(g_selfThreadId_, MSG_READ_FRAME, nullptr); + 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 < g_inferThreadNum_; i++) { - for (int j = 0; j < g_postThreadNum_; j++) { + } + } else { + for (int i = 0; i < inferThreadNum_; i++) { + for (int j = 0; j < postThreadNum_; j++) { shared_ptr preprocDataMsgEnd = make_shared(); - preprocDataMsgEnd->inferThreadId = g_nextThreadId_[i]; - preprocDataMsgEnd->postprocThreadId = g_postprocThreadId_[i*g_postThreadNum_+j]; + preprocDataMsgEnd->inferThreadId = nextThreadId_[i]; + preprocDataMsgEnd->postprocThreadId = postprocThreadId_[i*postThreadNum_+j]; ret = SendMessage(preprocDataMsgEnd->inferThreadId, MSG_PREPROC_END, preprocDataMsgEnd); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Send preprocess data failed, error %d", ret); diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/preprocess/preprocess.h b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/preprocess/preprocess.h index 7d6bdfab6..bc9eb8236 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/preprocess/preprocess.h +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/deeplabv3_multi_thread_multi_device/src/preprocess/preprocess.h @@ -28,9 +28,9 @@ class PreprocessThread : public AclLiteThread { public: - PreprocessThread(string& fileName, uint32_t modelWidth, - uint32_t modelHeight, uint32_t postThreadNum, - uint32_t inferThreadNum, aclrtContext& context); + PreprocessThread(string& fileName, uint32_t modelWidth, + uint32_t modelHeight, uint32_t postThreadNum, + uint32_t inferThreadNum, aclrtContext& context); ~PreprocessThread(); AclLiteError Init(); @@ -43,21 +43,21 @@ private: AclLiteError ReadFrame(shared_ptr &preprocDataMsg); private: - string g_filePath_; - aclrtRunMode g_runMode_; - uint32_t g_modelWidth_; - uint32_t g_modelHeight_; - int g_selfThreadId_; - vector g_nextThreadId_; - vector g_postprocThreadId_; - uint32_t g_inferThreadNum_; - uint32_t g_postThreadNum_; - int g_frameCnt_; - AclLiteImageProc g_dvpp_; - int g_inferChannel_; - int g_postChannel_; - int g_indexCount_; - aclrtContext g_context_; + string filePath_; + aclrtRunMode runMode_; + uint32_t modelWidth_; + uint32_t modelHeight_; + int selfThreadId_; + vector nextThreadId_; + vector postprocThreadId_; + uint32_t inferThreadNum_; + uint32_t postThreadNum_; + int frameCnt_; + AclLiteImageProc dvpp_; + int inferChannel_; + int postChannel_; + int indexCount_; + aclrtContext context_; }; #endif \ No newline at end of file -- Gitee From 2157bc42cef9f8700c1967711e106cf0a636c072 Mon Sep 17 00:00:00 2001 From: yy0131 <1595992831@qq.com> Date: Tue, 11 Oct 2022 16:10:30 +0800 Subject: [PATCH 09/14] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../YOLOV3_carColor_sample/inc/AclLiteUtils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteUtils.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteUtils.h index 8a10e7ae9..46ef63011 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteUtils.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteUtils.h @@ -38,7 +38,7 @@ * @param [in]: height: image height * @return bytes size of image */ -#define RGBU8_IMAGE_SIZE(width, height) ((width) * (height) * 3) +#define YUV420SP_SIZE(width, height) ((width) * (height) * 3 / 2) /** * @brief generate shared pointer of dvpp memory -- Gitee From f435a451e1bfeded14446b9ca05e763c449a0c91 Mon Sep 17 00:00:00 2001 From: yy0131 <1595992831@qq.com> Date: Tue, 11 Oct 2022 16:32:05 +0800 Subject: [PATCH 10/14] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=A7=84=E8=8C=83?= =?UTF-8?q?=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../YOLOV3_coco_detection_VENC/src/utils.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/utils.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/utils.cpp index c7a905de1..347bbe156 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/utils.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_VENC/src/utils.cpp @@ -75,10 +75,10 @@ bool Utils::IsPathExist(const string &path) void Utils::SplitPath(const string &path, vector &path_vec) { - char *tmp_path = strtok(const_castpath.c_str(), g_imagePathSeparator.c_str()); + char *tmp_path = strtok(const_cast(path.c_str()), g_imagePathSeparator.c_str()); while (tmp_path) { path_vec.emplace_back(tmp_path); - tmp_path = strtok(nullptr, char_split); + tmp_path = strtok(nullptr, g_imagePathSeparator.c_str()); } } -- Gitee From 46f57fb2591cdbf757103c8afa477f7c8f4745a0 Mon Sep 17 00:00:00 2001 From: yy0131 <1595992831@qq.com> Date: Tue, 11 Oct 2022 19:32:21 +0800 Subject: [PATCH 11/14] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=A7=84=E8=8C=83?= =?UTF-8?q?=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../YOLOV3_coco_detection_multi_thread_VENC/src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/src/main.cpp b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/src/main.cpp index 353352e53..5667d3f0e 100644 --- a/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/src/main.cpp +++ b/cplusplus/level2_simple_inference/n_performance/1_multi_process_thread/YOLOV3_coco_detection_multi_thread_VENC/src/main.cpp @@ -161,7 +161,7 @@ void Postprocess(cv::VideoWriter& outputVideo, aclrtContext context, Queue Date: Mon, 17 Oct 2022 08:57:02 +0800 Subject: [PATCH 12/14] =?UTF-8?q?310P=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../YOLOV3_carColor_sample/inc/AclLiteUtils.h | 7 ++++++ .../src/CropAndPasteHelper.cpp | 8 +++---- .../src/JpegDHelper.cpp | 22 ++++++++++++++----- .../inc/utils.h | 1 + .../src/dvpp_jpegd.cpp | 17 ++++++++++---- .../src/CMakeLists.txt | 1 + 6 files changed, 42 insertions(+), 14 deletions(-) diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteUtils.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteUtils.h index 46ef63011..3b0e50c13 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteUtils.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/inc/AclLiteUtils.h @@ -83,6 +83,13 @@ */ #define ALIGN_UP16(num) ALIGN_UP(num, 16) +/** + * @brief calculate number align with 64 + * @param [in]: num: the original number that to aligned + * @return the number after aligned + */ +#define ALIGN_UP64(num) ALIGN_UP(num, 64) + /** * @brief calculate number align with 128 * @param [in]: num: the original number that to aligned diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/CropAndPasteHelper.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/CropAndPasteHelper.cpp index 6376f6542..dcf53cb0a 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/CropAndPasteHelper.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/CropAndPasteHelper.cpp @@ -42,8 +42,8 @@ CropAndPasteHelper::CropAndPasteHelper(aclrtStream& stream, g_rbHorz_ = ((rbHorz >> 1) << 1) - 1; g_rbVert_ = ((rbVert >> 1) << 1) - 1; // odd - g_size_.width = g_rbHorz_ - g_ltHorz_; - g_size_.height = g_rbVert_ - g_ltVert_; + g_size_.width = g_rbHorz_ - g_ltHorz_ + 1; + g_size_.height = g_rbVert_ - g_ltVert_ + 1; } CropAndPasteHelper::CropAndPasteHelper(aclrtStream& stream, @@ -190,9 +190,9 @@ AclLiteError CropAndPasteHelper::Process(ImageData& cropedImage, ImageData& srcI // must even uint32_t pasteTopOffset = 0; // must odd - uint32_t pasteRightOffset = g_size_.width; + uint32_t pasteRightOffset = g_rbHorz_ - g_ltHorz_; // must odd - uint32_t pasteBottomOffset = g_size_.height; + uint32_t pasteBottomOffset = g_rbVert_ - g_ltVert_; g_pasteArea_ = acldvppCreateRoiConfig(pasteLeftOffset, pasteRightOffset, pasteTopOffset, pasteBottomOffset); diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/JpegDHelper.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/JpegDHelper.cpp index 4c63a8e76..358a555f2 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/JpegDHelper.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_carColor_sample/src/JpegDHelper.cpp @@ -15,6 +15,7 @@ */ #include +#include #include "acl/acl.h" #include "JpegDHelper.h" @@ -60,8 +61,8 @@ AclLiteError JpegDHelper::InitDecodeOutputDesc(ImageData& inputImage) acldvppSetPicDescData(g_decodeOutputDesc_, g_decodeOutBufferDev_); acldvppSetPicDescFormat(g_decodeOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); - acldvppSetPicDescWidth(g_decodeOutputDesc_, inputImage.width); - acldvppSetPicDescHeight(g_decodeOutputDesc_, inputImage.height); + acldvppSetPicDescWidth(g_decodeOutputDesc_, ALIGN_UP2(inputImage.width)); + acldvppSetPicDescHeight(g_decodeOutputDesc_, ALIGN_UP2(inputImage.height)); acldvppSetPicDescWidthStride(g_decodeOutputDesc_, decodeOutWidthStride); acldvppSetPicDescHeightStride(g_decodeOutputDesc_, decodeOutHeightStride); acldvppSetPicDescSize(g_decodeOutputDesc_, decodeOutBufferSize); @@ -91,10 +92,19 @@ AclLiteError JpegDHelper::Process(ImageData& dest, ImageData& src) return ACLLITE_ERROR_SYNC_STREAM; } dest.format = PIXEL_FORMAT_YUV_SEMIPLANAR_420; - dest.width = src.width; - dest.height = src.height; - dest.alignWidth = ALIGN_UP128(src.width); - dest.alignHeight = ALIGN_UP16(src.height); + auto socVersion = aclrtGetSocName(); + ACLLITE_LOG_INFO("Current soc version is %s", socVersion); + if (strncmp(socVersion, "Ascend310P3", sizeof("Ascend310P3") - 1) == 0) { + dest.width = ALIGN_UP2(src.width); + dest.height = ALIGN_UP2(src.height); + dest.alignWidth = ALIGN_UP64(src.width); // 64-byte alignment + dest.alignHeight = ALIGN_UP16(src.height); // 16-byte alignment + } else { + dest.width = src.width; + dest.height = src.height; + dest.alignWidth = ALIGN_UP128(src.width); // 128-byte alignment + dest.alignHeight = ALIGN_UP16(src.height); // 16-byte alignment + } dest.size = YUV420SP_SIZE(dest.alignWidth, dest.alignHeight); dest.data = SHARED_PTR_DVPP_BUF(g_decodeOutBufferDev_); diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/utils.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/utils.h index 944cbc129..09bdfcfe4 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/utils.h +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/inc/utils.h @@ -38,6 +38,7 @@ using namespace std; #define ALIGN_UP(num, align) (((num) + (align) - 1) & ~((align) - 1)) #define ALIGN_UP2(num) ALIGN_UP(num, 2) #define ALIGN_UP16(num) ALIGN_UP(num, 16) +#define ALIGN_UP64(num) ALIGN_UP(num, 64) #define ALIGN_UP128(num) ALIGN_UP(num, 128) #define SHARED_PTR_DVPP_BUF(buf) (shared_ptr((uint8_t *)(buf), [](uint8_t* p) { acldvppFree(p); })) diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_jpegd.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_jpegd.cpp index ef0aec206..72bc9eeb6 100644 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_jpegd.cpp +++ b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_dynamic_AIPP/src/dvpp_jpegd.cpp @@ -87,10 +87,19 @@ Result DvppJpegD::Process(ImageData& dest, ImageData& src) return FAILED; } - dest.width = src.width; - dest.height = src.height; - dest.alignWidth = ALIGN_UP128(src.width); - dest.alignHeight = ALIGN_UP16(src.height); + auto socVersion = aclrtGetSocName(); + INFO_LOG("Current soc version is %s", socVersion); + if (strncmp(socVersion, "Ascend310P3", sizeof("Ascend310P3") - 1) == 0) { + dest.width = ALIGN_UP2(src.width); + dest.height = ALIGN_UP2(src.height); + dest.alignWidth = ALIGN_UP64(src.width); // 64-byte alignment + dest.alignHeight = ALIGN_UP16(src.height); // 16-byte alignment + } else { + dest.width = src.width; + dest.height = src.height; + dest.alignWidth = ALIGN_UP128(src.width); // 128-byte alignment + dest.alignHeight = ALIGN_UP16(src.height); // 16-byte alignment + } dest.size = YUV420SP_SIZE(dest.alignWidth, dest.alignHeight); dest.data = SHARED_PTR_DVPP_BUF(g_decodeOutBufferDev_); INFO_LOG("convert image success"); diff --git a/cplusplus/level2_simple_inference/6_other/acl_compile_and_execute_conv2d/src/CMakeLists.txt b/cplusplus/level2_simple_inference/6_other/acl_compile_and_execute_conv2d/src/CMakeLists.txt index 675c354a1..45bf9c940 100644 --- a/cplusplus/level2_simple_inference/6_other/acl_compile_and_execute_conv2d/src/CMakeLists.txt +++ b/cplusplus/level2_simple_inference/6_other/acl_compile_and_execute_conv2d/src/CMakeLists.txt @@ -52,6 +52,7 @@ include_directories( # add host lib path link_directories( $ENV{INSTALL_DIR}/compiler/lib64/stub/ + $ENV{INSTALL_DIR}/atc/lib64/ ) add_executable(main -- Gitee From 830edb38359fa1207cc75c7193af0742e7eaf46c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E8=82=96=E9=98=B3?= <1971168308@qq.com> Date: Thu, 20 Oct 2022 02:13:09 +0000 Subject: [PATCH 13/14] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20cp?= =?UTF-8?q?lusplus/level2=5Fsimple=5Finference/2=5Fobject=5Fdetection/YOLO?= =?UTF-8?q?V3=5FVOC=5Fdetection=5Fpicture/objectdetection=5Fyolov3pb.iml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../objectdetection_yolov3pb.iml | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_VOC_detection_picture/objectdetection_yolov3pb.iml diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_VOC_detection_picture/objectdetection_yolov3pb.iml b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_VOC_detection_picture/objectdetection_yolov3pb.iml deleted file mode 100644 index b6553c63c..000000000 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_VOC_detection_picture/objectdetection_yolov3pb.iml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - \ No newline at end of file -- Gitee From d6f656dbee7e57e25405935f9d1f388fd275cde4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E8=82=96=E9=98=B3?= <1971168308@qq.com> Date: Thu, 20 Oct 2022 02:13:24 +0000 Subject: [PATCH 14/14] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20cp?= =?UTF-8?q?lusplus/level2=5Fsimple=5Finference/2=5Fobject=5Fdetection/YOLO?= =?UTF-8?q?V3=5FVOC=5Fdetection=5Fpicture/src/acl.json?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2_object_detection/YOLOV3_VOC_detection_picture/src/acl.json | 1 - 1 file changed, 1 deletion(-) delete mode 100644 cplusplus/level2_simple_inference/2_object_detection/YOLOV3_VOC_detection_picture/src/acl.json diff --git a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_VOC_detection_picture/src/acl.json b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_VOC_detection_picture/src/acl.json deleted file mode 100644 index 0967ef424..000000000 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_VOC_detection_picture/src/acl.json +++ /dev/null @@ -1 +0,0 @@ -{} -- Gitee