ソースを参照

update draw node

leon 4 週間 前
コミット
bda6eaf6ba

+ 22 - 45
src/nodes/draw/drawNode.cpp

@@ -59,57 +59,34 @@ static std::tuple<uint8_t, uint8_t, uint8_t> random_color(int id)
     return hsv2bgr(h_plane, s_plane, 1);
 }
 
-void DrawNode::work() 
+void DrawNode::handle_data(std::shared_ptr<meta::MetaData>& meta_data) 
 {
-    PLOGI.printf("DrawNode : [%s] start", name_.c_str());
-    while (running_)
+    cv::Mat image = meta_data->image.clone();
+    int image_width = image.cols;
+    int image_height = image.rows;
+    PositionManager<float> pm(getFontSize);
+    for (auto& box : meta_data->boxes)
     {
-        bool has_data = false;
-        for (auto& input_buffer : input_buffers_)
-        {
-            std::shared_ptr<meta::MetaData> metaData;
-            if (!input_buffer.second->try_pop(metaData))
-            {
-                continue;
-            }
-            has_data = true;
-            // printf("Node %s get data from %s\n", name_.c_str(), input_buffer.first.c_str());
-            cv::Mat image = metaData->image.clone();
-            int image_width = image.cols;
-            int image_height = image.rows;
-            PositionManager<float> pm(getFontSize);
-            for (auto& box : metaData->boxes)
-            {
-                uint8_t b, g, r;
-                std::tie(b, g, r) = random_color(box.class_id);
-                cv::rectangle(image, cv::Point(box.left, box.top), cv::Point(box.right, box.bottom), cv::Scalar(b, g, r), 2);
+        uint8_t b, g, r;
+        std::tie(b, g, r) = random_color(box.class_id);
+        cv::rectangle(image, cv::Point(box.left, box.top), cv::Point(box.right, box.bottom), cv::Scalar(b, g, r), 2);
 
-                std::tuple<int, int, int, int> pbox = std::make_tuple(box.left, box.top, box.right, box.bottom);
-                int x, y;
-                std::string text;
-                if (box.class_id != -1) 
-                {
-                    text = str_format("%s %.2f id=%d", box.label.c_str(), box.score, box.class_id);
-                }
-                else
-                {
-                    text = str_format("%s %.2f", box.label.c_str(), box.score);
-                }
-                std::tie(x, y) = pm.selectOptimalPosition(pbox, image_width, image_height, text);
-                
-                cv::putText(image, text, cv::Point(x, y), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(b, g, r), 2);
-            }
-            metaData->draw_image = image;
-            send_output_data(metaData);
+        std::tuple<int, int, int, int> pbox = std::make_tuple(box.left, box.top, box.right, box.bottom);
+        int x, y;
+        std::string text;
+        if (box.class_id != -1) 
+        {
+            text = str_format("%s %.2f id=%d", box.label.c_str(), box.score, box.class_id);
         }
-        if (!has_data) {
-            // printf("draw wait data\n");
-            std::unique_lock<std::mutex> lock(mutex_);
-            cond_var_->wait_for(lock, std::chrono::milliseconds(100), [this] {
-                return !running_;  // 等待时检查退出条件
-            });
+        else
+        {
+            text = str_format("%s %.2f", box.label.c_str(), box.score);
         }
+        std::tie(x, y) = pm.selectOptimalPosition(pbox, image_width, image_height, text);
+        
+        cv::putText(image, text, cv::Point(x, y), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(b, g, r), 2);
     }
+    meta_data->draw_image = image;
 }
 
 }

+ 1 - 1
src/nodes/draw/drawNode.hpp

@@ -17,7 +17,7 @@ public:
     DrawNode(const std::string& name) : BaseNode(name, NODE_TYPE::DES_NODE) {}
     virtual ~DrawNode() { stop(); };
 
-    void work() override;
+    void handle_data(std::shared_ptr<meta::MetaData>& meta_data) override;
 };
 
 } // namespace Node

+ 0 - 63
src/nodes/infer/inferNode.cpp

@@ -54,67 +54,4 @@ void InferNode::handle_data(std::shared_ptr<meta::MetaData>& meta_data)
     }
 }
 
