wangrong
2025-03-19 291afb9c81aae1d15d9e3edcea618d0a2e1fc5a6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "../nodes/vp_file_src_node.h"
#include "../nodes/vp_rtsp_src_node.h"
#include "../nodes/vp_rtsp_ffmpeg_src_node.h"
#include "../nodes/vp_split_node.h"
#include "../nodes/infers/vp_trt_vehicle_detector.h"
#include "../nodes/infers/vp_trt_vehicle_plate_detector.h"
#include "../nodes/infers/vp_trt_vehicle_color_classifier.h"
#include "../nodes/infers/vp_yolo_detector_node.h"
 
#include "../nodes/osd/vp_osd_node.h"
#include "../nodes/vp_sync_node.h"
#include "../nodes/track/vp_sort_track_node.h"
#include "../nodes/ba/vp_ba_jam_node.h"
#include "../nodes/ba/vp_ba_stop_node.h"
 
#include "../nodes/osd/vp_ba_stop_osd_node.h"
#include "../nodes/broker/vp_json_kafka_broker_node.h"
#include "../nodes/record/vp_record_node.h"
#include "../nodes/vp_screen_des_node.h"
#include "../nodes/vp_fake_des_node.h"
#include "../nodes/vp_placeholder_node.h"
 
#include "../utils/analysis_board/vp_analysis_board.h"
 
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <sstream>
 
// Function to read configuration from a file
std::vector<std::vector<std::string>> read_config_file(const std::string& file_path) {
    std::vector<std::vector<std::string>> config_lines;
    std::string line;
    try {
        std::ifstream infile(file_path);
        while (std::getline(infile, line)) {
            std::vector<std::string> params;
            size_t start = 0;
            size_t end = line.find(" ");
            while (end != std::string::npos) {
                params.push_back(line.substr(start, end - start));
                start = end + 1;
                end = line.find(" ", start);
            }
            params.push_back(line.substr(start, end));  // add the last parameter
            config_lines.push_back(params);
        }
    } catch (const std::exception& e) {
        std::cerr << "read config file ex: " << std::string(e.what()) << " <config_file>\n";
    } catch (...) {
        std::cerr << "read config file ex end1111\n";
    }
    return config_lines;
}
 
// Function to parse polygon coordinates from string
std::vector<vp_objects::vp_point> parse_polygon(const std::string& polygon_str, float resize_ratio) {
    std::vector<vp_objects::vp_point> polygon;
    std::stringstream ss(polygon_str);
    std::string point_str;
    while (std::getline(ss, point_str, ')')) {
        size_t start = point_str.find('(');
        if (start != std::string::npos) {
            std::string coordinates = point_str.substr(start + 1);
            size_t comma_pos = coordinates.find(',');
            if (comma_pos != std::string::npos) {
                int x = std::stoi(coordinates.substr(0, comma_pos));
                int y = std::stoi(coordinates.substr(comma_pos + 1));
                x = static_cast<int>(std::round(x * resize_ratio));
                y = static_cast<int>(std::round(y * resize_ratio));
                polygon.push_back(vp_objects::vp_point(x, y));
            }
        }
    }
    return polygon;
}
 
