浏览代码

添加从文件夹中获取图片

leon 3 周之前
父节点
当前提交
6892075b27
共有 3 个文件被更改,包括 67 次插入6 次删除
  1. 2 3
      src/graph/graph.hpp
  2. 63 2
      src/nodes/stream/streamNode.cpp
  3. 2 1
      src/nodes/stream/streamNode.hpp

+ 2 - 3
src/graph/graph.hpp

@@ -53,7 +53,7 @@ public:
             std::cout << "Starting pipeline: " << instance.pipeline_id << std::endl;
             for (auto it = instance.nodes.rbegin(); it != instance.nodes.rend(); ++it) 
             {
-              (*it)->start(); // Assuming a start() method exists
+              (*it)->start();
             }
         }
       }
@@ -66,10 +66,9 @@ public:
         if (!instance.nodes.empty()) 
         {
             std::cout << "Stopping pipeline: " << instance.pipeline_id << std::endl;
-            // Stop nodes (e.g., source first) or as required
             for (const auto& node : instance.nodes) 
             {
-                node->stop(); // Assuming a stop() method exists
+                node->stop();
             }
         }
       }

+ 63 - 2
src/nodes/stream/streamNode.cpp

@@ -3,6 +3,7 @@
 #include <iostream>        // For std::cerr
 #include <thread>          // For std::this_thread::sleep_for
 #include <chrono>          // For std::chrono::milliseconds
+#include <filesystem>
 
 namespace GNode
 {
@@ -46,7 +47,7 @@ bool StreamNode::open_stream() {
         PLOGI.printf("StreamNode [%s]: GPU Demuxer and Decoder created successfully.", name_.c_str());
         status_ = StreamStatus::OPENED;
     }
-    else
+    else if (decode_type_ == DecodeType::CPU)
     {
         cap_ = std::make_shared<cv::VideoCapture>();
         if (!cap_->open(stream_url_))
@@ -66,6 +67,11 @@ bool StreamNode::open_stream() {
         PLOGI.printf("StreamNode [%s]: CPU cv::VideoCapture opened successfully.", name_.c_str());
         status_ = StreamStatus::OPENED;
     }
+    else
+    {
+        status_ = StreamStatus::OPENED;
+        return true;
+    }
 
     frame_count_ = -1;
     return true;
@@ -109,10 +115,14 @@ void StreamNode::work()
             {
                 process_stream_cpu();
             }
-            else
+            else if (decode_type_ == DecodeType::GPU)
             {
                 process_stream_gpu();
             }
+            else
+            {
+                process_stream_foler();
+            }
 
             PLOGI.printf("StreamNode [%s]: Stream processing finished or stopped (Status: %d).",
                    name_.c_str(), static_cast<int>(status_));
@@ -135,6 +145,57 @@ void StreamNode::work()
     close_stream();
 }
 
+
+void StreamNode::process_stream_folder()
+{
+    std::vector<std::string> files;
+    if (!std::filesystem::exists(stream_url_) && !std::filesystem::is_directory(stream_url_)) 
+    {
+        running_.exchange(false);
+    } 
+    else
+    {
+        for (const auto& entry : std::filesystem::directory_iterator(stream_url_)) 
+        {
+            if (entry.is_regular_file()) 
+            {
+                files.push_back(entry.path().string());
+            }
+        }
+    }
+    for (const auto& file : files)
+    {
+        cv::Mat frame;
+        try 
+        {
+            frame = cv::imread(file);
+        } 
+        catch (const cv::Exception& ex) 
+        {
+           PLOGE.printf("StreamNode [%s] Error: Exception during cv::imread: %s", name_.c_str(), ex.what());
+           continue;
+        }
+
+        if (frame.empty())
+        {
+            continue;
+        }
+
+        frame_count_++;
+        if (frame_count_ % skip_frame_ != 0)
+        {
+            continue; // Skip frame
+        }
+
+        auto metaData = std::make_shared<meta::MetaData>();
+        metaData->image = frame.clone();
+        metaData->from = name_;
+        send_output_data(metaData);
+    }
+    PLOGI.printf("StreamNode [%s]: Exiting FOLDER processing loop (Running: %s, Status: %d).",
+            name_.c_str(), running_ ? "true" : "false", static_cast<int>(status_));
+}
+
 void StreamNode::process_stream_cpu()
 {
     if (!cap_ || !cap_->isOpened()) {

+ 2 - 1
src/nodes/stream/streamNode.hpp

@@ -18,7 +18,8 @@ namespace GNode
 enum class DecodeType
 {
     CPU = 0,
-    GPU = 1
+    GPU = 1,
+    FOLDER = 2
 };
 
 enum class StreamStatus{