common.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // Created by ubuntu on 3/16/23.
  3. //
  4. #ifndef JETSON_SEGMENT_COMMON_HPP
  5. #define JETSON_SEGMENT_COMMON_HPP
  6. #include "NvInfer.h"
  7. #include "opencv2/opencv.hpp"
  8. #include <sys/stat.h>
  9. #include <unistd.h>
  10. #define CHECK(call) \
  11. do { \
  12. const cudaError_t error_code = call; \
  13. if (error_code != cudaSuccess) { \
  14. printf("CUDA Error:\n"); \
  15. printf(" File: %s\n", __FILE__); \
  16. printf(" Line: %d\n", __LINE__); \
  17. printf(" Error code: %d\n", error_code); \
  18. printf(" Error text: %s\n", cudaGetErrorString(error_code)); \
  19. exit(1); \
  20. } \
  21. } while (0)
  22. class Logger: public nvinfer1::ILogger {
  23. public:
  24. nvinfer1::ILogger::Severity reportableSeverity;
  25. explicit Logger(nvinfer1::ILogger::Severity severity = nvinfer1::ILogger::Severity::kINFO):
  26. reportableSeverity(severity)
  27. {
  28. }
  29. void log(nvinfer1::ILogger::Severity severity, const char* msg) noexcept override
  30. {
  31. if (severity > reportableSeverity) {
  32. return;
  33. }
  34. switch (severity) {
  35. case nvinfer1::ILogger::Severity::kINTERNAL_ERROR:
  36. std::cerr << "INTERNAL_ERROR: ";
  37. break;
  38. case nvinfer1::ILogger::Severity::kERROR:
  39. std::cerr << "ERROR: ";
  40. break;
  41. case nvinfer1::ILogger::Severity::kWARNING:
  42. std::cerr << "WARNING: ";
  43. break;
  44. case nvinfer1::ILogger::Severity::kINFO:
  45. std::cerr << "INFO: ";
  46. break;
  47. default:
  48. std::cerr << "VERBOSE: ";
  49. break;
  50. }
  51. std::cerr << msg << std::endl;
  52. }
  53. };
  54. inline int get_size_by_dims(const nvinfer1::Dims& dims)
  55. {
  56. int size = 1;
  57. for (int i = 0; i < dims.nbDims; i++) {
  58. size *= dims.d[i];
  59. }
  60. return size;
  61. }
  62. inline int type_to_size(const nvinfer1::DataType& dataType)
  63. {
  64. switch (dataType) {
  65. case nvinfer1::DataType::kFLOAT:
  66. return 4;
  67. case nvinfer1::DataType::kHALF:
  68. return 2;
  69. case nvinfer1::DataType::kINT32:
  70. return 4;
  71. case nvinfer1::DataType::kINT8:
  72. return 1;
  73. case nvinfer1::DataType::kBOOL:
  74. return 1;
  75. default:
  76. return 4;
  77. }
  78. }
  79. inline static float clamp(float val, float min, float max)
  80. {
  81. return val > min ? (val < max ? val : max) : min;
  82. }
  83. inline bool IsPathExist(const std::string& path)
  84. {
  85. if (access(path.c_str(), 0) == F_OK) {
  86. return true;
  87. }
  88. return false;
  89. }
  90. inline bool IsFile(const std::string& path)
  91. {
  92. if (!IsPathExist(path)) {
  93. printf("%s:%d %s not exist\n", __FILE__, __LINE__, path.c_str());
  94. return false;
  95. }
  96. struct stat buffer;
  97. return (stat(path.c_str(), &buffer) == 0 && S_ISREG(buffer.st_mode));
  98. }
  99. inline bool IsFolder(const std::string& path)
  100. {
  101. if (!IsPathExist(path)) {
  102. return false;
  103. }
  104. struct stat buffer;
  105. return (stat(path.c_str(), &buffer) == 0 && S_ISDIR(buffer.st_mode));
  106. }
  107. namespace seg {
  108. struct Binding {
  109. size_t size = 1;
  110. size_t dsize = 1;
  111. nvinfer1::Dims dims;
  112. std::string name;
  113. };
  114. struct Object {
  115. cv::Rect_<float> rect;
  116. int label = 0;
  117. float prob = 0.0;
  118. cv::Mat boxMask;
  119. };
  120. struct PreParam {
  121. float ratio = 1.0f;
  122. float dw = 0.0f;
  123. float dh = 0.0f;
  124. float height = 0;
  125. float width = 0;
  126. };
  127. } // namespace seg
  128. #endif // JETSON_SEGMENT_COMMON_HPP