wangrong
2025-01-23 02819b5c047bb354b0ef2374e7c6a6ac4bbd5bba
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
#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>
 
// 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;
}
 
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;
        // std::vector<std::shared_ptr<vp_nodes::vp_fake_des_node>> des_nodes;
 
        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);
 
                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";
            }
        }
 
        auto split = std::make_shared<vp_nodes::vp_split_node>("split", true);
 
        split->attach_to(rtsp_nodes);  
 
        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();
        }
 
        // for debug purpose
        // vp_utils::vp_analysis_board board(rtsp_nodes);
        // board.display(1, false);
 
        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;
}