gsd_InferPlugin.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * @Author: lishengyin lishengyin@sz-sunwin.com
  3. * @Date: 2022-09-04 20:27:57
  4. * @LastEditors: lishengyin
  5. * @LastEditTime: 2022-10-25 11:12:35
  6. * @FilePath: /gsd_check/plugins/gsd_InferPlugin.h
  7. * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  8. */
  9. /**
  10. *
  11. * gsd_InferPlugin.h
  12. *
  13. */
  14. #pragma once
  15. #include <drogon/plugins/Plugin.h>
  16. #include <drogon/HttpController.h>
  17. #include <drogon/drogon.h>
  18. #include <chrono>
  19. #include <cmath>
  20. #include <opencv2/opencv.hpp>
  21. #include "Util/logger.h"
  22. #include "Util/SqlPool.h"
  23. #include "InferTools.hpp"
  24. #include <utility>
  25. #include <future>
  26. #include <iostream>
  27. #include <dirent.h>
  28. #include "Base64.h"
  29. #include "gsd_CenterGroup.h"
  30. #include "uuid.hpp"
  31. //using namespace toolkit;
  32. namespace gsd
  33. {
  34. /**
  35. * @description: InferTaskType
  36. * @param {*}
  37. * @return {*}
  38. */
  39. enum class InferPluginTaskType{
  40. CHECKT_TASK,
  41. CLASSIFY_TASK
  42. };
  43. class InferPlugin : public drogon::Plugin<InferPlugin>
  44. {
  45. public:
  46. InferPlugin() {}
  47. /// This method must be called by drogon to initialize and start the plugin.
  48. /// It must be implemented by the user.
  49. virtual void initAndStart(const Json::Value &config) override;
  50. /// This method must be called by drogon to shutdown the plugin.
  51. /// It must be implemented by the user.
  52. virtual void shutdown() override;
  53. /**
  54. * @description: StartTask
  55. * @return {*}
  56. */
  57. virtual void StartTask();
  58. /**
  59. * @description: 检查任务
  60. * @param {Mat&} img 图片
  61. * @param {CNStreamInferData&} data 前端的推理数据
  62. * @return {*}
  63. */
  64. virtual std::pair<bool, std::string> CheckTask(std::shared_ptr<cv::Mat> img, CNStreamInferData::Ptr data = nullptr);
  65. /**
  66. * @description: 获取结果
  67. * @param {CNStreamInferData&} data1
  68. * @param {CNStreamInferData&} data2
  69. * @return {*}
  70. */
  71. virtual std::pair<bool, std::string> getInfoResult(std::shared_ptr<cv::Mat> img, CNStreamInferData::Ptr data1, CNStreamInferData::Ptr data2);
  72. /**
  73. * @description: CreateTask
  74. * @return {*}
  75. */
  76. virtual std::future<std::pair<bool, std::string>> CreateTask(const drogon::HttpRequestPtr& req, InferPluginTaskType type = InferPluginTaskType::CHECKT_TASK);
  77. /**
  78. * @description: GetBboxCorner
  79. * @param {const InferInfo} object
  80. * @param {int} img_width
  81. * @param {int} img_height
  82. * @return {*}
  83. */
  84. static std::pair<cv::Point, cv::Point> GetBboxCorner(const InferInfo object, int img_width, int img_height);
  85. /**
  86. * @description: CalcThickness
  87. * @param {int} image_width
  88. * @param {float} thickness
  89. * @return {*}
  90. */
  91. static int CalcThickness(int image_width, float thickness);
  92. /**
  93. * @description: DrawBox
  94. * @param {const} cv
  95. * @param {const} cv
  96. * @param {const} cv
  97. * @return {*}
  98. */
  99. static void DrawBox(cv::Mat& image, const cv::Point& top_left, const cv::Point& bottom_right, const cv::Scalar color);
  100. /**
  101. * @description:释放资源
  102. * @return {*}
  103. */
  104. virtual void Destroy();
  105. protected:
  106. // tools
  107. InferTools::Ptr m_InferTools = nullptr;
  108. std::shared_ptr<ThreadPool> pool = nullptr;
  109. bool stop_ = false;
  110. };
  111. }