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 100755 index b6553c63c9b451c139f89936949d065830f369e0..0000000000000000000000000000000000000000 --- 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 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 100755 index 0967ef424bce6791893e9a57bb952f80fd536e93..0000000000000000000000000000000000000000 --- a/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_VOC_detection_picture/src/acl.json +++ /dev/null @@ -1 +0,0 @@ -{} 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 254498708002d505acc4e5c9674eba032285e6c2..888087ba312e3a4a042d685c3a2b185d0c1ddf90 --- 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 @@ -39,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; @@ -91,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; @@ -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 old mode 100755 new mode 100644 index ed573e209f40e8871cdefb38cd6fb8d2b37588b9..aefcb1eff5ac832a9ef53b7f536651a9d15ce789 --- 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" @@ -33,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, @@ -42,12 +44,14 @@ 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: - 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 old mode 100755 new mode 100644 index 62e6c23384d1f7082f1abf7bb308b35ced980eb7..3a2033a9ea737e34b8082c154dcb3baf22402f99 --- 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,11 +30,11 @@ 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, + AclLiteError Execute(std::vector& inferOutputs, void *data, uint32_t size); AclLiteError Execute(std::vector& inferOutputs); void DestroyInput(); @@ -42,28 +43,30 @@ 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 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 old mode 100755 new mode 100644 index 9e3386b363e5c11b232e0ba73bda6d126f627487..6e7cdbbd8238aa0fa3131b8db2492242f7a0e23b --- 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 @@ -25,17 +26,19 @@ class AclLiteResource { public: - AclLiteResource(); + AclLiteResource(); ~AclLiteResource(); AclLiteError Init(); 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 old mode 100755 new mode 100644 index d69254f642e68dcc3d34d31042e4d1b5e80423ae..30b885f68476d2021cff4594bc62a1efa620a5bd --- 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 @@ -27,7 +28,7 @@ enum MemoryType { MEMORY_NORMAL = 0, MEMORY_HOST, MEMORY_DEVICE, - MEMORY_DVPP, + MEMORY_DVPP, MEMORY_INVALID_TYPE }; @@ -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 old mode 100755 new mode 100644 index f23d8b90afe5e2ab0fe8ae1eb487120f89a66aff..3b0e50c131a13717b546d6716cb4dc7c61ce6411 --- 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 @@ -63,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)) @@ -82,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 @@ -95,8 +103,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 @@ -130,10 +138,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); /** @@ -144,11 +152,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); /** @@ -160,7 +168,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); /** @@ -169,66 +177,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); \ 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 old mode 100755 new mode 100644 index 03dc90e505f0850d72b775eaaaa2bee2ed3c8f96..feb896fa5b41067d13426a115881cfedd61a198b --- 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 @@ -23,7 +24,7 @@ #include "acl/ops/acl_dvpp.h" class CropAndPasteHelper { - public: +public: /** * @brief Constructor * @param [in] stream: stream @@ -38,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); /** @@ -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 old mode 100755 new mode 100644 index 9f1e231d8ce9d2724458ee260b4cbc8f9c675f9c..5179c6d2335ef3bed90ed850652badc762b97401 --- 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 @@ -57,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); /** @@ -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 old mode 100755 new mode 100644 index 28854f5de78b787137f1b740944b1e84932f08ff..4f161de2e23871d27f94413f68936f8b50152b3b --- 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 26fff6765b996bbc3482e7cfe2ad4363d35ba313..ade135a5f9a25878e68e8f31ad3ab4c13a24212f 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,13 +12,14 @@ * 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 +#include "acl/acl.h" #include "AclLiteModel.h" #include "AclLiteImageProc.h" #include "object_detect.h" @@ -31,29 +32,31 @@ 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(); 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); 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 626a4422ac515ac30bf1207136e38033363a1cd7..cf282021eb8ca173e17e0d2699674eabc736aef6 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,20 +12,21 @@ * 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 +#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" /** @@ -36,23 +37,23 @@ 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); private: void DestroyResource(); 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 599ed4a2b5c01b95ba5a55b0757f54c85b9c3291..1ea5208ef07d0dff62a413691f4df74bf1831ab9 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,12 +1,31 @@ -#include "acl/acl.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_CARCOLOR_SAMPLE_INC_PARAMS_H +#define YOLOV3_CARCOLOR_SAMPLE_INC_PARAMS_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 @@ -15,9 +34,11 @@ struct Rectangle { struct CarInfo { ImageData cropedImgs; // cropped image from original image - ImageData resizedImgs; //resized image for inference - Rectangle rectangle; - std::string text; //类别 - std::string carColor_result; //颜色 - float confidence; //颜色 -}; \ No newline at end of file + ImageData resizedImgs; // resized image for inference + Rectangle rectangle; + 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 old mode 100755 new mode 100644 index b6b39bd8d9812548a9a2f0e843fb91955db121f1..a12508f78db66bebed133022e642f05a4196ec7c --- 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" @@ -25,85 +23,92 @@ using namespace std; -AclLiteImageProc::AclLiteImageProc(): - isReleased_(false), - stream_(nullptr), - dvppChannelDesc_(nullptr), - isInitOk_(false) { +AclLiteImageProc::AclLiteImageProc() + : g_isReleased_(false), + g_stream_(nullptr), + g_dvppChannelDesc_(nullptr), + g_isInitOk_(false) +{ } -AclLiteImageProc::~AclLiteImageProc() { +AclLiteImageProc::~AclLiteImageProc() +{ DestroyResource(); } -void AclLiteImageProc::DestroyResource() { - if (isReleased_) { +void AclLiteImageProc::DestroyResource() +{ + 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_); +AclLiteError AclLiteImageProc::Init() +{ + 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; } AclLiteError AclLiteImageProc::Resize(ImageData& dest, ImageData& src, - uint32_t width, uint32_t height) { - ResizeHelper resizeOp(stream_, dvppChannelDesc_, width, 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) { - JpegDHelper jpegD(stream_, dvppChannelDesc_); +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(stream_, dvppChannelDesc_, + uint32_t rbHorz, uint32_t rbVert) +{ + CropAndPasteHelper crop(g_stream_, g_dvppChannelDesc_, ltHorz, ltVert, rbHorz, rbVert); return crop.Process(dest, src); } @@ -111,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(stream_, 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 9887de360ecd894224b7bed0c7ae7e4380132a1f..53b02addb685ac131a95214f6a99f573fb652a04 --- 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,38 +12,39 @@ * 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 "AclLiteModel.h" #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) { +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() { - if (isReleased_) { +void AclLiteModel::DestroyResource() +{ + if (g_isReleased_) { return; } @@ -51,19 +52,20 @@ void AclLiteModel::DestroyResource() { DestroyDesc(); DestroyInput(); DestroyOutput(); - isReleased_ = true; + g_isReleased_ = true; } -AclLiteError AclLiteModel::Init() { - aclError aclRet = aclrtGetRunMode(&runMode_); +AclLiteError AclLiteModel::Init() +{ + 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 +81,44 @@ 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_) { +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; } - 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", + 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) { - ACLLITE_LOG_ERROR("Create model(%s) description failed", - modelPath_.c_str()); +AclLiteError AclLiteModel::SetDesc() +{ + g_modelDesc_ = aclmdlCreateDesc(); + if (g_modelDesc_ == nullptr) { + ACLLITE_LOG_ERROR("Create model(%s) description failed", + 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()); + ACLLITE_LOG_ERROR("Get model(%s) description failed", + g_modelPath_.c_str()); return ACLLITE_ERROR_GET_MODEL_DESC; } @@ -124,25 +128,27 @@ 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; } } -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) { - 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 +159,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", + "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 +187,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 +212,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 +221,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,15 +267,15 @@ 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); + AclLiteError ret = GetOutputItem(out, i); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Get the %dth interference output failed, " "error: %d", i, ret); @@ -283,7 +289,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); @@ -304,8 +310,8 @@ AclLiteError AclLiteModel::GetOutputItem(InferenceOutput& out, return ACLLITE_ERROR_GET_DATA_BUFFER_SIZE; } - void* data = CopyDataToHost(dataBufferDev, bufferSize, - runMode_, MEMORY_NORMAL); + void* data = CopyDataToHost(dataBufferDev, bufferSize, + g_runMode_, MEMORY_NORMAL); if (data == nullptr) { ACLLITE_LOG_ERROR("Copy inference output to host failed"); return ACLLITE_ERROR_COPY_DATA; @@ -319,67 +325,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 old mode 100755 new mode 100644 index a8dab3a4e3c757e2887095fe316156506404065d..f43033b5373f1aff35e93f5a8401ffb2425c40cb --- 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,31 +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 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_("") { +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(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 +45,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); } @@ -74,26 +75,27 @@ AclLiteError AclLiteResource::Init() { return ACLLITE_OK; } -void AclLiteResource::Release() { - if (isReleased_) { +void AclLiteResource::Release() +{ + 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 +103,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 old mode 100755 new mode 100644 index d29d61bb53e3a3c58e36337c6ae077fa3e802853..e60c9a35926cf690936c5f04d44a6d426ec98168 --- 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,11 +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 #include @@ -28,6 +25,7 @@ #include #include #include +#include "AclLiteUtils.h" #include "acl/acl.h" #include "acl/ops/acl_dvpp.h" @@ -39,13 +37,14 @@ 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) { +bool IsPathExist(const string &path) +{ ifstream file(path); if (!file) { return false; @@ -53,10 +52,11 @@ 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) != kStatSuccess) { + if (stat(path.c_str(), &buf) != g_StatSuccess) { return false; } @@ -64,9 +64,10 @@ 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 = kImagePathSeparator.c_str(); + const char *charSplit = g_ImagePathSeparator.c_str(); char *imageFile = strtok(charPath, charSplit); while (imageFile) { pathVec.emplace_back(imageFile); @@ -74,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)) { @@ -86,7 +88,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); @@ -95,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); @@ -118,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); @@ -147,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); @@ -206,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, " @@ -238,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; @@ -255,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) { @@ -270,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) { @@ -288,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(); @@ -323,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; @@ -331,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 f6a40e3adccaaa6f68daadaef91824224ac1f4cf..dcf53cb0a1f5fbfae70cd3edbb1d249ce7d42172 --- 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" @@ -24,59 +22,63 @@ 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): - stream_(stream), - vpcOutBufferDev_(nullptr), - cropArea_(nullptr), - pasteArea_(nullptr), - vpcInputDesc_(nullptr), - vpcOutputDesc_(nullptr), - dvppChannelDesc_(dvppChannelDesc), - 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 - 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_ + 1; + g_size_.height = g_rbVert_ - g_ltVert_ + 1; } -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): - stream_(stream), - vpcOutBufferDev_(nullptr), - cropArea_(nullptr), - pasteArea_(nullptr), - vpcInputDesc_(nullptr), - vpcOutputDesc_(nullptr), - dvppChannelDesc_(dvppChannelDesc), - vpcOutBufferSize_(0) { - // Change the left top coordinate to even numver - ltHorz_ = (ltHorz >> 1) << 1; - 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; + 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) { - originalImageWidth_ = inputImage.width; - originalImageHeight_ = inputImage.height; +AclLiteError CropAndPasteHelper::InitCropAndPasteInputDesc(ImageData& inputImage) +{ + g_originalImageWidth_ = inputImage.width; + g_originalImageHeight_ = inputImage.height; uint32_t alignWidth = inputImage.alignWidth; uint32_t alignHeight = inputImage.alignHeight; if (alignWidth == 0 || alignHeight == 0) { @@ -87,64 +89,66 @@ 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; +AclLiteError CropAndPasteHelper::InitCropAndPasteOutputDesc() +{ + 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, - cropOutHeightStride); - aclError aclRet = acldvppMalloc(&vpcOutBufferDev_, vpcOutBufferSize_); + 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", - size_.width, size_.height, - vpcOutBufferSize_, aclRet); + "width %d, height %d size %d, error %d", + 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; } -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; @@ -158,25 +162,26 @@ 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; } // 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, - cropTopOffset, cropBottomOffset); - if (cropArea_ == nullptr) { - ACLLITE_LOG_ERROR("acldvppCreateRoiConfig cropArea_ failed"); + g_cropArea_ = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset, + cropTopOffset, cropBottomOffset); + if (g_cropArea_ == nullptr) { + ACLLITE_LOG_ERROR("acldvppCreateRoiConfig g_cropArea_ failed"); return ACLLITE_ERROR; } @@ -185,38 +190,38 @@ AclLiteError CropAndPasteHelper::Process(ImageData& cropedImage, ImageData& srcI // must even uint32_t pasteTopOffset = 0; // must odd - uint32_t pasteRightOffset = size_.width; + uint32_t pasteRightOffset = g_rbHorz_ - g_ltHorz_; // must odd - uint32_t pasteBottomOffset = size_.height; + uint32_t pasteBottomOffset = g_rbVert_ - g_ltVert_; - pasteArea_ = acldvppCreateRoiConfig(pasteLeftOffset, pasteRightOffset, - pasteTopOffset, pasteBottomOffset); - if (pasteArea_ == nullptr) { - ACLLITE_LOG_ERROR("acldvppCreateRoiConfig pasteArea_ failed"); + g_pasteArea_ = acldvppCreateRoiConfig(pasteLeftOffset, pasteRightOffset, + pasteTopOffset, pasteBottomOffset); + 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(); @@ -231,18 +236,18 @@ AclLiteError CropAndPasteHelper::ProcessCropPaste(ImageData& cropedImage, ImageD } // 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, - cropTopOffset, cropBottomOffset); - if (cropArea_ == nullptr) { - ACLLITE_LOG_ERROR("acldvppCreateRoiConfig cropArea_ failed"); + g_cropArea_ = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset, + cropTopOffset, cropBottomOffset); + if (g_cropArea_ == nullptr) { + ACLLITE_LOG_ERROR("acldvppCreateRoiConfig g_cropArea_ failed"); return ACLLITE_ERROR; } @@ -251,62 +256,63 @@ 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, - pasteTopOffset, pasteBottomOffset); - if (pasteArea_ == nullptr) { - ACLLITE_LOG_ERROR("acldvppCreateRoiConfig pasteArea_ failed"); + g_pasteArea_ = acldvppCreateRoiConfig(pasteLeftOffset, pasteRightOffset, + pasteTopOffset, pasteBottomOffset); + 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; } -void CropAndPasteHelper::DestroyCropAndPasteResource() { - if (cropArea_ != nullptr) { - (void)acldvppDestroyRoiConfig(cropArea_); - cropArea_ = nullptr; +void CropAndPasteHelper::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; } } \ 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 old mode 100755 new mode 100644 index 7401e97761a8d26ed39ce95d5ae52f7f0a56276b..358a555f272a45152c33e79c20b640fc200247eb --- 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,29 +12,30 @@ * 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" #include "JpegDHelper.h" using namespace std; -JpegDHelper::JpegDHelper(aclrtStream& stream, acldvppChannelDesc *dvppChannelDesc): - stream_(stream), - decodeOutBufferDev_(nullptr), - decodeOutputDesc_(nullptr), - 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) { @@ -43,66 +44,77 @@ AclLiteError JpegDHelper::InitDecodeOutputDesc(ImageData& inputImage) { return ACLLITE_ERROR_INVALID_ARGS; } - uint32_t decodeOutBufferSize = + 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_, ALIGN_UP2(inputImage.width)); + acldvppSetPicDescHeight(g_decodeOutputDesc_, ALIGN_UP2(inputImage.height)); + acldvppSetPicDescWidthStride(g_decodeOutputDesc_, decodeOutWidthStride); + acldvppSetPicDescHeightStride(g_decodeOutputDesc_, decodeOutHeightStride); + acldvppSetPicDescSize(g_decodeOutputDesc_, decodeOutBufferSize); 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(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; } 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(decodeOutBufferDev_); + dest.data = SHARED_PTR_DVPP_BUF(g_decodeOutBufferDev_); return ACLLITE_OK; } -void JpegDHelper::DestroyDecodeResource() { - if (decodeOutputDesc_ != nullptr) { - acldvppDestroyPicDesc(decodeOutputDesc_); - decodeOutputDesc_ = nullptr; +void JpegDHelper::DestroyDecodeResource() +{ + 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 old mode 100755 new mode 100644 index fc71264952d038ac19c8fc95665ad5c03a72f6e5..b6bab70048ddb6ef344ab1057470d6d119b29af3 --- 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 @@ -25,24 +22,27 @@ 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; + 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) { @@ -52,62 +52,64 @@ 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; +AclLiteError ResizeHelper::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) { 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) { +AclLiteError ResizeHelper::InitResizeResource(ImageData& inputImage) +{ + g_resizeConfig_ = acldvppCreateResizeConfig(); + if (g_resizeConfig_ == nullptr) { ACLLITE_LOG_ERROR("Dvpp resize init failed for create config failed"); return ACLLITE_ERROR_CREATE_RESIZE_CONFIG; } @@ -116,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"); @@ -135,44 +138,45 @@ 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(); return ACLLITE_OK; } -void ResizeHelper::DestroyResizeResource() { - if (resizeConfig_ != nullptr) { - (void)acldvppDestroyResizeConfig(resizeConfig_); - resizeConfig_ = nullptr; +void ResizeHelper::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_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 ae12855c03541451f0ee3e571f195ea7f0548dbf..194a85c8fa62e804a76c5bd71cf63a045ba76995 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,14 +12,12 @@ * 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 #include +#include "color_classify.h" #include "acl/acl.h" #include "opencv2/opencv.hpp" #include "opencv2/imgproc/types_c.h" @@ -27,64 +25,67 @@ 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){ +ColorClassify::ColorClassify() + : g_model_(g_modelPath), + g_batchSize_(g_batch), + g_isInited_(false), + g_isReleased_(false) +{ } -ColorClassify::~ColorClassify() { +ColorClassify::~ColorClassify() +{ DestroyResource(); } -AclLiteError ColorClassify::InitModelInput() { - aclError aclRet = aclrtGetRunMode(&runMode_); - inputSize_ = YUV420SP_SIZE(kModelWidth, kModelHeight) * batchSize_; +AclLiteError ColorClassify::InitModelInput() +{ + 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); + "error %d", aclRet); return ACLLITE_ERROR; } - inputBuf_ = (uint8_t *)buf; + g_inputBuf_ = (uint8_t *)buf; return ACLLITE_OK; } - -AclLiteError ColorClassify::Init() { - if (isInited_) { +AclLiteError ColorClassify::Init() +{ + 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,23 +97,24 @@ AclLiteError ColorClassify::Init() { return ACLLITE_ERROR; } - isInited_ = true; + g_isInited_ = true; 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 = 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); + 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) { 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; } @@ -121,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 = 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; @@ -135,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 * 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, + 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_) { - for (uint32_t k = 0; - k < batchSize_ - j && bufferSize > totalSize; + if (j < g_batchSize_) { + 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 == kInvalidSize) { - return kInvalidSize; + dataLen = CopyImageData(buffer + totalSize, + bufferSize - totalSize, + carInfo[carInfo.size() - 1].resizedImgs); + if (dataLen == g_invalidSize) { + return g_invalidSize; } totalSize += dataLen; } @@ -170,21 +173,22 @@ 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(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 +196,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; @@ -205,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, 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_, - 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 = 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,9 +240,9 @@ AclLiteError ColorClassify::Inference(vector &carInfo, return ACLLITE_OK; } -AclLiteError ColorClassify::Postprocess(std::vector& inferenceOutput, - std::vector& carInfo, const string& origImagePath) { - +AclLiteError ColorClassify::PostProcess(std::vector& inferenceOutput, + std::vector& carInfo, const string& origImagePath) +{ void* data = (void *)inferenceOutput[0].data.get(); if (data == nullptr) { return ACLLITE_ERROR; @@ -246,56 +250,57 @@ 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); } 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, - 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); - 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); + 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("/"); - 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; } - -} +} \ 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 0fe098318d11873cfe1b5b1daa844206696a51c7..91a91fb16ecc4bdc4c0244faaa3f80b7dd11b392 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 @@ -27,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; } @@ -38,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(); @@ -71,10 +70,10 @@ 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()); + imageFile.c_str()); continue; } std::vector detectInferOutput; @@ -84,19 +83,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,12 +105,11 @@ 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; } - } 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 22c998367065f441f4e2b0ebecc060096b16f5cb..8240b88b47abc173ceae9ee4b281c219d39db4e1 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,12 +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 object_detect.cpp */ -#include "object_detect.h" + #include #include +#include "object_detect.h" #include "acl/acl.h" #include "opencv2/opencv.hpp" #include "opencv2/imgproc/types_c.h" @@ -25,33 +24,36 @@ using namespace std; namespace { -const uint32_t kBBoxDataBufId = 0; -const uint32_t kBoxNumDataBufId = 1; -enum BBoxIndex { TOPLEFTX = 0, TOPLEFTY, BOTTOMRIGHTX, BOTTOMRIGHTY, SCORE, LABEL }; -uint32_t kModelWidth = 416; -uint32_t kModelHeight = 416; -const char* kModelPath = "../model/yolov3.om"; +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"; } -ObjectDetect::ObjectDetect(): -model_(kModelPath), -isInited_(false), -isReleased_(false){ - imageInfoSize_ = 0; - imageInfoBuf_ = nullptr; +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)kModelWidth, (float)kModelHeight, - (float)kModelWidth, (float)kModelHeight}; - imageInfoSize_ = sizeof(imageInfo); - imageInfoBuf_ = CopyDataToDevice((void *)imageInfo, imageInfoSize_, - runMode_, MEMORY_DEVICE); - if (imageInfoBuf_ == nullptr) { +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); + if (g_imageInfoBuf_ == nullptr) { ACLLITE_LOG_ERROR("Copy image info to device failed"); return ACLLITE_ERROR; } @@ -59,25 +61,26 @@ AclLiteError ObjectDetect::InitModelInput() { return ACLLITE_OK; } -AclLiteError ObjectDetect::Init() { - if (isInited_) { +AclLiteError ObjectDetect::Init() +{ + 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,27 +91,28 @@ 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, - 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; @@ -118,44 +122,47 @@ 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_); + 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; } - 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, - std::vector& carData) { - float* detectData = (float *)inferenceOutput[kBBoxDataBufId].data.get(); - if(detectData == nullptr){ +AclLiteError ObjectDetect::PostProcess(ImageData& image, std::vector& inferenceOutput, + std::vector& carData) +{ + 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]; + uint32_t scoreLine = 90; - - 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); - if (score < 90) { + if (score < scoreLine) { continue; } uint32_t objIndex = (uint32_t)detectData[totalBox * LABEL + i]; - if (objIndex == 2){ + uint32_t n = 2; + if (objIndex == n) { CarInfo carInfo; carInfo.rectangle.lt.x = detectData[totalBox * TOPLEFTX + i] * widthScale; carInfo.rectangle.lt.y = detectData[totalBox * TOPLEFTY + i] * heightScale; @@ -168,10 +175,11 @@ AclLiteError ObjectDetect::Postprocess(ImageData& image, std::vector @@ -27,26 +28,27 @@ * DvppProcess */ class DvppProcess { - public: +public: DvppProcess(); ~DvppProcess(); - Result InitResource(aclrtStream& stream, int imgWidth, int imgHeight); +Result InitResource(aclrtStream& stream, int imgWidth, int imgHeight); Result Venc(cv::Mat& srcImage); 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 old mode 100755 new mode 100644 index e28f9ebabf73a6e2ff408d2efbd29b41b72a26d7..96816648d89a855afee3921cc76f7013d0785393 --- 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,11 +12,13 @@ * 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" #include "acl/acl.h" @@ -70,8 +72,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 +104,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 old mode 100755 new mode 100644 index 7cdeaa30ed9785a2e2b84f2ee3861ecd71c47ad7..4787b70f5de490da28238b8c548c53c843766501 --- 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,17 +12,18 @@ * 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 #include "utils.h" #include "acl/acl.h" #include "model_process.h" #include "dvpp_process.h" -#include using namespace std; @@ -34,42 +35,43 @@ 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 + // Inference 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 + // 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_; +}; - 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 old mode 100755 new mode 100644 index 217ea3ad6eaf52261fe8ca95cfcbdcf9e8d3bc79..d5ac524cc6943cc5e5f4c184152a2940c5b55172 --- 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,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 e444d82a13f764a4969fd19f0d1d258336fc8330..e66eed293f41692491e17cdd084bca616396526a --- 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,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 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 runFlag_= true; +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 (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) @@ -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,140 +74,145 @@ 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() +{ } -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); 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 - 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) { +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); + } 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"); } } - 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(){ - - aclvencSetFrameConfigEos(vencFrameConfig_, 1); - aclvencSetFrameConfigForceIFrame(vencFrameConfig_, 0); - aclvencSendFrame(vencChannelDesc_, nullptr, nullptr, vencFrameConfig_, nullptr); +void DvppProcess::DestroyResource() +{ + 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 old mode 100755 new mode 100644 index 5c17b7fa88ea2188abd24b2aa87cfd13eba0dadd..2c520f0333389266a8f30f686012c380addc0b30 --- 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,21 @@ 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)){ +int main(int argc, char *argv[]) +{ + // Check the input when the application executes, which takes the path to the input video file + int argNum = 2; + if ((argc < argNum) || (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 +53,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 +72,35 @@ 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.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()); + 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"); @@ -111,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 545b1ae3c944d310d714fb80fac0045fe0524574..c1199cfb2f642bbf17a632d0fff01e9061944a3f --- 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,81 +12,90 @@ * 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 "model_process.h" #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) { + 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; @@ -98,28 +107,28 @@ Result ModelProcess::CreateDesc() void ModelProcess::DestroyDesc() { - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; + 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) { - input_ = aclmdlCreateDataset(); - if (input_ == nullptr) { + 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 +136,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); @@ -146,34 +155,34 @@ 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); @@ -189,7 +198,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); @@ -204,26 +213,26 @@ 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; } @@ -233,40 +242,38 @@ 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_; +} \ 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 c83773c4e19620651ca647f0dd828b4cb57cff4c..5ef3aa03da77eed35397778bae374df866303889 --- 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,49 +12,46 @@ * 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_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) }; @@ -62,18 +59,26 @@ 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(); +ObjectDetect::~ObjectDetect() +{ } -Result ObjectDetect::InitResource() { +Result ObjectDetect::InitResource() +{ // ACL init const char *aclConfigPath = "../src/acl.json"; aclError ret = aclInit(aclConfigPath); @@ -84,15 +89,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 +105,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 +123,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 +146,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_); + // 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; @@ -171,9 +176,9 @@ Result ObjectDetect::CreateModelInputdDataset() return SUCCESS; } -Result ObjectDetect::Init(int imgWidth, int imgHeight) { - - if (isInited_) { +Result ObjectDetect::Init(int imgWidth, int imgHeight) +{ + if (g_isInited_) { INFO_LOG("Object detection instance is initied already!"); return SUCCESS; } @@ -184,13 +189,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 +207,24 @@ 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 +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(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; @@ -227,35 +233,38 @@ Result ObjectDetect::Preprocess(cv::Mat& frame) { return SUCCESS; } -Result ObjectDetect::Inference(aclmdlDataset*& inferenceOutput) { - Result ret = model_.Execute(); +Result ObjectDetect::Inference(aclmdlDataset*& inferenceOutput) +{ + 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); + 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) / 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); - if (score < 90) continue; + if (score < scoreLine) continue; boundBox.rect.ltX = detectData[totalBox * TOPLEFTX + i] * widthScale; boundBox.rect.ltY = detectData[totalBox * TOPLEFTY + i] * heightScale; @@ -268,7 +277,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); } @@ -277,38 +286,37 @@ 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; } 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"); return nullptr; } - } - else { + } else { data = dataBufferDev; } @@ -316,27 +324,26 @@ 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; 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 +351,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; @@ -363,41 +369,40 @@ Result ObjectDetect::DrawBoundBoxToImage(vector& detectionResults,cv::Mat& void ObjectDetect::DestroyResource() { - aclrtFree(imageDataBuf_); - aclrtFree(imageInfoBuf_); + 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) { 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 1a9f200253a4461aa9fe5ce3bc56bfa243b4acc9..347bbe1565ce8918038582e46be84a3fcf741a40 --- 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,11 +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 #include @@ -27,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 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]; @@ -49,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) != kStatSuccess) { + if (stat(path.c_str(), &buf) != g_statSuccess) { return false; } @@ -64,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; @@ -72,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 = 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, 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); @@ -91,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 @@ -99,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)) { @@ -111,7 +113,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); @@ -125,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"); @@ -142,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) { @@ -160,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 92f5feead710eac39fc2ed5aaa5759abe942761d..55da65f6743e9e4f06541ea09370e2c741475234 --- 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 old mode 100755 new mode 100644 index aa9d943cdcb30ec129347b8322945bef759c8192..1f0595f08b201e2532f2979fe52c20a60d285751 --- 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 old mode 100755 new mode 100644 index 404d9b6175b5449dc720cb0aebbafed84e11e48d..4c23b6f51885c853584824061b4b9348a70026bc --- 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 @@ -51,7 +52,6 @@ public: */ Result InitOutputPara(int modelInputWidth, int modelInputHeight); - /** * @brief gett dvpp output * @param [in] outputBuffer: pointer which points to dvpp output buffer @@ -74,17 +74,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 old mode 100755 new mode 100644 index bde4a8cdd81399602929f0e61bd0e43efb64725d..4dd3ca2cc7b5b1bb09e7ae1255fcc10e3104c6e8 --- 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" @@ -25,7 +26,7 @@ * ModelProcess */ class ModelProcess { - public: +public: /** * @brief Constructor */ @@ -65,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); /** @@ -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 @@ -134,19 +135,20 @@ 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_; +private: + 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 old mode 100755 new mode 100644 index 0d95549fe33580d5779a0e9846f256d6b6ba609f..ad00fb497350b81e222563f7433355ac07e4173a --- 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,17 +12,18 @@ * 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 #include "utils.h" #include "acl/acl.h" #include "opencv2/opencv.hpp" #include "model_process.h" #include "dvpp_process.h" -#include using namespace std; @@ -31,7 +32,7 @@ using namespace std; */ class ObjectDetect { public: - ObjectDetect(const char* modelPath, + ObjectDetect(const char* modelPath, uint32_t modelWidth, uint32_t modelHeight); ~ObjectDetect(); @@ -39,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); @@ -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 old mode 100755 new mode 100644 index 80d71dd94c6d5e6f3c80274acb831285597fdd86..09bdfcfe4825d99e6ba740b583f90b4182a89634 --- 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 @@ -37,13 +38,15 @@ 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); })) #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(); } @@ -54,8 +57,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, @@ -125,3 +128,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 old mode 100755 new mode 100644 index 66787b7fbe3631dee4d0a317c4570d4ceb3b5321..72bc9eeb60c61a111aec2b54f3e9be9b2122667e --- 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 @@ -23,17 +20,19 @@ #include "utils.h" using namespace std; -DvppJpegD::DvppJpegD(aclrtStream& stream, acldvppChannelDesc *dvppChannelDesc) - : stream_(stream), dvppChannelDesc_(dvppChannelDesc), - decodeOutBufferDev_(nullptr), decodeOutputDesc_(nullptr){ +DvppJpegD::DvppJpegD(aclrtStream& stream, acldvppChannelDesc *dvppChannelDesc) + : 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) { @@ -41,66 +40,77 @@ 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(&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; } - 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(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 old mode 100755 new mode 100644 index 9a09e3ea304ad608981f7539787b9a0d576de9ee..d928cd2f3fa65f5e4096570b2c2a43cbed68195c --- 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,23 +22,26 @@ 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; } } @@ -49,30 +49,31 @@ 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); + 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) { - DvppJpegD jpegD(stream_, dvppChannelDesc_); +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 49e4bfe5aa6dafac174097a6717c90a578e5cff8..927062b58a6485f12ed9d926357dce42df2a32c5 --- 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,45 +22,49 @@ 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) { 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); - 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 +72,35 @@ 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) { +Result DvppResize::InitResizeResource(ImageData& inputImage) +{ + 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; } @@ -106,61 +108,63 @@ 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; } // 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 cf89819e48c2d2eba5b21af3a532b98916c55ca1..ffe1997baaec7332e5351e522f1f83a5ee86871b 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,30 @@ 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)){ +int main(int argc, char *argv[]) +{ + // Check input args: the path of input picture, and ignore hidden dotfile directory + int argNum = 2; + if ((argc != argNum) || (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); @@ -76,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 fbc9bf199dd7a4eeaed1848811957ed828e4bc0c..c9003e19c10e0ba6b42b1b6f624be21e468d108e --- 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,95 @@ * 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 "model_process.h" #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_) { +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; } - 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,15 +110,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::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"); @@ -131,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"); @@ -194,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"); @@ -257,11 +265,12 @@ 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 +280,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", + "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 +304,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 +318,12 @@ 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 +333,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", + "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 +357,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 +371,36 @@ 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 +416,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 +429,28 @@ 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 +460,38 @@ 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 old mode 100755 new mode 100644 index 901e481061acf6b94d3c821a8c13f1f2d8d4aa67..14bfc7e468c47e084d020e4127f2f3aef93477b2 --- 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,12 +12,10 @@ * 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 "object_detect.h" #include "opencv2/opencv.hpp" #include "acl/acl.h" #include "model_process.h" @@ -27,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) -: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() { +ObjectDetect::~ObjectDetect() +{ DestroyResource(); } -Result ObjectDetect::InitResource() { +Result ObjectDetect::InitResource() +{ // ACL init const char *aclConfigPath = "../src/acl.json"; aclError ret = aclInit(aclConfigPath); @@ -93,16 +94,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 +111,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; @@ -126,20 +127,21 @@ Result ObjectDetect::InitResource() { return SUCCESS; } -Result ObjectDetect::InitModel(const char* omModelPath) { - Result ret = model_.LoadModelFromFileWithMem(omModelPath); +Result ObjectDetect::InitModel(const char* 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 +150,16 @@ 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; } @@ -164,8 +167,9 @@ Result ObjectDetect::CreateImageInfoBuffer(){ return SUCCESS; } -Result ObjectDetect::Init() { - if (isInited_) { +Result ObjectDetect::Init() +{ + if (g_isInited_) { INFO_LOG("Object detection instance is initied already!"); return SUCCESS; } @@ -176,13 +180,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,28 +198,29 @@ Result ObjectDetect::Init() { return FAILED; } - isInited_ = true; + g_isInited_ = true; 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 - //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"); @@ -224,16 +229,17 @@ 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); - 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)) { @@ -246,23 +252,23 @@ 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, 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; @@ -271,79 +277,82 @@ 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 + // 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; } -Result ObjectDetect::Inference(aclmdlDataset*& inferenceOutput, -ImageData& resizedImage) { - Result ret = model_.CreateInput(resizedImage.data.get(), - resizedImage.size, imageInfoBuf_, imageInfoSize_); +Result ObjectDetect::Inference(aclmdlDataset*& inferenceOutput, ImageData& resizedImage) +{ + 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_); + uint32_t& reiszeMatLen, void*& reiszeMatBuffer) +{ + 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; } 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]; 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); @@ -356,13 +365,13 @@ 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); } DrowBoundBoxToImage(detectResults, origImagePath); - if (runMode_ == ACL_HOST) { + if (g_runMode_ == ACL_HOST) { delete[]((uint8_t *)detectData); delete[]((uint8_t*)boxNum); } @@ -371,19 +380,19 @@ 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]; 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); @@ -396,13 +405,13 @@ 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); } DrowBoundBoxToOpenCVImage(detectResults, origImagePath); - if (runMode_ == ACL_HOST) { + if (g_runMode_ == ACL_HOST) { delete[]((uint8_t *)detectData); delete[]((uint8_t*)boxNum); } @@ -412,30 +421,31 @@ 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; } 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"); @@ -450,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; @@ -458,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("/"); @@ -472,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; @@ -480,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("/"); @@ -493,35 +505,36 @@ 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(); @@ -529,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 46cfa271a8fca92559405c269c41049b1830d42c..a35fe525d004addaab5dded04aa2dfe949cc023c --- 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,11 +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 #include @@ -27,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 kImagePathSeparator = ","; -const int kStatSuccess = 0; -const std::string kFileSperator = "/"; -const std::string kPathSeparator = "/"; +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 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) != STAT_SUCCESS) { return false; } @@ -56,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; @@ -64,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 = 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, 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 +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 @@ -91,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)) { @@ -103,7 +104,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); @@ -117,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"); @@ -134,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) { @@ -152,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 @@ -181,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"); @@ -196,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; @@ -248,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 94de8e4d007b208d33f1f142a03d53ca985fec35..e71dc2692e288584e4de70a41a19a4057cee61b4 --- 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 old mode 100755 new mode 100644 index 37f6c145c27e5ec3a33f3e9268408167a773218b..c4896a268c240ef6a03e6285a200c5fb705badb1 --- 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 old mode 100755 new mode 100644 index e11b90e68f83d505343cb3ea8946b0576e7838d0..6bfb2997a33fbe669f2560c097abdbe6c4da8cf4 --- 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" @@ -50,7 +51,6 @@ public: */ Result InitOutputPara(int modelInputWidth, int modelInputHeight); - /** * @brief gett dvpp output * @param [in] outputBuffer: pointer which points to dvpp output buffer @@ -74,16 +74,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 old mode 100755 new mode 100644 index 33365bdf001b627a27e997a7091e388feda01be5..7f555f9c64f1ed5ac17a5d0568e46497440b6b89 --- 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" @@ -25,7 +26,7 @@ * ModelProcess */ class ModelProcess { - public: +public: /** * @brief Constructor */ @@ -101,17 +102,18 @@ 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_; +private: + 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 old mode 100755 new mode 100644 index dd346618d8cdabd0037126aef26ec8da741e2abd..99227b8a0a7b9e8179e7d49ea886cdc190beba64 --- 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,16 +12,17 @@ * 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 #include "utils.h" #include "acl/acl.h" #include "model_process.h" #include "dvpp_process.h" -#include using namespace std; @@ -30,7 +31,7 @@ using namespace std; */ class ObjectDetect { public: - ObjectDetect(const char* modelPath, + ObjectDetect(const char* modelPath, uint32_t modelWidth, uint32_t modelHeight); ~ObjectDetect(); @@ -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 old mode 100755 new mode 100644 index 557bd4929c7310de425a6ae0040b85ee92341df3..f14d45d1b14182a69e9c457341497597bce3419d --- 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,16 +12,17 @@ * 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 -#include "acl/acl.h" #include #include +#include "acl/acl.h" #include "opencv2/opencv.hpp" #include "opencv2/imgproc/types_c.h" @@ -43,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(); } @@ -54,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, @@ -120,3 +122,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 old mode 100755 new mode 100644 index df98ce0a716bca8f1c3ce934afd740fcf37f2751..85126733b32cd1cd52f2eac24e27244c536a565e --- 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,18 @@ 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) { @@ -40,33 +40,34 @@ 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(&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 +76,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 +94,16 @@ 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 old mode 100755 new mode 100644 index e8a5fb531257d868b0a7f02e7134258fc36db743..362ba3229b69394c655e99c389744ac9e22d28a5 --- 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,23 +22,26 @@ 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; } } @@ -49,29 +49,31 @@ 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); + 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) { - DvppJpegD jpegD(stream_, dvppChannelDesc_); +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 7c608463b203b6297d23cfc20d5aac27992655bb..fed9c03e7426a9d17d97c731867aea53587cbfa8 --- 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,51 +22,56 @@ 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) { 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); - 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) + uint32_t n = 2; + if (inputImage.width % n != 0) inputWidth--; - if(inputImage.height % 2 != 0) + if (inputImage.height % n != 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 +79,35 @@ 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) { +Result DvppResize::InitResizeResource(ImageData& inputImage) +{ + 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; } @@ -112,25 +115,26 @@ 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; } // 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 +146,32 @@ 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 351d96e3fef584976370b90c8d8d11bd2d807818..9baf502e9f6da68973088e87407cbb583430737c 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,30 @@ 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; +uint32_t argNum = 2; +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)){ +int main(int argc, char *argv[]) +{ + // Check input args: the path of input picture, and ignore hidden dotfile directory + if ((argc != argNum) || (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,24 +64,24 @@ 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) { 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 - //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 old mode 100755 new mode 100644 index 66ef4288bc0dd7bc3dd057eb36e3a6ad37e6cff1..6d624db3893ca2191c815321615229f5af1d9adc --- 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,82 @@ * 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 "model_process.h" #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 +97,19 @@ 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 +120,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 +134,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 +145,36 @@ 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 +190,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 +203,28 @@ 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; } @@ -225,37 +234,38 @@ 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_; -} +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 1d0bb6174721f2593ebeae8db0f9904ff06d0d7d..98b8f4a07a01431cf016682bcba7bbf15c690e1b --- 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,79 +12,79 @@ * 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 - +#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) -:deviceId_(0), context_(nullptr), stream_(nullptr), modelWidth_(modelWidth), -modelHeight_(modelHeight), isInited_(false), isDeviceSet_(false){ - imageInfoSize_ = 0; - imageInfoBuf_ = nullptr; - modelPath_ = modelPath; + 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); @@ -95,15 +95,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 +111,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,20 +127,21 @@ Result ObjectDetect::InitResource() { return SUCCESS; } -Result ObjectDetect::InitModel(const char* omModelPath) { - Result ret = model_.LoadModelFromFileWithMem(omModelPath); +Result ObjectDetect::InitModel(const char* 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 +150,16 @@ 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; } @@ -165,8 +167,9 @@ Result ObjectDetect::CreateImageInfoBuffer(){ return SUCCESS; } -Result ObjectDetect::Init() { - if (isInited_) { +Result ObjectDetect::Init() +{ + if (g_isInited_) { INFO_LOG("Object detection instance is initied already!"); return SUCCESS; } @@ -177,13 +180,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 +198,24 @@ Result ObjectDetect::Init() { return FAILED; } - isInited_ = true; + g_isInited_ = true; return SUCCESS; } -Result ObjectDetect::Preprocess(ImageData& resizedImage, ImageData& srcImage) { +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; @@ -221,40 +225,42 @@ Result ObjectDetect::Preprocess(ImageData& resizedImage, ImageData& srcImage) { } Result ObjectDetect::Inference(aclmdlDataset*& inferenceOutput, -ImageData& resizedImage) { - Result ret = model_.CreateInput(resizedImage.data.get(), + ImageData& resizedImage) +{ + 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; } 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]; 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; @@ -267,13 +273,13 @@ 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); } DrawBoundBoxToImage(detectResults, origImagePath); - if (runMode_ == ACL_HOST) { + if (g_runMode_ == ACL_HOST) { delete[]((uint8_t *)detectData); delete[]((uint8_t*)boxNum); } @@ -282,31 +288,32 @@ 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; } 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"); @@ -321,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; @@ -329,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"; @@ -346,35 +354,36 @@ 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 old mode 100755 new mode 100644 index f1c06d86f1b34d603308017724d3c037c3ad7f2e..af0abf98cd835accf5111737f9a59a5b68c7fddb --- 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,11 +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 #include @@ -27,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; } @@ -57,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; @@ -65,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); @@ -84,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 @@ -92,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)) { @@ -104,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); @@ -118,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); @@ -140,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); @@ -154,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"); @@ -162,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) { @@ -171,14 +175,13 @@ 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"); @@ -193,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"); @@ -210,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) { @@ -228,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 @@ -296,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/data/.keep b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_DVPP_with_AIPP/data/.keep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 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 e4f94db3b8719d1a863338ff1b9a8d0ebdc35207..b935cb0d5faf4cf3f0f65f2e16aea26600375b1d 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 cc449bc27ee3d6d83c9d0c01670b8fd6be970093..36f3143dd0fc43d838d1d96275aff9cf1f715d3d 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 09b654d87ac06f0dc741d08bd0b8fa95e28c5809..2b70ace5f75c62d759393b4af7b17c81eac887a8 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 d363407cd0379fae8473b5c71c3c899ef20aa878..de9e8542a2d253519ddcc095af2050485c390241 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 @@ -105,3 +115,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 cd39ccb157967ccf3af411e8c19e2b8743bb607d..4ecf5e559ed3c7ad9ec70d02deeea4c2d4d4b8f6 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,27 @@ -/** -* @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) - { +namespace +{ + uint32_t AlignSize(uint32_t origSize, uint32_t alignment) { if (alignment == 0) { return 0; } @@ -23,12 +30,13 @@ 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) { } @@ -40,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 +72,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) @@ -75,28 +83,28 @@ 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) { if ((modelInputWidth <= 0) || (modelInputHeight <= 0)) { ERROR_LOG("InitInput para invalid, modelInputWidth %d, modelInputHeight %d", - modelInputWidth, modelInputHeight); + 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; @@ -107,113 +115,113 @@ 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_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) { + 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 = 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; @@ -224,38 +232,38 @@ 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) { - (void)acldvppDestroyResizeConfig(resizeConfig_); - resizeConfig_ = nullptr; + // 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; } } @@ -304,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 8ce595e3aa418985c302921b56f89e46945382a2..329070ae65e252e30920bcb2d47af434efe3c561 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,12 +1,19 @@ -/** -* @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" 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 b8ff2537eaa4f89461df9506ebdf7a27b5ceceff..29b4584d5973184ab65717ced5540e382569ad98 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,19 +1,28 @@ -/** -* @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 "model_process.h" #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) { } @@ -27,12 +36,12 @@ ModelProcess::~ModelProcess() Result ModelProcess::LoadModel(const char *modelPath) { - if (loadFlag_) { + 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 +49,44 @@ 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) { + 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; @@ -90,41 +99,41 @@ Result ModelProcess::CreateDesc() void ModelProcess::DestroyDesc() { - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; + 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) +Result ModelProcess::CreateInput(void *input1, size_t input1Size, void *input2, size_t input2Size) { - vector inputData = {{input1, input1size}, {input2, 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", - 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); @@ -133,7 +142,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); @@ -147,35 +156,35 @@ Result ModelProcess::CreateInput(void *input1, size_t input1size, void *input2, 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)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 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 +201,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); @@ -207,75 +216,75 @@ 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; } void ModelProcess::UnloadModel() { - 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, 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_; + 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; } @@ -283,9 +292,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) { @@ -298,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 8c0512c9dc1a0bf3f3b169536839732603afc5a3..3c5419dcfc7dcf0fa21fabeb839b13f07299f679 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,66 +1,75 @@ -/** -* @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" -#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 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) }; } -SampleProcess::SampleProcess():deviceId_(0), context_(nullptr), stream_(nullptr) +SampleProcess::SampleProcess() + : g_deviceId_(0), g_context_(nullptr), g_stream_(nullptr) { } @@ -81,27 +90,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 +118,25 @@ 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) { +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]; @@ -146,13 +155,13 @@ 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); } DrawBoundBoxToImage(detectResults, picDesc.picName); - if (runMode_ == ACL_HOST) { + if (g_runMode_ == ACL_HOST) { delete[]((uint8_t *)detectData); delete[]((uint8_t*)boxNum); } @@ -161,30 +170,31 @@ 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; } 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"); @@ -199,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; @@ -207,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, 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 +228,17 @@ 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() { // dvpp init - DvppProcess dvppProcess(stream_); + DvppProcess dvppProcess(g_stream_); Result ret = dvppProcess.InitResource(); if (ret != SUCCESS) { ERROR_LOG("init dvpp resource failed"); @@ -261,10 +272,10 @@ 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 (runMode_ == ACL_HOST) + if (g_runMode_ == ACL_HOST) imageInfoBuf_ = Utils::CopyDataHostToDevice((void *)imageInfo, imageInfoSize_); else imageInfoBuf_ = Utils::CopyDataDeviceToDevice((void *)imageInfo, imageInfoSize_); @@ -275,10 +286,8 @@ 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) { INFO_LOG("start to process picture:%s", testPic[index].picName.c_str()); // 1.dvpp process @@ -316,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"); @@ -351,33 +360,33 @@ Result SampleProcess::Process() 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) { 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 8a4425a19d489f847d49acd55f7c37d24c8d4b6a..d777d32c1104d2f8ba69e3cc7adf1872de0006c9 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,16 +1,23 @@ -/** -* @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 #include +#include "utils.h" #if defined(_MSC_VER) #include @@ -75,7 +82,6 @@ Result Utils::GetDeviceBufferOfPicture(PicDesc &picDesc, void *&picDevBuffer, ui ERROR_LOG("picture file name is empty"); return FAILED; } - uint32_t inputBuffSize = 0; void *inputBuff = nullptr; Result ret = ReadBinFile(picDesc.picName, inputBuff, inputBuffSize); @@ -89,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, @@ -100,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) { @@ -114,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) { @@ -125,11 +125,8 @@ 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; } @@ -156,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) { @@ -174,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 e778640c802c2839b0c583a15e90851c3158d444..712ed69910b1564f716347efc170290a6702bf66 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 @@ -10,5 +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); -#endif //__DRAWING__H__ + 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_cropandpaste.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_picture_with_freetype/inc/dvpp_cropandpaste.h index 62796696b306a267fcc7e41cc8420bc4182c15c7..76371ed35f2e12dd6dd48596985ba4c9088cc5e3 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 old mode 100755 new mode 100644 index 94de8e4d007b208d33f1f142a03d53ca985fec35..3847a876b15bd17b7f3319ca7b32063aaf30c5a2 --- 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 old mode 100755 new mode 100644 index 850d1efcbb5f6119463bcd3cab1c048643376fcb..d4ff3d6939680c4b69240eb8441d83e0dff4053a --- 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 83530374084c184074a0a601b137b2900095f5c4..a56720dba3d6e12620795ef654480a166cae19a7 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 old mode 100755 new mode 100644 index 33365bdf001b627a27e997a7091e388feda01be5..7355f13ced0247bb1a55a7fbb1e299ce082d217d --- 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" @@ -25,7 +26,7 @@ * ModelProcess */ class ModelProcess { - public: +public: /** * @brief Constructor */ @@ -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 @@ -101,17 +102,18 @@ 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_; +private: + 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 old mode 100755 new mode 100644 index 7eb313666c21a899d7879cbe3526adeee1eb012f..6ce79bc8341a8161d8bdb7ad562c913a3838a178 --- 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,16 +12,17 @@ * 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 #include "utils.h" #include "acl/acl.h" #include "model_process.h" #include "dvpp_process.h" -#include using namespace std; @@ -30,7 +31,7 @@ using namespace std; */ class ObjectDetect { public: - ObjectDetect(const char* modelPath, + ObjectDetect(const char* modelPath, uint32_t modelWidth, uint32_t modelHeight); ~ObjectDetect(); @@ -46,26 +47,27 @@ 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: - 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 old mode 100755 new mode 100644 index 166acb6108df2d254d994d866685a839c3d810d9..0adcd16b12be0652b4cfa3366e404303ebf1ab24 --- 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 @@ -41,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(); } @@ -52,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, @@ -129,3 +131,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 6b108281ff30e3e4d8210e2a60ce769488fdce17..5025d4ff4800f12a62f1926a8faf9b7932d2d209 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,17 +1,35 @@ +/* +* 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 #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); } @@ -21,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 16c0919183389711fd9684967b7be8538ddd342a..f1c31a993b09e4b3a80f08b9e1d28d738fd213ae 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 @@ -24,12 +21,13 @@ 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; + 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() @@ -39,36 +37,36 @@ DvppCropAndPaste::~DvppCropAndPaste() Result DvppCropAndPaste::InitCropAndPasteInputDesc(ImageData& inputImage) { - originalImageWidth_ = inputImage.width; - originalImageHeight_ = inputImage.height; + 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); - 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; + 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,43 +74,44 @@ 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); +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) @@ -127,14 +126,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,37 +142,37 @@ 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; @@ -181,23 +180,23 @@ Result DvppCropAndPaste::Process(ImageData& resizedImage, ImageData& srcImage) void DvppCropAndPaste::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; } } 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 df98ce0a716bca8f1c3ce934afd740fcf37f2751..85126733b32cd1cd52f2eac24e27244c536a565e --- 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,18 @@ 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) { @@ -40,33 +40,34 @@ 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(&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 +76,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 +94,16 @@ 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 old mode 100755 new mode 100644 index f848226f4eb013545164cb7c4e5c180453fc5770..161b3bf520ec50a512bf12ea749713e931821b16 --- 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,23 +22,26 @@ 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; } } @@ -49,30 +49,31 @@ 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); + 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) { - DvppJpegD jpegD(stream_, dvppChannelDesc_); +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 1dc5a66b14b686975253c8ab05be35e98dd1eb9f..c53fe679c0de7fca5677e2c5522ba71123f544a6 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 @@ -9,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: \ @@ -24,31 +40,32 @@ const char *FreeTypeErrorMessage(FT_Error err) { } } -#define CHECK_FREETYPE(x) \ -{ \ +#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, @@ -65,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); } } } @@ -80,38 +97,41 @@ 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; uv_offset[1] = color.v; } -//cache glyphs -void RenderText(ImageData& image, int x, int y, const std::string &text, const YUVColor *color) { +// 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; 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 57a99d23cb2a6d09a91dbf906574b07c19dfb489..539226e0ce27436147288b8bc0cb362c1b186f0d 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,30 @@ 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)){ +int main(int argc, char *argv[]) +{ + // Check input args: the path of input picture, and ignore hidden dotfile directory + uint32_t argNum = 2; + if ((argc != argNum) || (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,25 +63,25 @@ 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); 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 - //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 old mode 100755 new mode 100644 index 66ef4288bc0dd7bc3dd057eb36e3a6ad37e6cff1..8c75a36d8931fe7570e9eaa2ad50c549b88da5ed --- 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,82 @@ * 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 +97,30 @@ 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 +128,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); @@ -140,34 +145,36 @@ 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 +190,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 +203,28 @@ 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; } @@ -225,37 +234,38 @@ 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_; +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 53d8dd3e8396e8fb6aa0497199726d6ef29e0433..a1b7c1e4b4424b277ec547d6439d652d83ea2b5f --- 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,13 +12,10 @@ * 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 +#include "object_detect.h" #include "acl/acl.h" #include "model_process.h" #include "utils.h" @@ -28,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; @@ -53,18 +50,20 @@ 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() { +ObjectDetect::~ObjectDetect() +{ DestroyResource(); } -Result ObjectDetect::InitResource() { +Result ObjectDetect::InitResource() +{ // ACL init const char *aclConfigPath = "../src/acl.json"; aclError ret = aclInit(aclConfigPath); @@ -75,16 +74,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 +91,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; @@ -108,20 +107,21 @@ Result ObjectDetect::InitResource() { return SUCCESS; } -Result ObjectDetect::InitModel(const char* omModelPath) { - Result ret = model_.LoadModelFromFileWithMem(omModelPath); +Result ObjectDetect::InitModel(const char* 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 +130,16 @@ 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; } @@ -146,8 +147,9 @@ Result ObjectDetect::CreateImageInfoBuffer(){ return SUCCESS; } -Result ObjectDetect::Init() { - if (isInited_) { +Result ObjectDetect::Init() +{ + if (g_isInited_) { INFO_LOG("Object detection instance is initied already!"); return SUCCESS; } @@ -158,13 +160,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,55 +178,57 @@ Result ObjectDetect::Init() { return FAILED; } - isInited_ = true; + g_isInited_ = true; return SUCCESS; } Result ObjectDetect::Preprocess(ImageData& resizedImage, ImageData& srcImage, -ImageData& oriImage) { - + 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(), + ImageData& resizedImage) +{ + 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; } Result ObjectDetect::Postprocess(ImageData& image, aclmdlDataset* modelOutput, -const string& origImagePath) { + const string& origImagePath) +{ uint32_t dataSize = 0; float* detectData = (float *)GetInferenceOutputItem(dataSize, modelOutput, kBBoxDataBufId); @@ -235,8 +239,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); @@ -249,13 +253,13 @@ 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); } DrawBoundBoxToImage(image, detectResults, origImagePath); - if (runMode_ == ACL_HOST) { + if (g_runMode_ == ACL_HOST) { delete[]((uint8_t *)detectData); delete[]((uint8_t*)boxNum); } @@ -263,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) { @@ -284,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 " @@ -312,7 +317,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 +331,36 @@ 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 old mode 100755 new mode 100644 index cf151bdf99b083ef646591070dec23e38c8723e0..c7fc98bdbc1f1089f6a5aff71abe55b08842d6e2 --- 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,11 +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 #include @@ -27,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; } @@ -57,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; @@ -65,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, 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); @@ -84,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 @@ -92,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)) { @@ -104,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); @@ -118,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); @@ -140,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); @@ -162,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) { @@ -171,14 +175,13 @@ 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"); @@ -193,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"); @@ -210,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; - if (mode == ACL_HOST){ +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); } @@ -234,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) { @@ -252,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 @@ -320,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 13f5358e60e06e3865e8e38d176f141fa6c9779d..501d24e33a8f9b7bab4d8c29c93594a9323659b5 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" @@ -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_video/inc/object_detect.h b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/inc/object_detect.h index 91a334e47444c36be1c9f27261b222dc1ec5ac71..be5ed3101c98ff590205a08e00a3a7b6a46cb339 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 7906ab5f9f486bc42de0bd5b2b5766d571483f6c..0000000000000000000000000000000000000000 --- 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 69516dd68e5740738d50e223749bc135ad0ee333..1aebbcd50b264c24aa26fac8b00455eeb6111c00 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 @@ -45,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(); } @@ -56,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, @@ -116,3 +118,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 d2219a457f695ac30fbc4cd62a700030ce80bce6..450491bfab709429fc53d1e08e8dda1530f6c509 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,55 @@ 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)){ +int main(int argc, char *argv[]) +{ + // Check the input when the application executes, which takes the path to the input video file + 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(kModelPath, kModelWidth, kModelHeight); - //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"); 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 - while(1) { - //Read a frame of an image + // Frame by frame reasoning + while (1) { + // 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 ret = detect.Postprocess(frame, inferenceOutput); if (ret != SUCCESS) { ERROR_LOG("Process model inference output data failed"); @@ -84,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 12ff2d6333c811351119913325e3472bbc6d728f..cc93db24dcf17f7d30513e5528e3e9e9ecf25c6f 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,81 +12,83 @@ * 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():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) { + 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; @@ -98,28 +100,27 @@ Result ModelProcess::CreateDesc() void ModelProcess::DestroyDesc() { - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; + 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 +128,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); @@ -146,34 +147,34 @@ 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); @@ -189,7 +190,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); @@ -204,26 +205,26 @@ 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; } @@ -232,38 +233,38 @@ 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_video/src/object_detect.cpp b/cplusplus/level2_simple_inference/2_object_detection/YOLOV3_coco_detection_video/src/object_detect.cpp index 1e7936d025005104e2712a3e57fd1ea8d3de440a..953931446a15097b32fd87a04dc24ec3a6937411 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,48 +24,52 @@ 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" }; -//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 + "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; +// 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); } } -ObjectDetect::~ObjectDetect() { +ObjectDetect::~ObjectDetect() +{ DestroyResource(); } -Result ObjectDetect::InitResource() { +Result ObjectDetect::InitResource() +{ // ACL init const char *aclConfigPath = "../src/acl.json"; aclError ret = aclInit(aclConfigPath); @@ -78,14 +80,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; @@ -94,52 +96,53 @@ Result ObjectDetect::InitResource() { return SUCCESS; } -Result ObjectDetect::InitModel(const char* omModelPath) { - Result ret = model_.LoadModelFromFileWithMem(omModelPath); +Result ObjectDetect::InitModel(const char* 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); + // 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; @@ -148,20 +151,21 @@ Result ObjectDetect::CreateModelInputdDataset() return SUCCESS; } -Result ObjectDetect::Init() { - //If it is already initialized, it is returned - if (isInited_) { +Result ObjectDetect::Init() +{ + // 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 +177,24 @@ 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 +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(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; @@ -198,62 +203,64 @@ Result ObjectDetect::Preprocess(cv::Mat& frame) { return SUCCESS; } -Result ObjectDetect::Inference(aclmdlDataset*& inferenceOutput) { - //Perform reasoning - Result ret = model_.Execute(); +Result ObjectDetect::Inference(aclmdlDataset*& inferenceOutput) +{ + // 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 + 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; 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; @@ -261,38 +268,37 @@ 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; } 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"); return nullptr; } - } - else { + } else { data = dataBufferDev; } @@ -301,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 - //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); } Result ObjectDetect::SendImage(vector& detectionResults, - cv::Mat& origImg) { + cv::Mat& origImg) +{ vector encodeImg; EncodeImage(encodeImg, origImg); @@ -321,8 +329,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; @@ -333,25 +341,23 @@ Result ObjectDetect::SendImage(vector& detectionResults, void ObjectDetect::DestroyResource() { - aclrtFree(imageDataBuf_); - aclrtFree(imageInfoBuf_); + 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_); + aclError 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) { 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 fcbdb337c823beab1791a110973044f01df91076..8dffe224d1f418aea00857a22fa0d5e0e9134b30 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 kImagePathSeparator = ","; -const int kStatSuccess = 0; -const std::string kFileSperator = "/"; -const std::string kPathSeparator = "/"; +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 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) != 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 = 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, 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 + 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); @@ -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 5b04b58c7c5f1ee13be7acb4bd0b4dc3daee1b96..629ef352bdaa1c5952831454af6541497184e2f7 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" @@ -28,18 +38,20 @@ 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(); - 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 415739706d74cb37b74753a19b1a996e61bbd80e..13d77aa7851efff19475f8fdb7899040a9915604 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,28 @@ -/** -* @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)){ + // Check the input when the application executes, which takes the path to the input video file + 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 96173041aa800b8c47725538644b46dd87bc2f00..831d69d062d549d7632c48b4244d9e40b5723b86 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,73 +1,83 @@ -/** -* @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 #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 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; // 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"; - 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): -cap_(nullptr), -model_(omModelPath), -streamName_(streamName) { - imageInfoSize_ = 0; - imageInfoBuf_ = nullptr; +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 ret = aclDev_.Init(); +AclLiteError SampleProcess::InitResource() +{ + AclLiteError ret = g_aclDev_.Init(); if (ret) { ACLLITE_LOG_ERROR("Init resource failed, error %d", ret); return ACLLITE_ERROR; @@ -77,25 +87,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(); - 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_runMode_ = g_aclDev_.GetRunMode(); + 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); + if (g_imageInfoBuf_ == nullptr) { ACLLITE_LOG_ERROR("Copy image info to device failed"); return ACLLITE_ERROR; } @@ -103,23 +113,24 @@ AclLiteError SampleProcess::InitResource() { return ACLLITE_OK; } -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()); +AclLiteError SampleProcess::OpenVideoCapture() +{ + 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; } @@ -127,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[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; - 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++) { @@ -147,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); } @@ -155,60 +168,63 @@ 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, 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); +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; - 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; - 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, g_modelInputWidth, g_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; @@ -216,22 +232,22 @@ 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; } } - outputVideo_.release(); + g_outputVideo_.release(); return ACLLITE_OK; } void SampleProcess::DestroyResource() { - if (cap_ != nullptr) { - cap_->Close(); - delete cap_; + 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 fcc9b5cb4c158758a189fe988be3b41db581bb1b..bc0722648389e42ac968f4379e6ef999cec01e4e 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 1367e5033dcddb3fb702b886fa1461d4bb250553..5936a5f56198b67228c0af85bdc1d9ba0d51c183 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 a8b03e991b39688d7e79ae20614a5f786c6d1d35..f5ffda37f60d2ec56585f6159f7437e2d9e3e178 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 @@ -33,19 +43,23 @@ 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) : - outputClass_(outputClass), outputBox_(outputBox), xScale_(xScale), yScale_(yScale), - 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 */ - 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 */ - virtual ~PostProcess() {} + virtual ~PostProcess() + { + } /** * @brief sort box @@ -76,9 +90,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 5ccde10ed5a0acccd92a8897d0a66f33db48c12e..03946ab2555c1e6bc884505f2acf9dbcbc1082c6 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 @@ -30,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_; } @@ -73,3 +85,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 c897fbad107a8631ad88477dd9af7a73afce0987..dd440767f0ab1413a9ab7c0b3564b357509497ef 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" @@ -14,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; } @@ -23,38 +31,42 @@ 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; } @@ -63,38 +75,42 @@ 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 + int byte1 = 16; + int byte2 = 2; + g_modelInputWidth_ = modelInputWidth; + g_modelInputHeight_ = modelInputHeight; + 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 - 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; @@ -103,107 +119,114 @@ 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_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) { +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 = 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; @@ -212,42 +235,45 @@ 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) { - acldvppDestroyResizeConfig(resizeConfig_); - resizeConfig_ = nullptr; +void DvppProcess::DestroyResource() +{ + // 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; } } -Result DvppProcess::Process() { +Result DvppProcess::Process() +{ // pic decode Result ret = InitDecodeOutputDesc(); if (ret != SUCCESS) { @@ -291,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 d1654ad7329eae3d9c6026fdce51576249ea8a7e..7dd069935eb472cb8c007c25b14f11ab27a03538 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,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); @@ -53,18 +52,18 @@ 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); } 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); @@ -90,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); @@ -118,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) { @@ -180,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 40978ec2feb1e0861057225cef50144ca591a166..532fcf0008b8ab8f429e45c3565f646dda259d97 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,35 +1,46 @@ -/** -* @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 "model_process.h" #include "utils.h" #include "acl/ops/acl_dvpp.h" 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() { +ModelProcess::~ModelProcess() +{ Finalize(); } -Result ModelProcess::Init(int deviceId) { +Result ModelProcess::Init(int deviceId) +{ // acl initialize aclError ret = aclInit(nullptr); if (ret != ACL_SUCCESS) { ERROR_LOG("aclInit failed, errorCode is %d", static_cast(ret)); return FAILED; } - initFlag_ = true; + g_initFlag_ = true; // open device ret = aclrtSetDevice(deviceId); @@ -38,10 +49,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; @@ -61,97 +72,101 @@ Result ModelProcess::Init(int deviceId) { return SUCCESS; } -void ModelProcess::Finalize() { +void ModelProcess::Finalize() +{ UnloadModel(); 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_) { +Result ModelProcess::LoadModel(const char *modelPath) +{ + 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_) { +void ModelProcess::UnloadModel() +{ + 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) { +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 +179,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); @@ -176,37 +191,39 @@ Result ModelProcess::CreateInput(void *inputDataBuffer, size_t bufferSize) { 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 *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) { +Result ModelProcess::CreateOutput() +{ + 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 +239,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); @@ -235,40 +252,43 @@ 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; } const aclmdlDataset *ModelProcess::GetModelOutputData() { - return output_; + 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; } @@ -276,7 +296,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; @@ -291,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 31e17d91bc1755641375efe15e73bb71377341f8..76c0187006b5b23b517e7262713e5eb8ca8c330f 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,14 +1,21 @@ -/** -* @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 "post_process.h" #include "opencv2/opencv.hpp" #include "opencv2/imgproc/types_c.h" @@ -35,37 +42,40 @@ 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) { - float *boxBuff = static_cast(outputBox_); +void PostProcess::SetBoxInfo(size_t index, BBox &box) +{ + 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) { +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); @@ -76,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); @@ -85,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; } @@ -95,22 +106,24 @@ void PostProcess::NMS(vector &boxes, vector &result) { } } -void PostProcess::DrawBoundBoxToImage(const vector &result) { - cv::Mat image = cv::imread(originImage_, CV_LOAD_IMAGE_UNCHANGED); +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 = 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; @@ -118,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(outputClass_); - for (size_t i = 0; i < kModelOutputBoxNum; ++i) { + float *classBuff = static_cast(g_outputClass_); + 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 9d1739604604a7c8a004e001fdd1b59ddcc246a7..e2055468155dbef991655244dea5473456979f43 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,16 +1,23 @@ -/** -* @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 #include +#include "utils.h" #if defined(_MSC_VER) #include @@ -23,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()); @@ -79,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; @@ -141,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/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 675c354a167afa90a080df440f5a6879e34c67a6..45bf9c940de55864448916a1fdbc2f3ca584a72c 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 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 d406dec2366941124e4e66ccad7579d124e73fd5..0f2e614b6496aa413d985392d747d3e754db47e7 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,17 +12,17 @@ * 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 #include "acl/acl.h" #include "acllite/AclLiteModel.h" #include "acllite/AclLiteUtils.h" -#include - #include "opencv2/imgproc/types_c.h" /** @@ -43,14 +43,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 86480f21c62c7e4f1db274ab0485298f6283bb7e..471aa548b9db5ef97d4fd6545701420e0300bd6b 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,42 +40,42 @@ struct message std::shared_ptr boxNum; }; -struct message_video -{ +struct message_video { int number; 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) : max_size_(maxSize), is_stoped_(false) {} +template class Queue { +public: + Queue(uint32_t maxSize = DEFAULT_MAX_QUEUE_SIZE) : maxSize_(maxSize), isStoped_(false) {} - ~BlockingQueue() {} + ~Queue() {} int Pop(T &item) { 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 - return 1; + if (isStoped_) { + 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(); } - full_cond_.notify_one(); + fullCond_.notify_one(); return 0; } @@ -86,24 +85,22 @@ 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 - return 1; + if (isStoped_) { + 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(); } - full_cond_.notify_one(); + fullCond_.notify_one(); return 0; } @@ -112,22 +109,20 @@ 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_) { return 1; } - if (queue_.size() >= max_size_) { - //APP_ERROR_QUEUE_FULL - return 3; + if (queue_.size() >= maxSize_) { + return APP_ERROR_QUEUE_FULL; } queue_.push_back(item); - empty_cond_.notify_one(); + emptyCond_.notify_one(); return 0; } @@ -136,23 +131,21 @@ 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 - return 1; + if (isStoped_) { + return APP_ERR_QUEUE_STOPED; } - if (queue_.size() >= max_size_) { - //APP_ERROR_QUEUE_FULL - return 3; + if (queue_.size() >= maxSize_) { + return APP_ERROR_QUEUE_FULL; } queue_.push_front(item); - empty_cond_.notify_one(); + emptyCond_.notify_one(); return 0; } @@ -161,18 +154,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 +174,7 @@ template class BlockingQueue { { std::unique_lock lock(mutex_); - if (!is_stoped_) { + if (!isStoped_) { return std::list(); } @@ -190,14 +183,12 @@ template class BlockingQueue { int GetBackItem(T &item) { - if (is_stoped_) { - //APP_ERR_QUEUE_STOPED - return 1; + if (isStoped_) { + return APP_ERR_QUEUE_STOPED; } if (queue_.empty()) { - //APP_ERR_QUEUE_EMPTY - return 2; + return APP_ERR_QUEUE_EMPTY; } item = queue_.back(); @@ -212,7 +203,7 @@ template class BlockingQueue { int IsFull() { std::unique_lock lock(mutex_); - return queue_.size() >= max_size_; + return queue_.size() >= maxSize_; } int GetSize() @@ -231,13 +222,14 @@ template class BlockingQueue { queue_.clear(); } - private: +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/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 e70e99de3ca0dd76fe57ab98382747ebdd0b03f9..dda68c857a4d342326ea77eb8bc4852cdcb946f7 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,75 +12,75 @@ * 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" -#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 kModelWidth = 416; - uint32_t kModelHeight = 416; - const char* kModelPath = "../model/yolov3.om"; - 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; + 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; 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 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); - while(1){ + while (1) { cv::Mat frame; - if (!capture.read(frame)){ + if (!capture.read(frame)) { ACLLITE_LOG_INFO("Video capture return false"); break; } @@ -88,66 +88,65 @@ 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); - } - else + usleep(timep); + } else break; } } premsg.videoIndex = threadNum; premsg.isLastFrame = 0; - while(1) { + 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); + while (1) { + if (queue_post->Pop(postmsg) != 0) { + usleep(timep); 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){ + uint32_t scoreLine = 90; + if (score < scoreLine) { continue; } boundBox.rect.ltX = detectData[totalBox * TOPLEFTX + i] * widthScale; @@ -156,7 +155,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); } @@ -167,12 +166,13 @@ 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[]) { - //init acl resource +int main(int argc, char *argv[]) +{ + // init acl resource AclLiteResource aclDev; AclLiteError ret = aclDev.Init(); if (ret) { @@ -180,7 +180,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) { @@ -206,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); @@ -230,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); + while (1) { + if (queue_pre.Pop(premsg) != 0) { + 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; } } @@ -264,31 +261,29 @@ 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) { 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){ + while (1) { + 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 b38c6dfebbbd580df954935430776e4395b2235c..819b050209f940bb9a1a302298a72e2359be4996 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,54 +12,54 @@ * 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" #include +#include "acl/acl.h" #include "acllite/AclLiteModel.h" #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; + 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() { - //Request image data memory for input model - aclError aclRet = aclrtMalloc(&imageDataBuf_, (size_t)(imageDataSize_), ACL_MEM_MALLOC_HUGE_FIRST); +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) { 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,28 +68,29 @@ AclLiteError ObjectDetect::CreateInput() { return ACLLITE_OK; } -AclLiteError ObjectDetect::Init() { - //If it is already initialized, it is returned - if (isInited_) { +AclLiteError ObjectDetect::Init() +{ + // 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 +98,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 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 = model_.Execute(inferenceOutput); + // Perform reasoning + ret = g_model_.Execute(inferenceOutput); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Execute model inference failed"); return ACLLITE_ERROR; @@ -123,10 +124,9 @@ AclLiteError ObjectDetect::Inference(std::vector& inferenceOutp void ObjectDetect::DestroyResource() { - model_.DestroyInput(); - model_.DestroyResource(); - - aclrtFree(imageDataBuf_); - aclrtFree(imageInfoBuf_); -} + 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 4ef384ebb5262bf530f2d7ce2f44a6a5391f0b4a..86085d6c4fab3600202e8b2e0866dac999f0ea82 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,24 +12,24 @@ * 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 #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); @@ -41,17 +41,19 @@ class ObjectDetect { void DestroyResource(); - private: +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 f8d814568bdaf5a2f50c9d2ebebf1a6c56e4aa2a..95c5b0ef7ad23d2709f0d4b336341bd9cd8b38fa 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; @@ -37,34 +37,34 @@ 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) : max_size_(maxSize), is_stoped_(false) {} + Queue(uint32_t maxSize = DEFAULT_MAX_QUEUE_SIZE) : maxSize_(maxSize), isStoped_(false) {} - ~BlockingQueue() {} + ~Queue() {} int Pop(T &item) { 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/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 d2bece41c494497e446a9b786c47108d3dbe4c56..5667d3f0ee082ed9e3daf97aad3db290edbd20c7 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,48 +33,51 @@ 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", - "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; + 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; 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 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); - while(1){ + while (1) { cv::Mat frame; - if (!capture.read(frame)){ + if (!capture.read(frame)) { ACLLITE_LOG_INFO("Video capture return false"); break; } @@ -85,26 +85,25 @@ 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); - } - else + usleep(timep1); + } else break; } } premsg.videoIndex = threadNum; premsg.isLastFrame = 0; - while(1) { + while (1) { if (queue_pre->Push(premsg) != 0) { - usleep(1000); + usleep(timep1); } else { ACLLITE_LOG_INFO("preprocess end"); @@ -113,38 +112,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, Queue* 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); + if (queue_post->Pop(postmsg) != 0) { + usleep(timep1); 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){ + uint32_t scoreLine = 90; + if (score < scoreLine) { continue; } boundBox.rect.ltX = detectData[totalBox * TOPLEFTX + i] * widthScale; @@ -153,7 +152,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,28 +161,28 @@ 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, FOUNT_SCALE, g_fontColor); } outputVideo << frame; } - g_video_need_postprocess--; + g_videoNeedPostprocess--; outputVideo.release(); ACLLITE_LOG_INFO("postprocess end"); } 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) { @@ -212,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,13 +225,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); + usleep(timep1); continue; } - if(msg.isLastFrame == 0) + if (msg.isLastFrame == 0) { video_need_preprocess = video_need_preprocess - 1; } @@ -241,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; } } @@ -255,25 +253,24 @@ 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) { if (queue_post[i].Push(msg) != 0) { - usleep(1000); - } - else { + usleep(timep1); + } else { break; } } } - while(1){ - if(!g_video_need_postprocess){ + while (1) { + 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 95be3b1163cc4e1efb3e69beb9cf531fb6a87851..819b050209f940bb9a1a302298a72e2359be4996 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,53 +12,54 @@ * 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 #include "acl/acl.h" #include "acllite/AclLiteModel.h" #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; + 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() { - //Request image data memory for input model - aclError aclRet = aclrtMalloc(&imageDataBuf_, (size_t)(imageDataSize_), ACL_MEM_MALLOC_HUGE_FIRST); +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) { 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; @@ -67,28 +68,29 @@ AclLiteError ObjectDetect::CreateInput() { return ACLLITE_OK; } -AclLiteError ObjectDetect::Init() { - //If it is already initialized, it is returned - if (isInited_) { +AclLiteError ObjectDetect::Init() +{ + // 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 +98,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 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 = model_.Execute(inferenceOutput); + // Perform reasoning + ret = g_model_.Execute(inferenceOutput); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Execute model inference failed"); return ACLLITE_ERROR; @@ -123,9 +124,9 @@ AclLiteError ObjectDetect::Inference(std::vector& inferenceOutp void ObjectDetect::DestroyResource() { - model_.DestroyInput(); - model_.DestroyResource(); + g_model_.DestroyInput(); + g_model_.DestroyResource(); - aclrtFree(imageDataBuf_); - aclrtFree(imageInfoBuf_); -} + 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/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 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 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 d36ad22bcbea81adfebbc81c11c15e7f8d2ac291..0ec9df56f45b19186a4009866c4ebeb8ee2fe828 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 @@ -24,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; @@ -45,14 +47,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", - "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 kVideoprocName = "videoprocess_0"; +const string g_videoprocName = "videoprocess_0"; struct PreprocDataMsg { int inferThreadId; @@ -63,7 +65,7 @@ struct PreprocDataMsg { int frameNum; ImageData resizedMat; ImageData imageFrame; - cv::Mat frame; //原图 + cv::Mat frame; // 原图 }; struct InferOutputMsg { @@ -81,4 +83,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/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 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 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 b06b0479bfb2646fd8efd59aa4eb6b52b9787878..902014da91e70b1e9c8c6d3512ebc878fe153474 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/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 da46b1af44651adcbc6bc87637f6358721c03b96..7ec78a472209d639c91456dad95ed5e872fa57d5 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 f9ebb3b55aa691490a9f4faa9ee4f724b61115cb..af9f34e666eeb78a447cc71f879afc8c16bea583 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" @@ -28,37 +26,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){ + aclrtContext& context) + : g_stream_(nullptr), g_context_(context), g_model_(modelPath), + g_modelWidth_(modelWidth), g_modelHeight_(modelHeight) +{ } -InferenceThread::~InferenceThread() { - aclError ret = aclrtSetCurrentContext(context_); +InferenceThread::~InferenceThread() +{ + 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 InferenceThread::Init() +{ + 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; @@ -68,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; @@ -82,44 +79,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(), - imageDevice.size); + 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) - { - AclLiteError ret = SendMessage(preprocDataMsg->postprocThreadId,MSG_INFER_OUTPUT, inferOutputMsg); - if(ret == ACLLITE_ERROR_ENQUEUE) - { - usleep(500); + shared_ptr &inferOutputMsg) +{ + while (1) { + AclLiteError ret = SendMessage(preprocDataMsg->postprocThreadId, MSG_INFER_OUTPUT, inferOutputMsg); + int timep = 500; + if (ret == ACLLITE_ERROR_ENQUEUE) { + 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; } @@ -128,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 d02916b28645eb1b315584bd5e6cf9ceb332806b..4ad492dea84877d27d31d53929e1abb1f35a8242 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 @@ -35,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(); @@ -50,12 +51,13 @@ private: shared_ptr &inferOutputMsg); void DestroyResource(); -private: - AclLiteModel model_; - uint32_t modelWidth_; - uint32_t modelHeight_; - aclrtRunMode runMode_; - aclrtStream stream_; - aclrtContext context_; +private: + 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 4ebb6107aa51c091dbf97a523e1c69cc21517827..9d834baaad85b46611ed385e850b2b5da44d0cff 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 @@ -41,23 +38,30 @@ 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_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; +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_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,44 +71,41 @@ 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, 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()); 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,36 +113,39 @@ 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, 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); - } -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(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); } 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(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); @@ -150,17 +154,19 @@ 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); - 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); } -void CreateThreadInstance(vector& threadTbl, AclLiteResource& aclDev) { +void CreateThreadInstance(vector& threadTbl, AclLiteResource& aclDev) +{ string dataAddr; uint32_t postNum; uint32_t videoHeight; @@ -173,12 +179,12 @@ 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++){ + 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; } - kContext.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 < kContext.size(); i++) { - aclrtDestroyContext(kContext[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,30 +246,32 @@ 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; } } - kModelWidth = atoi(argv[1]); - kModelHeight = atoi(argv[1]); - if(256 == atoi(argv[1])){ - kModelPath = "../model/AnimeGANv2_256.om"; - }else if(512 == atoi(argv[1])){ - kModelPath = "../model/AnimeGANv2_512.om"; - }else{ - kModelPath = "../model/AnimeGANv2_1024.om"; + g_modelWidth = atoi(argv[1]); + g_modelHeight = atoi(argv[1]); + if (g_width1 == atoi(argv[1])) { + g_modelPath = "../model/AnimeGANv2_256.om"; + } else if (g_width2 == atoi(argv[1])) { + g_modelPath = "../model/AnimeGANv2_512.om"; + } else { + 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 93a3750f2c4550ed6320842a5c881e14029f2f65..ceef13dddd36eeff632672a970b0e8a52fa8f582 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" @@ -25,29 +23,32 @@ using namespace std; -PostprocessThread::PostprocessThread(uint32_t outputWidth, uint32_t outputHeight) : -outputWidth_(outputWidth), 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); @@ -58,25 +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; - if(inferMsg->isLastFrame == 1){ + int timep = 500; + if (inferMsg->isLastFrame == 1) { sendFlag = MSG_ENCODE_FINISH; } - while(1) - { + while (1) { AclLiteError ret = SendMessage(inferMsg->videoProcessThreadId, sendFlag, postOutputMsg); - if(ret == ACLLITE_ERROR_ENQUEUE) - { - usleep(500); + if (ret == ACLLITE_ERROR_ENQUEUE) { + 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; } @@ -86,22 +83,24 @@ 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(); - 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 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 0a45f5be63609728d3ebca734cb0dfb0ee6f7354..94b3f23538cc168ff438d6fd31d3a8ef7e7e6507 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 @@ -28,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, @@ -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 ec713e5a429c4f75daed31dab5422f21fb03d276..95686b5df02573e04c1918e7e8927c8fafe369e6 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" @@ -27,32 +25,26 @@ 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) : -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++){ +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++){ + for (int j = 0; j < postThreadNum; j++) { postprocThreadId_.push_back(INVALID_INSTANCE_ID); } } } -PreprocessThread::~PreprocessThread() { +PreprocessThread::~PreprocessThread() +{ aclError ret = aclrtSetCurrentContext(context_); if (ret != ACL_SUCCESS) { ACLLITE_LOG_ERROR("PreprocessThread destructor set context failed, error: %d", ret); @@ -64,12 +56,13 @@ PreprocessThread::~PreprocessThread() { dvpp_.DestroyResource(); } -AclLiteError PreprocessThread::OpenVideoCapture() { +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", - videoPath_.c_str(), CAMERA_ID_0, CAMERA_ID_1); + videoPath_.c_str(), CAMERA_ID_0, CAMERA_ID_1); return ACLLITE_ERROR; } cap_ = new AclLiteVideoProc(cameraId); @@ -87,7 +80,7 @@ AclLiteError PreprocessThread::OpenVideoCapture() { return ACLLITE_ERROR; } - if(!cap_->IsOpened()) { + if (!cap_->IsOpened()) { delete cap_; ACLLITE_LOG_ERROR("Failed to open video"); return ACLLITE_ERROR; @@ -96,8 +89,8 @@ AclLiteError PreprocessThread::OpenVideoCapture() { return ACLLITE_OK; } -AclLiteError PreprocessThread::Init() { - +AclLiteError PreprocessThread::Init() +{ if (ACLLITE_OK != OpenVideoCapture()) { return ACLLITE_ERROR; } @@ -114,29 +107,27 @@ AclLiteError PreprocessThread::Init() { return ACLLITE_ERROR; } - //Get the relevant thread instance id + // Get the relevant thread instance id selfThreadId_ = SelfInstanceId(); - videoThreadId_ = GetAclLiteThreadIdByName(kVideoprocName); + 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 < inferThreadNum_; i++) - { - nextThreadId_[i] = GetAclLiteThreadIdByName(kInferName[i]); - if (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, nextThreadId_[i]); + i, 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 < 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*postThreadNum_+j, postprocThreadId_[i*postThreadNum_+j]); + i*postThreadNum_+j, postprocThreadId_[i*postThreadNum_+j]); return ACLLITE_ERROR; } } @@ -145,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; @@ -159,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); @@ -169,18 +159,20 @@ AclLiteError PreprocessThread::Process(int msgId, return ret; } -AclLiteError PreprocessThread::AppStart() { +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) { +AclLiteError PreprocessThread::ReadFrame(shared_ptr &preprocDataMsg) +{ inferChannel_ = frameCnt_ % inferThreadNum_; - if(inferChannel_ == 0){ + if (inferChannel_ == 0) { indexCount_ = (++indexCount_) % postThreadNum_; } postChannel_ = inferChannel_ * postThreadNum_ + indexCount_; @@ -193,7 +185,7 @@ AclLiteError PreprocessThread::ReadFrame(shared_ptr &preprocData preprocDataMsg->isLastFrame = 0; AclLiteError ret = cap_->Read(preprocDataMsg->imageFrame); - if (ret != ACLLITE_OK) { + if (ret != ACLLITE_OK) { preprocDataMsg->isLastFrame = 1; return ret; } @@ -201,8 +193,9 @@ AclLiteError PreprocessThread::ReadFrame(shared_ptr &preprocData return ACLLITE_OK; } -AclLiteError PreprocessThread::MsgProcess(shared_ptr &preprocDataMsg) { - if(preprocDataMsg->isLastFrame == 1) +AclLiteError PreprocessThread::MsgProcess(shared_ptr &preprocDataMsg) +{ + if (preprocDataMsg->isLastFrame == 1) return ACLLITE_OK; ImageData imageDevice; AclLiteError ret = CopyImageToDevice(imageDevice, preprocDataMsg->imageFrame, runMode_, MEMORY_DVPP); @@ -214,7 +207,7 @@ AclLiteError PreprocessThread::MsgProcess(shared_ptr &preprocDat ImageData resizedImage; ret = dvpp_.Resize(resizedImage, imageDevice, modelWidth_, 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_); @@ -235,22 +228,18 @@ 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){ + 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; } @@ -260,12 +249,10 @@ AclLiteError PreprocessThread::MsgSend(shared_ptr &preprocDataMs 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 < inferThreadNum_; i++) { + for (int j = 0; j < postThreadNum_; j++) { shared_ptr preprocDataMsgEnd = make_shared(); preprocDataMsgEnd->videoProcessThreadId = preprocDataMsg->videoProcessThreadId; preprocDataMsgEnd->channelId = preprocDataMsg->channelId; @@ -273,20 +260,14 @@ AclLiteError PreprocessThread::MsgSend(shared_ptr &preprocDataMs preprocDataMsgEnd->isLastFrame = preprocDataMsg->isLastFrame; preprocDataMsgEnd->inferThreadId = nextThreadId_[i]; preprocDataMsgEnd->postprocThreadId = postprocThreadId_[i*postThreadNum_+j]; - while(1) - { + 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 a8ab258e26b9354e5a1c23f38ce4969f23ccb946..2da46a04e94a93c96b05cb719202fd8b7907d067 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 @@ -28,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(); @@ -65,3 +66,4 @@ private: 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 c90738759cd2b63c985b32c365182998abb6635a..a62f9c6ab9eb9a5085f4d1be5219fbb7efb75a64 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 @@ -29,39 +27,44 @@ using namespace std; 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){ +VideoprocessThread::VideoprocessThread(int videoHeight, int videoWidth, int postNum) + : videoWidth_(videoWidth), + videoHeight_(videoHeight), + postNum_(postNum), + shutdown_(0) +{ } -VideoprocessThread::~VideoprocessThread() { +VideoprocessThread::~VideoprocessThread() +{ } -AclLiteError VideoprocessThread::Init() { - outputVideoPath_ = "./out_test_video.mp4"; +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'), 29.0, cv::Size(videoWidth_,videoHeight_)); + 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); shutdown_++; cout << "shutdown_ is " << shutdown_ << endl; - if(shutdown_ == postNum_){ + if (shutdown_ == postNum_) { ShutDownProcess(); SendMessage(g_MainThreadId, MSG_APP_EXIT, nullptr); } @@ -75,27 +78,24 @@ AclLiteError VideoprocessThread::Process(int msgId, shared_ptr data) { return ret; } -AclLiteError VideoprocessThread::ShutDownProcess() { +AclLiteError VideoprocessThread::ShutDownProcess() +{ int flag = 0; shared_ptr outputData[postNum_]; int noEmptyQueue[postNum_]; - for(int i = 0; i < postNum_; i++) - { - if(!postqueue_[i].empty()){ + 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] = postqueue_[noEmptyQueue[i]].front(); - 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; outputVideo_ << outputData[i]->resultImage; } @@ -104,31 +104,31 @@ AclLiteError VideoprocessThread::ShutDownProcess() { 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; } - 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; @@ -137,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[postNum_]; - for(int i = 0; i < postNum_; i++) - { - if(!postqueue_[i].empty()) + for (int i = 0; i < postNum_; i++) { + if (!postQueue_[i].empty()) flag++; } - if(flag == postNum_) - { - for(int i = 0; i < postNum_; i++) - { - outputData[i] = postqueue_[i].front(); - 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, postNum_-1); - for(int i = 0; i < postNum_; i++) - { + for (int i = 0; i < postNum_; i++) { cout <<"Current frame num is: " << outputData[i]->frameNum << endl; 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 e217a7b8dfde1fc810eb03651ce0a4f5fea81d87..40d94ce2e6e4176a73965c209ab9ab78001a49a0 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 @@ -30,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(); @@ -52,6 +53,7 @@ private: int shutdown_; string outputVideoPath_; cv::VideoWriter outputVideo_; - queue> postqueue_[16]; + 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 b5e219ecb670a988e50cfb03a7241d69c6aaf648..b7e81f93f24ffef0e9aa289cccee63266f4b16bc 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 @@ -24,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; @@ -45,12 +46,12 @@ 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", - "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 { @@ -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 ec2e19b8b6fe77e1b075ed2a3174536f176eb0f5..89fc025cd446a85aff2776f652c6350c607a318b 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" @@ -25,18 +23,17 @@ #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) : -stream_(nullptr), -context_(context), -model_(modelPath), -modelWidth_(modelWidth), -modelHeight_(modelHeight){ + uint32_t modelWidth, uint32_t modelHeight, + aclrtContext& context) + : stream_(nullptr), context_(context), model_(modelPath), + modelWidth_(modelWidth), modelHeight_(modelHeight) +{ } -InferenceThread::~InferenceThread() { +InferenceThread::~InferenceThread() +{ aclError ret = aclrtSetCurrentContext(context_); if (ret != ACL_SUCCESS) { ACLLITE_LOG_ERROR("InferenceThread destructor set context failed, error: %d", ret); @@ -44,8 +41,8 @@ InferenceThread::~InferenceThread() { model_.DestroyResource(); } -AclLiteError InferenceThread::Init() { - +AclLiteError InferenceThread::Init() +{ AclLiteError ret = aclrtCreateStream(&stream_); if (ret != ACL_ERROR_NONE) { ACLLITE_LOG_ERROR("Create acl stream failed, error %d", ret); @@ -68,8 +65,8 @@ 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; @@ -80,7 +77,7 @@ AclLiteError InferenceThread::ModelExecute(shared_ptr preprocDat return ACLLITE_ERROR; } ret = model_.CreateInput(imageDevice.data.get(), - imageDevice.size); + imageDevice.size); if (ret != ACLLITE_OK) { ACLLITE_LOG_ERROR("Create mode input dataset failed"); return ACLLITE_ERROR; @@ -97,44 +94,33 @@ AclLiteError InferenceThread::ModelExecute(shared_ptr preprocDat } AclLiteError InferenceThread::MsgSend(shared_ptr preprocDataMsg, - shared_ptr &inferOutputMsg) { - while(1) - { - AclLiteError ret = SendMessage(preprocDataMsg->postprocThreadId,MSG_INFER_OUTPUT, inferOutputMsg); - if(ret == ACLLITE_ERROR_ENQUEUE) - { - usleep(500); + shared_ptr &inferOutputMsg) +{ + while (1) { + AclLiteError ret = SendMessage(preprocDataMsg->postprocThreadId, MSG_INFER_OUTPUT, inferOutputMsg); + if (ret == ACLLITE_ERROR_ENQUEUE) { + 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) { - while(1) - { +AclLiteError InferenceThread::MsgSendEnd(shared_ptr preprocDataMsg) +{ + while (1) { AclLiteError ret = SendMessage(preprocDataMsg->postprocThreadId, MSG_ENCODE_FINISH, nullptr); - if(ret == ACLLITE_ERROR_ENQUEUE) - { - usleep(500); + if (ret == ACLLITE_ERROR_ENQUEUE) { + 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; } @@ -143,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); @@ -165,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 7a794f21deb56015fdf87ddc797c19de91120ffb..aa2f6763a869f5c5238487e97c507a8cf38dc388 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 @@ -35,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(); @@ -50,7 +51,7 @@ private: shared_ptr &inferOutputMsg); void DestroyResource(); -private: +private: AclLiteModel model_; uint32_t modelWidth_; uint32_t modelHeight_; @@ -59,3 +60,4 @@ private: 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 36c69413efac474afe09d930e149a96e21bc81aa..8316c5f67e0c5f8528c8466f7e5ba129954da3d1 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,78 +37,74 @@ 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 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) { - kExitCount--; - } - else + g_exitCount--; + } 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"); } - 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, 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()); 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()); } } @@ -119,24 +112,26 @@ 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, 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); 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(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); @@ -144,21 +139,23 @@ 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); - 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); threadTbl.push_back(param); - } + } } -void CreateThreadInstance(vector& threadTbl, AclLiteResource& aclDev) { +void CreateThreadInstance(vector& threadTbl, AclLiteResource& aclDev) +{ string dataAddr; uint32_t postNum; uint32_t outputHeight; @@ -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,27 +187,29 @@ void CreateThreadInstance(vector& threadTbl, AclLiteResource ACLLITE_LOG_ERROR("Create acl context failed, error:%d", ret); return; } - kContext.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 < kContext.size(); i++) { - aclrtDestroyContext(kContext[i]); - if(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); @@ -235,8 +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 6cea5e2f9c5bef54c527a8b3fdab5b41188773cb..599654041f4aebc6f6a7bc96bf065b635e6995ad 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" @@ -37,23 +35,27 @@ namespace { 40, 156, 177, 49, 48, 79, 89, 122}; } -PostprocessThread::PostprocessThread(uint32_t outputWidth, uint32_t outputHeight) : -outputWidth_(outputWidth), 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); @@ -71,42 +73,41 @@ 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){ + 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()]); @@ -117,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); } } @@ -129,4 +130,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 73a011c6f92957be3fe4002d7ed7b61a7ff9091f..00ac8d3a32f21db8e0c34a3a31df9948fc28b5a9 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 @@ -28,21 +29,20 @@ 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 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 2fd55837b3c303de908e2eb3415a9fd59d9b3f21..86b5243ff33f04c3873fc8b76a2c0263fca6beff 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" @@ -31,29 +29,26 @@ 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) : -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++){ +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++){ + for (int j = 0; j < postThreadNum; j++) { postprocThreadId_.push_back(INVALID_INSTANCE_ID); } } } -PreprocessThread::~PreprocessThread() { +PreprocessThread::~PreprocessThread() +{ aclError ret = aclrtSetCurrentContext(context_); if (ret != ACL_SUCCESS) { ACLLITE_LOG_ERROR("PreprocessThread destructor set context failed, error: %d", ret); @@ -61,7 +56,8 @@ PreprocessThread::~PreprocessThread() { dvpp_.DestroyResource(); } -AclLiteError PreprocessThread::Init() { +AclLiteError PreprocessThread::Init() +{ GetAllFiles(filePath_, fileVec); if (fileVec.empty()) { ACLLITE_LOG_ERROR("Failed to deal all empty path=%s.", filePath_.c_str()); @@ -80,27 +76,25 @@ AclLiteError PreprocessThread::Init() { return ACLLITE_ERROR; } - //Get the relevant thread instance id + // Get the relevant thread instance id selfThreadId_ = SelfInstanceId(); if (selfThreadId_ == INVALID_INSTANCE_ID) { ACLLITE_LOG_ERROR("Self instance id %d", 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 < inferThreadNum_; i++) { + nextThreadId_[i] = GetAclLiteThreadIdByName(g_inferName[i]); + if (nextThreadId_[i] == INVALID_INSTANCE_ID) { ACLLITE_LOG_ERROR("%d inference instance id %d", - i, nextThreadId_[i]); + i, 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 < 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*postThreadNum_+j, postprocThreadId_[i*postThreadNum_+j]); + i*postThreadNum_+j, postprocThreadId_[i*postThreadNum_+j]); return ACLLITE_ERROR; } } @@ -109,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; @@ -133,19 +127,21 @@ AclLiteError PreprocessThread::Process(int msgId, return ret; } -AclLiteError PreprocessThread::AppStart() { +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) { +AclLiteError PreprocessThread::ReadFrame(shared_ptr &preprocDataMsg) +{ inferChannel_ = frameCnt_ % inferThreadNum_; - if(inferChannel_ == 0){ - indexCount_ = (++indexCount_) % postThreadNum_; //postcount初始值为postnum-1; + if (inferChannel_ == 0) { + indexCount_ = (++indexCount_) % postThreadNum_; // postcount初始值为postnum-1; } postChannel_ = inferChannel_ * postThreadNum_ + indexCount_; @@ -154,7 +150,7 @@ AclLiteError PreprocessThread::ReadFrame(shared_ptr &preprocData preprocDataMsg->frameNum = frameCnt_; preprocDataMsg->isLastFrame = 0; - if (imageFile == fileVec.end()) { + if (imageFile == fileVec.end()) { preprocDataMsg->isLastFrame = 1; return ACLLITE_OK; } @@ -169,9 +165,10 @@ AclLiteError PreprocessThread::ReadFrame(shared_ptr &preprocData return ACLLITE_OK; } -AclLiteError PreprocessThread::MsgProcess(ImageData& imageFrame, - shared_ptr &preprocDataMsg) { - if(preprocDataMsg->isLastFrame == 1) +AclLiteError PreprocessThread::MsgProcess(ImageData& imageFrame, + shared_ptr &preprocDataMsg) +{ + if (preprocDataMsg->isLastFrame == 1) return ACLLITE_OK; ImageData imageDevice; AclLiteError ret = CopyImageToDevice(imageDevice, imageFrame, runMode_, MEMORY_DVPP); @@ -190,7 +187,7 @@ AclLiteError PreprocessThread::MsgProcess(ImageData& imageFrame, ImageData resizedImage; ret = dvpp_.Resize(resizedImage, yuvImage, modelWidth_, 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_); @@ -202,9 +199,10 @@ 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){ + 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); @@ -213,12 +211,10 @@ AclLiteError PreprocessThread::MsgSend(shared_ptr &preprocDataMs 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 < inferThreadNum_; i++) { + for (int j = 0; j < postThreadNum_; j++) { shared_ptr preprocDataMsgEnd = make_shared(); preprocDataMsgEnd->inferThreadId = nextThreadId_[i]; preprocDataMsgEnd->postprocThreadId = postprocThreadId_[i*postThreadNum_+j]; 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 5d0a9703e571d3f05bdbd360a164a8d6f2dc7b8f..bc9eb8236e7e756e29975940873880b589c22651 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 @@ -27,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(); @@ -59,3 +60,4 @@ private: aclrtContext context_; }; +#endif \ No newline at end of file