pipeline.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. bool set_config(const std::string& pipeline_id, const std::string& node_id, const meta::DrawConfigData& config)
  38. {
  39. auto instance = std::find_if(configured_pipelines_.begin(), configured_pipelines_.end(),
  40. [&pipeline_id](const PipelineInstance& instance) { return instance.pipeline_id == pipeline_id; });
  41. if (instance == configured_pipelines_.end())
  42. {
  43. std::cerr << "Pipeline with ID " << pipeline_id << " not found." << std::endl;
  44. return false;
  45. }
  46. auto node = std::find_if(instance->nodes.begin(), instance->nodes.end(),
  47. [&node_id](const std::shared_ptr<GNode::BaseNode>& node) { return node->get_name() == node_id; });
  48. if (node == instance->nodes.end())
  49. {
  50. std::cerr << "Node with ID " << node_id << " not found in pipeline " << pipeline_id << "." << std::endl;
  51. return false;
  52. }
  53. (*node)->set_config_data(std::make_shared<meta::DrawConfigData>(config));
  54. return true;
  55. }
  56. bool set_config(const std::string& pipeline_id, const std::string& node_id, const meta::AnalyzeConfigData& config)
  57. {
  58. auto instance = std::find_if(configured_pipelines_.begin(), configured_pipelines_.end(),
  59. [&pipeline_id](const PipelineInstance& instance) { return instance.pipeline_id == pipeline_id; });
  60. if (instance == configured_pipelines_.end())
  61. {
  62. std::cerr << "Pipeline with ID " << pipeline_id << " not found." << std::endl;
  63. return false;
  64. }
  65. auto node = std::find_if(instance->nodes.begin(), instance->nodes.end(),
  66. [&node_id](const std::shared_ptr<GNode::BaseNode>& node) { return node->get_name() == node_id; });
  67. if (node == instance->nodes.end())
  68. {
  69. std::cerr << "Node with ID " << node_id << " not found in pipeline " << pipeline_id << "." << std::endl;
  70. return false;
  71. }
  72. (*node)->set_config_data(std::make_shared<meta::AnalyzeConfigData>(config));
  73. return true;
  74. }
  75. void start_all_pipelines()
  76. {
  77. for (auto& instance : configured_pipelines_)
  78. {
  79. if (!instance.nodes.empty())
  80. {
  81. std::cout << "Starting pipeline: " << instance.pipeline_id << std::endl;
  82. for (auto it = instance.nodes.rbegin(); it != instance.nodes.rend(); ++it)
  83. {
  84. (*it)->start();
  85. }
  86. }
  87. }
  88. }
  89. void stop_all_pipelines()
  90. {
  91. for (auto& instance : configured_pipelines_)
  92. {
  93. if (!instance.nodes.empty())
  94. {
  95. std::cout << "Stopping pipeline: " << instance.pipeline_id << std::endl;
  96. for (const auto& node : instance.nodes)
  97. {
  98. node->stop();
  99. }
  100. }
  101. }
  102. }
  103. void start_pipelines_by_id(std::string pipeline_id)
  104. {
  105. auto instance = std::find_if(configured_pipelines_.begin(), configured_pipelines_.end(),
  106. [&pipeline_id](const PipelineInstance& instance) { return instance.pipeline_id == pipeline_id; });
  107. if (instance == configured_pipelines_.end())
  108. {
  109. std::cerr << "Pipeline with ID " << pipeline_id << " not found." << std::endl;
  110. return;
  111. }
  112. std::cout << "Starting pipeline: " << instance->pipeline_id << std::endl;
  113. // Start the pipeline by starting all nodes in reverse order
  114. for (auto it = instance->nodes.rbegin(); it != instance->nodes.rend(); ++it)
  115. {
  116. (*it)->start();
  117. }
  118. }
  119. void stop_pipelines_by_id(std::string pipeline_id)
  120. {
  121. auto instance = std::find_if(configured_pipelines_.begin(), configured_pipelines_.end(),
  122. [&pipeline_id](const PipelineInstance& instance) { return instance.pipeline_id == pipeline_id; });
  123. if (instance == configured_pipelines_.end())
  124. {
  125. std::cerr << "Pipeline with ID " << pipeline_id << " not found." << std::endl;
  126. return;
  127. }
  128. std::cout << "Stopping pipeline: " << instance->pipeline_id << std::endl;
  129. // Stop the pipeline by stopping all nodes
  130. for (const auto& node : instance->nodes)
  131. {
  132. node->stop();
  133. }
  134. }
  135. void show_all_pipeline()
  136. {
  137. for (const auto& instance : configured_pipelines_)
  138. {
  139. printf("Pipeline ID: %s\nDescription: %s\n", instance.pipeline_id.c_str(), instance.description.c_str());
  140. printf("start --> ");
  141. for (const auto& node : instance.nodes)
  142. {
  143. printf("%s --> ", node->get_name().c_str());
  144. }
  145. printf(" end\n");
  146. }
  147. }
  148. void show_pipeline_by_id(std::string pipeline_id)
  149. {
  150. auto instance = std::find_if(configured_pipelines_.begin(), configured_pipelines_.end(),
  151. [&pipeline_id](const PipelineInstance& instance) { return instance.pipeline_id == pipeline_id; });
  152. if (instance == configured_pipelines_.end())
  153. {
  154. std::cerr << "Pipeline with ID " << pipeline_id << " not found." << std::endl;
  155. return;
  156. }
  157. printf("Pipeline ID: %s\nDescription: %s\n", instance->pipeline_id.c_str(), instance->description.c_str());
  158. printf("start --> ");
  159. for (const auto& node : instance->nodes)
  160. {
  161. printf("%s --> ", node->get_name().c_str());
  162. }
  163. printf(" end\n");
  164. }
  165. void create_from_json(const std::string& json_path);
  166. void create_from_yaml(const std::string& yaml_path);
  167. private:
  168. std::unordered_map<std::string, std::shared_ptr<Infer>> shared_models_;
  169. std::vector<PipelineInstance> configured_pipelines_;
  170. };
  171. } // namespace Graph
  172. #endif // GRAPH_HPP__