From 8834cf99004c6fe73008f75c5bae65207703238e Mon Sep 17 00:00:00 2001 From: liwuhen Date: Thu, 21 Nov 2024 11:47:03 +0000 Subject: [PATCH] fix: fix c++ code style Signed-off-by: liwuhen --- .github/workflows/linters.yml | 8 +++++++- CMakeLists.txt | 4 +++- modules/app_yolo/appinterface/interface.h | 2 +- .../app_yolo/architecture/common/appconfig.h | 2 +- .../app_yolo/architecture/cuda/gpu_decode.cu | 19 ++++++++++++++++++- .../app_yolo/architecture/cuda/warpaffine.cu | 19 +++++++++++++++++++ .../app_yolo/architecture/cuda/warpaffine.hpp | 2 +- .../app_yolo/architecture/decodeprocessor.cpp | 4 ++-- modules/app_yolo/architecture/inference.cpp | 2 +- modules/app_yolo/architecture/module/module.h | 2 +- .../app_yolo/architecture/postprocessor.cpp | 2 +- .../app_yolo/architecture/preprocessor.cpp | 15 +++++++++------ modules/app_yolo/architecture/trt_infer.cpp | 8 ++++---- modules/app_yolo/architecture/utils.hpp | 2 +- modules/common/logging/logging.h | 3 +-- modules/common/msg_img/dataset.h | 4 ++-- modules/common/msg_img/img_msg.h | 8 ++++---- modules/common/msg_struct/task_struct.hpp | 16 ++++++++-------- modules/common/utils/colorprintf.h | 2 +- modules/common/utils/glog_msg.h | 2 +- modules/common/utils/std_buffer.h | 6 +++--- modules/common/utils/std_cmake.h | 8 ++++---- modules/common/utils/std_time.h | 3 +-- scripts/build.sh | 2 +- 24 files changed, 95 insertions(+), 50 deletions(-) mode change 100644 => 100755 modules/common/utils/std_cmake.h diff --git a/.github/workflows/linters.yml b/.github/workflows/linters.yml index d5a6450..eeb0e2b 100644 --- a/.github/workflows/linters.yml +++ b/.github/workflows/linters.yml @@ -45,4 +45,10 @@ jobs: reporter: github-pr-check level: warning flags: --linelength=120 - filter: "-runtime/references, -build/include" + filter: "-runtime/references,\ + -build/include,\ + -build/namespaces,\ + -build/header_guard,\ + -whitespace/line_length,\ + -whitespace/indent_namespace,\ + -runtime/string" # Ignore runtime checks on string usage. diff --git a/CMakeLists.txt b/CMakeLists.txt index 9a2b9da..553ba2a 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,9 @@ cmake_minimum_required(VERSION 3.10.2) option(ENABLE_CROSSCOMPILE "Enable crosscomplile for arm platform" OFF) -option(MODEL_FLAG "Enable crosscomplile model type") +set(MODEL_FLAG + "default" + CACHE STRING "Model configuration flag") add_definitions("-Wall -g") add_compile_options(-std=c++11) diff --git a/modules/app_yolo/appinterface/interface.h b/modules/app_yolo/appinterface/interface.h index 5bb069c..7725329 100644 --- a/modules/app_yolo/appinterface/interface.h +++ b/modules/app_yolo/appinterface/interface.h @@ -42,7 +42,7 @@ class InterfaceYolo { return instance; } - ~InterfaceYolo(){}; + ~InterfaceYolo() {} /** * @brief init. diff --git a/modules/app_yolo/architecture/common/appconfig.h b/modules/app_yolo/architecture/common/appconfig.h index b989c9c..83b5199 100644 --- a/modules/app_yolo/architecture/common/appconfig.h +++ b/modules/app_yolo/architecture/common/appconfig.h @@ -85,7 +85,7 @@ class AppConfig { REG_YAML_VAR(std::vector, predict_dim_); protected: - AppConfig(const std::string& config_filename); + explicit AppConfig(const std::string& config_filename); ~AppConfig() {} std::string config_filename_; diff --git a/modules/app_yolo/architecture/cuda/gpu_decode.cu b/modules/app_yolo/architecture/cuda/gpu_decode.cu index c279835..55e314a 100644 --- a/modules/app_yolo/architecture/cuda/gpu_decode.cu +++ b/modules/app_yolo/architecture/cuda/gpu_decode.cu @@ -1,3 +1,20 @@ +/* ================================================================== +* Copyright (c) 2024, LiWuHen. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 +* 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 + 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 @@ -97,7 +114,7 @@ static __global__ void fast_nms_kernel(float* bboxes, int max_objects, float thr // 如果是日常推理, 则可以使用这个gpu nms int position = (blockDim.x * blockIdx.x + threadIdx.x); int count = - min((int)*bboxes, max_objects); // *bboxes表示首地址的第一个元素。 count是bbox的数量。 + min(static_cast(*bboxes), max_objects); // *bboxes表示首地址的第一个元素。 count是bbox的数量。 if (position >= count) return; // left, top, right, bottom, confidence, class, keepflag diff --git a/modules/app_yolo/architecture/cuda/warpaffine.cu b/modules/app_yolo/architecture/cuda/warpaffine.cu index 99e962d..119a57f 100644 --- a/modules/app_yolo/architecture/cuda/warpaffine.cu +++ b/modules/app_yolo/architecture/cuda/warpaffine.cu @@ -1,3 +1,22 @@ + +/* ================================================================== +* Copyright (c) 2024, LiWuHen. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 +* 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 + 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 "warpaffine.hpp" __device__ void affine_project(Matrix3f affineMatrix_inv, int x, int y, float* proj_x, diff --git a/modules/app_yolo/architecture/cuda/warpaffine.hpp b/modules/app_yolo/architecture/cuda/warpaffine.hpp index 6e1dc58..096fac3 100644 --- a/modules/app_yolo/architecture/cuda/warpaffine.hpp +++ b/modules/app_yolo/architecture/cuda/warpaffine.hpp @@ -39,7 +39,7 @@ struct Norm { static dim3 grid_dims(int numJobs) { int numBlockThreads = numJobs < GPU_BLOCK_THREADS ? numJobs : GPU_BLOCK_THREADS; - return dim3(((numJobs * 10 + numBlockThreads - 1) / (float)numBlockThreads)); + return dim3(((numJobs * 10 + numBlockThreads - 1) / static_cast(numBlockThreads))); } static dim3 block_dims(int numJobs) { diff --git a/modules/app_yolo/architecture/decodeprocessor.cpp b/modules/app_yolo/architecture/decodeprocessor.cpp index f774b17..0c7c662 100644 --- a/modules/app_yolo/architecture/decodeprocessor.cpp +++ b/modules/app_yolo/architecture/decodeprocessor.cpp @@ -65,7 +65,7 @@ bool DecodeProcessor::SetParam(shared_ptr& parse_msgs) { if (parse_msgs != nullptr) { this->parsemsgs_ = parse_msgs; } - imgshape_["dst"] = make_pair(parsemsgs_->dst_img_h_, parsemsgs_->dst_img_w_); + imgshape_["dst"] = make_pair(parsemsgs_->dst_img_h_, parsemsgs_->dst_img_w_); GLOG_INFO("[SetParam]: DecodeProcessor module set param "); return true; @@ -81,7 +81,7 @@ bool DecodeProcessor::DataResourceRelease() {} */ bool DecodeProcessor::Inference(float* predict, InfertMsg& infer_msg, std::shared_ptr& bboxQueue) { - imgshape_["src"] = make_pair(infer_msg.height, infer_msg.width); + imgshape_["src"] = make_pair(infer_msg.height, infer_msg.width); vector box_result; CpuDecode(predict, box_result); diff --git a/modules/app_yolo/architecture/inference.cpp b/modules/app_yolo/architecture/inference.cpp index e1f1a6c..c4b3349 100644 --- a/modules/app_yolo/architecture/inference.cpp +++ b/modules/app_yolo/architecture/inference.cpp @@ -181,7 +181,7 @@ bool InferenceEngine::InferenceV1() { LOG(INFO) << " Begin inferenceV1 ... "; auto image = cv::imread(parsemsgs_->img_path_); auto data = trtInfer_->LoadFile(parsemsgs_->predict_path_); - float* ptr = (float*)data.data(); + float* ptr = reinterpret_cast(data.data()); int nelem = data.size() / sizeof(float); int nrows = nelem / 6.0; // 行数,预测的 object 的数量 diff --git a/modules/app_yolo/architecture/module/module.h b/modules/app_yolo/architecture/module/module.h index e3f1c13..4d1ac60 100644 --- a/modules/app_yolo/architecture/module/module.h +++ b/modules/app_yolo/architecture/module/module.h @@ -41,7 +41,7 @@ using namespace hpc::common; */ class InferModuleBase { public: - InferModuleBase(){}; + InferModuleBase() {} virtual ~InferModuleBase() = default; virtual bool Init() = 0; diff --git a/modules/app_yolo/architecture/postprocessor.cpp b/modules/app_yolo/architecture/postprocessor.cpp index f43f10a..c576407 100644 --- a/modules/app_yolo/architecture/postprocessor.cpp +++ b/modules/app_yolo/architecture/postprocessor.cpp @@ -119,7 +119,7 @@ void PostProcessor::Inference() { * @description: Visualization. */ void PostProcessor::Visualization(bool real_time, cv::Mat& img, int64_t timestamp, - vector& results) { + vector& results) { for (auto& box : results) { cv::rectangle(img, cv::Point(box.left, box.top), cv::Point(box.right, box.bottom), cv::Scalar(0, 255, 0), 2); diff --git a/modules/app_yolo/architecture/preprocessor.cpp b/modules/app_yolo/architecture/preprocessor.cpp index 504c6ad..e52ac64 100644 --- a/modules/app_yolo/architecture/preprocessor.cpp +++ b/modules/app_yolo/architecture/preprocessor.cpp @@ -81,7 +81,7 @@ bool PreProcessor::DataResourceRelease() {} * @description: Inference. */ bool PreProcessor::Inference(InfertMsg& input_msg, float* dstimg, DeviceMode inferMode, - cudaStream_t stream) { + cudaStream_t stream) { CalAffineMatrix(input_msg); switch (inferMode) { @@ -92,7 +92,6 @@ bool PreProcessor::Inference(InfertMsg& input_msg, float* dstimg, DeviceMode inf break; case DeviceMode::CPU_MODE: if (!CpuPreprocessor(input_msg.image, input_msg.timestamp, dstimg, stream)) - ; { return false; } break; default: @@ -126,8 +125,12 @@ bool PreProcessor::GpuPreprocessor(InfertMsg& input_msg, float* dstimg, cudaStre /** * @description: Cpu preprocessor. */ -bool PreProcessor::CpuPreprocessor(cv::Mat& srcimg, uint64_t timestamp, float* input_device_gpu, - cudaStream_t stream) { +bool PreProcessor::CpuPreprocessor( + cv::Mat& srcimg, + uint64_t timestamp, + float* input_device_gpu, + cudaStream_t stream) { + checkRuntime(cudaMallocHost(&input_data_host_, sizeof(float) * parsemsgs_->dstimg_size_)); float scale_x = parsemsgs_->dst_img_w_ / static_cast(parsemsgs_->src_img_w_); @@ -176,8 +179,8 @@ bool PreProcessor::CpuPreprocessor(cv::Mat& srcimg, uint64_t timestamp, float* i * @description: AffineMatrix. */ void PreProcessor::CalAffineMatrix(InfertMsg& input_msg) { - float scale_x = parsemsgs_->dst_img_w_ / (float)input_msg.width; - float scale_y = parsemsgs_->dst_img_h_ / (float)input_msg.height; + float scale_x = parsemsgs_->dst_img_w_ / static_cast(input_msg.width); + float scale_y = parsemsgs_->dst_img_h_ / static_cast(input_msg.height); float scale = min(scale_x, scale_y); input_msg.affineMatrix(0, 0) = scale; diff --git a/modules/app_yolo/architecture/trt_infer.cpp b/modules/app_yolo/architecture/trt_infer.cpp index 6d4a0ee..a3117c9 100644 --- a/modules/app_yolo/architecture/trt_infer.cpp +++ b/modules/app_yolo/architecture/trt_infer.cpp @@ -101,7 +101,7 @@ bool TrtInfer::Inference(float* output_img_device) { output_img_device, parsemsgs_->dstimg_size_ * sizeof(uint8_t), cudaMemcpyDeviceToDevice)); - bool success = execution_context_->enqueueV2((void**)gpu_buffers_, stream_, nullptr); + bool success = execution_context_->enqueueV2(reinterpret_cast(gpu_buffers_), stream_, nullptr); if (!success) { LOG(ERROR) << " Inference failed "; return false; @@ -236,8 +236,8 @@ bool TrtInfer::ParseModel() { string name = execution_context_->getEngine().getBindingName(i); auto dim = execution_context_->getBindingDimensions(i); - switch (execution_context_->getEngine().bindingIsInput(i)) // < tensorrt 8.5 - { + // < tensorrt 8.5 + switch (execution_context_->getEngine().bindingIsInput(i)) { case false: out_size++; binding_names_["output"].push_back(name); @@ -323,7 +323,7 @@ std::vector TrtInfer::LoadFile(const string& file) { in.seekg(0, ios::beg); data.resize(length); - in.read((char*)&data[0], length); + in.read(reinterpret_cast(&data[0]), length); } in.close(); return data; diff --git a/modules/app_yolo/architecture/utils.hpp b/modules/app_yolo/architecture/utils.hpp index 70e1fa1..76d9ceb 100644 --- a/modules/app_yolo/architecture/utils.hpp +++ b/modules/app_yolo/architecture/utils.hpp @@ -75,7 +75,7 @@ inline const char* severity_string(nvinfer1::ILogger::Severity t) { class TRTLogger : public nvinfer1::ILogger { public: - virtual void log(Severity severity, nvinfer1::AsciiChar const* msg) noexcept override { + void log(Severity severity, nvinfer1::AsciiChar const* msg) noexcept override { if (severity <= Severity::kINFO) { // 打印带颜色的字符,格式如下: // printf("\033[47;33m 打印的文本\033[0m"); diff --git a/modules/common/logging/logging.h b/modules/common/logging/logging.h index c6e051c..10b687b 100644 --- a/modules/common/logging/logging.h +++ b/modules/common/logging/logging.h @@ -17,7 +17,6 @@ #ifndef TENSORRT_LOGGING_H #define TENSORRT_LOGGING_H -//#include "NvInferRuntimeCommon.h" #include #include #include @@ -188,7 +187,7 @@ class LogStreamConsumer : protected LogStreamConsumerBase, public std::ostream { class Logger : public nvinfer1::ILogger { public: - Logger(Severity severity = Severity::kWARNING) : mReportableSeverity(severity) {} + explicit Logger(Severity severity = Severity::kWARNING) : mReportableSeverity(severity) {} //! //! \enum TestResult diff --git a/modules/common/msg_img/dataset.h b/modules/common/msg_img/dataset.h index a1d0f7d..2e81678 100644 --- a/modules/common/msg_img/dataset.h +++ b/modules/common/msg_img/dataset.h @@ -61,7 +61,8 @@ static const char* cocolabels[] = {"person", "bicycle", "car", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear", - "hair drier", "toothbrush"}; + "hair drier", "toothbrush" +}; // hsv 转 bgr static std::tuple hsv2bgr(float h, float s, float v) { @@ -114,7 +115,6 @@ static std::tuple hsv2bgr(float h, float s, float v) static std::tuple random_color(int id) { float h_plane = ((((unsigned int)id << 2) ^ 0x937151) % 100) / 100.0f; - ; float s_plane = ((((unsigned int)id << 3) ^ 0x315793) % 100) / 100.0f; return hsv2bgr(h_plane, s_plane, 1); } diff --git a/modules/common/msg_img/img_msg.h b/modules/common/msg_img/img_msg.h index 4b5805e..0a42cc1 100644 --- a/modules/common/msg_img/img_msg.h +++ b/modules/common/msg_img/img_msg.h @@ -42,18 +42,18 @@ class ImageInfos { frame_id_ = 0; timestamp_ = 0; image_ = cv::Mat(); - }; - ~ImageInfos(){}; + } + ~ImageInfos() {} bool DeepCopy(const ImageInfos* src) { this->image_ = src->image_.clone(); return true; - }; + } bool MoveCopy(const ImageInfos* src) { this->image_ = std::move(src->image_); return true; - }; + } public: uint16_t width_; diff --git a/modules/common/msg_struct/task_struct.hpp b/modules/common/msg_struct/task_struct.hpp index 543161c..04b9bac 100644 --- a/modules/common/msg_struct/task_struct.hpp +++ b/modules/common/msg_struct/task_struct.hpp @@ -51,13 +51,13 @@ struct Box { this->confidence = obj.confidence; this->label = obj.label; return *this; - }; + } }; struct InfertMsg { InfertMsg() : width(0), height(0), frame_id(0), timestamp(0), img_size(0) { this->image = cv::Mat(); - }; + } InfertMsg operator=(const InfertMsg& obj) { this->width = obj.width; @@ -73,7 +73,7 @@ struct InfertMsg { this->bboxes.emplace_back(box); } return *this; - }; + } public: uint32_t width; @@ -96,10 +96,10 @@ struct CVImage { std::vector data; // 存储连续的像素值 CVImage(); - CVImage(int width, int height, int channel) - : img_width(width), img_height(height), img_channel(channel), img_size(0){}; + CVImage(int width, int height, int channel) : img_width(width), \ + img_height(height), img_channel(channel), img_size(0) {} - ~CVImage(){}; + ~CVImage() {} CVImage operator=(const CVImage& obj) { this->img_width = obj.img_width; @@ -110,7 +110,7 @@ struct CVImage { std::memcpy(data.data(), obj.data.data(), length); // 拷贝数据 } return *this; - }; + } // 指针类型 void setDataFromArray(const uint8_t* obj, int size) { @@ -118,7 +118,7 @@ struct CVImage { if (obj != nullptr && size <= img_size) { std::memcpy(data.data(), obj, size); // 拷贝数据 } - }; + } }; using InputMsgQue = MsgQueue; diff --git a/modules/common/utils/colorprintf.h b/modules/common/utils/colorprintf.h index 1b3d64b..3ba7bbb 100644 --- a/modules/common/utils/colorprintf.h +++ b/modules/common/utils/colorprintf.h @@ -25,7 +25,7 @@ namespace hpc { namespace common { -//颜色宏定义 +// 颜色宏定义 #define NONEC "\033[0m" #define RED "\033[1;32;31m" #define LIGHT_RED "\033[1;31m" diff --git a/modules/common/utils/glog_msg.h b/modules/common/utils/glog_msg.h index df1d174..701178e 100644 --- a/modules/common/utils/glog_msg.h +++ b/modules/common/utils/glog_msg.h @@ -68,7 +68,7 @@ class GlogMsg { ~GlogMsg() { google::FlushLogFiles(google::GLOG_INFO); google::ShutdownGoogleLogging(); - }; + } bool init_ = false; std::mutex glog_mutex_; std::shared_ptr ParseMsgs_; diff --git a/modules/common/utils/std_buffer.h b/modules/common/utils/std_buffer.h index 7cbf745..53a52eb 100644 --- a/modules/common/utils/std_buffer.h +++ b/modules/common/utils/std_buffer.h @@ -34,7 +34,7 @@ namespace common { template class MsgQueue { public: - MsgQueue(int length) { length_ = std::max(1, length); }; + explicit MsgQueue(int length) { length_ = std::max(1, length); } ~MsgQueue() = default; void Push(const T& message); int Size() const; @@ -57,7 +57,7 @@ template void MsgQueue::Push(const T& message) { std::unique_lock lock(mutex_); queue_.push(message); - while ((int)queue_.size() > length_) { + while (static_cast(queue_.size()) > length_) { queue_.pop(); } lock.unlock(); @@ -100,7 +100,7 @@ void MsgQueue::Clear() { while (!queue_.empty()) { queue_.pop(); } -}; +} } // namespace common } // namespace hpc diff --git a/modules/common/utils/std_cmake.h b/modules/common/utils/std_cmake.h old mode 100644 new mode 100755 index 1ed68d3..c03696c --- a/modules/common/utils/std_cmake.h +++ b/modules/common/utils/std_cmake.h @@ -24,9 +24,9 @@ namespace hpc { namespace common { -#define MODEL_FLAG "OFF" +#define MODEL_FLAG "yolov5" -} // namespace common -} // namespace hpc +} //namespace common +} //namespace hpc -#endif // APP_COMMON_STD_CMAKE_H_IN__ +#endif // APP_COMMON_STD_CMAKE_H_IN__ diff --git a/modules/common/utils/std_time.h b/modules/common/utils/std_time.h index 3fda4e3..ded9aa7 100644 --- a/modules/common/utils/std_time.h +++ b/modules/common/utils/std_time.h @@ -34,8 +34,7 @@ namespace common { static time_t GetSystmeTime() { return static_cast(std::chrono::duration_cast( - std::chrono::system_clock::now().time_since_epoch()) - .count()); + std::chrono::system_clock::now().time_since_epoch()).count()); } } // namespace common diff --git a/scripts/build.sh b/scripts/build.sh index ffd249b..3192177 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -72,7 +72,7 @@ elif [ "${MODEL_FLAG}" == "yolox" ]; then fi # compiler -cmake "${COMM_ARGS}" "${CMAKE_DIR}" +cmake ${COMM_ARGS} "${CMAKE_DIR}" make -j8 && cd .. if [ "${PACK_FLAG}" == "ON" ] ; then