int main(int argc, char* argv[]) {
    VP_SET_LOG_LEVEL(vp_utils::vp_log_level::INFO);
    VP_LOGGER_INIT();
    // Read configuration file
    if (argc < 2) {
        std::cerr << "Usage: " << argv[0] << " <config_file>\n";
        return -1;
    }
 
    try {
        std::string config_file = argv[1];
        auto configs = read_config_file(config_file);
        VP_INFO(vp_utils::string_format("Launch a thread for each configuration  [%s]", config_file));
 
        // Correct type declarations
        std::string kafka_server_point = "";
        std::vector<std::string> rtsp_srcs;
        std::vector<int> channel_indexs;
        std::vector<int> skip_intervals;
        std::vector<bool> use_gpus;
        std::vector<float> resize_ratios;
 
        // Use shared pointers for nodes
        std::vector<std::shared_ptr<vp_nodes::vp_node>> rtsp_nodes;
        // std::vector<std::shared_ptr<vp_nodes::vp_src_node>> rtsp_nodes;
        // Des节点
        std::vector<std::shared_ptr<vp_nodes::vp_des_node>> des_nodes;
 
        // define a region in frame for every channel (value MUST in the scope of frame'size)
        std::map<int, std::vector<vp_objects::vp_point>> regions = {};
 
        for (const auto& config : configs) {
            if (config.size() >= 5) {
                float resize_ratio = std::stof(config[1]);
                int skip_interval = std::stoi(config[3]);
                int channel_index = std::stoi(config[4]);
                bool usegpu = config.size() >= 6 && config[5] == "1";
 
                rtsp_srcs.push_back(config[0]);
                resize_ratios.push_back(resize_ratio);
                kafka_server_point = config[2];
                skip_intervals.push_back(skip_interval);
                channel_indexs.push_back(channel_index);
                use_gpus.push_back(usegpu);
 
                // Read and parse the polygon coordinates
                if (config.size() >= 7) {
                    std::string polygon_str = config[6];  // Assuming the polygon coordinates are in the 7th column
                    regions[channel_index] = parse_polygon(polygon_str,resize_ratio);
                } else {
                    // Default region if polygon is not provided
                    regions[channel_index] = std::vector<vp_objects::vp_point>{
                        vp_objects::vp_point(280, 30),
                        vp_objects::vp_point(340, 30),
                        vp_objects::vp_point(600, 330),
                        vp_objects::vp_point(30, 350)
                    };
                }
 
                rtsp_nodes.push_back(std::make_shared<vp_nodes::vp_rtsp_ffmpeg_src_node>(
                    "rtsp_src_" + config[4], channel_index, config[0], resize_ratio, skip_interval, usegpu));
 
                des_nodes.push_back(std::make_shared<vp_nodes::vp_fake_des_node>("fake_des_" + config[4], channel_index));
 
            } else {
                std::cerr << "Invalid config line: ";
                for (const auto& param : config) {
                    std::cerr << param << " ";
                }
                std::cerr << "\n";
            }
        }
 
        // branch 0
        auto vehicle_detector = std::make_shared<vp_nodes::vp_yolo_detector_node>("vehicle_detector", "./vp_data/models/det_cls/yolov3-tiny-2022-0721_best.weights", "./vp_data/models/det_cls/yolov3-tiny-2022-0721.cfg", "./vp_data/models/det_cls/yolov3_tiny_5classes.txt");
        auto tracker = std::make_shared<vp_nodes::vp_sort_track_node>("sort_tracker");
 
        auto ba_stop = std::make_shared<vp_nodes::vp_ba_stop_node>("ba_stop", regions);
 
        auto json_kafka_broker_0 = std::make_shared<vp_nodes::vp_json_kafka_broker_node>("json_kafka_broker_0", kafka_server_point, "vp_ba_tracker", vp_nodes::vp_broke_for::BARESULT);
        auto osd_0 = std::make_shared<vp_nodes::vp_ba_stop_osd_node>("osd_0", "./vp_data/font/NotoSansCJKsc-Medium.otf");
        auto recorder = std::make_shared<vp_nodes::vp_record_node>("recorder", "./record", "./record");
        auto split = std::make_shared<vp_nodes::vp_split_node>("split", true);
 
        // for testing. USING fake_des node in production    
        auto fake_des_0 = std::make_shared<vp_nodes::vp_fake_des_node>("fake_des_0", 0);
 
        // construct pipeline
        vehicle_detector->attach_to(rtsp_nodes);
        tracker->attach_to({vehicle_detector});
        ba_stop->attach_to({tracker});
        json_kafka_broker_0->attach_to({ba_stop});
        osd_0->attach_to({json_kafka_broker_0});
 
        recorder->attach_to({osd_0});
        split->attach_to({recorder});  
 
        for (size_t i = 0; i < des_nodes.size(); ++i) {
            des_nodes[i]->attach_to({split});
        }
 
        for (auto node : rtsp_nodes) {
            ((vp_nodes::vp_src_node*)node.get())->start();
        }
 
        std::string wait;
        std::getline(std::cin, wait);
 
        for (auto node : rtsp_nodes) {
            ((vp_nodes::vp_src_node*)node.get())->detach_recursively();
        }
 
    } catch (const std::exception& e) {
        std::cerr << "read config file ex: " << std::string(e.what()) << " <config_file>\n";
    } catch (...) {
        std::cerr << "read config file ex end\n";
    }
 
    return 0;
}