-// void InferNode::work()
-// {
-//     PLOGI.printf("InferNode : [%s] start", name_.c_str());
-//     if (!model_)
-//     {
-//         PLOGE.printf("InferNode : [%s] model is nullptr", name_.c_str());
-//         return;
-//     }
-//     // 不同线程都需要指定显卡id,默认为0号显卡
-//     // 在主线程中创建的模型,创建时指定了显卡id
-//     // 推理的时候创建了另一个线程,需要指定同样的显卡id
-//     checkRuntime(cudaSetDevice(device_id_));
-//     while (running_)
-//     {
-//         // Timer timer("InferNode");
-//         bool has_data = false;
-//         for (auto& input_buffer : input_buffers_)
-//         {
-//             std::shared_ptr<meta::MetaData> metaData;
-//             if (!input_buffer.second->try_pop(metaData))
-//             {
-//                 continue;
-//             }
-//             has_data = true;
-//             // printf("Node %s get data from %s\n", name_.c_str(), input_buffer.first.c_str());
-//             cv::Mat image = metaData->image;
-//             int width = image.cols;
-//             int height = image.rows;
-
-//             auto det_result = model_->forward(tensor::cvimg(image), image.cols, image.rows, 0.0f, 0.0f);
-
-//             if (std::holds_alternative<data::BoxArray>(det_result)) 
-//             {
-//                 auto result = std::get<data::BoxArray>(det_result);
-//                 for (auto& r : result)
-//                 {
-//                     metaData->boxes.push_back(r);
-//                 }
-//             } 
-//             else if(std::holds_alternative<cv::Mat>(det_result))
-//             {
-//                 auto depth_mat = std::get<cv::Mat>(det_result);
-//                 // print_mat(depth_mat);
-//                 metaData->depth = depth_mat;
-
-//             }
-//             else
-//             {
-//                 PLOGE.printf("InferNode : [%s] Unexpected result type from model", name_.c_str());
-//                 throw std::runtime_error("Unexpected result type from model");
-//             }
-//             send_output_data(metaData);          
-//         }
-//         if (!has_data)
-//         {
-//             std::unique_lock<std::mutex> lock(mutex_);
-//             cond_var_->wait_for(lock, std::chrono::milliseconds(100), [this] {
-//                 return !running_;  // 等待时检查退出条件
-//             });
-//         }
-//     }
-// }
-
 }   // namespace StreamNode

+ 0 - 1
src/nodes/infer/inferNode.hpp

@@ -26,7 +26,6 @@ public:
         model_type_ = model_type;
     }
 
-    // void work() override;
     void handle_data(std::shared_ptr<meta::MetaData>& meta_data) override;
 
 private:

+ 1 - 38
src/nodes/record/recordNode.cpp

@@ -14,47 +14,10 @@ void RecordNode::handle_data(std::shared_ptr<meta::MetaData>& meta_data)
         if (!writer_.isOpened())
         {
             PLOGE.printf("RecordNode : [%s] cannot open video writer", name_.c_str());
-            break;
+            stop();
         }
     }
     writer_ << image;
 }
 
-// void RecordNode::work()
-// {
-//     PLOGI.printf("RecordNode : [%s] start", name_.c_str());
-//     while (running_)
-//     {
-//         bool has_data = false;
-//         for (auto& input_buffer : input_buffers_)
-//         {
-//             std::shared_ptr<meta::MetaData> metaData;
-//             if (!input_buffer.second->try_pop(metaData))
-//             {
-//                 continue;
-//             }
-//             has_data = true;
-//             // printf("Node %s get data from %s\n", name_.c_str(), input_buffer.first.c_str());
-//             cv::Mat image = metaData->draw_image;
-//             if (!writer_.isOpened())
-//             {
-//                 writer_.open(record_path_, fourcc_, fps_, cv::Size(image.cols, image.rows));
-//                 if (!writer_.isOpened())
-//                 {
-//                     PLOGE.printf("RecordNode : [%s] cannot open video writer", name_.c_str());
-//                     break;
-//                 }
-//             }
-//             writer_ << image;
-//         }
-//         if (!has_data) {
-//             std::unique_lock<std::mutex> lock(mutex_);
-//             cond_var_->wait_for(lock, std::chrono::milliseconds(100), [this] {
-//                 return !running_;  // 等待时检查退出条件
-//             });
-//         }
-//     }
-// }
-
-
 }   // namespace GNode