pipeline.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #ifndef PIPELINE_HPP__
  2. #define PIPELINE_HPP__
  3. #include <iostream>
  4. #include <vector>
  5. #include <string>
  6. #include <unordered_map>
  7. #include "nodes/track/trackNode.hpp"
  8. #include "common/json.hpp"
  9. #include "infer/infer.hpp"
  10. #include "nodes/base/base.hpp"
  11. #include "nodes/stream/streamNode.hpp"
  12. #include "nodes/infer/inferNode.hpp"
  13. #include "nodes/analyze/analyzeNode.hpp"
  14. #include "nodes/draw/drawNode.hpp"
  15. #include "nodes/record/recordNode.hpp"
  16. #include "nodes/httpPush/httpPush.hpp"
  17. namespace Pipe
  18. {
  19. struct PipelineInstance {
  20. std::string pipeline_id;
  21. std::string description;
  22. std::vector<std::shared_ptr<GNode::BaseNode>> nodes; // 只包含此 pipeline 的节点,按顺序
  23. };
  24. class PipelineManager
  25. {
  26. public:
  27. PipelineManager() = default;
  28. ~PipelineManager() { }
  29. // 获取配置好的 pipelines (只读)
  30. const std::vector<PipelineInstance>& getPipelines() const {
  31. return configured_pipelines_;
  32. }
  33. // 获取加载的共享模型 (只读)
  34. const std::unordered_map<std::string, std::shared_ptr<Infer>>& getSharedModels() const {
  35. return shared_models_;
  36. }
  37. void start_all_pipelines()
  38. {
  39. for (auto& instance : configured_pipelines_)
  40. {
  41. if (!instance.nodes.empty())
  42. {
  43. std::cout << "Starting pipeline: " << instance.pipeline_id << std::endl;
  44. for (auto it = instance.nodes.rbegin(); it != instance.nodes.rend(); ++it)
  45. {
  46. (*it)->start();
  47. }
  48. }
  49. }
  50. }
  51. void stop_all_pipelines()
  52. {
  53. for (auto& instance : configured_pipelines_)
  54. {
  55. if (!instance.nodes.empty())
  56. {
  57. std::cout << "Stopping pipeline: " << instance.pipeline_id << std::endl;
  58. for (const auto& node : instance.nodes)
  59. {
  60. node->stop();
  61. }
  62. }
  63. }
  64. }
  65. void start_pipelines_by_id(std::string pipeline_id)
  66. {
  67. auto instance = std::find_if(configured_pipelines_.begin(), configured_pipelines_.end(),
  68. [&pipeline_id](const PipelineInstance& instance) { return instance.pipeline_id == pipeline_id; });
  69. if (instance == configured_pipelines_.end())
  70. {
  71. std::cerr << "Pipeline with ID " << pipeline_id << " not found." << std::endl;
  72. return;
  73. }
  74. std::cout << "Starting pipeline: " << instance->pipeline_id << std::endl;
  75. // Start the pipeline by starting all nodes in reverse order
  76. for (auto it = instance->nodes.rbegin(); it != instance->nodes.rend(); ++it)
  77. {
  78. (*it)->start();
  79. }
  80. }
  81. void stop_pipelines_by_id(std::string pipeline_id)
  82. {
  83. auto instance = std::find_if(configured_pipelines_.begin(), configured_pipelines_.end(),
  84. [&pipeline_id](const PipelineInstance& instance) { return instance.pipeline_id == pipeline_id; });
  85. if (instance == configured_pipelines_.end())
  86. {
  87. std::cerr << "Pipeline with ID " << pipeline_id << " not found." << std::endl;
  88. return;
  89. }
  90. std::cout << "Stopping pipeline: " << instance->pipeline_id << std::endl;
  91. // Stop the pipeline by stopping all nodes
  92. for (const auto& node : instance->nodes)
  93. {
  94. node->stop();
  95. }
  96. }
  97. void show_all_pipeline()
  98. {
  99. for (const auto& pipeline : configured_pipelines_)
  100. {
  101. printf("Pipeline ID: %s\nDescription: %s\n", pipeline.pipeline_id.c_str(), pipeline.description.c_str());
  102. for (const auto& node : instance->nodes)
  103. {
  104. printf("%s --> ", node->get_name().c_str());
  105. }
  106. printf("\n");
  107. }
  108. }
  109. void show_pipeline_by_id(std::string pipeline_id)
  110. {
  111. auto instance = std::find_if(configured_pipelines_.begin(), configured_pipelines_.end(),
  112. [&pipeline_id](const PipelineInstance& instance) { return instance.pipeline_id == pipeline_id; });
  113. if (instance == configured_pipelines_.end())
  114. {
  115. std::cerr << "Pipeline with ID " << pipeline_id << " not found." << std::endl;
  116. return;
  117. }
  118. printf("Pipeline ID: %s\nDescription: %s\n", pipeline.pipeline_id.c_str(), pipeline.description.c_str());
  119. for (const auto& node : instance->nodes)
  120. {
  121. printf("%s --> ", node->get_name().c_str());
  122. }
  123. printf("\n");
  124. }
  125. void create_from_json(const std::string& json_path);
  126. private:
  127. std::unordered_map<std::string, std::shared_ptr<Infer>> shared_models_;
  128. std::vector<PipelineInstance> configured_pipelines_;
  129. };
  130. } // namespace Graph
  131. #endif // GRAPH_HPP__